Skip to content

Commit 1d4be08

Browse files
committed
Merge pull request #187 from telefonicaid/develop
Release 0.8.4
2 parents bb39fff + 65b9162 commit 1d4be08

19 files changed

+1011
-26
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.DS_Store
22
/node_modules/
33
/.idea/
4-
/doc/
54
/coverage/
65
/site
76
/report/

CHANGES_NEXT_RELEASE

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
- FIX 500 error while removing devices (#151)
2-
- FIX Change Device Not Found to 404 instead of 500 (#161).
3-
- ADD First version of the stats registry (#160).
4-
- FIX Reestructure files and folders (#164).
5-
- ADD Mongodb action to persist stats.
6-
- FIX Use Express Methods to check content type (#170).
1+
- Allow listDevices() to use a single param (#180).
2+
- Implement generic find function for the device registries.
3+
- Get info about iotA Version in IoTA-lib request (#184).

README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Given the aforementioned requirements, there are some aspects of the implementat
7676

7777
## <a name="usage"/> Usage
7878
### Library usage
79-
### Stats Registry
79+
#### Stats Registry
8080
The library provides a mechanism for the periodic reporting of stats related to the library's work. In order to activate
8181
the use of the periodic stats, it must be configured in the config file, as described in the [Configuration](#configuration)
8282
section.
@@ -106,10 +106,12 @@ In order to use this library, first you must require it:
106106
```
107107
var iotagentLib = require('iotagent-node-lib');
108108
```
109-
As a Lightweight M2M Server, the library supports four groups of features, one for each direction of the communication: client-to-server and server-to-client (and each flow both for the client and the server). Each feature set is defined in the following sections.
109+
The library supports four groups of features, one for each direction of the communication:
110+
client-to-server and server-to-client (and each flow both for the client and the server). Each feature set is defined
111+
in the following sections.
110112

111113
#### Operations
112-
##### iotagentLib.activate
114+
##### iotagentLib.activate()
113115
###### Signature
114116
```
115117
function activate(newConfig, callback)
@@ -207,7 +209,11 @@ attribute with the '_status' sufix.
207209
function listDevices(service, subservice, limit, offset, callback)
208210
```
209211
###### Description
210-
Return a list of all the devices registered in the specified service and subservice.
212+
Return a list of all the devices registered in the specified service and subservice. This function can be invoked in
213+
three different ways:
214+
* with just one parameter (the callback)
215+
* with three parameters (service, subservice and callback)
216+
* or with five parameters (including limit and offset).
211217
###### Params
212218
* service: service from where the devices will be retrieved.
213219
* subservice: subservice from where the devices will be retrieved.
@@ -318,6 +324,18 @@ Retrieve a device from the registry based on its entity name.
318324
###### Params
319325
* deviceName: Name of the entity associated to a device.
320326

327+
##### iotagentLib.getDevicesByAttribute()
328+
###### Signature
329+
```
330+
function getDevicesByAttribute(name, value, callback)
331+
```
332+
###### Description
333+
Retrieve all the devices having an attribute named `name` with value `value`.
334+
335+
###### Params
336+
* name: name of the attribute to match.
337+
* value: value to match in the attribute.
338+
321339
## <a name="librarytesting"/> IoT Library Testing
322340
### Agent Console
323341
A command line client to experiment with the library is packed with it. The command line client can be started using the following command:
@@ -479,6 +497,20 @@ These are the parameters that can be configured in the global section:
479497
* **subservice**: default subservice for the IoT Agent. If a device is being registered, and no subservice information comes with the device data, and no subservice information is configured for the given type, the default IoT agent subservice will be used instead. E.g.: '/gardens'.
480498
* **providerUrl**: URL to send in the Context Provider registration requests. Should represent the external IP of the deployed IoT Agent (the IP where the Context Broker will redirect the NGSI requests). E.g.: 'http://192.168.56.1:4041'.
481499
* **deviceRegistrationDuration**: duration of the registrations as Context Providers, in [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601) standard format. E.g.: 'P1M'.
500+
* **iotaVersion**: indicates the version of the IoTA that will be displayed in the about method (it should be filled automatically by each IoTA).
501+
502+
## <a name="aboutapi"/> About API
503+
The library provides a simple operation to retrieve information about the library and the IoTA using it. A GET request
504+
to the `/iot/about` path, will show a payload like the following:
505+
```
506+
{
507+
“version”:”0.5.2”,
508+
“libVersion”:”0.8.4”,
509+
"port":4041,
510+
"baseRoot":"/"
511+
}
512+
```
513+
the `version` field will be read from the `iotaVersion` field of the config, if it exists.
482514

483515
## <a name="provisioningapi"/> Device Provisioning API
484516
### Overview

bin/iotAgentTester.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ function queryContextAttribute(commands) {
9999
},
100100
headers: {
101101
'fiware-service': config.service,
102-
'fiware-servicepath': config.subservice,
103-
'X-Auth-Token': token
102+
'fiware-servicepath': config.subservice
104103
}
105104
};
106105

106+
if (token) {
107+
options.headers['X-Auth-Token'] = token;
108+
}
109+
107110
request(options, function(error, response, body) {
108111
if (error) {
109112
console.error('\nConnection error querying context: ' + error);
@@ -558,7 +561,7 @@ function listGroups(commands) {
558561
method: 'GET',
559562
headers: {
560563
'fiware-service': configIot.service,
561-
'fiware-servicepath': '/*'
564+
'fiware-servicepath': configIot.subservice
562565
}
563566
};
564567

@@ -614,7 +617,7 @@ function authenticate(command) {
614617
};
615618

616619
console.log('Authenticating to host [%s:%s] with user [%s] in service [%s]',
617-
command[0], command[1], command[2], commands[4]);
620+
command[0], command[1], command[2], command[4]);
618621

619622
console.log('----------------------------------------------------------------');
620623

bin/pruebaScript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
authenticate 52.17.64.28 5001 test "e=mc^2" ThinkingThings
2+
configIot 52.17.106.105 8082 ThinkingThings /test
3+
configCb 52.17.106.105 1026 ThinkingThings /test

doc/config-basic-example.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var config = {
2+
logLevel: 'DEBUG',
3+
contextBroker: {
4+
host: 'localhost',
5+
port: '1026'
6+
},
7+
server: {
8+
port: 4041
9+
},
10+
deviceRegistry: {
11+
type: 'memory'
12+
},
13+
types: {},
14+
service: 'howtoService',
15+
subservice: '/howto',
16+
providerUrl: 'http://localhost:4041',
17+
deviceRegistrationDuration: 'P1M',
18+
defaultType: 'Thing'
19+
};
20+
21+
module.exports = config;

doc/finalResult.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
var iotAgentLib = require('iotagent-node-lib'),
2+
http = require('http'),
3+
express = require('express'),
4+
request = require('request'),
5+
config = require('./config');
6+
7+
function parseUl(data, device) {
8+
function findType(name) {
9+
for (var i=0; i < device.active.length; i++) {
10+
if (device.active[i].name === name) {
11+
return device.active[i].type;
12+
}
13+
}
14+
15+
return null;
16+
}
17+
18+
function createAttribute(element) {
19+
var pair = element.split('|'),
20+
attribute = {
21+
name: pair[0],
22+
value: pair[1],
23+
type: findType(pair[0])
24+
};
25+
26+
return attribute;
27+
}
28+
29+
return data.split("#").map(createAttribute);
30+
}
31+
32+
function manageULRequest(req, res, next) {
33+
var values;
34+
35+
iotAgentLib.getDevice(req.query.i, function(error, device) {
36+
if (error) {
37+
res.status(404).send({
38+
message: 'Couldn\'t find the device: ' + JSON.stringify(error)
39+
});
40+
} else {
41+
values = parseUl(req.query.d, device);
42+
iotAgentLib.update(device.name, device.type, '', values, device, function(error) {
43+
if (error) {
44+
res.status(500).send({
45+
message: 'Error updating the device'
46+
});
47+
} else {
48+
res.status(200).send({
49+
message: 'Device successfully updated'
50+
});
51+
}
52+
});
53+
}
54+
});
55+
}
56+
57+
function initSouthbound(callback) {
58+
southboundServer = {
59+
server: null,
60+
app: express(),
61+
router: express.Router()
62+
};
63+
64+
southboundServer.app.set('port', 8080);
65+
southboundServer.app.set('host', '0.0.0.0');
66+
67+
southboundServer.router.get('/iot/d', manageULRequest);
68+
southboundServer.server = http.createServer(southboundServer.app);
69+
southboundServer.app.use('/', southboundServer.router);
70+
71+
southboundServer.server.listen(southboundServer.app.get('port'), southboundServer.app.get('host'), callback);
72+
}
73+
74+
function createResponse(id, type, attributes, body) {
75+
var values = body.split(','),
76+
responses = [];
77+
78+
for (var i = 0; i < attributes.length; i++) {
79+
responses.push({
80+
name: attributes[i],
81+
type: "string",
82+
value: values[i]
83+
});
84+
}
85+
86+
return {
87+
id: id,
88+
type: type,
89+
attributes: responses
90+
};
91+
}
92+
93+
function queryContextHandler(id, type, attributes, callback) {
94+
var options = {
95+
url: 'http://127.0.0.1:9999/iot/d',
96+
method: 'GET',
97+
qs: {
98+
q: attributes.join()
99+
}
100+
};
101+
102+
request(options, function (error, response, body) {
103+
if (error) {
104+
callback(error);
105+
} else {
106+
callback(null, createResponse(id, type, attributes, body));
107+
}
108+
});
109+
}
110+
111+
function createQueryFromAttributes(attributes) {
112+
var query = "";
113+
114+
for (var i in attributes) {
115+
query += attributes[i].name + '|' + attributes[i].value;
116+
117+
if (i != attributes.length -1) {
118+
query += ',';
119+
}
120+
}
121+
122+
return query;
123+
}
124+
125+
function updateContextHandler(id, type, attributes, callback) {
126+
var options = {
127+
url: 'http://127.0.0.1:9999/iot/d',
128+
method: 'GET',
129+
qs: {
130+
d: createQueryFromAttributes(attributes)
131+
}
132+
};
133+
134+
request(options, function (error, response, body) {
135+
if (error) {
136+
callback(error);
137+
} else {
138+
callback(null, {
139+
id: id,
140+
type: type,
141+
attributes: attributes
142+
});
143+
}
144+
});
145+
}
146+
147+
function provisioningHandler(device, callback) {
148+
console.log('\n\n* REGISTERING A NEW DEVICE:\n%s\n\n', JSON.stringify(device, null, 4));
149+
150+
device.type = 'CertifiedType';
151+
152+
callback(null, device);
153+
}
154+
155+
function configurationHandler(configuration, callback) {
156+
console.log('\n\n* REGISTERING A NEW CONFIGURATION:\n%s\n\n', JSON.stringify(configuration, null, 4));
157+
callback(null, configuration);
158+
}
159+
160+
iotAgentLib.activate(config, function(error) {
161+
if (error) {
162+
console.log('There was an error activating the IOTA');
163+
process.exit(1);
164+
} else {
165+
iotAgentLib.setDataUpdateHandler(updateContextHandler);
166+
iotAgentLib.setDataQueryHandler(queryContextHandler);
167+
iotAgentLib.setConfigurationHandler(configurationHandler);
168+
iotAgentLib.setProvisioningHandler(provisioningHandler);
169+
170+
initSouthbound(function (error) {
171+
if (error) {
172+
console.log('Could not initialize South bound API due to the following error: %s', error);
173+
} else {
174+
console.log('Both APIs started successfully');
175+
}
176+
});
177+
}
178+
});
179+
180+

0 commit comments

Comments
 (0)