forked from iobroker-community-adapters/ioBroker.ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.js
executable file
·863 lines (821 loc) · 24.7 KB
/
ring.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
/* eslint-disable no-prototype-builtins */
/* jshint -W097 */
/* jshint -W030 */
/* jshint strict:true */
/* jslint node: true */
/* jslint esversion: 6 */
'use strict';
const {RingRestClient} = require('ring-client-api/lib/api/rest-client');
const utils = require('@iobroker/adapter-core');
const objectHelper = require('@apollon/iobroker-tools').objectHelper; // Get common adapter utils
const ringapiclient = require(__dirname + '/lib/ringapiclient');
const datapoints = require(__dirname + '/lib/datapoints');
const semver = require('semver');
const path = require('path');
const fs = require('fs');
let isWin = process.platform.startsWith('win');
let ring = null;
let ringdevices = {};
let errorcountmax = 10;
let errorcounter = 0;
let states = {};
const adapterName = require('./package.json').name.split('.').pop();
const adapterNodeVer = require('./package.json').engines.node;
let adapter;
function startAdapter(options) {
options = options || {};
options.systemConfig = true;
options.name = adapterName;
adapter = new utils.Adapter(options);
/**
* is called when adapter shuts down - callback has to be called under any circumstances!
*/
adapter.on('unload', (callback) => {
try {
adapter.log.info('Closing Adapter');
callback();
} catch (e) {
callback();
}
});
/**
* Listen for sendTo messages
*/
adapter.on('message', (msg) => {
adapter.sendTo(msg.from, msg.command, 'Execute command ' + msg.command, msg.callback);
});
/**
* Listen for object Changes
*/
adapter.on('objectChange', (id, obj) => {
// adapter.log.info('objectChange for id ' + id);
});
/**
* Listen State chnages
*/
adapter.on('stateChange', (id, state) => {
objectHelper.handleStateChange(id, state);
});
/**
* is called when databases are connected and adapter received configuration.
* start here!
*/
adapter.on('ready', () => {
adapter.getForeignObject('system.config', (err, obj) => {
if (adapter.config.password) {
if (obj && obj.native && obj.native.secret) {
adapter.config.password = decrypt(obj.native.secret, adapter.config.password);
} else {
adapter.config.password = decrypt('Zgfr56gFe87jJOM', adapter.config.password);
}
}
// adapter.subscribeStates(adapter.namespace + '.*.Livestream.livestreamrequest');
adapter.subscribeStates('*');
objectHelper.init(adapter);
main();
});
});
return adapter;
}
/**
* Change the external Sentry Logging. After changing the Logging
* the adapter restarts once
* @param {*} id : adapter.config.sentry_enable for example
*/
async function setSentryLogging(value) {
try {
value = value === true;
let idSentry = 'system.adapter.' + adapter.namespace + '.plugins.sentry.enabled';
let stateSentry = await adapter.getForeignStateAsync(idSentry);
if (stateSentry && stateSentry.val !== value) {
await adapter.setForeignStateAsync(idSentry, value);
adapter.log.info('Restarting Adapter because of changeing Sentry settings');
adapter.restart();
return true;
}
} catch (error) {
return false;
}
return false;
}
/**
* Get two face auth refreshtoken
*/
async function refreshToken() {
if (!adapter.config.twofaceauth) return;
adapter.log.info('Setting two face authentication and delete email and password from configuration afterwards');
let restClient = new RingRestClient({
email: adapter.config.email,
password: adapter.config.password,
});
let auth = await restClient.getCurrentAuth();
let refreshtoken = auth.refresh_token;
if (!refreshtoken) {
return;
}
adapter.log.info('Two face authentication successfully set. Adapter is restarting!');
await adapter.extendForeignObjectAsync('system.adapter.' + adapter.namespace, {
native: {
twofaceauth: false,
email: '',
password: '',
refreshtoken: refreshtoken
}
});
}
/**
* Password decrypt
* @param {*} key
* @param {*} value
*/
function decrypt(key, value) {
let result = '';
for (let i = 0; i < value.length; ++i) {
result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
}
return result;
}
/**
*
* @param {*} key
* @param {*} value
*/
function encrypt(key, value) {
var result = '';
for (var i = 0; i < value.length; ++i) {
result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
}
return result;
}
/**
*
* @param {*} file
*/
function delFile(file) {
if (adapter.existsSync(adapter.namespace, file)) adapter.delFile(adapter.namespace, file);
}
/**
* Build error messages
* @param {*} error
*/
function printErrorMessage(error) {
if (!error) {
return;
}
if (!Array.isArray(error)) {
error = [error];
}
for (let i in error) {
adapter.log.info(error[i]);
}
}
/**
* restart adapter
*/
function restartAdapter() {
adapter.getForeignObject('system.adapter.' + adapter.namespace, (err, obj) => {
if (obj) adapter.setForeignObject('system.adapter.' + adapter.namespace, obj);
});
} // endFunctionRestartAdapter
/**
* set info channel
* @param {*} ring
* @param {*} id
*/
async function setInfo(ring, id) {
try {
let doorb = await ring.getAllRingsDevice(id);
let kind = ring.getKind(id);
let deviceId = kind + '_' + id;
let channelId = deviceId + '.Info';
// Create Deivce
objectHelper.setOrUpdateObject(deviceId, {
type: 'device',
common: {
name: 'Device ' + id
},
native: {}
}, ['name']);
// Create Channel
objectHelper.setOrUpdateObject(channelId, {
type: 'channel',
common: {
name: 'Info ' + id
},
native: {}
}, ['name']);
let info = datapoints.getObjectByName('info');
for (let i in info) {
let value = doorb[i];
let stateId = channelId + '.' + i;
let common = info[i];
let controlFunction;
if (!states.hasOwnProperty(stateId) || states[stateId] !== value) {
objectHelper.setOrUpdateObject(stateId, {
type: 'state',
common: common
}, ['name'], value, controlFunction);
}
states[stateId] = value;
}
objectHelper.processObjectQueue(() => {
});
} catch (error) {
throw new Error('Error setInfo(): ' + error);
}
}
/**
* health infos
* @param {*} ring
* @param {*} id
*/
async function setHealth(ring, id) {
try {
let health = await ring.getHealthSummarie(id); // health
let deviceId = ring.getKind(id) + '_' + id;
let channelId = deviceId + '.Info';
// Create Deivce
objectHelper.setOrUpdateObject(deviceId, {
type: 'device',
common: {
name: 'Device ' + id
},
native: {}
}, ['name']);
// Create Channel
objectHelper.setOrUpdateObject(channelId, {
type: 'channel',
common: {
name: 'Info ' + id
},
native: {}
}, ['name']);
let info = datapoints.getObjectByName('health');
for (let i in info) {
let value = health[i];
let stateId = channelId + '.' + i;
let common = info[i];
if (!states.hasOwnProperty(stateId) || states[stateId] !== value) {
objectHelper.setOrUpdateObject(stateId, {
type: 'state',
common: common
}, ['name'], value);
}
states[stateId] = value;
}
objectHelper.processObjectQueue(() => {
});
} catch (error) {
throw new Error('Error setHealth(): ' + error);
}
}
/**
* light infos
* @param {*} ring
* @param {*} id
*/
async function setLight(ring, id, deviceData, init) {
try {
if (!deviceData || deviceData.health === undefined && deviceData.health.floodlight_on == undefined)
return;
let deviceId = ring.getKind(id) + '_' + id;
let channelId = deviceId + '.Light';
// Create Deivce
objectHelper.setOrUpdateObject(deviceId, {
type: 'device',
common: {
name: 'Device ' + id
},
native: {}
}, ['name']);
// Create Channel
objectHelper.setOrUpdateObject(channelId, {
type: 'channel',
common: {
name: 'Light ' + id
},
native: {}
}, ['name']);
let info = datapoints.getObjectByName('light');
let value = deviceData.health.floodlight_on;
for (let i in info) {
let ownValue = value;
let controlFunction;
let stateId = channelId + '.' + i;
let common = info[i];
if (i === 'light_switch' && init) {
controlFunction = async (controlVal) => {
try {
await ring.setLight(id, controlVal);
} catch (error) {
adapter.log.error(error);
}
};
}
if (!states.hasOwnProperty(stateId) || states[stateId] !== value) {
objectHelper.setOrUpdateObject(stateId, {
type: 'state',
common: common
}, ['name'], value, controlFunction);
}
states[stateId] = value;
}
objectHelper.processObjectQueue(() => {
});
} catch (error) {
throw new Error('Error setLight(): ' + error);
}
}
async function processSnapshotInfo(info, channelId, snapshot, deviceId, vis, ring, id) {
for (let i in info) {
let controlFunction;
let value = null;
let stateId = channelId + '.' + i;
let common = info[i];
let type = 'state';
switch (i) {
case 'jpg':
if (snapshot) await adapter.setBinaryStateAsync(stateId, snapshot.image);
break;
case 'snapshot':
type = 'meta';
// http://<ip-iobroker>:<port-vis>/<ring-instanz>/<device>.snapshot/snapshot.jpg
// http://192.168.1.10:8082/ring.0/doorbell_4711.snapshot/snapshot.jpg
if (snapshot) {
// const path = `${adapter.config.path}/${deviceId}_${snapshot.filename}`;
const path = `${deviceId}/${snapshot.filename}`;
await adapter.writeFile(adapter.namespace, path, snapshot.image);
}
break;
case 'snapshot_url':
vis = await adapter.getForeignObjectAsync('system.adapter.web.0');
if (vis && vis.native) {
let secure = vis.native.secure ? 'https' : 'http';
if (snapshot) {
value = `${secure}://${adapter.host}:${vis.native.port}/${adapter.namespace}/${deviceId}/${snapshot.filename}`;
}
}
break;
case 'snapshot_file':
value = await updateCurrentFile(snapshot, stateId, deviceId, value, adapter.config.del_old_snapshot);
break;
case 'snapshotrequest':
controlFunction = async (value) => {
if (value == true) {
try {
await setSnapshot(ring, id);
} catch (error) {
adapter.log.error(error);
}
}
};
break;
default:
break;
}
if (!states.hasOwnProperty(stateId) || states[stateId] !== value) {
objectHelper.setOrUpdateObject(stateId, {
type: type,
common: common
}, ['name'], value, controlFunction);
}
states[stateId] = value;
}
}
/**
* make snapshot
* @param {*} ring
* @param {*} id
* @param {*} image
*/
async function setSnapshot(ring, id, init) {
try {
let kind = ring.getKind(id);
let deviceId = kind + '_' + id;
let channelId = deviceId;
let file = path.join(adapter.config.path, adapter.config.filename_snapshot);
let snapshot = init ? undefined : await ring.getSnapshot(id, file);
if (!init && !snapshot) return;
let info = datapoints.getObjectByName('snapshot');
let vis;
// Create Deivce
objectHelper.setOrUpdateObject(deviceId, {
type: 'device',
common: {
name: 'Device ' + id
},
native: {}
}, ['name']);
await processSnapshotInfo(info, channelId, snapshot, deviceId, vis, ring, id);
objectHelper.processObjectQueue(() => {
});
} catch (error) {
throw new Error('Error setSanpshot()): ' + error);
}
}
async function updateCurrentFile(file, stateId, deviceId, value, deleteOldSetting) {
if (!file) {
return value;
}
try {
let oldState = await adapter.getStateAsync(stateId);
if (oldState && oldState.val && deleteOldSetting) {
delFile(oldState.val);
await adapter.delFileAsync(adapter.namespace, `${deviceId}/${path.basename(oldState.val)}`);
}
} catch (error) {
//
}
value = path.join(file.pathname, file.filename);
return value;
}
/**
* make lviestream
* @param {*} ring
* @param {*} id
* @param {*} image
*/
async function setLivetream(ring, id, init) {
try {
let kind = ring.getKind(id);
let deviceId = kind + '_' + id;
let channelId = deviceId;
// if(!init) await ring.getLiveStreamSIP(id);
let file = path.join(adapter.config.path, adapter.config.filename_livestream);
let livestream = init ? undefined : await ring.getLiveStream(id, file);
if (!init && !livestream) return;
let info = datapoints.getObjectByName('livestream');
let vis;
// Create Deivce
objectHelper.setOrUpdateObject(deviceId, {
type: 'device',
common: {
name: 'Device ' + id
},
native: {}
}, ['name']);
for (let i in info) {
let controlFunction;
let value = null;
let stateId = channelId + '.' + i;
let common = info[i];
let type = 'state';
switch (i) {
case 'mp4':
if (livestream) await adapter.setBinaryStateAsync(stateId, livestream.video);
break;
case 'livestream':
type = 'meta';
// http://<ip-iobroker>:<port-vis>/<ring-instanz>/<device>.livestream/livestream.jpg
// http://192.168.1.10:8082/ring.0/doorbell_4711.livestream/livestream.jpg
if (livestream) {
const path = `${deviceId}/${livestream.filename}`;
adapter.log.debug(`Write Livestream to: ${path}`);
await adapter.writeFile(adapter.namespace, path, livestream.video);
}
break;
case 'livestream_url':
vis = await adapter.getForeignObjectAsync('system.adapter.web.0');
if (vis && vis.native && vis.native) {
let secure = vis.native.secure ? 'https' : 'http';
if (livestream) {
value = `${secure}://${adapter.host}:${vis.native.port}/${adapter.namespace}/${deviceId}/${livestream.filename}`;
}
}
break;
case 'livestream_file':
value = await updateCurrentFile(livestream, stateId, deviceId, value, adapter.config.del_old_livestream);
break;
case 'livestreamrequest':
controlFunction = async (value) => {
if (value == true) {
try {
await setLivetream(ring, id);
} catch (error) {
adapter.log.error(error);
}
}
};
break;
default:
break;
}
if (!states.hasOwnProperty(stateId) || states[stateId] !== value) {
objectHelper.setOrUpdateObject(stateId, {
type: type,
common: common
}, ['name'], value, controlFunction);
}
states[stateId] = value;
}
objectHelper.processObjectQueue(() => {
});
} catch (error) {
throw new Error('Error setLivetream()): ' + error);
}
}
/**
* Ring and Motions infos
* @param {*} ring
* @param {*} id
* @param {*} ding
*/
async function setDingDong(ring, id, ding) {
try {
let kind = ring.getKind(id);
let deviceId = kind + '_' + id;
let channelId = deviceId;
// Create Deivce
objectHelper.setOrUpdateObject(deviceId, {
type: 'device',
common: {
name: 'Device ' + id
},
native: {}
}, ['name']);
let info = datapoints.getObjectByName('dingdong');
for (let i in info) {
let controlFunction;
let value = null;
if (ding && ding.hasOwnProperty(i)) {
value = ding[i];
}
let stateId = channelId + '.' + i;
let common = info[i];
let type = 'state';
if (kind != 'cameras' && i == 'light') {
continue;
}
switch (i) {
case 'light':
controlFunction = async (value) => {
if (value == true) {
try {
await ring.setLight(ring, id, value);
} catch (error) {
adapter.log.error(error);
}
}
};
break;
case 'expires_in':
/*
controlFunction = function (value) {
if (value) {
clearTimeout(timerDingDong);
timerDingDong = setTimeout(() => {
(async () => {
await setDingDong(ring, id, ding, true);
})();
}, value * 1000);
}
};
*/
break;
default:
break;
}
if (!states.hasOwnProperty(stateId) || states[stateId] !== value) {
objectHelper.setOrUpdateObject(stateId, {
type: type,
common: common
}, ['name'], value, controlFunction);
}
}
objectHelper.processObjectQueue(() => {
});
} catch (error) {
throw new Error('Error setDingDong(): ' + error);
}
}
/**
* set History Infos. Only Motion and Dings will be shown
* @param {*} ring
* @param {*} id
*/
async function setHistory(ring, id) {
let history;
let videos;
try {
history = await ring.getHistory(id);
videos = await ring.getLastVideos(id);
let deviceId = ring.getKind(id) + '_' + id;
let channelId = deviceId + '.History';
// Create Deivce
objectHelper.setOrUpdateObject(deviceId, {
type: 'device',
common: {
name: 'Device ' + id
},
native: {}
}, ['name']);
// Create Channel
objectHelper.setOrUpdateObject(channelId, {
type: 'channel',
common: {
name: 'History'
},
native: {}
}, ['name']);
let info = datapoints.getObjectByName('history');
let counter = null;
for (let i in history) {
if (history[i].kind == 'motion' || history[i].kind == 'ding' || history[i].kind == 'ringing' || history[i].kind == 'on_demand') {
counter = i;
break;
}
}
for (let i in info) {
let value = null;
if (counter != null) {
switch (i) {
case 'history_url':
value = videos && videos[counter];
break;
case 'created_at':
value = history && history[counter][i].toString();
break;
default:
value = history && history[counter][i];
}
}
let stateId = channelId + '.' + i;
let common = info[i];
if (!states.hasOwnProperty(stateId) || states[stateId] !== value) {
objectHelper.setOrUpdateObject(stateId, {
type: 'state',
common: common
}, ['name'], value);
}
states[stateId] = value;
}
objectHelper.processObjectQueue(() => {
});
} catch (error) {
if (!history) {
throw new Error('Error setHistory(): ' + error);
}
}
}
async function startDevice(id, dbids, j) {
adapter.log.info('Starting Ring Device for Id ' + id);
executeImmediateGuarded(async () => {await setInfo(ring, id);}, this);
executeImmediateGuarded(async () => {await setHealth(ring, id);}, this);
executeImmediateGuarded(async () => {await setHistory(ring, id);}, this);
executeImmediateGuarded(async () => {await setDingDong(ring, id, true);}, this);
executeImmediateGuarded(async () => {await setSnapshot(ring, id, true);}, this);
executeImmediateGuarded(async () => {await setLivetream(ring, id, true);}, this);
executeImmediateGuarded(async () => {await setLight(ring, id, dbids[j], true);}, this);
// healthtimeout = await pollHealth(ring, id);
// On Event ding or motion do something
await ring.eventOnNewDing(id, async (ding) => {
adapter.log.info('Ding Dong for Id ' + id + ' (' + ding.kind + ', ' + ding.state + ')');
adapter.log.silly('Ding Dong for Id ' + id + JSON.stringify(ding));
executeImmediateGuarded(async () => {await setDingDong(ring, id, ding);}, this);
if (ding.kind != 'on_demand') {
executeImmediateGuarded(async () => {await setSnapshot(ring, id);}, this);
executeImmediateGuarded(async () => {await setLivetream(ring, id);}, this);
}
});
await ring.eventOnSnapshot(id, async (data) => {
});
await ring.eventOnLivestream(id, async (data) => {
});
await ring.eventOnRefreshTokenUpdated(id, async (data) => {
if (!(data && data.newRefreshToken != adapter.config.refreshtoken)) {
return;
}
adapter.log.info('Old refresh token : ' + data.oldRefreshToken);
adapter.log.info('New refresh token : ' + data.newRefreshToken);
adapter.log.info('Two face authentication successfully set. Adapter is restarting!');
let obj = await adapter.getForeignObjectAsync('system.config');
let secret = obj && obj.native && obj.native.secret ? obj.native.secret : 'Zgfr56gFe87jJOM';
await adapter.extendForeignObjectAsync('system.adapter.' + adapter.namespace, {
native: {
twofaceauth: false,
email: adapter.config.email,
password: encrypt(secret, adapter.config.password),
refreshtoken: data.newRefreshToken
}
});
});
ringdevices[id] = true; // add Device to Array
}
/**
* main function for ring
*/
async function ringer() {
let dbids;
try {
ring = ring || new ringapiclient.RingApiClient(adapter);
dbids = await ring.getAllRingsDevices();
errorcounter = 0;
} catch (error) {
// if, error we will get a new ring connection
errorcounter++;
ringdevices = {};
states = {};
ring = null; // we start from beginning
adapter.log.error(error);
if (errorcounter >= errorcountmax) {
adapter.log.error('To many connection errors, restarting adapter');
restartAdapter();
}
return;
}
try {
if (!ring || !dbids) {
return;
}
for (let j in dbids) {
let id = dbids[j].id;
// If device exist skipp function!
if (!id) {
continue;
}
if (!ringdevices[id]) {
await startDevice(id, dbids, j);
} else {
executeImmediateGuarded(async () => {await setInfo(ring, id);}, this);
executeImmediateGuarded(async () => {await setHealth(ring, id);}, this);
executeImmediateGuarded(async () => {await setHistory(ring, id);}, this);
executeImmediateGuarded(async() => { await setLight(ring, id, dbids[j], false);}, this);
let deviceId = ring.getKind(id) + '_' + id;
adapter.getObject(deviceId, (err, object) => {
if (err || !object) {
delete ringdevices[id];
}
});
}
}
} catch (error) {
throw new Error(error);
}
}
async function poll_ringer() {
// await doorbot.main(adapter);
let pollsec = adapter.config.pollsec;
if (errorcounter > 0) {
let wait = 60;
pollsec = adapter.config.pollsec > wait ? adapter.config.pollsec : wait;
}
try {
await ringer();
} catch (error) {
//
}
setTimeout(async () => {
await poll_ringer();
}, pollsec * 1000);
}
function executeImmediateGuarded(f, thisContext) {
setImmediate(async () => {
try {
await f.bind(thisContext)();
} catch (error) {
adapter.log.error(error);
}
});
}
/**
* Main
*/
async function main() {
isWin = process.platform.startsWith('win');
try {
adapter.log.info('Starting Adapter ' + adapter.namespace + ' in version ' + adapter.version);
const dataDir = (adapter.systemConfig) ? adapter.systemConfig.dataDir : '';
const snapshotDir = path.normalize(utils.controllerDir + '/' + dataDir + adapter.namespace.replace('.', '_'));
if (await setSentryLogging(adapter.config.sentry_enable)) return;
await refreshToken();
adapter.config.recordtime_livestream = adapter.config.recordtime_livestream || 0;
if (adapter.config.path === '') {
adapter.config.path = path.join(snapshotDir, 'snapshot'); // '/Users/thorsten.stueben/Downloads/public'
}
adapter.config.filename_snapshot = adapter.config.filename_snapshot || 'snapshot.jpg';
adapter.config.filename_livestream = adapter.config.filename_livestream || 'livestream.mp4';
if (!fs.existsSync(adapter.config.path)) {
fs.mkdirSync(adapter.config.path, {recursive: true});
if (!isWin) {
fs.chmodSync(adapter.config.path, 508);
}
}
if (!semver.satisfies(process.version, adapterNodeVer)) {
adapter.log.error(`Required node version ${adapterNodeVer} not satisfied with current version ${process.version}.`);
return;
}
await poll_ringer();
} catch (error) {
adapter.log.error('Could not start the Adapter ' + adapter.namespace + ' in version ' + adapter.version + ' because of ' + error);
adapter.terminate();
}
}
/**
* If started as allInOne mode => return function to create instance
*/
if (typeof module !== 'undefined' && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}