-
Notifications
You must be signed in to change notification settings - Fork 22
/
mdah.js
2847 lines (2656 loc) · 106 KB
/
mdah.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
//##############################################################################
// This file is part of MyDomoAtHome - https://github.com/empierre/MyDomoAtHome
// Copyright (C) 2014-2023 Emmanuel PIERRE ([email protected])
//
// MyDomoAtHome is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MyDomoAtHome 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MyDomoAtHome. If not, see <http://www.gnu.org/licenses/>.
//##############################################################################
// dependencies
var http = require("http");
var https = require('https');
var express = require("express");
var _ = require("underscore");
var path = require('path');
var request = require("request");
var nconf = require('nconf');
var fs = require('fs');
var os = require("os");
var moment = require('moment');
var morgan = require('morgan');
var methodOverride = require('method-override');
var bodyParser = require('body-parser');
var basicAuth = require('basic-auth');
var errorHandler = require('errorhandler');
var requester = require('sync-request');
var winston = require('winston');
var pjson = require('./package.json');
var isBase64 = require('is-base64');
var app = express();
global.logger = winston;
//working variaboles
var last_version_dt;
var last_version = getLastVersion();
var ver = pjson.version;
var device_tab = {};
var room_tab = [];
var domo_room_tab = [];
var device = {MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected:null};
var app_name = "MyDomoAtHome";
var port = process.env.PORT || '3002';
var passcode=process.env.SEC||'';
var tempmode = 'C';
app.set('port', port);
app.set('view engine', 'ejs');
//var home = process.env.MDAH_HOME || path.resolve(__dirname + "/..");
var home = process.cwd();
//app.use(morgan('combined'));
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
if (process.env.MDAH_HOME) {
app.use(express.static(path.join(process.env.MDAH_HOME + '/public')));
app.set('views', path.resolve(process.env.MDAH_HOME + '/views'));
logger.add(winston.transports.File({filename: process.env.MDAH_HOME+'/var/usage.log'}));
} else {
app.use(express.static(path.join(__dirname + '/public')));
app.set('views', path.resolve(__dirname + '/views'));
logger.add(new winston.transports.File({filename: '/var/log/mydomoathome/usage.log'}));
}
function onError(error) {
if (error) {
logger.warn("No global conf in /etc/mydomoathome:" + err.message);
}
}
function loadLocalConf() {
// load conf file
if (fileExists('./config.json')) {
try {
nconf.use('file', {file: './config.json'}, onError);
} catch (err) {
// This will not catch the throw!
logger.error("Global conf parsing issue !");
logger.error(err);
return;
}
}
}
function loadGlobalConf() {
if (fileExists('/etc/mydomoathome/config.json')) {
try {
nconf.use('file', {file: '/etc/mydomoathome/config.json'}, onError);
} catch (err) {
// This will not catch the throw!
logger.error("Global conf parsing issue !");
logger.error(err);
return;
}
}
if (fileExists('/var/packages/MyDomoAtHome/etc/config.json')) {
try {
nconf.use('file', {file: '/etc/mydomoathome/config.json'}, onError);
} catch (err) {
// This will not catch the throw!
logger.error("Global conf parsing issue !");
logger.error(err);
return;
}
}
}
function loadConf() {
try {
nconf.load(function (err) {
if (err) {
logger.warn("Conf load error:" + err.message);
return;
}
});
} catch (err) {
// This will not catch the throw!
logger.error("Global conf load issue !");
logger.error(err);
return;
}
}
function getConf(){
if (!(nconf.get('port') || (nconf.get('app_name')))) {
logger.warn('basic configuration not found in /etc/mydomoathome/config.json, defaulting')
} else {
app.set('port', nconf.get('port') || process.env.PORT );
app_name = nconf.get('app_name') || "MyDomoAtHome";
passcode = nconf.get('passcode') || passcode;
if (nconf.get('tempmode') == 'F') {
tempmode = 'F';
} else {
tempmode = 'C';
}
}
if (!(nconf.get('domoticz:host') || (nconf.get('domoticz:port')))) {
logger.warn('domoticz access configuration not found in /etc/mydomoathome/config.json, defaulting')
}
}
function fileExists(filePath) {
try {
return fs.statSync(filePath).isFile();
}
catch (err) {
return false;
}
}
function versionCompare(v1, v2, options) {
var lexicographical = options && options.lexicographical,
zeroExtend = options && options.zeroExtend,
v1parts = v1.split('.'),
v2parts = v2.split('.');
function isValidPart(x) {
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
}
if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
return NaN;
}
if (zeroExtend) {
while (v1parts.length < v2parts.length) v1parts.push("0");
while (v2parts.length < v1parts.length) v2parts.push("0");
}
if (!lexicographical) {
v1parts = v1parts.map(Number);
v2parts = v2parts.map(Number);
}
for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
return 1;
}
if (v1parts[i] == v2parts[i]) {
continue;
}
else if (v1parts[i] > v2parts[i]) {
return 1;
}
else {
return -1;
}
}
if (v1parts.length != v2parts.length) {
return -1;
}
return 0;
}
function getURL(req) {
var protocole = nconf.get('domoticz:ssl') === true ? 'https' : 'http';
var host = nconf.get('domoticz:host')||'127.0.0.1';
var port = nconf.get('domoticz:port')||process.env.DOMO_PORT||'8080';
var path = nconf.get('domoticz:path')||'/';
var oldpath = nconf.get('domo_path');
var cmd = "json.htm";
// In case of secondary AUTH Basic authentication
var secure = false;
if (nconf.get('domoticz:auth') && nconf.get('domoticz:auth:username') && nconf.get('domoticz:auth:password')) {
var secure = nconf.get('domoticz:auth:username') + ":" + nconf.get('domoticz:auth:password') + "@";
}
if (req && req.headers.authorization) {
//streamlined primary to secondary auth
const base64Credentials = req.headers.authorization.split(' ')[1];
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
const [username, password] = credentials.split(':');
var secure=username+":"+password+"@";
}
//logger.info("cred "+secure);
if (secure) {
var url = protocole + '://' + secure + host + ':' + port + path + cmd;
} else {
var url = protocole + '://' + host + ':' + port + path + cmd;
}
if (process.env.DOMO) {
return process.env.DOMO+"/json.htm";
} else if (oldpath) {
return oldpath+"/json.htm";
} else {
return url;
}
};
function getLastVersion() {
var now = moment();
if (typeof last_version_dt !== 'undefined' && last_version_dt !== null && (last_version_dt.isBefore(moment().add(2, 'h')))) {
return (last_version);
} else {
var options = {
url: "https://api.github.com/repos/empierre/MyDomoAtHome/releases/latest",
headers: {
'User-Agent': 'request'
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
last_version_dt = now;
last_version = data.tag_name;
logger.info("Refreshing version cache: "+data.tag_name);
return (data.tag_name);
} else {
return ("unknown");
}
});
}
}
function getSettingsSecPassword() {
var url = getURL(req) + "?type=command¶m=getsettings";
var res = requester('GET', url);
if (res.statusCode!=200) {return({})};
logger.info(url);
var js = JSON.parse(res.body.toString('utf-8'));
return (js.result[0].SecPassword);
}
function devSt(data) {
var rbl;
var dimmable;
if (data.HaveDimmer === 'true') {
dimmable = 1
} else {
dimmable = 0
}
var deviceId = data.deviceId;
switch (data.Status) {
case "On":
rbl = 1;
var mydev = device_tab[deviceId];
if (mydev) {
if (dimmable) {
mydev.Action = 0;
mydev.MaxDimLevel = data.MaxDimLevel;
} else {
mydev.Action = 1;
}
device_tab[deviceId] = mydev;
} else {
mydev = {MaxDimLevel: null, Action: 1, graph: null, Selector: null};
}
device_tab[deviceId] = mydev;
break;
case "Off":
rbl = 0;
var mydev = device_tab[deviceId];
if (mydev) {
if (dimmable) {
mydev.Action = 0;
mydev.MaxDimLevel = data.MaxDimLevel;
} else {
mydev.Action = 1;
}
device_tab[deviceId] = mydev;
} else {
mydev = {MaxDimLevel: null, Action: 1, graph: null, Selector: null};
}
device_tab[deviceId] = mydev;
break;
case "Open":
rbl = 1;
var mydev = device_tab[deviceId];
if (mydev) {
mydev.Action = 2;
device_tab[deviceId] = mydev;
} else {
mydev = {MaxDimLevel: null, Action: 2, graph: null, Selector: null};
}
device_tab[deviceId] = mydev;
break;
case "Closed":
rbl = 0;
var mydev = device_tab[deviceId];
if (mydev) {
mydev.Action = 2;
device_tab[deviceId] = mydev;
} else {
mydev = {MaxDimLevel: null, Action: 2, graph: null, Selector: null};
}
device_tab[deviceId] = mydev;
break;
case "Panic":
rbl = 1;
var mydev = device_tab[deviceId];
if (mydev) {
mydev.Action = 3;
device_tab[deviceId] = mydev;
} else {
mydev = {MaxDimLevel: null, Action: 3, graph: null, Selector: null};
}
device_tab[deviceId] = mydev;
break;
case "Normal":
rbl = 0;
var mydev = device_tab[deviceId];
if (mydev) {
mydev.Action = 3;
device_tab[deviceId] = mydev;
} else {
mydev = {MaxDimLevel: null, Action: 3, graph: null, Selector: null};
}
device_tab[deviceId] = mydev;
break;
default:
rbl = data.Status;
break;
}
return rbl;
}
function DevSwitch(data) {
var status = 0;
switch (data.Status) {
case 'On':
case 'Open':
case 'Panic':
case 'All On':
status = 1;
break;
case 'Normal':
case 'Off':
case 'Closed':
case 'All Off':
status = 0;
break;
default:
status = 0;
break;
}
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
params.push({"key": "Status", "value": status.toString()});
if(data.Energy) {
params.push({"key": "Energy", "value": data.Energy});
}
//TODO key Energy
myfeed.params = params;
return (myfeed);
}
function DevMultiSwitch(data) {
var ptrn4 = /[\s]+|/;
var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf();
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMultiSwitch", "room": "Switches"};
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMultiSwitch", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMultiSwitch", "room": "Switches"};
room_tab.Switches=1;
}
var params = [];
params.push({"key": "LastRun", "value": dt});
var status;
//console.log("L:"+data.Level);
var dataLevelNames;
if (isBase64(data.LevelNames)) {
dataLevelNames = Buffer.from(data.LevelNames, 'base64').toString("ascii");; // Ta-da
} else {
dataLevelNames=data.LevelNames;
}
var res = dataLevelNames.split('|').join(',');
if(data.LevelOffHidden) {
res = res.replace ("Off,","");
}
var ret = dataLevelNames.split('|');
var mydev = {MaxDimLevel: null, Action: null, graph: null, Selector: ret};
//console.log(mydev);
device_tab[data.idx] = mydev;
var lvl = Math.round((data.Level) / 10);
//console.log(lvl);
params.push({"key": "Value", "value": ret[lvl].toString()});
params.push({"key": "Choices", "value": res});
myfeed.params = params;
return (myfeed);
};
function DevPush(data) {
var status = 0;
switch (data.SwitchType) {
case 'Push On Button':
status = 1;
break;
case 'Push Off Button':
status = 0;
break;
default:
status = 0;
break;
}
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
//params.push({"key": "Status", "value": status.toString()});
params.push({"key": "pulseable", "value": "1"});
myfeed.params = params;
return (myfeed);
}
function DevRGBLight(data) {
var status = 0;
status = devSt(data);
switch (data.SwitchType) {
case 'Push On Button':
status = 1;
break;
case 'Push Off Button':
status = 0;
break;
case 'On/Off':
if (data.Status == 'On') {status=1};
if (data.Status == 'Off') {status=0};
default:
status = 0;
break;
}
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevRGBLight", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevRGBLight", "room": "Switches"};
room_tab.Switches=1;
}
if (data.Status.match(/Set Level/) || (data.HaveDimmer === true) || (data.HaveDimmer === 'true')) {
var mydev = {MaxDimLevel: null, Action: null, graph: null};
if (device_tab[data.idx]) {
mydev = device_tab[data.idx];
}
mydev.MaxDimLevel = data.MaxDimLevel;
mydev.Action = 0;
device_tab[data.idx] = mydev;
params = [];
if (data.Status == 'Off') {
params.push({"key": "Status", "value": "0"});
} else {
params.push({"key": "Status", "value": "1"});//hyp: set level only when on
}
params.push({"key": "dimmable", "value": "1"});
if ((data.Level==0)&&(data.LevelInt>0)) {
params.push({"key": "Level", "value": data.LevelInt.toString()});
} else {
params.push({"key": "Level", "value": data.Level.toString()});
}
if(data.Energy) {
params.push({"key": "Energy", "value": data.Energy});
}
//TODO whitechannel
//TODO color
//TODO Energy value unit
myfeed.params = params;
} else {
params = [];
if (data.HaveDimmer === true) {
params.push({"key": "Status", "value": status});
}
params.push({"key": "dimmable", "value": "1"});
myfeed.params = params;
}
return (myfeed);
};
function DevDimmer(data) {
var status = 0;
var myfeed = '';
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
myfeed = {"id": data.idx, "name": data.Name, "type": "DevDimmer", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
myfeed = {"id": data.idx, "name": data.Name, "type": "DevDimmer", "room": "Switches"};
room_tab.Switches=1;
}
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
status = devSt(data);
if (data.Status.match(/Set Level/)) {
status = 1;
}
var mydev = {MaxDimLevel: null, Action: null, graph: null};
if (device_tab[data.idx]) {
mydev = device_tab[data.idx];
}
mydev.MaxDimLevel = data.MaxDimLevel;
mydev.Action = 0;
//console.log(mydev);
device_tab[data.idx] = mydev;
params = [];
params.push({"key": "Status", "value": status.toString()});
if(status == 0) {
params.push({"key": "Level", "value": "0"});
} else {
params.push({"key": "Level", "value": data.Level.toString()});
}
if(data.Energy) {
params.push({"key": "Energy", "value": data.Energy});
}
myfeed.params = params;
return (myfeed);
};
function DevShutterInverted(data) {
var status = 0;
status = devSt(data);
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
var lvl = 0;
var stoppable = 0;
var mydev = {MaxDimLevel: null, Action: null, graph: null};
if (device_tab[data.idx]) {
mydev = device_tab[data.idx];
}
mydev.Action = 5;
mydev.MaxDimLevel = data.MaxDimLevel;
device_tab[data.idx] = mydev;
//console.log(data.Status+" "+data.Level);
if (data.Status === 'Open') {
lvl = 100;
status = 1;
} else if (data.Status.match(/Set Level/) || (data.HaveDimmer === 'true') || (data.HaveDimmer === true)) {
lvl = data.Level;
//console.log(data.status+" "+lvl);
if (lvl > 0) {
status = 1
} else {
status = 0
} ;
} else {
lvl = data.Level || 0;
status = 0;
} ;
//console.log(data.idx+" "+status+" "+lvl);
if ((data.SwitchType === 'Venetian Blinds EU') || (data.SwitchType === 'Venetian Blinds US') || (data.SwitchType === 'RollerTrol, Hasta new')) {
stoppable = 1;
}
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"};
room_tab.Switches=1;
}
//console.log(data.idx+" "+status+" "+lvl);
params = [];
params.push({"key": "Level", "value": lvl.toString()});
params.push({"key": "stopable", "value": stoppable.toString()});
params.push({"key": "pulsable", "value": "1"});
myfeed.params = params;
//console.log(params);
return (myfeed);
};
function DevShutter(data) {
var status = 0;
status = devSt(data);
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
var lvl = 0;
var stoppable = 0;
var mydev = {MaxDimLevel: null, Action: null, graph: null};
if (device_tab[data.idx]) {
mydev = device_tab[data.idx];
}
mydev.Action = 6;
mydev.MaxDimLevel = data.MaxDimLevel;
device_tab[data.idx] = mydev;
//console.log(data.Status+" "+data.Level);
//console.log(data.idx+" "+data.Status+" "+status+" "+lvl);
if (data.Status == 'Open') {
//console.log("Open"+data.idx+" "+data.Status+" "+status+" "+lvl);
lvl = 100;
status = 1;
} else if (data.Status == 'Closed') {
lvl = 0;
status = 0;
//console.log("aClosed "+data.idx+" "+data.Status+" "+status+" "+lvl);
} else if (data.Status.match(/Set Level/) || (data.HaveDimmer === 'true')|| (data.HaveDimmer === true) ) {
lvl = data.Level;
//console.log(data.status+" "+lvl);
if (lvl > 0) {
status = 1
} else {
status = 0
}
//console.log("mixed: "+data.idx+" "+data.Status+" "+status+" "+lvl);
}
//console.log(data.idx+" "+status+" "+lvl);
if ((data.SwitchType === 'Venetian Blinds EU') || (data.SwitchType === 'Venetian Blinds US') || (data.SwitchType === 'RollerTrol, Hasta new')|| (data.SwitchType === 'Blinds')) {
stoppable = 1;
}
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
//params.push({"key": "Status", "value": status});
if (data.Status === 'Closed') {
params.push({"key": "Level", "value": 0});
} else if (data.Status === 'Open') {
params.push({"key": "Level", "value": 100});
} else {
params.push({"key": "Level", "value": lvl.toString()});
}
//console.log(data.idx+" "+data.Status+" "+status+" "+lvl);
params.push({"key": "stopable", "value": stoppable.toString()});
params.push({"key": "pulsable", "value": "0"});
myfeed.params = params;
return (myfeed);
}
function DevMotion(data) {
var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf();
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMotion", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMotion", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMotion", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
var value = devSt(data);
//console.log(data.Status+" "+value);
params.push({"key": "Armable", "value": "0"});
params.push({"key": "ackable", "value": "0"});
params.push({"key": "Armed", "value": "1"});
params.push({"key": "Tripped", "value": value.toString()});
params.push({"key": "lasttrip", "value": dt.toString()});
myfeed.params = params;
return (myfeed);
}
function DevDoor(data) {//TODO
var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf();
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevDoor", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevDoor", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevDoor", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
var value = devSt(data);
params.push({"key": "armable", "value": "0"});
params.push({"key": "Ackable", "value": "0"});
params.push({"key": "Armed", "value": "1"});
params.push({"key": "Tripped", "value": value.toString()});
params.push({"key": "lasttrip", "value": dt.toString()});
myfeed.params = params;
return (myfeed);
}
function DevLock(data) {
var status = 0;
switch (data.Status) {
case 'Locked':
status = 1;
break;
case 'Unlocked':
status = 0;
break;
default:
status = 0;
break;
}
//Protected device
var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null};
if (! mydev && (data.Protected === 'true')) {
mydev.Protected = 1;
}
device_tab[data.idx] = mydev;
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevLock", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevLock", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
params.push({"key": "Status", "value": status.toString()});
myfeed.params = params;
return (myfeed);}
function DevSmoke(data) {
var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf();
var ackable = 0;
if (data.Type == 'Security') {
ackable = 1;
}
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSmoke", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSmoke", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSmoke", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
var value = devSt(data);
params.push({"key": "Armable", "value": "0"});
params.push({"key": "ackable", "value": ackable.toString()});
params.push({"key": "Armed", "value": "1"});
params.push({"key": "Tripped", "value": value.toString()});
params.push({"key": "lasttrip", "value": dt.toString()});
myfeed.params = params;
return (myfeed);
}
function DevFlood(data) {
var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf();
var ackable = 0;
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevFlood", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevFlood", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevFlood", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
var value = devSt(data);
params.push({"key": "armable", "value": "0"});
params.push({"key": "Ackable", "value": ackable.toString()});
params.push({"key": "Armed", "value": "1"});
params.push({"key": "Tripped", "value": value.toString()});
params.push({"key": "lasttrip", "value": dt.toString()});
myfeed.params = params;
return (myfeed);
}
function DevCO2(data) {
var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf();
var ackable = 0;
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
var value = devSt(data);
params.push({"key": "armable", "value": "0"});
params.push({"key": "Ackable", "value": ackable.toString()});
params.push({"key": "Armed", "value": "1"});
params.push({"key": "Tripped", "value": value.toString()});
params.push({"key": "lasttrip", "value": dt.toString()});
myfeed.params = params;
return (myfeed);
}
function DevCO2Alert(data) {
var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf();
var ackable = 0;
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2Alert", "room": "Switches"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2Alert", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2Alert", "room": "Switches"};
room_tab.Switches=1;
}
params = [];
var value = devSt(data);
params.push({"key": "armable", "value": "0"});
params.push({"key": "ackable", "value": ackable.toString()});
params.push({"key": "Armed", "value": "1"});
params.push({"key": "Tripped", "value": value.toString()});
params.push({"key": "lasttrip", "value": dt.toString()});
myfeed.params = params;
return (myfeed);
}
function DevGenericSensor(data) {
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": "Utility"};
room_tab.Utility=1;
}
if (data.Status) {
params = [];
params.push({"key": "Value", "value": data.Status.toString()});
myfeed.params = params;
} else if (data.Data) {
params = [];
//console.log(data;)
params.push({"key": "Value", "value": data.Data.toString()});
myfeed.params = params;
}
return (myfeed);
}
function DevGenericSensorT(data) {
var ptrn = /(-?[0-9]+(?:\.[0-9]+)?) ?(.+)/;
var res = data.Data.match(ptrn).slice(1);
var value = res[0];
var suffix = res[1];
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": "Utility"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": "Utility"};
room_tab.Utility=1;
}
params = [];
params.push({"key": "Value", "value": value.toString(), "unit": suffix.toString(), "graphable": "true"});
myfeed.params = params;
return (myfeed);
}
function DevElectricity(data) {
room_tab.Utility = 1;
var ptrn1 = /(\d+) Watt/;
var ptrn2 = /([0-9]+(?:\.[0-9]+)?) Watt/;
var ptrn3 = /[\s,]+/;
var ptrn4 = /([0-9]+(?:\.[0-9]+)?) kWh/;
var myfeed;
var params = [];
var combo = [];
if (data.UsageDeliv) {
//Energy pannel
//develectricity Usage/CounterToday
var myfeed1 = {"id": data.idx + "_L1", "name": data.Name, "type": "DevElectricity", "room": "Utility"};
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed1 = {"id": data.idx + "_L1", "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed1 = {"id": data.idx + "_L1", "name": data.Name, "type": "DevElectricity", "room": "Utility"};
room_tab.Utility=1;
}
var params = []
var res = ptrn2.exec(data.Usage);
var usage = 0;
if (res != null) {
usage = Math.ceil(Number(res[1]));
}
var res = ptrn4.exec(data.CounterToday);
var usageToday = 0;
if (res != null) {
usageToday = Math.ceil(Number(res[1]));
}
params.push({"key": "Watts", "value": usage, "unit": "W"});
params.push({"key": "ConsoTotal", "value": Math.ceil(usageToday), "unit": "kWh", "graphable": "true"});
myfeed1.params = params;
combo.push(myfeed1);
//develectricity UsageDeliv/CounterDelivToday
var params = [];
if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) {
var myfeed2 = {"id": data.idx + "_L2", "name": data.Name + "Deliv", "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]};
} else {
var myfeed2 = {"id": data.idx + "_L2", "name": data.Name + "Deliv", "type": "DevElectricity", "room": "Utility"};
room_tab.Utility=1;
}
var res = ptrn2.exec(data.UsageDeliv);
var usagedeliv = 0;
if (res != null) {
usagedeliv = Math.ceil(Number(res[1]));
}
var res = ptrn4.exec(data.CounterDelivToday);
var usageDelivToday = 0;
if (res != null) {
usageDelivToday = Math.ceil(Number(res[1]));