-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.js
1031 lines (909 loc) · 41.6 KB
/
mqtt.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let MQTT = function() {
let config = require('./config.js');
let mqtt = this;
this.mqtt = require('mqtt');
this.connected = false;
let conn = this.connected;
debug = require('./debug.js');
this.components = require('./components').setFacility(this, 'mqtt');
let firstConnect = true;
if (config.mqtt.enabled) {
let options = {
will: {
topic: config.mqtt.topicPrefix + '/raspy-status',
payload: 'OFF',
qos: 2,
retain: true
}
};
this.MQTTDevices = {};
this.MQTTDevices.raspys = {}
this.client = this.mqtt.connect(config.mqtt.broker, options);
let c = this.client;
this.client.on('connect', function() {
let statusTopic = config.mqtt.topicPrefix + '/status';
debug.log(4, 'mqtt', 'Connected succesfully to MQTT broker: ' + config.mqtt.broker);
mqtt.connected = true;
c.subscribe(statusTopic, {"rap": true}, function(error){
if (error)
debug.log(2, 'mqtt', 'Problem with subscribing to topic: ' + statusTopic + ' error: ' + error.toString());
else
debug.log(4, 'mqtt', 'Succesfully subscribed to topic: ' + statusTopic);
});
/* sending all device sttus upon connection restablishment*/
if (firstConnect == false) {
publishAllDevices();
setTimeout(function() {
publishAllDevicesStateOffline();
}, 15000);
setTimeout(function() {
publishAllDevicesState();
}, 20000);
}
firstConnect = false;
});
this.client.on('error', function(error) {
debug.log(1, 'mqtt', 'Problem connecting with MQTT broker: ' + error.toString());
});
this.client.on('message', function(topic, payload){
debug.log(4, 'mqtt', 'Message received: ' + payload + ' from topic: ' + topic);
console.log(" -- MQTT -- BFPDevice command payload: " + payload)
if (!verifyTopic(topic)) {
debug.log(4, 'mqtt', 'Topic in incorrect format, cannot be processed');
return;
}
if (topic.split('/')[0] == 'status'){
HAStatus(topic, payload);
return;
}
let cmd = topic.split('/')[4];
if (cmd == 'direction-cmd') {
directionCommand(topic, payload);
} else if (cmd == 'position-cmd') {
positionCommand(topic, payload);
} else if (cmd == 'tilt-cmd') {
tiltCommand(topic, payload);
} else if (cmd == 'switch-cmd') {
switchCommand(topic, payload);
} else {
debug.log(1, 'mqtt', 'Unknown CMD received: ' + cmd);
}
});
this.client.on('error', function(error){
debug.log(1, 'mqtt', 'Unknown Error received: ' + error.toString());
});
this.client.on('disconnect', function(){
debug.log(1, 'mqtt', 'Disconnect packet received from the broker.');
mqtt.connected = false;
});
this.client.on('offline', function(){
debug.log(1, 'mqtt', 'Broker goes offline.');
setAllDevicesToIgnore();
mqtt.connected = false;
});
setInterval(function(){
if (require('./mqtt.js').connected == true) {
debug.log(5, 'mqtt', 'Publishing raspy-status ON/online')
c.publish(config.mqtt.topicPrefix + '/raspy-status', 'ON');
c.publish(config.mqtt.topicPrefix + '/raspy-status/availability', 'online');
}
}, 30000);
} else {
debug.log(1, 'mqtt', 'MQTT disabled through configuration');
}
}
let mqtt = new MQTT();
/**
* Function to verify if the topic has the right format and all its contents
* TODO: implement verification
*/
function verifyTopic(topic) {
return true;
}
function HAStatus(topic, payload) {
debug.log(1, 'mqtt', 'Received status information from HA: ' + payload);
}
function directionCommand(topic, payload) {
let bfp = require('./bfp.js');
let arif = require('./arif.js');
let mem = require('./mem.js');
let config = require('./config.js');
let debug = require('./debug.js');
let accountID = config.cloud.id;
let raspyID = topic.split('/')[1];
let ardID = topic.split('/')[2];
let devID = topic.split('/')[3];
let device = mem.getDeviceStatus(accountID, raspyID, ardID, devID);
if (typeof(device) == 'undefined') {
/* this situation normally shouldn't happen. It means that we were subscribed to a topic based on mem device, but this device disappeared later */
debug.log(1, 'mqtt', 'Received MQTT command for unknown device! Doing nothing.');
return;
}
/* commenting it out as it looks like it is not necessary feature */
let savedTopic = require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic];
if (typeof(savedTopic) != 'undefined') {
if (require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish == true) {
debug.log(5, 'mqtt', 'Ignoring this MQTT message as it is first after connectivity established with Home Assistant.');
require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish = false;
return;
}
let BFPDeviceCommand = bfp.BFPCreateDeviceCommandShade(device, null, payload);
BFPDeviceCommand.body.IP = device.IP;
arif.sendCommand(BFPDeviceCommand.body, BFPDeviceCommand.header.command, function(message) {
});
}
}
function positionCommand(topic, payload) {
let bfp = require('./bfp.js');
let arif = require('./arif.js');
let mem = require('./mem.js');
let config = require('./config.js');
let debug = require('./debug.js');
let accountID = config.cloud.id;
let raspyID = topic.split('/')[1];
let ardID = topic.split('/')[2];
let devID = topic.split('/')[3];
let device = mem.getDeviceStatus(accountID, raspyID, ardID, devID);
if (typeof(device) == 'undefined') {
/* this situation normally shouldn't happen. It means that we were subscribed to a topic based on mem device, but this device disappeared later */
debug.log(1, 'mqtt', 'Received MQTT command for unknown device! Doing nothing.');
return;
}
/* commenting it out as it looks like it is not necessary feature */
let savedTopic = require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic];
if (typeof(savedTopic) != 'undefined') {
if (require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish == true) {
debug.log(5, 'mqtt', 'Ignoring this MQTT message as it is first after connectivity established with Home Assistant.');
require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish = false;
return;
}
let BFPDeviceCommand = bfp.BFPCreateDeviceCommandShade(device, payload, 'shadePOS');
BFPDeviceCommand.body.IP = device.IP;
arif.sendCommand(BFPDeviceCommand.body, BFPDeviceCommand.header.command, function(message) {
});
}
}
function tiltCommand(topic, payload) {
let bfp = require('./bfp.js');
let arif = require('./arif.js');
let mem = require('./mem.js');
let config = require('./config.js');
let debug = require('./debug.js');
let accountID = config.cloud.id;
let raspyID = topic.split('/')[1];
let ardID = topic.split('/')[2];
let devID = topic.split('/')[3];
let device = mem.getDeviceStatus(accountID, raspyID, ardID, devID);
if (typeof(device) == 'undefined') {
/* this situation normally shouldn't happen. It means that we were subscribed to a topic based on mem device, but this device disappeared later */
debug.log(1, 'mqtt', 'Received MQTT command for unknown device! Doing nothing.');
return;
}
/* commenting it out as it looks like it is not necessary feature */
let savedTopic = require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic];
if (typeof(savedTopic) != 'undefined') {
if (require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish == true) {
debug.log(5, 'mqtt', 'Ignoring this MQTT message as it is first after connectivity established with Home Assistant.');
require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish = false;
return;
}
let newValue;
if (payload <= 30)
newValue = 90;
else if (payload > 30 && payload <= 70)
newValue = 45;
else if (payload > 70)
newValue = 0;
let BFPDeviceCommand = bfp.BFPCreateDeviceCommandShade(device, newValue, 'shadeTILT');
BFPDeviceCommand.body.IP = device.IP;
arif.sendCommand(BFPDeviceCommand.body, BFPDeviceCommand.header.command, function(message) {
});
}
}
function switchCommand(topic, payload) {
let bfp = require('./bfp.js');
let arif = require('./arif.js');
let mem = require('./mem.js');
let config = require('./config.js');
let debug = require('./debug.js');
let accountID = config.cloud.id;
let raspyID = topic.split('/')[1];
let ardID = topic.split('/')[2];
let devID = topic.split('/')[3];
let device = mem.getDeviceStatus(accountID, raspyID, ardID, devID);
if (typeof(device) == 'undefined') {
/* this situation normally shouldn't happen. It means that we were subscribed to a topic based on mem device, but this device disappeared later */
debug.log(1, 'mqtt', 'Received MQTT command for unknown device! Doing nothing.');
return;
}
/* commenting it out as it looks like it is not necessary feature */
/*if (require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish == true) {
debug.log(5, 'mqtt', 'Ignoring this MQTT message as it is first after connectivity established with Home Assistant.');
require('./mqtt.js').MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[topic].ignoreFirstPublish = false;
return;
}*/
if (payload == 'ON') {
arif.sendCommand(device, 'lightON', function(message) {
//debug.log(5, 'mqtt', 'Received response from ARiF: ' + JSON.stringify(message));
});
} else if (payload == 'OFF') {
arif.sendCommand(device, 'lightOFF', function(message) {
//debug.log(5, 'mqtt', 'Received response from ARiF: ' + JSON.stringify(message));
});
} else {
debug.log(5, 'mqtt', 'Unrecognized payload received: ' + payload + ' on topic: ' + topic);
}
}
function publishAllDevices() {
let config = require('./config.js');
let mem = require('./mem.js');
let debug = require('./debug.js');
let mqtt = require('./mqtt.js');
devices = mem.getClientDevices(config.cloud.id);
for (var raspyID in devices.raspys) {
if (raspyID > 0 && raspyID < 999) { // control if variable is actually a raspyID or meta
for (var ardID in devices.raspys[raspyID].arduinos) {
if (ardID > 0 && ardID < 256) {
for (var devID in devices.raspys[raspyID].arduinos[ardID].devices) {
if (devID > 0 && devID < 256) {
//debug.log(5, 'mqtt', 'Publishing over MQTT device data of raspyID: ' + raspyID + ' ardID: ' + ardID + ' devID: ' + devID);
if (devices.raspys[raspyID].arduinos[ardID].devices[devID].devType == 'shade') {
if (devices.raspys[raspyID].arduinos[ardID].devices[devID].alive == true) {
let position = devices.raspys[raspyID].arduinos[ardID].devices[devID].position;
let tilt = devices.raspys[raspyID].arduinos[ardID].devices[devID].tilt;
mqtt.publishDeviceOnline(raspyID, ardID, devID);
mqtt.publishShadePosition(raspyID, ardID, devID, position);
mqtt.publishShadeTilt(raspyID, ardID, devID, tilt);
} else {
mqtt.publishShadeOffline(raspyID, ardID, devID);
}
} else if (devices.raspys[raspyID].arduinos[ardID].devices[devID].devType == 'digitOUT') {
if (devices.raspys[raspyID].arduinos[ardID].devices[devID].alive == true) {
let status = devices.raspys[raspyID].arduinos[ardID].devices[devID].value;
mqtt.publishLightStatus(raspyID, ardID, devID, status);
} else {
mqtt.publishShadeOffline(raspyID, ardID, devID);
}
}
}
}
}
}
}
}
}
function publishAllDevicesState() {
let config = require('./config.js');
let mem = require('./mem.js');
let debug = require('./debug.js');
let mqtt = require('./mqtt.js');
devices = mem.getClientDevices(config.cloud.id);
debug.log(5, 'mqtt', 'Publishing over MQTT all Devices State');
for (var raspyID in devices.raspys) {
if (raspyID > 0 && raspyID < 999) { // control if variable is actually a raspyID or meta
for (var ardID in devices.raspys[raspyID].arduinos) {
if (ardID > 0 && ardID < 256) {
for (var devID in devices.raspys[raspyID].arduinos[ardID].devices) {
if (devID > 0 && devID < 256) {
if (devices.raspys[raspyID].arduinos[ardID].devices[devID].alive == true) {
mqtt.publishDeviceOnline(raspyID, ardID, devID);
} else {
mqtt.publishShadeOffline(raspyID, ardID, devID);
}
}
}
}
}
}
}
}
function publishAllDevicesStateOffline() {
let config = require('./config.js');
let mem = require('./mem.js');
let debug = require('./debug.js');
let mqtt = require('./mqtt.js');
devices = mem.getClientDevices(config.cloud.id);
debug.log(5, 'mqtt', 'Publishing over MQTT all Devices as Offline');
for (var raspyID in devices.raspys) {
if (raspyID > 0 && raspyID < 999) { // control if variable is actually a raspyID or meta
for (var ardID in devices.raspys[raspyID].arduinos) {
if (ardID > 0 && ardID < 256) {
for (var devID in devices.raspys[raspyID].arduinos[ardID].devices) {
if (devID > 0 && devID < 256) {
mqtt.publishDeviceOffline(raspyID, ardID, devID);
}
}
}
}
}
}
}
function setAllDevicesToIgnore() {
let mqtt = require('./mqtt.js');
let config = require('./config.js');
let debug = require('./debug.js')
let topicPrefix;
let commandTopic;
let setPositionTopic;
let tiltCommandTopic;
for (var raspyID in mqtt.MQTTDevices.raspys) {
for (var ardID in mqtt.MQTTDevices.raspys[raspyID].arduinos) {
for (var devID in mqtt.MQTTDevices.raspys[raspyID].arduinos[ardID].devices) {
topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
if (mqtt.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType == 'shade') {
commandTopic = topicPrefix + '/direction-cmd';
setPositionTopic = topicPrefix + '/position-cmd';
tiltCommandTopic = topicPrefix + '/tilt-cmd';
mqtt.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic].ignoreFirstPublish = true;
mqtt.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[setPositionTopic].ignoreFirstPublish = true;
mqtt.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[tiltCommandTopic].ignoreFirstPublish = true;
} else if (mqtt.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType == 'digitOUT') {
statusTopic = topicPrefix + '/switch-cmd';
mqtt.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[statusTopic].ignoreFirstPublish = true;
}
debug.log(5, 'mqtt', 'Setting raspyID: ' + raspyID + ' ardID: ' + ardID + ' devID: ' + devID + ' to ignore first incoming MQTT message.')
}
}
}
}
/*
* Function to subscribe to all topics necessary for Lights to run.
*/
MQTT.prototype.subscribeLight = function(raspyID, ardID, devID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let commandTopic = topicPrefix + '/switch-cmd';
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not subscribing to any topic due to disabled MQTT by configuration.');
return;
}
if (typeof(this.MQTTDevices.raspys[raspyID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID] = {};
this.MQTTDevices.raspys[raspyID].arduinos = {};
}
if (typeof(this.MQTTDevices.raspys[raspyID].arduinos[ardID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices = {};
}
if (typeof(this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType = 'digitOUT';
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic].ignoreFirstPublish = true;
} else if (this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType != 'digitOUT') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType = 'digitOUT';
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic].ignoreFirstPublish = true;
} else {
debug.log(5, 'mqtt', 'Device: ' + devID + ', ardID: ' + ardID + ', topic: ' + commandTopic + ' already subscribed. Doing nothing.');
return;
}
this.client.subscribe(commandTopic, {"rap": true}, function(error){
if (error)
debug.log(2, 'mqtt', 'Problem with subscribing to topic: ' + commandTopic + ' error: ' + error.toString());
else
debug.log(4, 'mqtt', 'Succesfully subscribed to topic: ' + commandTopic);
});
}
/*
* Function to subscribe to all topics necessary for Shades to run.
*/
MQTT.prototype.subscribeShade = function(raspyID, ardID, devID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let commandTopic = topicPrefix + '/direction-cmd';
let setPositionTopic = topicPrefix + '/position-cmd';
let tiltCommandTopic = topicPrefix + '/tilt-cmd';
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not subscribing to any topic due to disabled MQTT by configuration.');
return;
}
if (typeof(this.MQTTDevices.raspys[raspyID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID] = {};
this.MQTTDevices.raspys[raspyID].arduinos = {};
}
if (typeof(this.MQTTDevices.raspys[raspyID].arduinos[ardID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices = {};
}
if (typeof(this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType = 'shade';
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[setPositionTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[tiltCommandTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic].ignoreFirstPublish = true;
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[setPositionTopic].ignoreFirstPublish = true;
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[tiltCommandTopic].ignoreFirstPublish = true;
} else if (this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType != 'shade') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType = 'shade';
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[setPositionTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[tiltCommandTopic] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic].ignoreFirstPublish = true;
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[setPositionTopic].ignoreFirstPublish = true;
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[tiltCommandTopic].ignoreFirstPublish = true;
} else {
debug.log(5, 'mqtt', 'Device: ' + devID + ', ardID: ' + ardID + ', shade topics already subscribed. Doing nothing.');
return;
}
this.client.subscribe(commandTopic, {"rap": true}, function(error){
if (error)
debug.log(2, 'mqtt', 'Problem with subscribing to topic: ' + commandTopic + ' error: ' + error.toString());
else
debug.log(4, 'mqtt', 'Succesfully subscribed to topic: ' + commandTopic);
});
this.client.subscribe(setPositionTopic, {"rap": true}, function(error){
if (error)
debug.log(2, 'mqtt', 'Problem with subscribing to topic: ' + setPositionTopic + ' error: ' + error.toString());
else
debug.log(4, 'mqtt', 'Succesfully subscribed to topic: ' + setPositionTopic);
});
this.client.subscribe(tiltCommandTopic, {"rap": true}, function(error){
if (error)
debug.log(2, 'mqtt', 'Problem with subscribing to topic: ' + tiltCommandTopic + ' error: ' + error.toString());
else
debug.log(4, 'mqtt', 'Succesfully subscribed to topic: ' + tiltCommandTopic);
});
var commandTopicIgnoreFirstPublish = this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[commandTopic].ignoreFirstPublish;
var positionTopicIgnoreFirstPublish = this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[setPositionTopic].ignoreFirstPublish;
var tiltCommandTopicIgnoreFirstPublish = this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[tiltCommandTopic].ignoreFirstPublish;
setTimeout(function() {
commandTopicIgnoreFirstPublish = false;
positionTopicIgnoreFirstPublish = false;
tiltCommandTopicIgnoreFirstPublish = false;
}, 3000)
}
/*
* Function to subscribe to all topics necessary for Temperature sensor to run.
*/
MQTT.prototype.subscribeTemp = function(raspyID, ardID, devID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let statusTopic = topicPrefix + '/temp-status';
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not subscribing to any topic due to disabled MQTT by configuration.');
return;
}
if (typeof(this.MQTTDevices.raspys[raspyID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID] = {};
this.MQTTDevices.raspys[raspyID].arduinos = {};
}
if (typeof(this.MQTTDevices.raspys[raspyID].arduinos[ardID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices = {};
}
if (typeof(this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID]) == 'undefined') {
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID] = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].devType = 'temp';
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics = {};
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].topics[statusTopic] = {};
}
this.client.subscribe(statusTopic, {"rap": true}, function(error){
if (error)
debug.log(2, 'mqtt', 'Problem with subscribing to topic: ' + statusTopic + ' error: ' + error.toString());
else
debug.log(4, 'mqtt', 'Succesfully subscribed to topic: ' + statusTopic);
});
}
/**
* Publish the arduino binary_sensor configuration to HA
*/
MQTT.prototype.configureArduino = function(raspyID, ardID, name) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let configString = '{"name": "' + name + '", "unique_id": "binary_sensor.arduino-' + ardID + '", \
"state_topic": "velen-mqtt/001/' + ardID + '/arduino-status", "payload_on": "ON", "object_id": "arduino-' + ardID + '", \
"payload_off": "OFF", "availability_topic": "velen-mqtt/001/' + ardID + '/arduino-availability", \
"payload_available": "online", "payload_not_available": "offline", "qos": 0, "device_class": "connectivity"}';
let discoveryTopic = config.mqtt.discoveryPrefix + '/binary_sensor/arduino-' + ardID + '/config';
debug.log(4, 'mqtt', 'Publishing Arduino binary_sensor configuration, ardID: '+ ardID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
}
/**
* Publish empty string to remove the configure arduino binary_sensor to HA
*/
MQTT.prototype.removeArduino = function(raspyID, ardID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let discoveryTopic = config.mqtt.discoveryPrefix + '/binary_sensor/arduino-' + ardID + '/config';
let configString = ''
debug.log(4, 'mqtt', 'Removing arduino binary_sensor configuration of ardID: ' + ardID);
this.client.publish(discoveryTopic, configString, options);
}
/**
* Rename the binary_sensor arduino from the HA config
*/
MQTT.prototype.renameArduino = function(raspyID, ardID, name) {
this.removeArduino(raspyID, ardID);
this.configureArduino(raspyID, ardID, name);
}
/**
* Send MQTT based temperature sensor configuration to HA
*/
MQTT.prototype.configureTemp = function(raspyID, ardID, devID, name) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let configString;
let discoveryString;
configString = '{"name": "' + name + '", "state_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/temp-status", \
"availability_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/availability", \
"unit_of_measurement": "°C", "device_class": "temperature", "object_id": "temp-' + ardID + '-' + devID + '", \
"unique_id": "sensor.temp-' + ardID + '-' + devID + '"}';
discoveryTopic = config.mqtt.discoveryPrefix + '/sensor/arduino-' + ardID + '-temp-' + devID + '/config';
debug.log(4, 'mqtt', 'Publishing Temp sensor configuration, ardID: '+ ardID + ', devID: ' + devID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
}
/**
* Send MQTT based humidity sensor configuration to HA
*/
MQTT.prototype.configureHumidity = function(raspyID, ardID, devID, name) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let configString;
let discoveryString;
configString = '{"name": "' + name + '", "state_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/humidity-status", \
"availability_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/availability", \
"unit_of_measurement": "%", "device_class": "humidity", "object_id": "humidity-' + ardID + '-' + devID + '", \
"unique_id": "sensor.humidity-' + ardID + '-' + devID + '"}';
discoveryTopic = config.mqtt.discoveryPrefix + '/sensor/arduino-' + ardID + '-humidity-' + devID + '/config';
debug.log(4, 'mqtt', 'Publishing Humidity sensor configuration, ardID: '+ ardID + ', devID: ' + devID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
}
/**
* remove MQTT based temperature sensor configuration from HA
*/
MQTT.prototype.removeTemp = function(raspyID, ardID, devID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let discoveryTopic;
let configString = ''
discoveryTopic = config.mqtt.discoveryPrefix + '/sensor/arduino-' + ardID + '-temp-' + devID + '/config';
debug.log(4, 'mqtt', 'Removing Temp sensor configuration of ardID: ' + ardID + ', devID: ' + devID);
this.client.publish(discoveryTopic, configString, options);
}
/**
* Send MQTT based light configuration to HA
*/
MQTT.prototype.configureLight = function(raspyID, ardID, devID, name, extType) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let configString;
let discoveryString;
this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].extType = extType;
if (extType == 0) { /* light */
configString = '{"name": "' + name + '", "state_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/status", \
"command_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/switch-cmd", "object_id": "light-' + ardID + '-' + devID + '", \
"availability_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/availability", \
"qos": 0, "payload_on": "ON", "payload_off": "OFF", "state_on": "ON", "state_off": "OFF", \
"optimistic": "false", "unique_id": "light.velen-' + ardID + '-' + devID + '"}';
discoveryTopic = config.mqtt.discoveryPrefix + '/light/arduino-' + ardID + '-light-' + devID + '/config';
debug.log(4, 'mqtt', 'Publishing Light configuration, ardID: '+ ardID + ', devID: ' + devID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
} else if (extType == 1) { /* binary sensor */
configString = '{"name": "' + name + '", "state_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/status", \
"availability_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/availability", "object_id": "sensor-' + ardID + '-' + devID + '", \
"qos": 0, "payload_on": "ON", "payload_off": "OFF", "state_on": "ON", "state_off": "OFF", \
"optimistic": "false", "unique_id": "binary_sensor.sensor-' + ardID + '-' + devID + '"}';
discoveryTopic = config.mqtt.discoveryPrefix + '/binary_sensor/arduino-' + ardID + '-binary-sensor-' + devID + '/config';
debug.log(4, 'mqtt', 'Publishing Normal Binary Sensor configuration, ardID: '+ ardID + ', devID: ' + devID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
} else if (extType == 2) { /* binary sensor reversed */
configString = '{"name": "' + name + '", "state_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/status", \
"availability_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/availability", "object_id": "sensor-' + ardID + '-' + devID + '", \
"qos": 0, "payload_on": "OFF", "payload_off": "ON", "state_on": "OFF", "state_off": "ON", \
"optimistic": "false", "unique_id": "binary_sensor.sensor-' + ardID + '-' + devID + '"}';
discoveryTopic = config.mqtt.discoveryPrefix + '/binary_sensor/arduino-' + ardID + '-binary-sensor-' + devID + '/config';
debug.log(4, 'mqtt', 'Publishing Reversed Binary Sensor configuration, ardID: '+ ardID + ', devID: ' + devID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
} else if (extType == 3) { /* energy meter */
configString = '{"name": "' + name + '", "state_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/status", \
"availability_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/availability", "object_id": "sensor-' + ardID + '-' + devID + '", \
"qos": 0, "payload_available": "online", "payload_not_available": "offline", \
"device_class": "energy", "unit_of_measurement": "kWh", "state_class": "total_increasing", \
"unique_id": "sensor.sensor-' + ardID + '-' + devID + '"}';
discoveryTopic = config.mqtt.discoveryPrefix + '/sensor/arduino-' + ardID + '-sensor-' + devID + '/config';
debug.log(4, 'mqtt', 'Publishing Power Meter configuration, ardID: '+ ardID + ', devID: ' + devID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
} else if (extType == 4) { /* water meter */
} else {
debug.log(1, 'mqtt', 'Something went wrong when publishing MQTT configuration, wrong extType: ' + extType);
}
}
MQTT.prototype.removeLight = function(raspyID, ardID, devID, extType) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let discoveryTopic;
let configString = ''
if (extType == 0) {
discoveryTopic = config.mqtt.discoveryPrefix + '/light/arduino-' + ardID + '-light-' + devID + '/config';
debug.log(4, 'mqtt', 'Removing Light configuration of ardID: ' + ardID + ', devID: ' + devID);
this.client.publish(discoveryTopic, configString, options);
} else if (extType == 1 || extType == 2) {
discoveryTopic = config.mqtt.discoveryPrefix + '/binary_sensor/arduino-' + ardID + '-binary-sensor-' + devID + '/config';
debug.log(4, 'mqtt', 'Removing Binary Sensor configuration of ardID: ' + ardID + ', devID: ' + devID);
this.client.publish(discoveryTopic, configString, options);
} else if (extType == 3 || extType == 4) {
discoveryTopic = config.mqtt.discoveryPrefix + '/sensor/arduino-' + ardID + '-sensor-' + devID + '/config';
debug.log(4, 'mqtt', 'Removing Meter (Sensor) configuration of ardID: ' + ardID + ', devID: ' + devID);
this.client.publish(discoveryTopic, configString, options);
} else {
debug.log(1, 'mqtt', 'Something went wrong when removing MQTT configuration, wrong extType: ' + extType);
}
}
MQTT.prototype.renameLight = function(raspyID, ardID, devID, name) {
this.removeLight(raspyID, ardID, devID);
this.configureLight(raspyID, ardID, devID, name);
}
/**
* Send MQTT based shade configuration to HA
*/
MQTT.prototype.configureShade = function(raspyID, ardID, devID, name) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let configString = '{"name": "' + name + '", \
"unique_id": "cover.velen-' + ardID + '-' + devID + '", \
"object_id": "shade-' + ardID + '-' + devID + '", \
"command_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/direction-cmd", \
"availability_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/availability", \
"set_position_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/position-cmd", \
"position_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/position-status", \
"tilt_command_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/tilt-cmd", \
"tilt_status_topic": "velen-mqtt/001/' + ardID + '/' + devID + '/tilt-status", \
"qos": 0, \
"payload_open": "OPEN", \
"payload_close": "CLOSE", \
"payload_stop": "STOP", \
"position_open": 100, \
"position_closed": 0, \
"optimistic": false, \
"retain": true, \
"payload_available": "online", \
"payload_not_available": "offline", \
"set_position_template": "{% if position < 10 %} 100 {% elif position > 10 and position <= 35 %} 75 {% elif position > 35 and position <= 65 %} 50 {% elif position > 65 and position <= 90 %} 25 {% else %} 0 {% endif %}", \
"tilt_min": 0, \
"tilt_max": 100, \
"tilt_closed_value": 0, \
"tilt_opened_value": 100}';
let discoveryTopic = config.mqtt.discoveryPrefix + '/cover/arduino-' + ardID + '-cover-' + devID + '/config';
debug.log(4, 'mqtt', 'Publishing Shade configuration, ardID: '+ ardID + ', devID: ' + devID + ' to HA: ' + configString + ', onto topic: ' + discoveryTopic);
this.client.publish(discoveryTopic, configString, options);
}
MQTT.prototype.removeShade = function(raspyID, ardID, devID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix;
let options = {
qos: 2,
retain: true
};
let discoveryTopic = config.mqtt.discoveryPrefix + '/cover/arduino-' + ardID + '-cover-' + devID + '/config';
let configString = ''
debug.log(4, 'mqtt', 'Removing Shade configuration of ardID: ' + ardID + ', devID: ' + devID);
this.client.publish(discoveryTopic, configString, options);
}
MQTT.prototype.changeNameShade = function(raspyID, ardID, devID, name) {
this.removeShade(raspyID, ardID, devID);
this.configureShade(raspyID, ardID, devID, name);
}
MQTT.prototype.publishArduinoOnline = function(raspyID, ardID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID;
let statusTopic = topicPrefix + '/arduino-status';
let availabilityTopic = topicPrefix + '/arduino-availability';
let options = {
qos: 2,
retain: true
};
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not subscribing to any topic due to disabled MQTT by configuration.');
return;
}
debug.log(4, 'mqtt', 'Publishing payload: Arduino ON to status topic: ' + availabilityTopic);
this.client.publish(availabilityTopic, 'online', options);
this.client.publish(statusTopic, 'ON', options);
}
MQTT.prototype.publishArduinoOffline = function(raspyID, ardID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID;
let statusTopic = topicPrefix + '/arduino-status';
let availabilityTopic = topicPrefix + '/arduino-availability';
let options = {
qos: 2,
retain: true
};
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not subscribing to any topic due to disabled MQTT by configuration.');
return;
}
debug.log(4, 'mqtt', 'Publishing payload: Arduino OFF to status topic: ' + availabilityTopic);
this.client.publish(availabilityTopic, 'online', options);
this.client.publish(statusTopic, 'OFF', options);
}
MQTT.prototype.publishDeviceOnline = function(raspyID, ardID, devID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let availabilityTopic = topicPrefix + '/availability';
let options = {
qos: 2,
retain: true
};
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not subscribing to any topic due to disabled MQTT by configuration.');
return;
}
debug.log(4, 'mqtt', 'Publishing payload: Device online to availabilityTopic: ' + availabilityTopic);
this.client.publish(availabilityTopic, 'online', options);
}
MQTT.prototype.publishDeviceOffline = function(raspyID, ardID, devID) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let availabilityTopic = topicPrefix + '/availability';
let options = {
qos: 2,
retain: true
};
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not subscribing to any topic due to disabled MQTT by configuration.');
return;
}
debug.log(4, 'mqtt', 'Publishing payload: Device offline to availabilityTopic: ' + availabilityTopic);
this.client.publish(availabilityTopic, 'offline', options);
}
MQTT.prototype.publishShadePosition = function(raspyID, ardID, devID, position) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let positionTopic = topicPrefix + '/position-status';
let options = {
qos: 2,
retain: true
};
/* reversing the percentage order for HA */
let newPosition;
if (position == 0)
newPosition = '100';
else if (position == 25)
newPosition = '75';
else if (position == 50)
newPosition = '50';
else if (position == 75)
newPosition = '25';
else if (position == 100)
newPosition = '0';
else {
debug.log(4, 'mqtt', 'Incorrect value of position: ' + position + ', not publishing anything.');
return;
}
if (!config.mqtt.enabled) {
debug.log(5, 'mqtt', 'Not publishing to any topic due to disabled MQTT by configuration.');
return;
}
debug.log(4, 'mqtt', 'Publishing payload: ' + newPosition + ' to positionTopic: ' + positionTopic);
this.client.publish(positionTopic, newPosition, options);
}
MQTT.prototype.publishShadeTilt = function(raspyID, ardID, devID, tilt) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let tiltStatusTopic = topicPrefix + '/tilt-status';
let options = {
qos: 2,
retain: true
};
let newTilt;
if (tilt == 0)
newTilt = '100';
else if (tilt == 45)
newTilt = '50';
else if (tilt == 90)
newTilt = '0';
else {
debug.log(4, 'mqtt', 'Incorrect value of tilt: ' + tilt + ', not publishing anything.');
return;
}
if (!config.mqtt.enabled) {
debug.log(4, 'mqtt', 'Not publishing due to disabled MQTT by configuration.');
return;
}
debug.log(4, 'mqtt', 'Publishing payload: ' + newTilt + ' to tiltStatusTopic: ' + tiltStatusTopic);
this.client.publish(tiltStatusTopic, newTilt, options);
}
MQTT.prototype.publishLightStatus = function(raspyID, ardID, devID, status) {
let debug = require('./debug.js');
let config = require('./config.js');
let topicPrefix = config.mqtt.topicPrefix + '/' + raspyID + '/' + ardID + '/' + devID;
let statusTopic = topicPrefix + '/status';
let options = {
qos: 2,
retain: true
};
if (!config.mqtt.enabled) {
debug.log(4, 'mqtt', 'Not publishing due to disabled MQTT by configuration.');
return;
}
extType = this.MQTTDevices.raspys[raspyID].arduinos[ardID].devices[devID].extType;
if (extType == 0 || extType == 1 || extType == 2) { /* lights and binary sensors */
if (status == 0) {
debug.log(4, 'mqtt', 'Publishing payload: OFF to light/sensor status topic: ' + statusTopic);
this.client.publish(statusTopic, 'OFF', options);
} else if (status == 1) {
debug.log(4, 'mqtt', 'Publishing payload: ON to light/sensor status topic: ' + statusTopic);
this.client.publish(statusTopic, 'ON', options);
} else {
debug.log(1, 'mqtt', 'Incorrect status value on: ' + statusTopic + ', status: ' + status);
}
} else if (extType == 3 || extType == 4) { /* meters */
debug.log(4, 'mqtt', 'Publishing payload: ' + status + ' to meter (sensor) status topic: ' + statusTopic);