Skip to content

Commit bb92da7

Browse files
committed
Merge pull request #381 from telefonicaid/task/addTimestampProcessingPlugin
Task/add timestamp processing plugin
2 parents 0b6df99 + 43f15a5 commit bb92da7

File tree

7 files changed

+252
-1
lines changed

7 files changed

+252
-1
lines changed

CHANGES_NEXT_RELEASE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@
2222
- Fix Endpoint attribute not stored in mongo (#369)
2323
- Fix Add a prefix for all the environment variables (#375)
2424
- Assess command_result field (#374)
25+
- Add TimeInstant processing plugin (#380)
26+

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,10 @@ inclusion in the system, regardless of the value they carried in the Southbound
13331333
the events in the IoT Agent with the configured type name will be marked as events. The event name can be configured
13341334
in the `config.eventType` attribute.
13351335

1336+
#### Timestamp Processing Plugin (timestampProcess)
1337+
This plugin processes the entity attributes looking for a TimeInstant attribute. If one is found, the plugin add a
1338+
TimeInstant attribute as metadata for every other attribute in the same request.
1339+
13361340
## <a name="development"/> Development documentation
13371341
### Contributions
13381342
All contributions to this project are welcome. Developers planning to contribute should follow the [Contribution Guidelines](./docs/contribution.md)

lib/fiware-iotagent-lib.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,6 @@ exports.commandLine = require('./command/commandLine');
202202
exports.dataPlugins = {
203203
compressTimestamp: require('./plugins/compressTimestamp'),
204204
attributeAlias: require('./plugins/attributeAlias'),
205-
addEvents: require('./plugins/addEvent')
205+
addEvents: require('./plugins/addEvent'),
206+
timestampProcess: require('./plugins/timestampProcessPlugin')
206207
};

lib/plugins/timestampProcessPlugin.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U
3+
*
4+
* This file is part of fiware-iotagent-lib
5+
*
6+
* fiware-iotagent-lib is free software: you can redistribute it and/or
7+
* modify it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the License,
9+
* or (at your option) any later version.
10+
*
11+
* fiware-iotagent-lib is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public
17+
* License along with fiware-iotagent-lib.
18+
* If not, seehttp://www.gnu.org/licenses/.
19+
*
20+
* For those usages not covered by the GNU Affero General Public License
21+
* please contact with::[email protected]
22+
*/
23+
24+
'use strict';
25+
26+
var errors = require('../errors'),
27+
constants = require('../constants'),
28+
logger = require('logops'),
29+
context = {
30+
op: 'IoTAgentNGSI.TimestampProcessPlugin'
31+
};
32+
33+
/**
34+
* Looks for Thinking Thing modules and parses them, updating the entity with the transformed value.
35+
*
36+
* @param {Object} entity NGSI Entity as it would have been sent before the plugin.
37+
*/
38+
function updatePlugin(entity, entityType, callback) {
39+
var timestamp;
40+
41+
function insertMetadata(element) {
42+
if (element.name !== constants.TIMESTAMP_ATTRIBUTE) {
43+
element.metadatas = [{
44+
name: constants.TIMESTAMP_ATTRIBUTE,
45+
type: constants.TIMESTAMP_TYPE,
46+
value: timestamp.value
47+
}];
48+
}
49+
50+
return element;
51+
}
52+
53+
if (entity.contextElements && entity.contextElements[0] && entity.contextElements[0].attributes) {
54+
for (var i in entity.contextElements[0].attributes) {
55+
if (entity.contextElements[0].attributes[i].name === constants.TIMESTAMP_ATTRIBUTE) {
56+
timestamp = entity.contextElements[0].attributes[i];
57+
}
58+
}
59+
60+
if (timestamp) {
61+
entity.contextElements[0].attributes = entity.contextElements[0].attributes.map(insertMetadata);
62+
}
63+
64+
callback(null, entity, entityType);
65+
} else {
66+
logger.error(context, 'Bad payload received while processing timestamps');
67+
callback(new errors.WrongSyntax(entity));
68+
}
69+
70+
}
71+
72+
exports.update = updatePlugin;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"contextElements": [
3+
{
4+
"type": "Light",
5+
"isPattern": "false",
6+
"id": "light1",
7+
"attributes": [
8+
{
9+
"name": "state",
10+
"type": "Boolean",
11+
"value": "true",
12+
"metadatas": [
13+
{
14+
"name": "TimeInstant",
15+
"type": "ISO8601",
16+
"value": "2016-05-30T16:25:22.304Z"
17+
}
18+
]
19+
},
20+
{
21+
"name": "TimeInstant",
22+
"type": "ISO8601",
23+
"value": "2016-05-30T16:25:22.304Z"
24+
}
25+
]
26+
}
27+
],
28+
"updateAction": "UPDATE"
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"contextResponses": [
3+
{
4+
"contextElement": {
5+
"attributes": [
6+
{
7+
"name": "state",
8+
"type": "Boolean",
9+
"value": ""
10+
},
11+
{
12+
"name": "TimeInstant",
13+
"type": "ISO8601",
14+
"value": ""
15+
}
16+
],
17+
"id": "light1",
18+
"isPattern": "false",
19+
"type": "Light"
20+
},
21+
"statusCode": {
22+
"code": "200",
23+
"reasonPhrase": "OK"
24+
}
25+
}
26+
]
27+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U
3+
*
4+
* This file is part of fiware-iotagent-lib
5+
*
6+
* fiware-iotagent-lib is free software: you can redistribute it and/or
7+
* modify it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the License,
9+
* or (at your option) any later version.
10+
*
11+
* fiware-iotagent-lib is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public
17+
* License along with fiware-iotagent-lib.
18+
* If not, seehttp://www.gnu.org/licenses/.
19+
*
20+
* For those usages not covered by the GNU Affero General Public License
21+
* please contact with::[email protected]
22+
*/
23+
24+
'use strict';
25+
26+
var iotAgentLib = require('../../../lib/fiware-iotagent-lib'),
27+
utils = require('../../tools/utils'),
28+
should = require('should'),
29+
logger = require('logops'),
30+
nock = require('nock'),
31+
contextBrokerMock,
32+
iotAgentConfig = {
33+
contextBroker: {
34+
host: '192.168.1.1',
35+
port: '1026'
36+
},
37+
server: {
38+
port: 4041
39+
},
40+
types: {
41+
'Light': {
42+
commands: [],
43+
type: 'Light',
44+
lazy: [
45+
{
46+
name: 'temperature',
47+
type: 'centigrades'
48+
}
49+
],
50+
active: [
51+
{
52+
name: 'pressure',
53+
type: 'Hgmm'
54+
}
55+
]
56+
}
57+
},
58+
service: 'smartGondor',
59+
subservice: 'gardens',
60+
providerUrl: 'http://smartGondor.com',
61+
deviceRegistrationDuration: 'P1M',
62+
throttling: 'PT5S'
63+
};
64+
65+
describe('Timestamp processing plugin', function() {
66+
beforeEach(function(done) {
67+
logger.setLevel('FATAL');
68+
69+
iotAgentLib.activate(iotAgentConfig, function() {
70+
iotAgentLib.clearAll(function() {
71+
iotAgentLib.addUpdateMiddleware(iotAgentLib.dataPlugins.timestampProcess.update);
72+
done();
73+
});
74+
});
75+
});
76+
77+
afterEach(function(done) {
78+
iotAgentLib.clearAll(function() {
79+
iotAgentLib.deactivate(done);
80+
});
81+
});
82+
describe('When an update comes with a timestamp through the plugin', function() {
83+
var values = [
84+
{
85+
name: 'state',
86+
type: 'Boolean',
87+
value: 'true'
88+
},
89+
{
90+
name: 'TimeInstant',
91+
type: 'ISO8601',
92+
value: '2016-05-30T16:25:22.304Z'
93+
}
94+
];
95+
96+
beforeEach(function() {
97+
nock.cleanAll();
98+
99+
contextBrokerMock = nock('http://192.168.1.1:1026')
100+
.matchHeader('fiware-service', 'smartGondor')
101+
.matchHeader('fiware-servicepath', 'gardens')
102+
.post('/v1/updateContext', utils.readExampleFile(
103+
'./test/unit/examples/contextRequests/updateContextProcessTimestamp1.json'))
104+
.reply(200, utils.readExampleFile(
105+
'./test/unit/examples/contextResponses/updateContextProcessTimestamp1Success.json'));
106+
});
107+
108+
it('should return an entity with all its timestamps expanded to have separators', function(done) {
109+
iotAgentLib.update('light1', 'Light', '', values, function(error) {
110+
should.not.exist(error);
111+
contextBrokerMock.done();
112+
done();
113+
});
114+
});
115+
});
116+
});

0 commit comments

Comments
 (0)