/
commonBindings.js
364 lines (339 loc) · 13.7 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
/*
* 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: Fernando Méndez, Daniel Calvo - ATOS Research & Innovation
*/
/* eslint-disable no-prototype-builtins */
const iotAgentLib = require('iotagent-node-lib');
const regenerateTransid = iotAgentLib.regenerateTransid;
const config = require('./configService');
const transportSelector = require('./transportSelector');
const intoTrans = iotAgentLib.intoTrans;
const finishSouthBoundTransaction = iotAgentLib.finishSouthBoundTransaction;
const fillService = iotAgentLib.fillService;
const _ = require('underscore');
const utils = require('./iotaUtils');
const async = require('async');
const ulParser = require('./ulParser');
const constants = require('./constants');
let context = {
op: 'IOTAUL.Common.Binding'
};
/**
* 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.
* @return {String} String identifier of the attribute type.
*/
function guessType(attribute, device) {
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;
}
return constants.DEFAULT_ATTRIBUTE_TYPE;
}
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 JSON object received.
*/
function manageConfigurationRequest(apiKey, deviceId, device, objMessage) {
utils.manageConfiguration(
apiKey,
deviceId,
device,
objMessage,
async.apply(sendConfigurationToDevice, device),
function (error) {
if (error) {
iotAgentLib.alarms.raise(constants.MQTTB_ALARM, error);
} else {
iotAgentLib.alarms.release(constants.MQTTB_ALARM);
context = fillService(context, device);
config
.getLogger()
.debug(context, 'Configuration request finished for APIKey %s and Device %s', apiKey, deviceId);
}
finishSouthBoundTransaction(null);
}
);
}
/**
* Utility function used to reduce UL Payload group arrays and process them to update their value in the Context Broker.
* This function process a new element of the payload group, generating a function ready to be called with a callback
* in a async.series() call.
*
* @param {Object} device Object containing all the information about the device sending the measures.
* @param {String} apikey APIKey of the service the device belongs to.
* @param {Array} previous Array of prepared functions that send information to the Context Broker.
* @param {Object} current Information sent by the device.
* @param {Number} index Index of the group in the array.
* @return {Array} Updated array of functions.
*/
/* eslint-disable-next-line no-unused-vars */
function processMeasureGroup(device, apikey, previous, current, index) {
const values = [];
if (current.command) {
previous.push(
iotAgentLib.setCommandResult.bind(
null,
device.name,
config.getConfig().iota.defaultResource,
apikey,
current.command,
current.value,
constants.COMMAND_STATUS_COMPLETED,
device
)
);
} else {
for (const k in current) {
if (current.hasOwnProperty(k)) {
values.push({
name: k,
type: guessType(k, device),
value: current[k]
});
}
}
previous.push(iotAgentLib.update.bind(null, device.name, device.type, '', values, device));
}
return previous;
}
/**
* 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 {Object} device Device object containing all the information about a device.
* @param {String} messageStr UL payload parsed to string.
*/
function multipleMeasures(apiKey, device, messageStr) {
let updates = [];
let parsedMessage;
context = fillService(context, device);
config.getLogger().debug(context, 'Processing multiple measures for device %s with apiKey %s', device.id, apiKey);
// Parse Message
try {
parsedMessage = ulParser.parse(messageStr);
} catch (e) {
config.getLogger().error(context, 'MEASURES-003: Parse error parsing incoming message %', messageStr);
return;
}
config.getLogger().debug(context, 'stringMessage: %s parsedMessage: %s', messageStr, parsedMessage);
updates = parsedMessage.reduce(processMeasureGroup.bind(null, device, apiKey), []);
async.series(updates, 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 {
// prettier-ignore
config.getLogger().info(
context,
'Multiple measures for device %s with apiKey %s successfully updated',
device.id,
apiKey
);
}
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} attribute Name of the attribute to update.
* @param {Object} device Device object containing all the information about a device.
* @param {Buffer} messageStr Raw message coming from the client parsed to string.
* @param {Buffer} message Raw message coming from the client.
*/
function singleMeasure(apiKey, attribute, device, messageStr, message) {
context = fillService(context, device);
config
.getLogger()
.debug('Processing single measure %s for device %s with apiKey %s', messageStr, device.id, apiKey);
let value;
try {
value = JSON.parse(messageStr);
} catch (e) {
value = messageStr || message.toString('hex');
}
const values = [
{
name: attribute,
type: guessType(attribute, device),
value: value
}
];
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 {
// prettier-ignore
config.getLogger().debug(
context,
'Single measure for device %s with apiKey %s successfully updated',
device.id,
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/attributes[/<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('/');
let parsedMessage;
if (topicInformation[1].toLowerCase() === 'ul') {
topicInformation.splice(1, 1);
}
const apiKey = topicInformation[1];
const deviceId = topicInformation[2];
const messageStr = message.toString();
function processMessageForDevice(device, apiKey, topicInformation) {
iotAgentLib.alarms.release(constants.MQTTB_ALARM);
if (
topicInformation[3] === constants.CONFIGURATION_SUFIX &&
topicInformation[4] === constants.CONFIGURATION_COMMAND_SUFIX &&
message
) {
parsedMessage = ulParser.parseConfigurationRequest(messageStr);
manageConfigurationRequest(apiKey, deviceId, device, parsedMessage);
} else if (topicInformation[3] === constants.CONFIGURATION_COMMAND_UPDATE) {
const commandObj = ulParser.result(messageStr);
utils.updateCommand(
apiKey,
device,
commandObj.result,
commandObj.command,
constants.COMMAND_STATUS_COMPLETED,
function (error) {
config.getLogger().debug('Command updated with result: %s', error);
}
);
} else if (topicInformation[4]) {
singleMeasure(apiKey, topicInformation[4], device, messageStr, message);
} else if (topicInformation[3] === constants.MEASURES_SUFIX) {
multipleMeasures(apiKey, device, messageStr);
} else {
context = fillService(context, device);
config.getLogger().error(
context,
/*jshint quotmark: double */
"MEASURES-004: 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);
}
}
utils.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.processMeasureGroup = processMeasureGroup;
exports.guessType = guessType;