-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.js
executable file
·1024 lines (944 loc) · 44.6 KB
/
meta.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
//"use strict";
process.env.StartupPath = __dirname;
const path = require('path');
const settings = require(path.join(__dirname,'settings'));
const neeoapi = require("neeo-sdk");
const metacontrol = require(path.join(__dirname,'metaController'));
//Discovery tools
var mdns = require('multicast-dns')()
const find = require('local-devices');
var discoveryBuffer = __dirname + '/resultsDiscovery.json'
const fs = require('fs');
var activatedModule = path.join(__dirname,'active');
if (settings.activeLibrary) {activatedModule = settings.activeLibrary;}
const BUTTONHIDE = '__';
const DATASTOREEXTENSION = 'DataStore.json';
const DEFAULT = 'default'; //NEEO SDK deviceId default value for devices
const mqtt = require('mqtt');
const { metaMessage, LOG_TYPE, initialiseLogComponents, initialiseLogSeverity,OverrideLoglevel, getLoglevels } = require("./metaMessage");
config = [{brainip : '', brainport : ''}];
function returnBrainIp() { return config.brainip;}
var brainDiscovered = false;
var brainConsoleGivenIP = undefined;
var driverTable = [];
var localDevices = [];
exports.localDevices = localDevices;
exports.neeoBrainIp = returnBrainIp;
var mqttClient;
//LOGGING SETUP AND WRAPPING
//Disable the NEEO library console warning.
console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
function metaLog(message) {
let initMessage = { component:'meta', type:LOG_TYPE.ERROR, content:'', deviceId: null };
let myMessage = {...initMessage, ...message}
return metaMessage (myMessage);
}
function networkDiscovery() {
try {
fs.readFile(discoveryBuffer, (err, data) => {
if (err) {
metaLog({type:LOG_TYPE.INFO, content:'No discovery file, starting to discover now.'});
}
else {
if (data && (data != '')) {
metaLog({type:LOG_TYPE.INFO, content:'Discovery buffer loaded, scanning changes now'});
let savedDevices = JSON.parse(data);
savedDevices.forEach((dev) => {localDevices.push(dev);})
}
}
//Unleaching discovery
mdns.on('response', function(response) {
let myObjectPTR, myObjectIP, myObjectMac;
let hasChanged = false;
let myName, myShortName, myIP, myMac, myPort = undefined
if (response.additionals) {
myObjectPTR = response.additionals.find((answer) => {return answer.type == 'PTR'});
if (myObjectPTR && myObjectPTR.data) {
myName = myName?myName:myObjectPTR.data;
};
if (myObjectPTR && myObjectPTR.name) {
myShortName = myShortName?myShortName:myObjectPTR.name;
};
myObjectPTR = response.additionals.find((answer) => {return (answer.type == 'SRV')});
if (myObjectPTR && myObjectPTR.data) {
myPort = myPort?myPort:myObjectPTR.data.port;
};
myObjectIP = response.additionals.find((answer) => {return answer.type == 'A'});
if (myObjectIP && myObjectIP.data) {myIP = myObjectIP.data};
myObjectMac = response.additionals.find((answer) => {return answer.type == 'AAA'});
if (myObjectMac && myObjectMac.data) {myMac = myObjectMac.data};
}
if (response.authorities) {
myObjectPTR = response.authorities.find((answer) => {return answer.type == 'PTR'});
if (myObjectPTR && myObjectPTR.data) {
myName = myName?myName:myObjectPTR.data;
};
if (myObjectPTR && myObjectPTR.name) {
myShortName = myShortName?myShortName:myObjectPTR.name;
};
myObjectPTR = response.authorities.find((answer) => {return (answer.type == 'SRV')});
if (myObjectPTR && myObjectPTR.data) {
myPort = myPort?myPort:myObjectPTR.data.port;
};
myObjectIP = response.authorities.find((answer) => {return answer.type == 'A'});
if (myObjectIP && myObjectIP.data) {myIP = myObjectIP.data};
myObjectMac = response.authorities.find((answer) => {return answer.type == 'AAA'});
if (myObjectMac && myObjectMac.data) {myMac = myObjectMac.data};
}
if (response.answers) {
myObjectPTR = response.answers.find((answer) => {return (answer.type == 'PTR')});
if (myObjectPTR && myObjectPTR.data) {
myName = myName?myName:myObjectPTR.data;
};
if (myObjectPTR && myObjectPTR.name) {
myShortName = myShortName?myShortName:myObjectPTR.name;
};
myObjectPTR = response.answers.find((answer) => {return (answer.type == 'SRV')});
if (myObjectPTR && myObjectPTR.data) {
myPort = myPort?myPort:myObjectPTR.data.port;
};
// if (myObjectPTR && myObjectPTR.name) {
// myShortName = myShortName?myShortName:myObjectPTR.name;
// };
myObjectIP = response.answers.find((answer) => {return answer.type == 'A'});
if (myObjectIP && myObjectIP.data) {myIP = myObjectIP.data};
myObjectMac = response.answers.find((answer) => {return answer.type == 'AAA'});
if (myObjectMac && myObjectMac.data) {myMac = myObjectMac.data};
}
if (localDevices.findIndex((device)=>{return (device.name == myName && device.short == myShortName && device.ip == myIP&& device.port == myPort &&device.mac == myMac)})<0) {
if (myIP!=undefined && myIP.startsWith("192")) {
find(myIP).then(device => {
if (device) {myMac = device.mac;};
indport = localDevices.findIndex((device)=>{return (device.name == myName && device.short == myShortName && device.ip == myIP&&device.mac == myMac)});//avoid device with too many ports
if (indport<0) {
if (myIP!=undefined || myMac!=undefined) {
hasChanged = true;
localDevices.push({"name":myName,"ip":myIP,"mac":myMac, "short":myShortName, "port":myPort});
}
}
else {
hasChanged = true;
localDevices[indport].port = myPort;
}
if (hasChanged) {
if (delayWrite == undefined) {//In order to be gentle on storage.
delayWrite = setTimeout(() => {
fs.writeFile(discoveryBuffer, JSON.stringify(localDevices), err => {
if (err) {
metaLog({type:LOG_TYPE.ERROR, content:"Error writing the discovery file. " + err});
} else {
metaLog({type:LOG_TYPE.WARNING, content:"Discovery updated"});
}
})
delayWrite = undefined;
}, 10000);
}
}
})
}
}
});
//multicast-dns
let delayWrite = undefined; //Timer to avoid writting too much on the drive.
setTimeout(() => {
metaLog({type:LOG_TYPE.WARNING, content:"stopping discovery process."});
mdns.destroy();
}, 300000);
})
return null;
}
catch (err) {
metaLog({type:LOG_TYPE.ERROR, content:"Error during discovery process."});
metaLog({type:LOG_TYPE.ERROR, content:err});
}
}
function getConfig() {
return new Promise(function (resolve, reject) {
fs.readFile(__dirname + '/config.js', (err, data) => {
if (err) {
metaLog({type:LOG_TYPE.ERROR, content:'No config file, the initial setup will be launched'});
resolve(null);
}
else {
if (data && (data != '')) {
config = JSON.parse(data);
resolve(config);
}
else {
resolve (config);}
}
})
})
}
function getHelper (HelpTable, prop, deviceId) {
return HelpTable[HelpTable.findIndex((item) => { return (item.name==prop && item.deviceId==deviceId) })];
}
function getIndividualActivatedDrivers(files, driverList, driverIterator) {
return new Promise(function (resolve, reject) {
try {
if (files) {
if (driverIterator < files.length) {
if (files[driverIterator].endsWith(".json")) {
if (!files[driverIterator].endsWith(DATASTOREEXTENSION)){ //To separate from datastore
metaLog({type:LOG_TYPE.INFO,content:'Activating drivers: ' + files[driverIterator]});
fs.readFile(path.join(activatedModule,files[driverIterator]), (err, data) => {
if (data) {
try {
const driver = JSON.parse(data);
driver.filename = files[driverIterator];
if (driver.template) { //persisted variables management
driver.template.filename = files[driverIterator];
}
driverList.push(driver);
}
catch (err) {
metaLog({type:LOG_TYPE.ERROR, content:'Error parsing driver : ' + files[driverIterator]});
metaLog({type:LOG_TYPE.ERROR, content:err});
}
} // if (data)
if (err) {
metaLog({type:LOG_TYPE.ERROR, content:'Loading the driver file : ' + files[driverIterator]});
metaLog({type:LOG_TYPE.ERROR, content:err});
} //if (err)
resolve(getIndividualActivatedDrivers(files, driverList, driverIterator+1));
}) //readFile
} // .endsWith(DATASTOREEXTENSION)
else {//console.log("Skipping datastore",files[driverIterator]);
resolve(getIndividualActivatedDrivers(files, driverList, driverIterator+1));
}
} //endsWith(".json"
else {metaLog({type:LOG_TYPE.WARNING, content:'Skipping unknown extension '+files[driverIterator]});
resolve(getIndividualActivatedDrivers(files, driverList, driverIterator+1));
}
} //(driverIterator < files.length)
else {
resolve(driverList)
}
}
else {resolve([])
}
}
catch (err) {metaLog({type:LOG_TYPE.ERROR, content:"Error occurred during driver load "+err})}
})
}
function getActivatedDrivers() {
return new Promise(function (resolve, reject) {
metaLog({type:LOG_TYPE.VERBOSE, content:'Searching drivers in : ' + activatedModule});
fs.readdir(activatedModule, (err, files) => {
metaLog({type:LOG_TYPE.VERBOSE,content:'drivers found'});
var driverList = [];
getIndividualActivatedDrivers(files, driverList,0).then((list) => {
resolve(list);
})
})
})
}
function getDataStorePath(filename) {
try {
if (filename) {
return path.join(activatedModule, filename.split('.json')[0] + '-DataStore.json');
}
else {return null;}
}
catch (err) {
metaLog({type:LOG_TYPE.ERROR, content:'your path (' + filename + ') given seems to be wrong :'});
metaLog({type:LOG_TYPE.ERROR, content:err});
}
}
function createDevices () {
return new Promise(function (resolve, reject) {
getActivatedDrivers().then((drivers) => {
const driverCreationTable = [];
drivers.forEach((driver) => {
driverCreationTable.push(executeDriverCreation(driver));
})
Promise.all(driverCreationTable).then((driverTab) => {
driverTable = driverTab;
resolve(driverTab);
})
})
})
}
function discoveredDriverListBuilder(inputRawDriverList, outputPreparedDriverList, indent, controller, targetDeviceId, driver) {
return new Promise (function (resolve, reject) {
//targetDeviceId = undefined; //void the logic and force creation of all devices.
if (indent < inputRawDriverList.length) {
if (inputRawDriverList[indent].dynamicname && inputRawDriverList[indent].dynamicname != "") {
if (targetDeviceId == undefined || targetDeviceId == inputRawDriverList[indent].dynamicid)
{
inputRawDriverList[indent].name = driver.name;
inputRawDriverList[indent].type = driver.type;
inputRawDriverList[indent].version = driver.version;
inputRawDriverList[indent].manufacturer = driver.manufacturer;
inputRawDriverList[indent].icon = driver.icon;
inputRawDriverList[indent].alwayson = driver.alwayson;
executeDriverCreation(inputRawDriverList[indent], controller, inputRawDriverList[indent].dynamicid).then((builtdevice) => {
builtdevice.addCapability("dynamicDevice");
const discoveredDevice = {
id:inputRawDriverList[indent].dynamicid,
name:inputRawDriverList[indent].dynamicname,
reachable:true,
device : builtdevice
}
outputPreparedDriverList.push(discoveredDevice);
driverTable.push(builtdevice);
if (targetDeviceId == undefined) {//initial creation of the driver, need the full list to be returned
resolve(discoveredDriverListBuilder(inputRawDriverList, outputPreparedDriverList, indent+1, controller, targetDeviceId, driver));
}
else {//on the spot creation of a specific driver, we leave after creation.
resolve(outputPreparedDriverList);
}
})
}//all these else to ensure proper timely construction and not a resolve before end of creation.
else {
resolve(discoveredDriverListBuilder(inputRawDriverList, outputPreparedDriverList, indent+1, controller, targetDeviceId, driver));
}
}
else {
resolve(discoveredDriverListBuilder(inputRawDriverList, outputPreparedDriverList, indent+1, controller, targetDeviceId, driver));
}
}
else {
resolve (outputPreparedDriverList);
}
})
}
function instanciationHelper(controller, givenResult, jsonDriver) {
try {
jsonDriver = JSON.stringify(jsonDriver);
let slicedDriver = jsonDriver.split("DYNAMIK_INST_START ");
let recontructedDriver = slicedDriver[0];
for (let index = 1; index < slicedDriver.length; index++) {
//TODO Correct ugly hack suppressing the escape of quote..
let tempoResult = slicedDriver[index].split(" DYNAMIK_INST_END")[0].replace(/\\/g, "");
//let tempoResult = slicedDriver[index].split(" DYNAMIK_INST_END")[0];
tempoResult = controller.vault.readVariables(tempoResult, DEFAULT);
tempoResult = controller.assignTo("$Result", tempoResult, givenResult);
recontructedDriver = recontructedDriver + tempoResult;
recontructedDriver = recontructedDriver + slicedDriver[index].split(" DYNAMIK_INST_END")[1];
}
recontructedDriver = controller.vault.readVariables(recontructedDriver, DEFAULT);
metaLog({type:LOG_TYPE.VERBOSE, content:'Driver has been reconstructed.'});
//metaLog({type:LOG_TYPE.DEBUG, content:recontructedDriver});
return JSON.parse(recontructedDriver);
}
catch (err) {
return;
}
}
function prepareCommand(controller, commandArray, deviceId, index) {
return new Promise(function (resolve, reject) {
if (commandArray && commandArray.length>index) {
controller.actionManager(deviceId, commandArray[index].type, commandArray[index].command, commandArray[index].queryresult, commandArray[index].evaldo, commandArray[index].evalwrite)
.then(()=>{
resolve(prepareCommand(controller, commandArray, deviceId, index+1));
})
}
else {
resolve()}
})
}
function discoveryDriverPreparator(controller, driver, deviceId) {
return new Promise(function (resolve, reject) {
try {
if (driver.discover) {
controller.vault.retrieveValueFromDataStore("ToInitiate","default").then((ToInitiate)=>{
if (ToInitiate == undefined) {ToInitiate = true;}
metaLog({deviceId: deviceId, type:LOG_TYPE.VERBOSE, content:"ToInitiate " + ToInitiate});
prepareCommand(controller, driver.discover.initcommandset, deviceId, ToInitiate?0:(driver.discover.initcommandset?driver.discover.initcommandset.length:0)).then(()=> {
let instanciationTable = [];
metaLog({deviceId: deviceId, type:LOG_TYPE.DEBUG, content:"Driver Discovery preparation."});
controller.initiateProcessor(driver.discover.command.type).then(() => {
controller.commandProcessor(driver.discover.command.command, driver.discover.command.type, deviceId).then((result)=>{
controller.queryProcessor(result, driver.discover.command.queryresult, driver.discover.command.type, deviceId).then((result) => {
if (driver.discover.command.evalwrite) {controller.evalWrite(driver.discover.command.evalwrite, result, deviceId)};
metaLog({deviceId: deviceId, type:LOG_TYPE.DEBUG, content:'discovery Driver Preparation, query result'});
metaLog({deviceId: deviceId, type:LOG_TYPE.DEBUG, content:result});
if (!Array.isArray(result)) {
let tempo = [];
tempo.push(result);
result = tempo;
}
result.forEach(element => {
driverInstance = instanciationHelper(controller, element, driver.template);
if (driverInstance != undefined) {
instanciationTable.push(driverInstance);
}
});
resolve(instanciationTable)
})
})
})
})
})
.catch((err) => {
metaLog({type:LOG_TYPE.ERROR, content:'Driver has no ToInitiate Persisted Variable.'});
metaLog({type:LOG_TYPE.ERROR, content:err});
})
}
else {
resolve();
}
}
catch (error) {
metaLog({type:LOG_TYPE.ERROR, content:'Couldn\'t construct the driver.'});
metaLog({type:LOG_TYPE.ERROR, content:error});
}
})
}
function getRegistrationCode(controller, credentials, driver, deviceId){
return new Promise(function (resolve, reject) {
controller.vault.addVariable("RegistrationCode", credentials.securityCode, deviceId, true)
registerDevice(controller, driver, deviceId).then((result)=>{
if (result) {
resolve(true);
}
else {
resolve(false)
}
})
})
}
function registerDevice(controller, driver, deviceId) {
return new Promise(function (resolve, reject) {
controller.actionManager(DEFAULT, driver.register.registrationcommand.type, driver.register.registrationcommand.command,
driver.register.registrationcommand.queryresult, '', driver.register.registrationcommand.evalwrite)
.then((result) => {
controller.reInitVariablesValues(deviceId);
controller.reInitConnectionsValues(deviceId);
if (controller.vault.getValue("IsRegistered", deviceId)) {
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:"Registration success"});
resolve(true);
}
else {
metaLog({deviceId: deviceId, type:LOG_TYPE.WARNING, content:'Registration Failure'});
resolve(false);
}
})
})
}
function isDeviceRegistered(controller, driver, deviceId) {
return new Promise(function (resolve, reject) {
let retValue = controller.vault.getValue("IsRegistered", deviceId);
metaLog({deviceId: deviceId, type:LOG_TYPE.VERBOSE, content:'is registered ? : ' + retValue});
if (retValue) {resolve(retValue);}
else {
prepareCommand(controller, driver.register.commandset, deviceId, 0).then(()=> {
registerDevice(controller, driver, deviceId).then((result)=>{
metaLog({deviceId: deviceId, type:LOG_TYPE.VERBOSE, content:'the result of the registration process is '+result});
if (result) {
resolve(true);
}
else {
resolve(false)
}
})
})
}
})
}
function createController(hubController, driver) {//Discovery specific
if (hubController) {//We are inside a discovered item no new controller to be created.
return hubController;
}
else {//normal device, controller to be created.
const controller = new metacontrol(driver);
return controller;
}
}
function assignControllers(controller, driver, deviceId) {
for (var prop in driver.buttons) { // Dynamic creation of all buttons
if (Object.prototype.hasOwnProperty.call(driver.buttons, prop)) {
controller.addButton(deviceId, prop, driver.buttons[prop])
}
}
for (var prop in driver.images) { // Dynamic creation of all images
if (Object.prototype.hasOwnProperty.call(driver.images, prop)) {
controller.addImageHelper(deviceId, prop, driver.images[prop].listen)
}
}
for (var prop in driver.labels) { // Dynamic creation of all labels
if (Object.prototype.hasOwnProperty.call(driver.labels, prop)) {
controller.addLabelHelper(deviceId, prop, driver.labels[prop].listen, driver.labels[prop].actionlisten)
}
}
for (var prop in driver.sensors) { // Dynamic creation of all sensors
if (Object.prototype.hasOwnProperty.call(driver.sensors, prop)) {
controller.addSensorHelper(deviceId, prop, driver.sensors[prop].listen)
}
}
for (var prop in driver.switches) { // Dynamic creation of all sliders
if (Object.prototype.hasOwnProperty.call(driver.switches, prop)) {
controller.addSwitchHelper(deviceId, prop, driver.switches[prop].listen, driver.switches[prop].evaldo);
}
}
for (var prop in driver.sliders) { // Dynamic creation of all sliders
if (Object.prototype.hasOwnProperty.call(driver.sliders, prop)) {
controller.addSliderHelper(deviceId, driver.sliders[prop].listen, driver.sliders[prop].evaldo, prop);
}
}
for (var prop in driver.directories) { // Dynamic creation of directories
if (Object.prototype.hasOwnProperty.call(driver.directories, prop)) {
const theHelper = controller.addDirectoryHelper(deviceId, prop);
for (var feed in driver.directories[prop].feeders) {
let feedConfig = {"name":feed,
"label":driver.directories[prop].feeders[feed].label,
"commandset":driver.directories[prop].feeders[feed].commandset,
};
theHelper.addFeederHelper(feedConfig);
}
}
}
}
function executeDriverCreation (driver, hubController, passedDeviceId) {
return new Promise(function (resolve, reject) {
//driverTable.length = 0; //Reset the table without cleaning the previous reference (to avoid destructing other devices when running Discovery).
let deviceId = passedDeviceId ? passedDeviceId : DEFAULT; //to add the deviceId of the real discovered device in the Helpers
let controller = createController(hubController, driver);
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:'creating the driver: ' + deviceId + " with controller: " + controller.name});
//TODO check if this is still usefull
//if (hubController) {controller.assignDiscoverHubController(hubController)}; //if the device is a discovered device.
const theDevice = neeoapi.buildDevice(settings.driverPrefix + driver.name)
theDevice.setType(driver.type);
theDevice.setDriverVersion(driver.version);
theDevice.setManufacturer(driver.manufacturer);
if (driver.icon) {
theDevice.setIcon(driver.icon)
}
if (driver.deviceCapabilities) {
try {
driver.deviceCapabilities.forEach(capa => {
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:"Driver "+driver.name+" has device capability "+capa+"added"});
theDevice.addCapability(capa);
})
}
catch (err) {console.log("Handling device capabilities got an error",err)}
}
if (driver.alwayson) {
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:"Driver "+driver.name+" requested with always ON "});
theDevice.addCapability("alwaysOn");
}
if (theDevice.supportsTiming()) {theDevice.defineTiming({ powerOnDelayMs: 200, sourceSwitchDelayMs: 50, shutdownDelayMs: 100 })};
//CREATING VARIABLES
for (var prop in driver.variables) { // Initialisation of the variables
if (Object.prototype.hasOwnProperty.call(driver.variables, prop)) {
controller.vault.addVariable(prop, driver.variables[prop], deviceId)
}
}
if (driver.persistedvariables){
for (var prop in driver.persistedvariables) { // Initialisation of the variables to be persisted
if (Object.prototype.hasOwnProperty.call(driver.persistedvariables, prop)) {
controller.vault.addVariable(prop, driver.persistedvariables[prop], deviceId, true);
}
}
}
controller.vault.initialiseVault(getDataStorePath(driver.filename)).then(() => {//Retrieve the value form the vault
if (driver.discover)
if (driver.discover.useHub)
{metaLog({deviceId: deviceId, type:LOG_TYPE.VERBOSE, content:"Trying to get HUB-datastore from: "+driver.discover.useHub})
let bb = controller.vault.initialiseHubVault(path.join(settings.activeLibrary,driver.discover.useHub + '-DataStore.json'))
if (bb == "")
metaLog({deviceId: deviceId, type:LOG_TYPE.ERROR, content:"HUB-datastore not available..."})
}
//CREATING CONTROLLERS
assignControllers(controller, driver, deviceId);
//GET ALL CONNECTIONS
if (driver.webSocket) {
controller.addConnection({"name":"webSocket", "descriptor":"", "connector":"", "deviceId":deviceId})
}
if (driver.socketIO) {
controller.addConnection({"name":"socketIO", "descriptor":driver.socketIO, "connector":"", "deviceId":deviceId})
}
if (driver.net) {
controller.addConnection({"name":"net", "descriptor":driver.net, "connector":"", "deviceId":deviceId})
}
if (driver.jsontcp) {
controller.addConnection({"name":"jsontcp", "descriptor":driver.jsontcp, "connector":"", "deviceId":deviceId})
}
if (settings.mqtt) {
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:'Creating the connection MQTT for meta'});
controller.addConnection({"name":"mqtt", "descriptor":settings.mqtt, "connector":mqttClient, "deviceId":deviceId})//early loading
}
if (driver.mqtt) {
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:'Creating the connection MQTT for driver'});
controller.addConnection({"name":"mqtt", "descriptor":driver.mqtt, "connector":"", "deviceId":deviceId})
}
if (driver.repl) {
controller.addConnection({"name":"repl", "descriptor":driver.repl, "connector":"", "deviceId":deviceId})
}
//Registration
if (driver.register) {
//need to test internal variable here.... same story than discovery my friend...
// prepareCommand(controller, driver.register.commandset, deviceId, 0).then(()=> {
theDevice.enableRegistration(
{
type: 'SECURITY_CODE',
headerText: driver.register.registerheadertext,
description: driver.register.registerdescription,
},
{
register: (credentials) => getRegistrationCode(controller, credentials, driver, deviceId),
isRegistered: () => {return new Promise(function (resolve, reject) {isDeviceRegistered(controller, driver, deviceId).then((res)=>{resolve(res)})})},
})
}
//DISCOVERY
if (driver.discover) {
metaLog({deviceId: deviceId, type:LOG_TYPE.WARNING, content:'Creating discovery process for ' + controller.name});
theDevice.enableDiscovery(
{
headerText: driver.discover.welcomeheadertext,
description: driver.discover.welcomedescription,
enableDynamicDeviceBuilder: true,
},
(targetDeviceId) => {
let ind0 = controller.discoveredDevices.findIndex(dev => {return dev.id == targetDeviceId});
if (ind0>=0) {
metaLog({deviceId: deviceId, type:LOG_TYPE.DEBUG, content:"And we have found it"});
metaLog({deviceId: deviceId, type:LOG_TYPE.DEBUG, content:'Skipping duplicate discovery of device:'+targetDeviceId});
}
else {
metaLog({deviceId: deviceId, type:LOG_TYPE.DEBUG, content:"Device was not created before: "+targetDeviceId});
return new Promise(function (resolve, reject) {
const formatedTable = [];
metaLog({deviceId: deviceId, type:LOG_TYPE.DEBUG, content:'Discovering this device: '+targetDeviceId});
if (targetDeviceId) {
let ind = controller.discoveredDevices.findIndex(dev => {return dev.id == targetDeviceId});
if (ind>=0) {
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:"DRIVER CACHE USED"});
formatedTable.push(controller.discoveredDevices[ind]);
resolve(formatedTable);
return;
}
}
discoveryDriverPreparator(controller, driver, deviceId).then((driverList) => {
discoveredDriverListBuilder(driverList, formatedTable, 0, controller, targetDeviceId, driver).then((outputTable) => {
outputTable.forEach(output => {if (controller.discoveredDevices.findIndex(dev => {dev.id == output.id})<0) {controller.discoveredDevices.push(output)}});
resolve(outputTable);
})
})
})
}
}
)
}
controller.reInitConnectionsValues(deviceId);
//CREATING LISTENERS
for (var prop in driver.listeners) { // Initialisation of the variables
if (Object.prototype.hasOwnProperty.call(driver.listeners, prop)) {
controller.addListener({
name : prop,
deviceId: deviceId,
isHub: driver.listeners[prop].isHub,
interested: [],
interestedAndUsing: [],
type : driver.listeners[prop].type,
command : driver.listeners[prop].command,
initialCommand : driver.listeners[prop].command,
timer : "", //prepare the listener to save the timer here.
pooltime : driver.listeners[prop].pooltime,
poolduration : driver.listeners[prop].poolduration,
queryresult : driver.listeners[prop].queryresult,
evalwrite : driver.listeners[prop].evalwrite,
evaldo : driver.listeners[prop].evaldo
})
}
}
//CREATING INDIVIDUAL SHORTCUTS
for (var prop in driver.buttons) { // Dynamic creation of all buttons
if (Object.prototype.hasOwnProperty.call(driver.buttons, prop)) {
if (theDevice.buttons.findIndex((item) => {return (item.param.name == prop)})<0) {//not button of same name (in case included in a widget)
if (!prop.startsWith(BUTTONHIDE)){ //If the button doesnt need to be hidden.
theDevice.addButton({name: prop, label: (driver.buttons[prop].label == '') ? (prop) : (driver.buttons[prop].label)})
}
}
}
}
for (var prop in driver.images) { // Dynamic creation of all images
if (Object.prototype.hasOwnProperty.call(driver.images, prop)) {
if (theDevice.imageUrls.findIndex((item) => {return (item.param.name == prop)})<0) {//not image of same name (in case included in a widget)
const helperI = getHelper(controller.imageH, prop, deviceId);
theDevice.addImageUrl({name: prop, label: (driver.images[prop].label == '') ? (prop) : (driver.images[prop].label),
size : driver.images[prop].size},
(theDeviceId) => helperI.get(theDeviceId))
}
}
}
for (var prop in driver.labels) { // Dynamic creation of all labels
if (Object.prototype.hasOwnProperty.call(driver.labels, prop)) {
if (theDevice.textLabels.findIndex((item) => {return (item.param.name == prop)})<0) {//not item of same name (in case included in a widget)
const helperL = getHelper(controller.labelH, prop, deviceId);
theDevice.addTextLabel({name: prop, label: (driver.labels[prop].label == '') ? (prop) : (driver.labels[prop].label)},
helperL.get);
}
}
}
for (var prop in driver.sensors) { // Dynamic creation of all sensors
if (Object.prototype.hasOwnProperty.call(driver.sensors, prop)) {
if (theDevice.sensors.findIndex((item) => {return (item.param.name == prop)})<0) {//not item of same name (in case included in a widget)
const helperSe = getHelper(controller.sensorH, prop, deviceId);
theDevice.addSensor({name: prop, label: (driver.sensors[prop].label == '') ? (prop) : (driver.sensors[prop].label),
type:driver.sensors[prop].type},
{
getter: helperSe.get
});
}
}
}
for (var prop in driver.switches) { // Dynamic creation of all switches
if (Object.prototype.hasOwnProperty.call(driver.switches, prop)) {
if (theDevice.switches.findIndex((item) => {return (item.param.name == prop)})<0) {//not item of same name (in case included in a widget)
const helperSw = getHelper(controller.switchH, prop, deviceId);
theDevice.addSwitch({
name: prop,
label: (driver.switches[prop].label == '') ? (prop) : (driver.switches[prop].label),
},
{
setter: helperSw.set, getter: helperSw.get
})
}
}
}
for (var prop in driver.sliders) { // Dynamic creation of all sliders
if (Object.prototype.hasOwnProperty.call(driver.sliders, prop)) {
if (theDevice.sliders.findIndex((item) => {return (item.param.name == prop)})<0) {//not slider of same name (in case included in a widget)
const helperS = getHelper(controller.sliderH, prop, deviceId);
theDevice.addSlider({
name: prop,
label: (driver.sliders[prop].label == '') ? (prop) : (driver.sliders[prop].label),
range: [0,100], unit: driver.sliders[prop].unit
},
{
setter: helperS.set, getter: helperS.get
})
}
}
}
for (var prop in driver.directories) { // Dynamic creation of directories
if (Object.prototype.hasOwnProperty.call(driver.directories, prop)) {
if (theDevice.directories.findIndex((item) => {return (item.param.name == prop)})<0) {//not directory of same name (in case included in a widget)
const helperD = getHelper(controller.directoryH, prop, deviceId);
theDevice.addDirectory({
name: prop,
label: (driver.directories[prop].label == '') ? (prop) : (driver.directories[prop].label),
}, helperD.browse)
}
}
}
theDevice.addButtonHandler((name, theDeviceId) => {controller.onButtonPressed(name, theDeviceId)})
theDevice.registerSubscriptionFunction((updateCallback) => {controller.sendComponentUpdate = updateCallback});
theDevice.registerInitialiseFunction((theDeviceId) => {}); //Don't want to initialise to early because of registration and initiation mecanism
theDevice.registerDeviceSubscriptionHandler(
{
deviceAdded: (theDeviceId) => {
metaLog({deviceId: theDeviceId, type:LOG_TYPE.VERBOSE, content:'device added'});
controller.dynamicallyAssignSubscription(theDeviceId,false);
},
deviceRemoved: (theDeviceId) => {
metaLog({deviceId: theDeviceId, type:LOG_TYPE.VERBOSE, content:'device removed'});
},
initializeDeviceList: (theDeviceIds) => {
metaLog({deviceId: "theDeviceIds", type:LOG_TYPE.VERBOSE, content:"INITIALIZED DEVICES:" + theDeviceIds});
},
}
)
metaLog({deviceId: deviceId, type:LOG_TYPE.INFO, content:"Device " + driver.name + " has been created."});
enableMQTT(controller, deviceId);
resolve(theDevice);
});
})
}
//DISCOVERING BRAIN
function discoverBrain() {
return new Promise(function (resolve, reject) {
metaLog({type:LOG_TYPE.INFO, content:"Trying to discover a NEEO Brain..."});
brainDiscovered = true;
neeoapi.discoverOneBrain()
.then((brain) => {
metaLog({type:LOG_TYPE.INFO, content:"Brain Discovered at IP : " + brain.iparray.toString()});
config.brainip = brain.iparray.toString();
resolve();
})
.catch ((err) => {
metaLog({type:LOG_TYPE.FATAL, content:"Brain couldn't be discovered using the neeo-sdk framework." + err});
metaLog({type:LOG_TYPE.FATAL, content:"Now trying using .meta internal discovery." + err});
setTimeout(() => {
let ind = localDevices.findIndex((device) => {return device.short == '_neeo._tcp.local'});
if (ind < 0) {//second chance
ind = localDevices.findIndex((device) => {
if (device.name) {
return (device.name.startsWith("neeo-") || device.name.startsWith("NEEO-"))
}
});
}
if (ind >= 0) {
config.brainip = localDevices[ind].ip;
metaLog({type:LOG_TYPE.WARNING, content:"The discovery seems to be a success. Brain found at " + config.brainip});
resolve();
}
metaLog({type:LOG_TYPE.FATAL, content:".meta internal discovery didn't find the neeo brain, check the connection." + err});
}, 2000);
})
})
}
function setupNeeo(forceDiscovery) {
return new Promise(function (resolve, reject) {
if (forceDiscovery) {
discoverBrain().then(() => {
runNeeo();
})
}
else if (brainConsoleGivenIP) {
config.brainip = brainConsoleGivenIP;
metaLog({type:LOG_TYPE.INFO, content:"Using brain-IP from CommandLine: " + brainConsoleGivenIP});
runNeeo();
}
else
if (!config.brainip || config.brainip == ''){
discoverBrain().then(() => {
runNeeo();
})
}
else {
runNeeo();
}
resolve();
})
}
function runNeeo () {
return new Promise(function (resolve, reject) {
if (!config.brainport) {config.brainport = settings.defaultPort}
const neeoSettings = {
brain: config.brainip.toString(),
port: config.brainport.toString(),
name: settings.runtimeName,
devices: driverTable
};
metaLog({type:LOG_TYPE.INFO, content:"Current directory: " + __dirname});
metaLog({type:LOG_TYPE.INFO, content:"Trying to start the meta."});
process.env.BRAINIP = neeoSettings.brain;
process.env.BRAINPORT = neeoSettings.port;
neeoapi.startServer(neeoSettings)
.then((result) => {
metaLog({type:LOG_TYPE.INFO, content:"Driver running, you can search it on the neeo app."});
if (brainDiscovered) {
fs.writeFile(__dirname + '/config.js', "{\"brainip\":\"" + neeoSettings.brain + "\", \"brainport\":\"" + neeoSettings.port + "\"}", err => {
if (err) {
metaLog({type:LOG_TYPE.ERROR, content:"Error writing the config file. " + err});
} else {
metaLog({type:LOG_TYPE.INFO, content:"Initial configuration saved"});
}
resolve();
})
}
})
.catch(err => {
metaLog({type:LOG_TYPE.ERROR, content:'Failed running Neeo with error: ' + err});
process.exit(1);
});
})
}
function enableMQTT (cont, deviceId) {
// Though we might not use MQTT to SEND data (if not necessary, why shoudl we), we WILL listen for commands that are send to us over MQTT
mqttClient.subscribe(settings.mqtt_topic + cont.name + "/#", () => {});
mqttClient.on('message', function (topic, value) {
try {
let theTopic = topic.split("/");
if (theTopic.length == 6 && theTopic[5] == "set") {
if (theTopic[3] == "button") {
cont.onButtonPressed(theTopic[4], theTopic[2]);
}
else if (theTopic[3] == "slider") {
let sliI = cont.sliderH.findIndex((sli)=>{return sli.name == theTopic[4]});
if (sliI>=0){
cont.sliderH[sliI].set(theTopic[2], value)
}
}
else if (theTopic[3] == "switch") {
let sliI = cont.switchH.findIndex((sli)=>{return sli.name == theTopic[4]});
if (sliI>=0){
cont.switchH[sliI].set(theTopic[2], value)
}
}
else if (theTopic[3] == "image") {
let imaI = cont.imageH.findIndex((ima)=>{return ima.name == theTopic[4]});
if (imaI>=0){
cont.imageH[imaI].set(theTopic[2], value)
}
}
else if (theTopic[3] == "label") {
let labI = cont.labelH.findIndex((lab)=>{return lab.name == theTopic[4]});
if (labI>=0){
cont.labelH[labI].set(theTopic[2], value)
}
}
}
}
catch (err) {
metaLog({type:LOG_TYPE.ERROR, content:'Parsing incomming message on: '+settings.mqtt_topic + cont.name + "/command"});
metaLog({type:LOG_TYPE.ERROR, content:err});
}
})
}
//MAIN
process.chdir(__dirname);
if (process.argv.length>2) {
try {
if (process.argv[2]) {
let arguments = JSON.parse(process.argv[2]);
if (arguments.Brain) {
brainConsoleGivenIP = arguments.Brain;
}
if (arguments.CompLevel)
{initialiseLogSeverity("QUIET");
try
{arguments.CompLevel.forEach(function(obj)
{ OverrideLoglevel(obj.LogSeverity,obj.Component)})
}
catch (err) {console.log("Error in arguments CompLevel",err)}
}
else
{
if (arguments.LogSeverity) {
initialiseLogSeverity(arguments.LogSeverity);
}
if (arguments.Components) {
initialiseLogComponents(arguments.Components);
}
}
}
else {
metaLog({type:LOG_TYPE.FATAL, content:'Wrong arguments: ' + process.argv[2] + (process.argv.length>3? ' ' + process.argv[3]: '') + ' You can try for example node meta \'{"Brain":"192.168.1.144","LogSeverity":"INFO","Components":["meta"]}\', Or example: node meta \'{"Brain":"localhost","LogSeverity":"VERBOSE","Components":["metaController", "variablesVault"]}\', all items are optionals, LogSeverity can be VERBOSE, INFO, WARNING or QUIET, components can be meta, metaController, variablesVault, processingManager, sensorHelper, sliderHeper, switchHelper, imageHelper or directoryHelper if you want to focus the logs on a specific function. If components is empty, all modules are shown.'});
process.exit();
}
}
catch (err)
{console.log("Catch error setting loglevel",err)
metaLog({type:LOG_TYPE.FATAL, content:'Wrong arguments: ' + process.argv[2] + (process.argv.length>3? ' ' + process.argv[3]: '') + ' You can try for example node meta \'{"Brain":"192.168.1.144","LogSeverity":"INFO","Components":["meta"]}\', Or example: node meta \'{"Brain":"localhost","LogSeverity":"VERBOSE","Components":["metaController", "variablesVault"]}\', all items are optionals, LogSeverity can be VERBOSE, INFO, WARNING or QUIET, components can be meta, metaController, variablesVault, processingManager, sensorHelper, sliderHeper, switchHelper, imageHelper or directoryHelper if you want to focus the logs on a specific function. If components is empty, all modules are shown.'});
metaLog({type:LOG_TYPE.FATAL, content:err});
process.exit();
}
}
else
initialiseLogSeverity("QUIET");
metaLog({type:LOG_TYPE.ALWAYS, content:'META Starting'});