Skip to content

Commit

Permalink
Merge 716f799 into 2e8f1f5
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-fox committed Feb 27, 2019
2 parents 2e8f1f5 + 716f799 commit a4b0c5b
Show file tree
Hide file tree
Showing 113 changed files with 6,479 additions and 5,015 deletions.
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"singleQuote": true,
"parser": "flow",
"printWidth": 120,
"trailingComma": "none",
"tabWidth": 4
}
70 changes: 44 additions & 26 deletions bin/agentConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,64 +32,66 @@ var readline = require('readline'),
separator = '\n\n\t';

var commands = {
'start': {
start: {
parameters: [],
description: '\tStart the IoT Agent',
handler: startApp
},
'stop': {
stop: {
parameters: [],
description: '\tStop the IoT Agent',
handler: stopApp
},
'register': {
register: {
parameters: ['id', 'type'],
description: '\tRegister a new device in the IoT Agent. The attributes to register will be extracted from the\n' +
description:
'\tRegister a new device in the IoT Agent. The attributes to register will be extracted from the\n' +
'\ttype configuration',
handler: registerDevice
},
'unregister': {
unregister: {
parameters: ['id', 'type'],
description: '\tUnregister the selected device',
handler: unregisterDevice
},
'showConfig': {
showConfig: {
parameters: [],
description: '\tShow the current configuration file',
handler: showConfig
},
'config': {
config: {
parameters: ['newConfig'],
description: '\tChange the configuration file to a new one',
handler: changeConfig
},
'updatevalue': {
updatevalue: {
parameters: ['deviceId', 'deviceType', 'attributes'],
description: '\tUpdate a device value in the Context Broker. The attributes should be triads with the following\n' +
description:
'\tUpdate a device value in the Context Broker. The attributes should be triads with the following\n' +
'\tformat: "name/type/value" sepparated by commas.',
handler: updateDeviceValue
},
'listdevices': {
listdevices: {
parameters: [],
description: '\tList all the devices that have been registered in this IoT Agent session\n',
handler: listDevices
}
};

function handleError(message) {
return function (error) {
return function(error) {
if (error) {
console.log('\n\033[31mERROR:\033[0m %s', error.message);
} else {
console.log(message)
console.log(message);
}

commandUtils.prompt();
};
}

function listDevices() {
iotAgentLib.listDevices(config.service, config.subservice, function (error, devices) {
iotAgentLib.listDevices(config.service, config.subservice, function(error, devices) {
if (error) {
console.log('\n\033[31mERROR:\033[0m %s', error.message);
} else {
Expand All @@ -115,7 +117,6 @@ function extractAttributes(attributeString, callback) {
type: fields[1]
};


if (fields[2]) {
attribute.value = fields[2];
}
Expand All @@ -135,8 +136,12 @@ function changeConfig(command) {
}

function writeHandler(id, type, attributes, callback) {
console.log('\n\nFake WRITE handler for update in entity [%s] with type [%s]\n%s\n',
id, type, JSON.stringify(attributes, null, 4));
console.log(
'\n\nFake WRITE handler for update in entity [%s] with type [%s]\n%s\n',
id,
type,
JSON.stringify(attributes, null, 4)
);

callback(null, {
id: id,
Expand All @@ -146,8 +151,12 @@ function writeHandler(id, type, attributes, callback) {
}

function readHandler(id, type, attributes, callback) {
console.log('\n\nFake READ handler for update in entity [%s] with type [%s]\n%s\n'
, id, type, JSON.stringify(attributes, null, 4));
console.log(
'\n\nFake READ handler for update in entity [%s] with type [%s]\n%s\n',
id,
type,
JSON.stringify(attributes, null, 4)
);

var sensorData = {
id: id,
Expand Down Expand Up @@ -181,7 +190,7 @@ function exitAgent(command) {
}

function registerDevice(command) {
var device = {
var device = {
id: command[0],
type: command[1]
};
Expand All @@ -199,14 +208,23 @@ function updateDeviceValue(command) {
iotAgentLib.getDevice(command[0], function(error, device) {
if (device) {
extractAttributes(command[2], function(error, attributes) {
iotAgentLib.update(device.name, device.type, '', attributes, device,
handleError('Device value updated'));
iotAgentLib.update(
device.name,
device.type,
'',
attributes,
device,
handleError('Device value updated')
);
});
} else {
async.waterfall([
async.apply(extractAttributes, command[2]),
async.apply(iotAgentLib.update, command[0], command[1], '')
], handleError('Device value updated'));
async.waterfall(
[
async.apply(extractAttributes, command[2]),
async.apply(iotAgentLib.update, command[0], command[1], '')
],
handleError('Device value updated')
);
}
});
}
Expand All @@ -223,7 +241,7 @@ function queryHandler(id, type, attributes, callback) {
}

function updateHandler(id, type, attributes, callback) {
console.log("Update message received for device with id [%s] and type [%s]", id, type);
console.log('Update message received for device with id [%s] and type [%s]', id, type);

callback(null, {
type: type,
Expand Down
2 changes: 1 addition & 1 deletion bin/iotAgentTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ var commandLine = require('../lib/command/commandLine'),

commandLine.init(configCb, configIot);

clUtils.initialize(commandLine.commands, 'IoT Agent tester> ');
clUtils.initialize(commandLine.commands, 'IoT Agent tester> ');
3 changes: 1 addition & 2 deletions config-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ var config = {
deviceRegistry: {
type: 'memory'
},
types: {
},
types: {},
service: 'tester',
subservice: '/test',
providerUrl: 'http://192.168.56.1:4041',
Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var config = {
type: 'memory'
},
types: {
'Light': {
Light: {
url: '/',
apikey: '',
type: 'Light',
Expand Down
21 changes: 10 additions & 11 deletions doc/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ var http = require('http'),
express = require('express');

function dummyResponse(req, res) {
console.log('Processing response to [%s] [%s] [%j]', req.method, req.path, req.query);
console.log('Processing response to [%s] [%s] [%j]', req.method, req.path, req.query);
res.status(200).send('89');
}

function initEcho(callback) {
echoServer = {
echoServer = {
server: null,
app: express(),
router: express.Router()
};
};

echoServer.app.set('port', 9999);
echoServer.app.set('host', '0.0.0.0');
Expand All @@ -22,11 +22,10 @@ function initEcho(callback) {
echoServer.server.listen(echoServer.app.get('port'), echoServer.app.get('host'), callback);
}

initEcho(function (error) {
if (error) {
console.log('Could not initialize echo server: %s', error);
} else {
console.log('Echo server started successfully');
}
});

initEcho(function(error) {
if (error) {
console.log('Could not initialize echo server: %s', error);
} else {
console.log('Echo server started successfully');
}
});
34 changes: 16 additions & 18 deletions doc/finalResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var iotAgentLib = require('iotagent-node-lib'),

function parseUl(data, device) {
function findType(name) {
for (var i=0; i < device.active.length; i++) {
for (var i = 0; i < device.active.length; i++) {
if (device.active[i].name === name) {
return device.active[i].type;
}
Expand All @@ -22,11 +22,11 @@ function parseUl(data, device) {
value: pair[1],
type: findType(pair[0])
};

return attribute;
}
return data.split("#").map(createAttribute);

return data.split('#').map(createAttribute);
}

function manageULRequest(req, res, next) {
Expand All @@ -35,23 +35,23 @@ function manageULRequest(req, res, next) {
iotAgentLib.getDevice(req.query.i, function(error, device) {
if (error) {
res.status(404).send({
message: 'Couldn\'t find the device: ' + JSON.stringify(error)
message: "Couldn't find the device: " + JSON.stringify(error)
});
} else {
values = parseUl(req.query.d, device);
iotAgentLib.update(device.name, device.type, '', values, device, function(error) {
if (error) {
res.status(500).send({
message: 'Error updating the device'
});
});
} else {
res.status(200).send({
message: 'Device successfully updated'
});
}
}
});
}
});
});
}

function initSouthbound(callback) {
Expand All @@ -77,9 +77,9 @@ function createResponse(id, type, attributes, body) {

for (var i = 0; i < attributes.length; i++) {
responses.push({
name: attributes[i],
type: "string",
value: values[i]
name: attributes[i],
type: 'string',
value: values[i]
});
}

Expand All @@ -99,7 +99,7 @@ function queryContextHandler(id, type, attributes, callback) {
}
};

request(options, function (error, response, body) {
request(options, function(error, response, body) {
if (error) {
callback(error);
} else {
Expand All @@ -109,12 +109,12 @@ function queryContextHandler(id, type, attributes, callback) {
}

function createQueryFromAttributes(attributes) {
var query = "";
var query = '';

for (var i in attributes) {
query += attributes[i].name + '|' + attributes[i].value;

if (i != attributes.length -1) {
if (i != attributes.length - 1) {
query += ',';
}
}
Expand All @@ -131,7 +131,7 @@ function updateContextHandler(id, type, attributes, callback) {
}
};

request(options, function (error, response, body) {
request(options, function(error, response, body) {
if (error) {
callback(error);
} else {
Expand Down Expand Up @@ -167,7 +167,7 @@ iotAgentLib.activate(config, function(error) {
iotAgentLib.setConfigurationHandler(configurationHandler);
iotAgentLib.setProvisioningHandler(provisioningHandler);

initSouthbound(function (error) {
initSouthbound(function(error) {
if (error) {
console.log('Could not initialize South bound API due to the following error: %s', error);
} else {
Expand All @@ -176,5 +176,3 @@ iotAgentLib.activate(config, function(error) {
});
}
});


12 changes: 11 additions & 1 deletion doc/usermanual.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
+ [Continuous testing](#continuous-testing)
+ [Code Coverage](#code-coverage)
+ [Clean](#clean)
+ [Prettify Code](#prettify-code)


## Usage
Expand Down Expand Up @@ -717,9 +718,18 @@ npm run test:coverage

### Clean

Removes `node_modules` and `coverage` folders, and `package-lock.json` file so that a fresh copy of the project is restored.
Removes `node_modules` and `coverage` folders, and `package-lock.json` file so that a fresh copy of the project is restored.

```bash
# Use git-bash on Windows
npm run clean
```

### Prettify Code

Runs the [prettier](https://prettier.io) code formatter to ensure consistent code style (whitespacing, parameter placement and breakup of long lines etc.) within the codebase.

```bash
# Use git-bash on Windows
npm run prettier
```
Loading

0 comments on commit a4b0c5b

Please sign in to comment.