-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
1712 lines (1501 loc) · 54.8 KB
/
main.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
/**
*
* nuki adapter
*
*
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
// you have to require the utils module and call adapter function
var utils = require('@iobroker/adapter-core'); // Get common adapter utils
var express = require('express'); // call express
var bodyParser = require("body-parser");
var request = require('request');
var crypto = require('crypto');
var nacl = require('tweetnacl'); // cryptographic functions
var buffer = require("buffer");
// nacl.utils = require('tweetnacl-util'); // encoding & decoding
// var nsecret = require('tweetnacl.secret');
// const _sodium = require('libsodium-wrappers');
// REST server
var app = express();
var timer = null;
var ipInfo = require('ip');
var hostIp = ipInfo.address();
// Global variables
var bridgeId = null;
var bridgeType = 0;
var bridgeHwId = null;
var bridgeFwVer = null;
var bridgeWifiFwVer = null;
var bridgeAppVer = null;
var bridgeIp = null;
var bridgePort = '0';
var bridgeToken = null;
var forcePlainToken = null;
var bridgeName = null;
var interval = null;
var hostCb = null;
var cbSet = false;
var callbackId = null;
var hostPort = null;
var timeOut = 3000;
var actionTimeOut, sleepTimeOut;
var semver = require('semver');
// you have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.template.0
//var adapter = new utils.Adapter('nuki');
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {name: 'nuki'});
adapter = new utils.Adapter(options);
//adapter.log.debug('Adapter generated');
// adapter.useFormatDate = true; // load from ;system.config the global date format
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', function (obj) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
console.log('send command');
// Send response in callback if required
if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
});
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function () {
main();
});
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function (callback) {
try {
if (actionTimeOut) clearTimeout(actionTimeOut);
if (sleepTimeOut) clearTimeout(sleepTimeOut);
if (timer) clearInterval(timer);
if (cbSet) {
hostCb = false;
removeCallback(callbackId);
}
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});
// is called if a subscribed object changes
adapter.on('objectChange', function (id, obj) {
// Warning, obj can be null if it was deleted
adapter.log.debug(`objectChange ${id} ${JSON.stringify(obj)}`);
});
// is called if a subscribed state changes
adapter.on('stateChange', function (id, state) {
let path = id.split('.',5);
let nukiId = path[2];
let actionState = path[4];
// Warning, state can be null if it was deleted
adapter.log.debug(`stateChange ${id} ${JSON.stringify(state)}`);
// you can use the ack flag to detect if it is status (true) or command (false)
if (state && !state.ack) {
if (actionState == 'action') {
setLockAction(nukiId, state.val);
} else {
if (state.val == false) {
if (actionState == 'lockAction') {
setLockAction(nukiId, '2');
} else if (actionState == 'rtoAction') {
setLockAction(nukiId, '2');
}
} else {
switch (actionState) {
case 'lockAction':
// fall through
case 'rtoAction':
setLockAction(nukiId, '1');
break;
case 'openAction':
setLockAction(nukiId, '3');
break;
case 'unlockLocknGoAction':
// fall through
case 'cmActiveAction':
setLockAction(nukiId, '4');
break;
case 'openLocknGoAction':
// fall through
case 'cmDeactiveAction':
setLockAction(nukiId, '5');
break;
default:
adapter.log.warn(`unrecognized actionState (${actionState})`);
break;
}
}
}
}
});
return adapter;
};
function initBridgeStates(_name, _token) {
let timeStamp = new Date().toISOString().substr(0,19) + '+00:00';
adapter.setObjectNotExists(`${bridgeId}`, {
type: 'device',
common: {
name: _name
},
native: {}
});
adapter.setObjectNotExists(`${bridgeId}.info`, {
type: 'channel',
common: {
name: 'Info'
},
native: {}
});
adapter.setObjectNotExists(`${bridgeId}.info.bridgeIp`, {
type: 'state',
common: {
name: 'IP-Adresse',
type: 'string',
write: false,
role: 'info.ip',
def: bridgeIp
},
native: {}
});
adapter.setObjectNotExists(`${bridgeId}.info.bridgePort`, {
type: 'state',
common: {
name: 'Port',
type: 'string',
write: false,
role: 'info.port',
def: bridgePort
},
native: {}
});
adapter.setObjectNotExists(`${bridgeId}.info.bridgeToken`, {
type: 'state',
common: {
name: 'Token',
type: 'string',
write: false,
role: 'text',
def: _token
},
native: {}
});
adapter.setObjectNotExists(`${bridgeId}.info.bridgeType`, {
type: 'state',
common: {
name: 'Typ',
type: 'number',
write: false,
states: {
1: 'Hardware Bridge',
2: 'Software Bridge',
},
role: 'value',
def: bridgeType
},
native: {}
});
if (bridgeType == 1) {
adapter.setObjectNotExists(`${bridgeId}.info.hardwareId`, {
type: 'state',
common: {
name: 'HardwareID',
type: 'string',
write: false,
role: 'text',
def: bridgeHwId
},
native: {}
});
adapter.setObjectNotExists(`${bridgeId}.info.firmwareVersion`, {
type: 'state',
common: {
name: 'Firmware',
type: 'string',
write: false,
role: 'text'
},
native: {}
});
adapter.setObjectNotExists(`${bridgeId}.info.wifiFirmwareVersion`, {
type: 'state',
common: {
name: 'WiFi Firmware',
type: 'string',
write: false,
role: 'text'
},
native: {}
});
} else {
adapter.setObjectNotExists(`${bridgeId}.info.appVersion`, {
type: 'state',
common: {
name: 'App Version',
type: 'string',
write: false,
role: 'text'
},
native: {}
});
}
adapter.setObjectNotExists(`${bridgeId}.info.timestamp`, {
type: 'state',
common: {
name: 'Zuletzt aktualisiert',
type: 'string',
write: false,
role: 'date'
},
native: {}
});
setBridgeState(timeStamp);
}
function initNukiDeviceStates(_obj) {
let nukiState = _obj.lastKnownState;
let deviceType = 1;
let firmwareVersion = '';
// device
adapter.setObjectNotExists(`${_obj.nukiId}`, {
type: 'device',
common: {
name: _obj.name
},
native: {}
});
// device info
adapter.setObjectNotExists(`${_obj.nukiId}.info`, {
type: 'channel',
common: {
name: 'Information'
},
native: {}
});
// device states
adapter.setObjectNotExists(`${_obj.nukiId}.states`, {
type: 'channel',
common: {
name: 'Status'
},
native: {}
});
if (_obj.hasOwnProperty('deviceType')) {
deviceType = _obj.deviceType;
} else {
deviceType = get_devicetype_by_statename(nukiState.stateName)
}
switch(deviceType) {
case 0:
initNukiLockStates(_obj.nukiId);
break;
case 2:
initNukiOpenerStates(_obj.nukiId);
break;
case 3:
initNukiLockStates(_obj.nukiId);
break;
case 4:
initNukiLockStates(_obj.nukiId);
break;
default:
adapter.log.error(`Unknown device type (${deviceType}). Setting minimal states.`);
deviceType = 1;
break;
}
adapter.setObjectNotExists(`${_obj.nukiId}.info.deviceType`, {
type: 'state',
common: {
name: 'Typ',
type: 'number',
write: false,
states: {
0: 'Nuki Smart Lock 1.0/2.0',
1: 'unknown device',
2: 'Nuki Opener',
3: 'Nuki Smart Door',
4: 'Nuki Smart Lock 3.0 (Pro)',
},
def: deviceType,
role: 'value'
},
native: {}
});
if (_obj.hasOwnProperty('firmwareVersion')) {
firmwareVersion = _obj.firmwareVersion
adapter.setObjectNotExists(`${_obj.nukiId}.info.firmwareVersion`, {
type: 'state',
common: {
name: 'Firmware',
type: 'string',
write: false,
role: 'text',
def: firmwareVersion
},
native: {}
});
}
if (nukiState.hasOwnProperty('ringactionState')) {
adapter.setObjectNotExists(`${_obj.nukiId}.states.ringactionState`, {
type: 'state',
common: {
name: 'Klingel betätigt',
type: 'boolean',
write: false,
role: 'indicator'
},
native: {}
});
// listen to changes
adapter.subscribeStates(`${_obj.nukiId}.states.ringactionState`);
}
if (nukiState.hasOwnProperty('ringactionTimestamp')) {
adapter.setObjectNotExists(`${_obj.nukiId}.states.ringactionTimestamp`, {
type: 'state',
common: {
name: 'Letzte Klingelbetätigung',
type: 'string',
write: false,
role: 'date'
},
native: {}
});
}
adapter.setObjectNotExists(`${_obj.nukiId}.info.batteryCritical`, {
type: 'state',
common: {
name: 'Batterie schwach',
type: 'boolean',
write: false,
role: 'indicator.lowbat'
},
native: {}
});
// listen to changes
adapter.subscribeStates(`${_obj.nukiId}.info.batteryCritical`);
if (nukiState.hasOwnProperty('batteryCharging')) {
adapter.setObjectNotExists(`${_obj.nukiId}.info.batteryCharging`, {
type: 'state',
common: {
name: 'Batterie lädt',
type: 'boolean',
write: false,
role: 'indicator.maintenance.lowbat'
},
native: {}
});
// listen to changes
adapter.subscribeStates(`${_obj.nukiId}.info.batteryCharging`);
}
if (nukiState.hasOwnProperty('batteryChargeState')) {
adapter.setObjectNotExists(`${_obj.nukiId}.info.batteryChargeState`, {
type: 'state',
common: {
name: 'Ladezustand der Batterie',
type: 'number',
write: false,
role: 'value.battery'
},
native: {}
});
// listen to changes
adapter.subscribeStates(`${_obj.nukiId}.info.batteryChargeState`);
}
if (nukiState.hasOwnProperty('keypadBatteryCritical')) {
adapter.setObjectNotExists(`${_obj.nukiId}.info.keypadBatteryCritical`, {
type: 'state',
common: {
name: 'KeyPad-Batterie schwach',
type: 'boolean',
write: false,
role: 'indicator.lowbat'
},
native: {}
});
// listen to changes
adapter.subscribeStates(`${_obj.nukiId}.info.keypadBatteryCritical`);
}
adapter.setObjectNotExists(`${_obj.nukiId}.states.timestamp`, {
type: 'state',
common: {
name: 'Zuletzt aktualisiert',
type: 'string',
write: false,
role: 'date'
},
native: {}
});
// set states
setLockState(_obj.nukiId, deviceType, nukiState, firmwareVersion);
}
function initNukiLockStates(_nukiId) {
let doorsensorState = 4;
adapter.setObjectNotExists(`${_nukiId}.info.mode`, {
type: 'state',
common: {
name: 'Modus',
type: 'number',
write: false,
states: {
2: 'door mode',
3: '-',
},
role: 'value'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.states.lockState`, {
type: 'state',
common: {
name: 'Nuki aufgeschlossen',
type: 'boolean',
write: false,
role: 'sensor.lock'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.states.state`, {
type: 'state',
common: {
name: 'Status',
type: 'number',
write: false,
states: {
0: 'uncalibrated',
1: 'locked',
2: 'unlocking',
3: 'unlocked',
4: 'locking',
5: 'unlatched',
6: 'unlocked (lock n go)',
7: 'unlatching',
253: '-',
254: 'motor blocked',
255: 'undefined',
},
role: 'value'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.states.doorState`, {
type: 'state',
common: {
name: 'Türsensor',
type: 'number',
write: false,
states: {
1: 'deactivated',
2: 'door closed',
3: 'door opened',
4: 'door state unknown',
5: 'calibrating',
},
role: 'value',
def: doorsensorState
},
native: {}
});
// device actions
adapter.setObjectNotExists(`${_nukiId}.actions`, {
type: 'channel',
common: {
name: 'Aktionen'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.action`, {
type: 'state',
common: {
name: 'Aktion',
type: 'number',
states: {
0: '',
1: 'unlock',
2: 'lock',
3: 'unlatch',
4: 'lock‘n’go',
5: 'lock‘n’go with unlatch',
},
role: 'value'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.lockAction`, {
type: 'state',
common: {
name: 'Tür auf-/abschließen',
type: 'boolean',
write: true,
role: 'switch.lock.door'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.openAction`, {
type: 'state',
common: {
name: 'Tür öffnen',
type: 'boolean',
write: true,
read: false,
role: 'button.open.door'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.unlockLocknGoAction`, {
type: 'state',
common: {
name: 'Tür aufschließen (lock‘n’go)',
type: 'boolean',
write: true,
read: false,
role: 'button.open.door'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.openLocknGoAction`, {
type: 'state',
common: {
name: 'Tür öffnen (lock‘n’go)',
type: 'boolean',
write: true,
read: false,
role: 'button.open.door'
},
native: {}
});
// listen to changes
adapter.subscribeStates(`${_nukiId}.actions.*Action`);
adapter.subscribeStates(`${_nukiId}.actions.action`);
}
function initNukiOpenerStates(_nukiId) {
adapter.setObjectNotExists(`${_nukiId}.info.mode`, {
type: 'state',
common: {
name: 'Modus',
type: 'number',
write: false,
states: {
2: 'door mode',
3: 'continuous mode',
},
role: 'value'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.states.lockState`, {
type: 'state',
common: {
name: 'Ring to Open aktiv',
type: 'boolean',
write: false,
role: 'indicator'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.states.state`, {
type: 'state',
common: {
name: 'Status',
type: 'number',
write: false,
states: {
0: 'untrained',
1: 'online',
2: '-',
3: 'rto active',
4: '-',
5: 'open',
6: '-',
7: 'opening',
253: 'boot run',
254: '-',
255: 'undefined',
},
role: 'value'
},
native: {}
});
// device actions
adapter.setObjectNotExists(`${_nukiId}.actions`, {
type: 'channel',
common: {
name: 'Aktionen'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.action`, {
type: 'state',
common: {
name: 'Aktion',
type: 'number',
states: {
0: '',
1: 'activate rto',
2: 'deactivate rto',
3: 'electric strike actuation',
4: 'activate continuous mode',
5: 'deactivate continuous mode',
},
role: 'value'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.rtoAction`, {
type: 'state',
common: {
name: 'Ring to Open de-/aktivieren',
type: 'boolean',
write: true,
role: 'switch.lock.door'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.openAction`, {
type: 'state',
common: {
name: 'öffnen',
type: 'boolean',
write: true,
read: false,
role: 'button.open.door'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.cmActiveAction`, {
type: 'state',
common: {
name: 'Dauermodus einschalten',
type: 'boolean',
write: true,
read: false,
role: 'button.open.door'
},
native: {}
});
adapter.setObjectNotExists(`${_nukiId}.actions.cmDeactiveAction`, {
type: 'state',
common: {
name: 'Dauermodus ausschalten',
type: 'boolean',
write: true,
read: false,
role: 'button.open.door'
},
native: {}
});
// listen to changes
adapter.subscribeStates(`${_nukiId}.actions.*Action`);
adapter.subscribeStates(`${_nukiId}.actions.action`);
}
function setBridgeState(_timestamp) {
if (bridgeType == 1) {
// set firmware version
adapter.setState(`${bridgeId}.info.firmwareVersion`, {val: bridgeFwVer, ack: true});
// set WiFi firmware version
adapter.setState(`${bridgeId}.info.wifiFirmwareVersion`, {val: bridgeWifiFwVer, ack: true});
} else if (bridgeType == 2) {
// set app version
adapter.setState(`${bridgeId}.info.appVersion`, {val: bridgeAppVer, ack: true});
}
// set timestamp
adapter.setState(`${bridgeId}.info.timestamp`, {val: _timestamp, ack: true});
}
function setLockState(_nukiId, _deviceType, _nukiState, _firmWare) {
let timeStamp = null;
if (_nukiState == null) {
// no state set
return;
}
// set device type
adapter.setState(`${_nukiId}.info.deviceType`, {val: _deviceType, ack: true});
// set battery status
adapter.setState(`${_nukiId}.info.batteryCritical`, {val: _nukiState.batteryCritical, ack: true});
if (_nukiState.hasOwnProperty('batteryCharging')) {
if (_nukiState.batteryCharging != null) {
// set battery charge status
adapter.setState(`${_nukiId}.info.batteryCharging`, {val: _nukiState.batteryCharging, ack: true});
}
}
if (_nukiState.hasOwnProperty('batteryChargeState')) {
if (_nukiState.batteryChargeState != null) {
// set battery charge level
adapter.setState(`${_nukiId}.info.batteryChargeState`, {val: _nukiState.batteryChargeState, ack: true});
}
}
if (_nukiState.hasOwnProperty('keypadBatteryCritical')) {
if (_nukiState.keypadBatteryCritical != null) {
// set keypad battery status
adapter.setState(`${_nukiId}.info.keypadBatteryCritical`, {val: _nukiState.keypadBatteryCritical, ack: true});
}
}
// set timestamp
if (_nukiState.hasOwnProperty('timestamp')) {
timeStamp = _nukiState.timestamp;
} else {
timeStamp = new Date().toISOString().substr(0,19) + '+00:00';
}
adapter.setState(`${_nukiId}.states.timestamp`, {val: timeStamp, ack: true});
// lock or opener?
if (_deviceType == 0 || _deviceType == 3 || _deviceType == 4) {
// set lock action and state
switch(_nukiState.state) {
case 1:
// fall through
case 4:
adapter.setState(`${_nukiId}.states.lockState`, {val: false, ack: true});
adapter.setState(`${_nukiId}.actions.lockAction`, {val: false, ack: true});
break;
case 2:
// fall through
case 3:
// fall through
case 5:
// fall through
case 6:
// fall through
case 7:
adapter.setState(`${_nukiId}.states.lockState`, {val: true, ack: true});
adapter.setState(`${_nukiId}.actions.lockAction`, {val: true, ack: true});
break;
default:
adapter.setState(`${_nukiId}.states.lockState`, {val: true, ack: true});
adapter.setState(`${_nukiId}.actions.lockAction`, {val: true, ack: true});
break;
}
} else if (_deviceType == 2) {
// set opener action and state
switch(_nukiState.state) {
case 1:
// fall through
case 4:
adapter.setState(`${_nukiId}.states.lockState`, {val: false, ack: true});
adapter.setState(`${_nukiId}.actions.rtoAction`, {val: false, ack: true});
break;
case 2:
// fall through
case 3:
// fall through
case 5:
// fall through
case 6:
// fall through
case 7:
adapter.setState(`${_nukiId}.states.lockState`, {val: true, ack: true});
adapter.setState(`${_nukiId}.actions.rtoAction`, {val: true, ack: true});
break;
default:
adapter.setState(`${_nukiId}.states.lockState`, {val: true, ack: true});
adapter.setState(`${_nukiId}.actions.rtoAction`, {val: true, ack: true});
break;
}
} else {
// unknown device
return;
}
// reset action state after delay
actionTimeOut = setTimeout(function() {
adapter.setState(`${_nukiId}.actions.action`, {val: 0, ack: true});
}, timeOut);
// set mode
let mode = 0;
mode = _nukiState.mode;
adapter.setState(`${_nukiId}.info.mode`, {val: mode, ack: true});
// set status
let state = 0;
state = _nukiState.state;
adapter.setState(`${_nukiId}.states.state`, {val: state, ack: true});
if (_nukiState.hasOwnProperty('ringactionState') && _nukiState.ringactionState != null) {
// set doorsensor status
adapter.setState(`${_nukiId}.states.ringactionState`, {val: _nukiState.ringactionState, ack: true});
}
if (_nukiState.hasOwnProperty('ringactionTimestamp') && _nukiState.ringactionTimestamp != '') {
// set doorsensor status
adapter.setState(`${_nukiId}.states.ringactionTimestamp`, {val: _nukiState.ringactionTimestamp, ack: true});
}
if (_nukiState.hasOwnProperty('doorsensorState')) {
// set doorsensor status
let doorState = 0;
doorState = _nukiState.doorsensorState;
adapter.setState(`${_nukiId}.states.doorState`, {val: doorState, ack: true});
}
if (_firmWare != null && _firmWare != '') {
// set firmware version
adapter.setState(`${_nukiId}.info.firmwareVersion`, {val: _firmWare, ack: true});
}
}
function updateAllLockStates(_content, _init) {
let obj = null;
let deviceType = 0;
let nukilock = 0;
if (_content == null) {
adapter.log.error('no content');
return;
}
for (nukilock in _content) {
obj = _content[nukilock];
if (obj) {
if (_init) {
adapter.log.debug(`found Nuki device: ${obj.nukiId}`);
initNukiDeviceStates(obj);
} else {
adapter.log.debug(`updating Nuki device: ${obj.nukiId}`);
if (obj.hasOwnProperty('deviceType')) {
deviceType = obj.deviceType;
} else {
deviceType = 0;
}
if (obj.hasOwnProperty('firmwareVersion')) {
setLockState(obj.nukiId, deviceType, obj.lastKnownState, obj.firmwareVersion);
} else {
setLockState(obj.nukiId, deviceType, obj.lastKnownState);
}
}
}
}
}
function getLockState(_nukiId, _forced) {
if (_forced) {
// retrieve states directly from the device
adapter.getState(`${_nukiId}.info.deviceType`, function (err, state) {
let deviceType = 0;
let lockStateUrl = null;
let timeStamp = '';
deviceType = state.val;
if (err) {
adapter.log.error(err);
return;
}
if (!deviceType || deviceType == 1) {
lockStateUrl = `http://${bridgeIp}:${bridgePort}/lockState?nukiId=${_nukiId}&${get_token()}`;
} else {
lockStateUrl = `http://${bridgeIp}:${bridgePort}/lockState?nukiId=${_nukiId}&deviceType=${deviceType}&${get_token()}`;
}
request(
{
url: lockStateUrl,
json: true
},
function (error, response, content) {
let doorsensorState = 4;
let doorsensorStateName = 'door state unknown';
let keypadBatteryCritical = null;
let ringactionState = null;
let ringactionTimestamp = '';
adapter.log.debug(`state requested: ${lockStateUrl}`);
if (error) {
adapter.log.error(error);
return;
}
if (response.statusCode != 200) {
switch (response.statusCode) {
case 401:
adapter.log.error('Given token is invalid.');