-
Notifications
You must be signed in to change notification settings - Fork 55
/
MQTTBinding.js
449 lines (419 loc) · 16.4 KB
/
MQTTBinding.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* 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::[[email protected]]
*/
/* eslint-disable consistent-return */
const fs = require('fs');
const iotAgentLib = require('iotagent-node-lib');
const commonBindings = require('./../commonBindings');
const utils = require('../iotaUtils');
const ulParser = require('../ulParser');
const mqtt = require('mqtt');
const async = require('async');
const constants = require('../constants');
const fillService = iotAgentLib.fillService;
let context = {
op: 'IOTAUL.MQTT.Binding'
};
let mqttClient;
let mqttConn;
const config = require('../configService');
/**
* Generate the list of global topics to listen to.
*/
function generateTopics(callback) {
const topics = [];
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
config.getLogger().debug(context, 'Generating topics');
// With leading slashes
topics.push(constants.MQTT_SHARE_SUBSCRIPTION_GROUP + '/+/+/' + constants.MEASURES_SUFIX + '/+');
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
'/' +
constants.MQTT_TOPIC_PROTOCOL +
'/+/+/' +
constants.MEASURES_SUFIX +
'/+'
);
topics.push(constants.MQTT_SHARE_SUBSCRIPTION_GROUP + '/+/+/' + constants.MEASURES_SUFIX);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
'/' +
constants.MQTT_TOPIC_PROTOCOL +
'/+/+/' +
constants.MEASURES_SUFIX
);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
'/+/+/' +
constants.CONFIGURATION_SUFIX +
'/' +
constants.CONFIGURATION_COMMAND_SUFIX
);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
'/' +
constants.MQTT_TOPIC_PROTOCOL +
'/+/+/' +
constants.CONFIGURATION_SUFIX +
'/' +
constants.CONFIGURATION_COMMAND_SUFIX
);
topics.push(constants.MQTT_SHARE_SUBSCRIPTION_GROUP + '/+/+/' + constants.CONFIGURATION_COMMAND_UPDATE);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
'/' +
constants.MQTT_TOPIC_PROTOCOL +
'/+/+/' +
constants.CONFIGURATION_COMMAND_UPDATE
);
//Without leading slashes
topics.push(constants.MQTT_SHARE_SUBSCRIPTION_GROUP + '+/+/' + constants.MEASURES_SUFIX + '/+');
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
constants.MQTT_TOPIC_PROTOCOL +
'/+/+/' +
constants.MEASURES_SUFIX +
'/+'
);
topics.push(constants.MQTT_SHARE_SUBSCRIPTION_GROUP + '+/+/' + constants.MEASURES_SUFIX);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP + constants.MQTT_TOPIC_PROTOCOL + '/+/+/' + constants.MEASURES_SUFIX
);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
'+/+/' +
constants.CONFIGURATION_SUFIX +
'/' +
constants.CONFIGURATION_COMMAND_SUFIX
);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
constants.MQTT_TOPIC_PROTOCOL +
'/+/+/' +
constants.CONFIGURATION_SUFIX +
'/' +
constants.CONFIGURATION_COMMAND_SUFIX
);
topics.push(constants.MQTT_SHARE_SUBSCRIPTION_GROUP + '+/+/' + constants.CONFIGURATION_COMMAND_UPDATE);
topics.push(
constants.MQTT_SHARE_SUBSCRIPTION_GROUP +
constants.MQTT_TOPIC_PROTOCOL +
'/+/+/' +
constants.CONFIGURATION_COMMAND_UPDATE
);
callback(null, topics);
}
/**
* Recreate the MQTT subscriptions.
*/
function recreateSubscriptions(callback) {
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
config.getLogger().debug(context, 'Recreating global subscriptions for all devices');
function subscribeToTopics(topics, callback) {
config.getLogger().debug('Subscribing to topics: %j', topics);
const options = {};
mqttClient.subscribe(topics, options, function (error) {
if (error) {
iotAgentLib.alarms.raise(constants.MQTTB_ALARM, error);
config.getLogger().error(context, ' GLOBAL-001: Error subscribing to topics: %s', error);
callback(error);
} else {
iotAgentLib.alarms.release(constants.MQTTB_ALARM);
config.getLogger().info('Successfully subscribed to the following topics: %j', topics);
if (callback) {
callback(null);
}
}
});
}
async.waterfall([generateTopics, subscribeToTopics], callback);
}
/**
* Unsubscribe the MQTT Client for all the topics of all the devices of all the services.
*/
function unsubscribeAll(callback) {
function unsubscribeFromTopics(topics, callback) {
mqttClient.unsubscribe(topics, null);
callback();
}
async.waterfall([generateTopics, unsubscribeFromTopics], callback);
}
/**
* Generate a function that executes the given command in the device.
*
* @param {String} apiKey APIKey of the device's service or default APIKey.
* @param {Object} device Object containing all the information about a device.
* @param {Object} attribute Attribute in NGSI format.
* @return {Function} Command execution function ready to be called with async.series.
*/
function generateCommandExecution(apiKey, device, attribute) {
const cmdName = attribute.name;
const cmdAttributes = attribute.value;
const options = {};
const payload = ulParser.createCommandPayload(device, cmdName, cmdAttributes);
const commands = Object.assign({}, ...device.commands.map((c) => ({ [c.name]: c })));
context = fillService(context, device);
options.qos =
commands[cmdName].mqtt && commands[cmdName].mqtt.qos
? parseInt(commands[cmdName].mqtt.qos)
: config.getConfig().mqtt.qos
? parseInt(config.getConfig().mqtt.qos)
: 0;
options.retain =
commands[cmdName].mqtt && commands[cmdName].mqtt.retain
? commands[cmdName].mqtt.retain
: config.getConfig().mqtt.retain
? config.getConfig().mqtt.retain
: false;
const commandTopic = '/' + apiKey + '/' + device.id + '/cmd';
config
.getLogger()
.info(
context,
'Sending command execution to %s with payload %s and mqtt options %j',
commandTopic,
payload,
options
);
return mqttClient.publish.bind(mqttClient, commandTopic, payload, options);
}
/**
* Handles a command execution request coming from the Context Broker. This handler should:
* - Identify the device affected by the command.
* - Send the command to the appropriate MQTT topic.
* - Update the command status in the Context Broker.
*
* @param {Object} device Device data stored in the IOTA.
* @param {String} attributes Command attributes (in NGSIv1 format).
*/
function commandHandler(device, attributes, callback) {
context = fillService(context, device);
config.getLogger().debug(context, 'Handling MQTT command for device %s', device.id);
utils.getEffectiveApiKey(device.service, device.subservice, device, function (error, apiKey) {
async.series(attributes.map(generateCommandExecution.bind(null, apiKey, device)), callback);
});
}
/**
* Extract all the information from a Context Broker response and send it to the topic indicated by the APIKey and
* DeviceId.
*
* @param {String} apiKey API Key for the Device Group
* @param {String} deviceId ID of the Device.
* @param {Object} results Context Broker response.
*/
function sendConfigurationToDevice(apiKey, group, deviceId, results, callback) {
const configurations = utils.createConfigurationNotification(results);
const options = {};
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
if (config.getConfig().mqtt.qos) {
options.qos = parseInt(config.getConfig().mqtt.qos) || 0;
}
if (config.getConfig().mqtt.retain === true) {
options.retain = config.getConfig().mqtt.retain;
}
const payload = ulParser.createConfigurationPayload(deviceId, configurations);
config
.getLogger()
.debug(
context,
'Sending requested configuration to device %s with apikey %s and payload %s ',
deviceId,
apiKey,
payload
);
const leadingSlash = config.getConfig().mqtt.avoidLeadingSlash ? '' : '/';
const commandTopic =
leadingSlash +
apiKey +
'/' +
deviceId +
'/' +
constants.CONFIGURATION_SUFIX +
'/' +
constants.CONFIGURATION_VALUES_SUFIX;
if (mqttClient.connected) {
mqttClient.publish(commandTopic, payload, options, (error) => {
if (error) {
config
.getLogger()
.error(
context,
'Error %j in Configuration: %j sent to the device %s with mqtt options %j',
error,
payload,
commandTopic,
options
);
}
});
config.getLogger().info(context, 'Configuration: %j was sent to the device %s', payload, commandTopic);
} else {
config
.getLogger()
.error(
context,
'Configuration: %j was not sent to the device %s due to not connected',
payload,
commandTopic
);
}
callback();
}
/**
* Device provisioning handler.
*
* @param {Object} device Device object containing all the information about the provisioned device.
*/
function deviceProvisioningHandler(device, callback) {
callback(null, device);
}
/**
* Device updating handler.
*
* @param {Object} device Device object containing all the information about the provisioned device.
*/
function deviceUpdatingHandler(device, oldDevice, callback) {
callback(null, device);
}
/**
* Starts the IoT Agent with the passed configuration. This method also starts the listeners for all the transport
* binding plugins.
*/
function start(callback) {
const mqttConfig = config.getConfig().mqtt;
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
if (!mqttConfig) {
return config.getLogger().error(context, 'Error MQTT is not configured');
}
if (mqttConfig.disabled) {
return config.getLogger().warn(context, 'MQTT is disabled');
}
const rejectUnauthorized =
typeof mqttConfig.rejectUnauthorized === 'boolean' ? mqttConfig.rejectUnauthorized : true;
let rndSuffix = '_' + Math.random().toString(16).substr(2, 8);
const options = {
protocol: mqttConfig.protocol ? mqttConfig.protocol : 'mqtt',
host: mqttConfig.host ? mqttConfig.host : 'localhost',
port: mqttConfig.port ? mqttConfig.port : 1883,
key: mqttConfig.key ? fs.readFileSync(mqttConfig.key, 'utf8') : null,
ca: mqttConfig.ca ? fs.readFileSync(mqttConfig.ca, 'utf8') : null,
cert: mqttConfig.cert ? fs.readFileSync(mqttConfig.cert, 'utf8') : null,
rejectUnauthorized,
username: mqttConfig.username ? mqttConfig.username : null,
password: mqttConfig.password ? mqttConfig.password : null,
clean: typeof mqttConfig.clean === 'boolean' ? mqttConfig.clean : true,
clientId: mqttConfig.clientId ? mqttConfig.clientId + rndSuffix : 'iotaul' + rndSuffix,
keepalive: mqttConfig.keepalive ? parseInt(mqttConfig.keepalive) : 60,
connectTimeout: 60 * 60 * 1000
};
const retries = mqttConfig.retries ? mqttConfig.retries : constants.MQTT_DEFAULT_RETRIES;
const retryTime = mqttConfig.retryTime ? mqttConfig.retryTime : constants.MQTT_DEFAULT_RETRY_TIME;
let isConnecting = false;
let numRetried = 1; // retries will be disabled when MQTT_DEFAULT_RETRIES=0
config
.getLogger()
.info(context, 'Starting MQTT binding with options %j retries %s retryTIme %s', options, retries, retryTime);
function createConnection(callback) {
config.getLogger().info(context, 'creating connection');
if (isConnecting) {
return;
}
isConnecting = true;
// Ensure clientId is unique when reconnect to avoid loop closing old connection which the same name
rndSuffix = '_' + Math.random().toString(16).substr(2, 8);
options.clientId = mqttConfig.clientId ? mqttConfig.clientId + rndSuffix : 'iotaul' + rndSuffix;
mqttClient = mqtt.connect(options.protocol + '://' + mqttConfig.host + ':' + mqttConfig.port, options);
isConnecting = false;
if (!mqttClient) {
config.getLogger().error(context, 'error mqttClient not created');
if (numRetried <= retries) {
numRetried++;
return setTimeout(createConnection, retryTime * 1000, callback);
}
}
mqttClient.on('error', function (e) {
/*jshint quotmark: double */
config.getLogger().fatal("GLOBAL-002: Couldn't connect with MQTT broker: %j", e);
/*jshint quotmark: single */
mqttClient.end();
});
mqttClient.on('message', commonBindings.mqttMessageHandler);
/* eslint-disable-next-line no-unused-vars */
mqttClient.on('connect', function (ack) {
config.getLogger().info(context, 'MQTT Client connected');
recreateSubscriptions();
numRetried = 1;
});
mqttClient.on('reconnect', function () {
config.getLogger().debug(context, 'MQTT Client reconnect');
});
mqttClient.on('offline', function () {
config.getLogger().debug(context, 'MQTT Client offline');
});
mqttClient.on('close', function () {
config.getLogger().info(context, 'MQTT Client closed');
// If mqttConn is null, the connection has been closed on purpose
if (mqttConn) {
config.getLogger().debug(context, 'MQTT Client closed connected? %s', mqttClient.connected);
if (!mqttClient.connected && numRetried <= retries) {
config.getLogger().warn(context, 'reconnecting #%s..', numRetried);
numRetried++;
return setTimeout(createConnection, retryTime * 1000);
}
} else {
// Do Nothing
}
});
config.getLogger().info(context, 'connected');
mqttConn = mqttClient;
if (callback) {
callback();
}
} // function createConnection
async.waterfall([createConnection], function (error) {
if (error) {
config.getLogger().info('MQTT error %j', error);
}
callback();
});
}
/**
* Stops the IoT Agent and all the transport plugins.
*/
function stop(callback) {
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
config.getLogger().info('Stopping MQTT Binding');
async.series([unsubscribeAll, mqttClient.end.bind(mqttClient, true)], function () {
config.getLogger().info('MQTT Binding Stopped');
if (mqttConn) {
mqttConn = null;
}
callback();
});
}
exports.sendConfigurationToDevice = sendConfigurationToDevice;
exports.deviceProvisioningHandler = deviceProvisioningHandler;
exports.deviceUpdatingHandler = deviceUpdatingHandler;
exports.commandHandler = commandHandler;
exports.start = start;
exports.stop = stop;
exports.protocol = 'MQTT';