-
Notifications
You must be signed in to change notification settings - Fork 89
/
commonBindings.js
423 lines (399 loc) · 17 KB
/
commonBindings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of iotagent-ul
*
* iotagent-ul is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* iotagent-ul is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with iotagent-ul.
* If not, seehttp://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[iot_support@tid.es]
*
* Modified by: Daniel Calvo - ATOS Research & Innovation
*/
/* eslint-disable no-prototype-builtins */
const iotAgentLib = require('iotagent-node-lib');
const regenerateTransid = iotAgentLib.regenerateTransid;
const intoTrans = iotAgentLib.intoTrans;
const finishSouthBoundTransaction = iotAgentLib.finishSouthBoundTransaction;
const fillService = iotAgentLib.fillService;
const _ = require('underscore');
const commandHandler = require('./commandHandler');
const transportSelector = require('./transportSelector');
const async = require('async');
const iotaUtils = require('./iotaUtils');
const constants = require('./constants');
let context = {
op: 'IoTAgentJSON.commonBinding'
};
const config = require('./configService');
/**
* Parse a message received from a Topic.
*
* @param {Buffer} message Message to be parsed
* @return {Object} Parsed message or null if an error has occurred.
*/
function parseMessage(message) {
let parsedMessage;
let messageArray;
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
const stringMessage = message.toString();
try {
parsedMessage = JSON.parse(stringMessage);
} catch (e) {
parsedMessage = message.toString('hex');
}
config.getLogger().debug(context, 'stringMessage: %s parsedMessage: %s', stringMessage, parsedMessage);
messageArray = [];
if (Array.isArray(parsedMessage)) {
messageArray = parsedMessage;
} else {
messageArray.push(parsedMessage);
}
config.getLogger().debug(context, 'parserMessage array: %s', messageArray);
return messageArray;
}
/**
* Find the attribute given by its name between all the active attributes of the given device, returning its type, or
* null otherwise.
*
* @param {String} attribute Name of the attribute to find.
* @param {Object} device Device object containing all the information about a device.
* @param {measureType} type Type of measure attribute according with measure when available (ngsiv2 and ngsild measures)
* @return {String} String identifier of the attribute type.
*/
function guessType(attribute, device, measureType) {
if (device.active) {
for (let i = 0; i < device.active.length; i++) {
if (device.active[i].name === attribute) {
return device.active[i].type;
}
}
}
if (attribute === constants.TIMESTAMP_ATTRIBUTE) {
return constants.TIMESTAMP_TYPE_NGSI2;
}
if (measureType) {
return measureType;
} else {
return constants.DEFAULT_ATTRIBUTE_TYPE;
}
}
function extractAttributes(device, current, payloadType) {
let values = [];
const ctxt = fillService(context, device);
config.getLogger().debug(ctxt, 'extractAttributes current %j payloadType %j', current, payloadType);
if (payloadType && [constants.PAYLOAD_NGSIv2, constants.PAYLOAD_NGSILD].includes(payloadType.toLowerCase())) {
let arrayEntities = [];
if (current.hasOwnProperty('actionType') && current.hasOwnProperty('entities')) {
arrayEntities = current.entities;
} else {
arrayEntities = [current];
}
for (const entity of arrayEntities) {
const valuesEntity = [];
for (const k in entity) {
if (entity.hasOwnProperty(k)) {
if (['id', 'type'].includes(k)) {
// Include ngsi id and type as measures by inserting here as is
// and later in iota-node-lib sendUpdateValueNgsi2 rename as measure_X
valuesEntity.push({
name: k,
type: guessType(k, device, null),
value: entity[k]
});
} else {
if (payloadType.toLowerCase() === constants.PAYLOAD_NGSIv2) {
valuesEntity.push({
name: k,
type: guessType(k, device, entity[k].type),
value: entity[k].value,
metadata: entity[k].metadata ? entity[k].metadata : undefined
});
} else if (payloadType.toLowerCase() === constants.PAYLOAD_NGSILD) {
const ent = {
name: k
};
if (k.toLowerCase() === '@context') {
ent.type = '@context';
ent.value = entity[k];
} else {
if (entity[k].type) {
ent.type = guessType(k, device, entity[k].type);
if (['property', 'geoproperty'].includes(entity[k].type.toLowerCase())) {
ent.value = entity[k].value;
} else if (entity[k].type.toLowerCase() === 'relationship') {
ent.value = entity[k].object;
}
}
// Add other stuff as metadata
for (const key in entity[k]) {
if (!['type', 'value', 'object'].includes(key.toLowerCase())) {
if (!ent.metadata) {
ent.metadata = {};
}
ent.metadata[key] = { value: entity[k][key] };
}
}
}
valuesEntity.push(ent);
}
}
}
}
if (arrayEntities.length > 1) {
values.push(valuesEntity); // like a multimeasure
} else {
values = valuesEntity;
}
}
} else {
for (const k in current) {
if (current.hasOwnProperty(k)) {
values.push({
name: k,
type: guessType(k, device, null),
value: current[k]
});
}
}
}
return values;
}
function sendConfigurationToDevice(device, apiKey, group, deviceId, results, callback) {
iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', apiKey, function (
error,
foundGroup
) {
if (!error) {
group = foundGroup;
}
transportSelector.applyFunctionFromBinding(
[apiKey, group, deviceId, results],
'sendConfigurationToDevice',
device.transport || group.transport || config.getConfig().defaultTransport,
callback
);
});
}
/**
* Deals with configuration requests coming from the device. Whenever a new configuration requests arrives with a list
* of attributes to retrieve, this handler asks the Context Broker for the values of those attributes, and publish a
* new message in the "/1234/MQTT_2/configuration/values" topic
*
* @param {String} apiKey API Key corresponding to the Devices configuration.
* @param {String} deviceId Id of the device to be updated.
* @param {Object} device Device object containing all the information about a device.
* @param {Object} objMessage Array of JSON object received.
*/
function manageConfigurationRequest(apiKey, deviceId, device, objMessage) {
const ctxt = fillService(context, device);
for (let i = 0; i < objMessage.length; i++) {
iotaUtils.manageConfiguration(
apiKey,
deviceId,
device,
objMessage[i],
async.apply(sendConfigurationToDevice, device),
function (error) {
if (error) {
iotAgentLib.alarms.raise(constants.MQTTB_ALARM, error);
} else {
iotAgentLib.alarms.release(constants.MQTTB_ALARM);
config
.getLogger()
.debug(ctxt, 'Configuration request finished for APIKey %s and Device %s', apiKey, deviceId);
}
finishSouthBoundTransaction(null);
}
);
}
}
/**
* Adds a single measure to the context broker. The message for single measures contains the direct value to
* be inserted in the attribute, given by its name.
*
* @param {String} apiKey API Key corresponding to the Devices configuration.
* @param {String} deviceId Id of the device to be updated.
* @param {String} attribute Name of the attribute to update.
* @param {Object} device Device object containing all the information about a device.
* @param {Object} parsedMessage ParsedMessage (JSON or string) message coming from the client.
*/
function singleMeasure(apiKey, deviceId, attribute, device, parsedMessage) {
context = fillService(context, device);
config.getLogger().debug(context, 'Processing single measure for device %s with apiKey %s', deviceId, apiKey);
const values = [
{
name: attribute,
type: guessType(attribute, device, null),
value: parsedMessage[0]
}
];
config.getLogger().debug(context, 'values updates %s', JSON.stringify(values));
iotAgentLib.update(device.name, device.type, '', values, device, function (error) {
if (error) {
config.getLogger().error(
context,
/*jshint quotmark: double */
"MEASURES-002: Couldn't send the updated values to the Context Broker due to an error: %j",
/*jshint quotmark: single */
error
);
} else {
config
.getLogger()
.debug(context, 'Single measure for device %s with apiKey %s successfully updated', deviceId, apiKey);
}
finishSouthBoundTransaction(null);
});
}
/**
* Adds multiple measures to the Context Broker. Multiple measures come in the form of single-level JSON objects,
* whose keys are the attribute names and whose values are the attribute values.
*
* @param {String} apiKey API Key corresponding to the Devices configuration.
* @param {String} deviceId Id of the device to be updated.
* @param {Object} device Device object containing all the information about a device.
* @param {Object} messageObj Array of JSON object sent using.
*/
function multipleMeasures(apiKey, deviceId, device, messageObj) {
let measure;
const ctxt = fillService(context, device);
config.getLogger().debug(context, 'Processing multiple measures for device %s with apiKey %s', deviceId, apiKey);
for (let j = 0; j < messageObj.length; j++) {
measure = messageObj[j];
let attributesArray = [];
const values = extractAttributes(device, measure, device.payloadType);
if (values && values[0] && values[0][0]) {
// multi measure is extracted due to payloadType is ngsi
attributesArray = values;
} else {
attributesArray = [values];
}
config
.getLogger()
.debug(
context,
'Processing multiple measures for device %s with apiKey %s values %j',
deviceId,
apiKey,
attributesArray
);
iotAgentLib.update(device.name, device.type, '', attributesArray, device, function (error) {
if (error) {
config.getLogger().error(
ctxt,
/*jshint quotmark: double */
"MEASURES-002: Couldn't send the updated values to the Context Broker due to an error: %j",
/*jshint quotmark: single */
error
);
} else {
config
.getLogger()
.info(
ctxt,
'Multiple measures for device %s with apiKey %s successfully updated',
deviceId,
apiKey
);
}
finishSouthBoundTransaction(null);
});
}
}
/**
* Handles an incoming message, extracting the API Key, device Id and attribute to update (in the case of single
* measures) from the topic.
*
* @param {String} topic Topic of the form: '/<APIKey>/deviceId/attrs[/<attributeName>]'.
* @param {Object} message message body (Object or Buffer, depending on the value).
*/
function messageHandler(topic, message) {
if (topic[0] !== '/') {
topic = '/' + topic;
}
const topicInformation = topic.split('/');
if (topicInformation[1].toLowerCase() === 'json') {
topicInformation.splice(1, 1);
}
const apiKey = topicInformation[1];
const deviceId = topicInformation[2];
const parsedMessage = parseMessage(message);
function processMessageForDevice(device, apiKey, topicInformation) {
if (topicInformation[3] === 'configuration' && topicInformation[4] === 'commands' && parsedMessage) {
manageConfigurationRequest(apiKey, deviceId, device, parsedMessage);
} else if (topicInformation[4]) {
singleMeasure(apiKey, deviceId, topicInformation[4], device, parsedMessage);
} else if (topicInformation[3] === constants.CONFIGURATION_COMMAND_UPDATE) {
for (let i = 0; i < parsedMessage.length; i++) {
commandHandler.updateCommand(apiKey, deviceId, device, parsedMessage[i]);
}
} else if (parsedMessage && Array.isArray(parsedMessage) && parsedMessage.every((x) => typeof x === 'object')) {
// it must be an array of object
multipleMeasures(apiKey, deviceId, device, parsedMessage);
} else {
context = fillService(context, device);
config.getLogger().error(
context,
/*jshint quotmark: double */
"Couldn't process message %s for device %j due to format issues.",
/*jshint quotmark: single */
message,
device
);
}
}
function processDeviceMeasure(error, device) {
if (error) {
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
config.getLogger().warn(context, 'MEASURES-004: Device not found for topic %s', topic);
} else {
const localContext = _.clone(context);
localContext.service = device.service;
localContext.subservice = device.subservice;
intoTrans(localContext, processMessageForDevice)(device, apiKey, topicInformation);
}
}
iotAgentLib.alarms.release(constants.MQTTB_ALARM);
iotaUtils.retrieveDevice(deviceId, apiKey, processDeviceMeasure);
}
/**
* Handles an incoming AMQP message, extracting the API Key, device Id and attribute to update (in the case of single
* measures) from the AMQP topic.
*
* @param {String} topic Topic of the form: '/<APIKey>/deviceId/attributes[/<attributeName>]'.
* @param {Object} message AMQP message body (Object or Buffer, depending on the value).
*/
function amqpMessageHandler(topic, message) {
regenerateTransid(topic);
messageHandler(topic, message);
}
/**
* Handles an incoming MQTT message, extracting the API Key, device Id and attribute to update (in the case of single
* measures) from the MQTT topic.
*
* @param {String} topic Topic of the form: '/<APIKey>/deviceId/attributes[/<attributeName>]'.
* @param {Object} message MQTT message body (Object or Buffer, depending on the value).
*/
function mqttMessageHandler(topic, message) {
regenerateTransid(topic);
config.getLogger().debug(context, 'message topic: %s', topic);
messageHandler(topic, message);
}
exports.amqpMessageHandler = amqpMessageHandler;
exports.mqttMessageHandler = mqttMessageHandler;
exports.messageHandler = messageHandler;
exports.extractAttributes = extractAttributes;
exports.guessType = guessType;