/
broker.js
332 lines (293 loc) · 12.1 KB
/
broker.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
const mqtt = require("mqtt");
var TopicArray = require("./Topics.js");
var SendQueue = require("./SendQueue.js");
var handleMQTT = require("./messagehandling.js");
class brokerMQTT {
constructor(app) {
this.logmodule = app.logmodule;
this.handleMessage = new handleMQTT(app);
//this.Homey = require('homey');
this.Homey = app.homey;
this.connectedClient = null;
this.topicArray = new TopicArray();
this.sendqueue = new SendQueue();
this.brokerState = "DISCONNECTED";
this.errorOccured = false;
}
/**
* getBrokerURL - create broker URL based on several settings.
*
* @return {type} String with the URL of the broker to connect to.
*/
getBrokerURL() {
var urlBroker = []
if (this.Homey.settings.get('tls') == true) {
urlBroker.push("mqtts://");
} else {
urlBroker.push("mqtt://");
};
urlBroker.push(this.Homey.settings.get('url'));
urlBroker.push(":"+this.Homey.settings.get('ip_port'));
this.logmodule.writelog('info', "Broker URL: "+ urlBroker.join(''));
return urlBroker.join('');
}
/**
* getConnectOptions - create structure with several options.
* Options are depending on several settings from the settings page.
*
* @return {type} returns structure with the options to use when connecting.
*/
getConnectOptions() {
var clientID = 'homey_' + Math.random().toString(16).substr(2, 8);
var rejectUnauth = true;
if ( this.Homey.settings.get('selfsigned') == true) {
rejectUnauth = false;
}
var keepalive = parseInt(this.Homey.settings.get('keepalive'));
if (isNaN(keepalive)) {
keepalive = 60;
}
this.logmodule.writelog('info', "keepalive: " + keepalive);
if ( this.Homey.settings.get('custom_clientid') == true) {
clientID = this.Homey.settings.get('clientid');
}
this.logmodule.writelog('info', "clientID = "+ clientID);
var lwt_struct = {};
var lwt_topic = this.Homey.settings.get('lwt_topic');
var lwt_message = this.Homey.settings.get('lwt_message');
if (lwt_topic) {
this.logmodule.writelog('debug', "lwt_topic = "+ lwt_topic);
lwt_struct.topic = lwt_topic;
} else {
lwt_struct.topic = clientID+"/status";
this.logmodule.writelog('debug', "lwt_topic = "+ lwt_struct.topic);
}
if (lwt_message) {
this.logmodule.writelog('debug', "lwt_message = "+ lwt_message);
lwt_struct.payload = lwt_message;
} else {
lwt_struct.payload = "MQTT client Offline";
this.logmodule.writelog('debug', "lwt_message = "+ lwt_struct.payload);
}
lwt_struct.qos = 0;
lwt_struct.retain = true;
var connect_options = {};
connect_options.keepalive = keepalive;
connect_options.username = this.Homey.settings.get('user');
connect_options.password = this.Homey.settings.get('password');
connect_options.rejectUnauthorized = rejectUnauth;
connect_options.clientId = clientID;
// If LWT is enabled, add lwt struct
if ( this.Homey.settings.get('use_lwt') == true) {
this.logmodule.writelog('debug', "lwt_truct = "+ JSON.stringify(lwt_struct));
connect_options.will = lwt_struct;
}
// connect_options.protocolVersion = 3;
this.logmodule.writelog('info', "rejectUnauthorized: " + connect_options.rejectUnauthorized);
return connect_options;
}
/**
* connectToBroker - description
*
* @param {type} args description
* @param {type} state description
* @return {type} description
*/
connectToBroker(args, state) {
const ref = this;
if (this.connectedClient == null && ref.brokerState !== "CONNECTING") {
this.logmodule.writelog("connectedClient == null");
try {
ref.brokerState = 'CONNECTING';
this.connectedClient = mqtt.connect(this.getBrokerURL(), this.getConnectOptions());
} catch(err) {
ref.brokerState = "DISCONNECTED";
ref.logmodule.writelog('error', "connectToBroker: " +err);
}
ref.connectedClient.on('reconnect', function() {
ref.brokerState = "RECONNECTING";
ref.logmodule.writelog('info', "MQTT Reconnect");
ref.logmodule.writelog('info', "Broker State: " + ref.brokerState);
});
ref.connectedClient.on('close', function() {
ref.logmodule.writelog('info', "MQTT Closed");
ref.brokerState = "DISCONNECTED";
ref.logmodule.writelog('info', "Broker State: " + ref.brokerState);
});
ref.connectedClient.on('offline', function() {
ref.logmodule.writelog('info', "MQTT Offline");
if (ref.brokerState == "CONNECTED") {
ref.logmodule.writelog('error', ref.Homey.__("notifications.mqtt_offline"));
}
ref.brokerState = "DISCONNECTED";
ref.logmodule.writelog('info', "Broker State: " + ref.brokerState);
});
ref.connectedClient.on('error', function(error) {
if (!ref.errorOccured) {
ref.logmodule.writelog('error', "MQTT error occured: " + error);
ref.brokerState = "ERROR";
ref.errorOccured = true;
ref.logmodule.writelog('info', "Broker state: " + ref.brokerState);
} else {
ref.logmodule.writelog('info', "MQTT error occured: " + error);
}
});
// On connection ...
ref.connectedClient.on('connect', function (connack) {
if (ref.errorOccured || ref.brokerState == "RECONNECTING") {
ref.logmodule.writelog('error', ref.Homey.__("notifications.mqtt_online"));
}
ref.brokerState = "CONNECTED";
ref.errorOccured = false;
ref.logmodule.writelog('info', "MQTT client connected");
ref.logmodule.writelog('info', "Connected Topics: " + ref.getTopicArray().getTriggerTopics());
ref.logmodule.writelog('info', "Broker State: " + ref.brokerState);
// try to empty SendQueue
while (!ref.sendqueue.isEmpty()) {
ref.logmodule.writelog('debug', "sending queued messages");
ref.sendMessageToTopic(ref.sendqueue.removeMessage());
}
});
this.connectedClient.on('message',function(topic, message, packet) {
// When a message is received, call receiveMessage for further processing
ref.logmodule.writelog('info', "OnMessage called");
ref.handleMessage.receiveMessage(topic, message, args, state);
});
};
}
/**
* subscribeToTopic - description
*
* @param {type} topicName description
* @return {type} description
*/
subscribeToTopic(topicName, callback, api) {
if (!this.topicArray.active(topicName)) {
// Connect if no client available
if (this.connectedClient == null) {
this.connectToBroker();
}
if (!this.topicArray.exists(topicName)) {
if (api) {
this.logmodule.writelog('info', "subscribing to api topic " + topicName);
this.topicArray.addApiTopic(topicName);
} else {
this.logmodule.writelog('info', "subscribing to trigger topic " + topicName);
this.topicArray.addTriggerTopic(topicName);
}
}
// First topic registration?
if (!this.topicArray.getTopic(topicName).isRegistered()) {
// Subscribe to topic
this.connectedClient.subscribe(topicName, (error) => {
// success?
if (error) {
this.logmodule.writelog('error', "failed to subscribed to topic " + topicName);
this.logmodule.writelog('error', error);
if (this.topicArray.getTopic(topicName) !== null) {
if (!this.topicArray.getTopic(topicName).isRegistered()) {
this.topicArray.remove(topicName);
this.logmodule.writelog('debug', "Removed from topiclist: " + topicName);
}
}
} else {
// Fill the array with known topics so I can check if I need to subscribe
this.topicArray.getTopic(topicName).setRegistered(true);
this.logmodule.writelog('info', "successfully subscribed to topic " + topicName);
}
});
}
} else { // already registered
this.logmodule.writelog('info', "already subscribed to topic " + topicName);
if (callback && typeof callback === 'function') {
callback(null, {});
}
}
}
subscribeToApiTopic(topic, callback) {
this.subscribeToTopic(topic, callback, true);
}
/**
* sendMessageToTopic - description
*
* @param {type} args description
* @return {type} description
*/
sendMessageToTopic(args) {
this.logmodule.writelog('info', "SendMessageToTopic called");
this.logmodule.writelog('debug', "SendMessageToTopic: " + JSON.stringify(args));
// this.logmodule.writelog('debug', "qos: " + parseInt(args.qos));
// validate
if (!args) {
this.logmodule.writelog('error', "SendMessageToTopic: no arguments provided");
return;
}
if (!args.mqttTopic) {
this.logmodule.writelog('error', "SendMessageToTopic: no mqttTopic provided in arguments");
this.logmodule.writelog('debug', "arguments: ");
this.logmodule.writelog('debug', JSON.stringify(args, null, 2));
return;
}
// send
try {
let qos = typeof args.qos === 'string' ? parseInt(args.qos) : args.qos;
let publish_options = {
qos: qos && qos >= 0 && qos <= 2 ? qos : 0,
retain: args.retain === true || args.retain === '1' || args.retain === 'true'
};
this.logmodule.writelog('debug', "publish_options: " + JSON.stringify(publish_options));
// Check if there is already a connection to the broker
if (!this.connectedClient || this.brokerState === "CONNECTING") {
// There is no connection, so create a connection and send the message
if (this.brokerState !== "CONNECTING") {
this.logmodule.writelog('info', "Broker not connected, attempting connection");
this.connectToBroker(args);
} else {
this.logmodule.writelog('info', "Broker not available, waiting for connection");
}
// add message to the senqueue.
this.sendqueue.addMessage(args);
} else {
// parse objects to string
if (args.mqttMessage && typeof args.mqttMessage !== 'string' && typeof args.mqttMessage !== null) {
args.mqttMessage = JSON.stringify(args.mqttMessage);
}
// publish messsage to topic
this.connectedClient.publish(args.mqttTopic, args.mqttMessage, publish_options, () =>
this.logmodule.writelog('debug', "send " + args.mqttMessage + " on topic " + args.mqttTopic)
);
}
} catch (err) {
this.logmodule.writelog('error', "sendMessageToTopic: " + err);
}
}
/**
* getConnectedClient - description
*
* @return {type} description
*/
getConnectedClient() {
return this.connectedClient;
}
/**
* clearConnectedClient - description
*
* @return {type} description
*/
clearConnectedClient() {
this.connectedClient = null;
}
/**
* updateRef - description
*
* @param {type} app description
* @return {type} description
*/
updateRef(app) {
this.handleMessage.updateRef(app);
}
getTopicArray() {
return this.topicArray;
}
}
module.exports = brokerMQTT;