-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMMM-MQTTbridge.js
457 lines (417 loc) · 16.9 KB
/
MMM-MQTTbridge.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
450
451
452
453
454
455
456
457
/* eslint-disable indent */
/* global Module */
/* MagicMirror²
* Module: MMM-MQTTbridge
* MIT Licensed.
*/
Module.register("MMM-MQTTbridge", {
defaults: {
debug: false,
mqttDictConf: "./dict/mqttDictionary.js",
notiDictConf: "./dict/notiDictionary.js",
mqttServer: "mqtt://:@localhost:1883",
stringifyPayload: true,
newlineReplacement: null,
notiConfig: {}, //default values will be set in start function
mqttConfig: {}, //default values will be set in start function
},
getScripts: function () {
return [this.file('node_modules/jsonpath-plus/dist/index-browser-umd.js')];
},
start: function () {
const self = this
Log.info("Starting module: " + self.name);
self.config.mqttConfig = Object.assign({
qos: 0,
retain: false,
clean: true,
rejectUnauthorized: true,
listenMqtt: false,
interval: 300000,
onConnectMessages: []
},self.config.mqttConfig)
self.config.notiConfig = Object.assign({
qos: 0,
listenNoti: false,
ignoreNotiId: [],
ignoreNotiSender: [],
onConnectNotifications: []
},self.config.notiConfig)
self.sendSocketNotification("CONFIG", self.config);
self.loaded = false;
self.mqttVal = "";
setTimeout(() => {
self.updateMqtt();
}, 500);
self.cnotiHook = {}
self.cnotiMqttCommands = {}
self.cmqttHook = {}
self.cmqttNotiCommands = {}
self.ctopicsWithJsonpath = {}
self.lastNotiValues = {}
self.lastMqttValues = {}
},
isAString: function(x) {
return Object.prototype.toString.call(x) === "[object String]"
},
validateCondition: function(source, value, type, lastValue){
if (type == "eq"){
if ((typeof source === "number") || (this.isAString(source))){
return source === value
} else {
return JSON.stringify(source) === value
}
} else if (type == "incl"){
if (this.isAString(source)){
return source === value
} else {
return JSON.stringify(source).includes(value)
}
} else if (type == "mt") {
if (this.isAString(source)){
return new RegExp(value).test(source)
} else {
return new RegExp(value).test(JSON.stringify(source))
}
} else if (type == "lt"){
return source < value
} else if (type == "le"){
return source <= value
} else if (type == "gt"){
return source > value
} else if (type == "ge"){
return source >= value
} else if (type == "time") {
if (lastValue != null) {
if ((Date.now() - lastValue[1]) > value){
return true
} else {
return false
}
} else {
return true
}
} else if (type == "tdiff") {
if (lastValue != null) {
if (JSON.stringify(source) != lastValue[0]){
return true
} else {
if (value > 0){
if ((Date.now() - lastValue[1]) > value){
return true
} else {
return false
}
} else {
return false
}
}
} else {
return true
}
}
return false
},
//https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string
tryParseJSONObject: function (jsonString) {
try {
var o = JSON.parse(jsonString);
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o;
}
}
catch (e) { }
return false;
},
updateMqtt: function () {
const self = this
self.sendSocketNotification("MQTT_BRIDGE_CONNECT"); //request to connect to the MQTT broker
setTimeout(() => {
self.updateMqtt();
}, self.config.mqttConfig.interval);
},
publishNotiToMqtt: function(topic, payload, options = {}) {
const self = this
if (self.isAString(payload)){
self.sendSocketNotification("MQTT_MESSAGE_SEND", {
mqttServer: self.config.mqttServer,
topic: topic,
payload: payload,
options: options
});
} else {
self.sendSocketNotification("MQTT_MESSAGE_SEND", {
mqttServer: self.config.mqttServer,
topic: topic,
payload: JSON.stringify(payload),
options: options
});
}
},
mqttToNoti: function (payload) {
const self = this
let msg = payload.data
let curMqttHook = self.cmqttHook[payload.topic]
if (typeof curMqttHook !== "undefined"){
//if there are configured jsonpath settings for this topic we create the json object and the jsonpath values
//this way they only get calculated once even if they are used by more than one element
if (typeof self.ctopicsWithJsonpath[payload.topic] !== "undefined"){
let jsonObj = self.tryParseJSONObject(msg)
if (jsonObj != false){
for(let curPath in self.ctopicsWithJsonpath[payload.topic]){
let value = JSONPath.JSONPath({ path: curPath, json: jsonObj });
if(Array.isArray(value) && (value.length == 1)){
value = value[0]
}
self.ctopicsWithJsonpath[payload.topic][curPath] = value
}
}
}
for(let curHookIdx=0; curHookIdx < curMqttHook.length; curHookIdx++){
let curHookConfig = curMqttHook[curHookIdx]
// {
// payloadValue: '{"state": "ON"}',
// mqttNotiCmd: ["Command 1"]
// },
let value = msg
//if a jsonpath is configured in commad configuration we use the pre-calculated value now
//if the message was not a valid JSON we still use the raw value and write a message to the log instead
if(typeof curHookConfig.jsonpath !== "undefined") {
value = self.ctopicsWithJsonpath[payload.topic][curHookConfig.jsonpath]
if(value == null){
this.sendSocketNotification("LOG","[MQTT bridge] Invalid JSON: There is configured a jsonpath setting for topic "+payload.topic + " but the message: "+msg+" is not a valid JSON. Using original value instead!");
value = msg
}
}
//now we need to replace all new lines in the message if "newlineReplacement" is configured
//either in the global option or special in this configuration
if (typeof curHookConfig.valueFormat !== "undefined") {
let newlineReplacement = curHookConfig.newlineReplacement || self.config.newlineReplacement
if (newlineReplacement != null) {
value = String(value).replace(/(?:\r\n|\r|\n)/g, newlineReplacement)
}
value = eval(eval("`" + curHookConfig.valueFormat + "`"))
}
//now that we have the parsed and replaced value we can check if the payloadValue is matched (if payloadValue is present)
if (
(typeof curHookConfig.payloadValue === "undefined") ||
(curHookConfig.payloadValue == value)
){
//if additional conditions are configured we will now check if all of them match
//only if all of them match further processing is done
let conditionsValid = true
if (typeof curHookConfig.conditions !== "undefined"){
let curLastValues = self.lastMqttValues[payload.topic][curHookIdx] || null
for(let curCondIdx = 0; curCondIdx < curHookConfig.conditions.length; curCondIdx++){
let curCondition = curHookConfig.conditions[curCondIdx]
if((typeof curCondition["type"] !== "undefined") && (typeof curCondition["value"] !== "undefined")){
if(!self.validateCondition(value,curCondition["value"],curCondition["type"],curLastValues)){
conditionsValid = false
break
}
}
}
}
//if all preconditions met we process the command configurations now
if (conditionsValid){
self.lastMqttValues[payload.topic][curHookIdx] = [JSON.stringify(value), Date.now()]
let mqttCmds = curHookConfig.mqttNotiCmd || []
for(let curCmdIdx = 0; curCmdIdx < mqttCmds.length; curCmdIdx++){
let curCmdConfigs = self.cmqttNotiCommands[mqttCmds[curCmdIdx]]
for(let curCmdConfIdx = 0; curCmdConfIdx < curCmdConfigs.length; curCmdConfIdx++){
let curCmdConf = curCmdConfigs[curCmdConfIdx]
// {
// commandId: "Command 1",
// notiID: "REMOTE_ACTION",
// notiPayload: {action: 'MONITORON'}
// },
if (typeof curCmdConf.notiID !== "undefined"){
if (typeof curCmdConf.notiPayload === "undefined") {
self.sendNotification(curCmdConf.notiID, value)
if (self.config.debug){
this.sendSocketNotification("LOG","[MQTT bridge] MQTT -> NOTI issued: " + curCmdConf.notiID + ", payload: "+ value);
}
} else {
self.sendNotification(curCmdConf.notiID, curCmdConf.notiPayload)
if (self.config.debug){
this.sendSocketNotification("LOG","[MQTT bridge] MQTT -> NOTI issued: " + curCmdConf.notiID + ", payload: "+ JSON.stringify(curCmdConf.notiPayload));
}
}
} else {
this.sendSocketNotification("LOG","[MQTT bridge] MQTT -> NOTI error: Skipping notification cause \"notiID\" is missing. "+JSON.stringify(curCmdConf));
}
}
}
}
}
}
}
},
notiToMqtt: function(notification, payload) {
const self = this
let curNotiHooks = self.cnotiHook[notification]
if (typeof curNotiHooks !== "undefined"){
for(let curHookIdx = 0; curHookIdx < curNotiHooks.length; curHookIdx++){
let curHookConfig = curNotiHooks[curHookIdx]
// {
// payloadValue: true,
// notiMqttCmd: ["SCREENON"]
// },
//now we need to replace all new lines in the message if "newlineReplacement" is configured
//either in the global option or special in this configuration
let value = payload
if (typeof curHookConfig.valueFormat !== "undefined") {
let newlineReplacement = curHookConfig.newlineReplacement || self.config.newlineReplacement
if (newlineReplacement != null) {
value = String(value).replace(/(?:\r\n|\r|\n)/g, newlineReplacement)
}
value = eval(eval("`" + curHookConfig.valueFormat + "`"))
}
if (
(typeof curHookConfig.payloadValue === "undefined") ||
(JSON.stringify(curHookConfig.payloadValue) == JSON.stringify(value))
){
//if additional conditions are configured we will now check if all of them match
//only if all of them match further processing is done
let conditionsValid = true
if (typeof curHookConfig.conditions !== "undefined"){
let curLastValues = self.lastNotiValues[notification][curHookIdx] || null
for(let curCondIdx = 0; curCondIdx < curHookConfig.conditions.length; curCondIdx++){
let curCondition = curHookConfig.conditions[curCondIdx]
if(typeof curCondition["type"] !== "undefined"){
if (typeof curCondition["value"] !== "undefined") {
if(!self.validateCondition(value,curCondition["value"],curCondition["type"],curLastValues)){
conditionsValid = false
break
}
}
}
}
}
//if all preconditions met we process the command configurations now
if(conditionsValid){
self.lastNotiValues[notification][curHookIdx] = [JSON.stringify(value),Date.now()]
let notiCmds = curHookConfig.notiMqttCmd || []
for(let curCmdIdx = 0; curCmdIdx < notiCmds.length; curCmdIdx++){
let curCmdConfigs = self.cnotiMqttCommands[notiCmds[curCmdIdx]] || []
for(let curCmdConfIdx = 0; curCmdConfIdx < curCmdConfigs.length; curCmdConfIdx++){
let curCmdConf = curCmdConfigs[curCmdConfIdx]
// {
// commandId: "SCREENON",
// mqttTopic: "magicmirror/state",
// mqttMsgPayload: '{"state":"ON"}',
// options: {"qos": 1, "retain": false},
// retain: true,
// qos: 0
// },
if (typeof curCmdConf.mqttTopic !== "undefined"){
let curStringifyPayload
if(typeof curCmdConf.stringifyPayload !== "undefined"){
curStringifyPayload = curCmdConf.stringifyPayload
} else {
curStringifyPayload = self.config.stringifyPayload
}
let msg
if (typeof curCmdConf.mqttMsgPayload === "undefined") {
if(curStringifyPayload){
msg = JSON.stringify(value)
} else {
msg = value
}
} else {
if(curStringifyPayload){
msg = JSON.stringify(curCmdConf.mqttMsgPayload)
} else {
msg = curCmdConf.mqttMsgPayload
}
}
let curOptions = curCmdConf.options || {}
if (typeof curCmdConf.retain !== "undefined"){
curOptions["retain"] = curCmdConf.retain
} else {
curOptions["retain"] = self.config.mqttConfig.retain
}
if (typeof curCmdConf.qos !== "undefined"){
curOptions["qos"] = curCmdConf.qos
} else {
curOptions["qos"] = self.config.mqttConfig.qos
}
self.publishNotiToMqtt(curCmdConf.mqttTopic, msg, curOptions);
} else {
this.sendSocketNotification("LOG","[MQTT bridge] NOTI -> MQTT error: Skipping mqtt publish cause \"mqttTopic\" is missing. " + JSON.stringify(curCmdConf));
}
}
}
}
}
}
}
},
socketNotificationReceived: function (notification, payload) {
const self = this
switch (notification) {
// START MQTT to NOTI logic
case "MQTT_MESSAGE_RECEIVED":
self.mqttToNoti(payload);
break;
// END of MQTT to NOTI logic
case "ERROR":
self.sendNotification("SHOW_ALERT", payload);
break;
case "DICTIONARIES": //use dictionaries from external files at module sturt-up
self.cnotiHook = payload.cnotiHook;
self.cnotiMqttCommands = payload.cnotiMqttCommands;
self.cmqttHook = payload.cmqttHook;
self.cmqttNotiCommands = payload.cmqttNotiCommands;
self.ctopicsWithJsonpath = payload.ctopicsWithJsonpath;
for (let curNotification in self.cnotiHook) {
self.lastNotiValues[curNotification] = {};
}
for (let curTopic in self.cmqttHook) {
self.lastMqttValues[curTopic] = {};
}
break;
case "CONNECTED_AND_SUBSCRIBED":
for (let curMsg of self.config.mqttConfig.onConnectMessages){
self.publishNotiToMqtt(curMsg.topic, curMsg.msg, curMsg.options || {})
}
for (let curNoti of self.config.notiConfig.onConnectNotifications){
if (typeof curNoti.payload !== "undefined"){
self.sendNotification(curNoti.notification, curNoti.payload)
} else {
self.sendNotification(curNoti.notification)
}
}
}
},
notificationReceived: function (notification, payload, sender) {
const self = this
// START of NOTIFICATIONS to MQTT logic
// Filtering...
if (!self.config.notiConfig.listenNoti) { return; } // check whether we need to listen for the NOTIFICATIONS. Return if "false"
var sndname = "system"; //sender name default is "system"
if (!sender === false) { sndname = sender.name; }; //if no SENDER specified in NOTIFICATION, the SENDER is left as "system" (according to MM documentation), otherwise - use sender name
// exclude NOTIFICATIONS where SENDER in ignored list
for (var x in self.config.notiConfig.ignoreNotiSender)
{
if (sndname == self.config.notiConfig.ignoreNotiSender[x]) { return; }
}
// exclude NOTIFICATIONS where NOTIFICATION ID in ignored list
for (var x in self.config.notiConfig.ignoreNotiId)
{
if (notification == self.config.notiConfig.ignoreNotiId[x]) { return; }
}
if (typeof self.cnotiHook[notification] !== "undefined"){
if (typeof payload !== "undefined"){
self.notiToMqtt(notification, payload);
} else {
self.notiToMqtt(notification, "");
}
}
}
// END of NOTIFICATIONS to MQTT logic
});