/
main.js
6089 lines (5698 loc) · 282 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
'use strict';
const utils = require('@iobroker/adapter-core');
class Residents extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'residents',
});
this.initialized = false;
this.language = '';
this.residents = [];
this.roomies = [];
this.pets = [];
this.guests = [];
this.presenceFollowingMapping = {};
this.subscriptions = [];
this.foreignSubscriptions = [];
this.presenceSubscriptionMapping = {};
this.wayhomeSubscriptionMapping = {};
this.states = [];
this.parentInstances = [];
this.absentTimeout = null;
this.overnightTimeout = null;
this.calculationTimeout = null;
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Adapter instance startup
* @returns void
*/
async onReady() {
this.roomies = this.config.roomie != undefined ? this.config.roomie : [];
this.pets = this.config.pet != undefined ? this.config.pet : [];
this.guests = this.config.guest != undefined ? this.config.guest : [];
this.residents = this.roomies;
this.residents = this.residents.concat(this.pets);
this.residents = this.residents.concat(this.guests);
const systemConfig = await this.getForeignObjectAsync('system.config');
this.language = systemConfig && systemConfig.common.language ? systemConfig.common.language : 'en';
if (this.config.language != '') this.language = this.config.language;
///////////////////////////
// Update adapter instance configuration
const adapterObj = await this.getForeignObjectAsync('system.adapter.residents');
const instanceObj = await this.getForeignObjectAsync('system.adapter.' + this.namespace);
if (adapterObj != undefined && instanceObj != undefined) {
let updatedInstanceObj = Boolean(false);
['stateTranslations'].forEach((property) => {
if (instanceObj.native[property] == undefined) {
instanceObj.native[property] = adapterObj.native[property];
updatedInstanceObj = true;
} else {
for (const element in adapterObj.native[property]) {
if (instanceObj.native[property][element] == undefined) {
instanceObj.native[property].push(adapterObj.native[property][element]);
updatedInstanceObj = true;
}
}
}
});
if (updatedInstanceObj == true) {
await this.setForeignObjectAsync('system.adapter.' + this.namespace, instanceObj);
this.log.info('Updated adapter instance configuration after adapter update');
}
}
///////////////////////////
// Create/Update global objects
const residentialStateTexts = {
en: {
0: 'Extended Absence',
1: 'Away',
2: 'Pet Care',
3: 'Way Home',
4: 'Home',
5: 'Do Not Disturb',
6: 'Wind Down',
7: 'Bedtime',
8: 'Got Up',
9: 'Night Walk',
10: 'Wake Up',
11: 'Night',
},
de: {
0: 'Längere Abwesenheit',
1: 'Abwesend',
2: 'Haustierpflege',
3: 'Nachhauseweg',
4: 'zu Hause',
5: 'Nicht stören',
6: 'Entspannen',
7: 'Schlafenszeit',
8: 'Aufgestanden',
9: 'Nachtwanderung',
10: 'Aufwecken',
11: 'Nacht',
},
ru: {
0: 'Расширенное отсутствие',
1: 'Вдали',
2: 'Уход за домашними животными',
3: 'Путь домой',
4: 'дома',
5: 'Не беспокоить',
6: 'Расслабьтесь',
7: 'Время сна',
8: 'Запущен и работает',
9: 'Ночной поход',
10: 'Проснись',
11: 'Ночь',
},
pt: {
0: 'Ausência estendida',
1: 'A caminho',
2: 'Pet Care',
3: 'Caminho',
4: 'Em casa',
5: 'Não Perturbar',
6: 'Relaxe',
7: 'Hora de dormir',
8: 'Em funcionamento',
9: 'Caminhada nocturna',
10: 'Acorda',
11: 'Noite',
},
nl: {
0: 'Verlengde Absence',
1: 'Weg',
2: 'Huisdier thuis',
3: 'Naar huis',
4: 'Thuis',
5: 'Niet Storen',
6: 'Relax',
7: 'Bedtijd',
8: 'Op',
9: 'Nachtwandeling',
10: 'Wakker worden',
11: 'Nacht',
},
fr: {
0: 'Absence prolongée',
1: 'Absent',
2: 'Soins pour animaux',
3: 'Chemin de retour',
4: 'Chez soi',
5: 'Ne pas déranger',
6: 'Détendre',
7: 'Heure du coucher',
8: 'Levé',
9: 'Marche de nuit',
10: 'Réveil',
11: 'Nuit',
},
it: {
0: 'Assenza estesa',
1: 'Via',
2: 'Cura degli animali',
3: 'Via di casa',
4: 'A casa',
5: 'Non disturbare',
6: 'Rilassarsi',
7: 'Ora di dormire',
8: 'Alzati',
9: 'Passeggiata notturna',
10: 'Svegliarsi',
11: 'Notte',
},
es: {
0: 'Ausencia ampliada',
1: 'Fuera de casa',
2: 'Cuidado de mascotas',
3: 'Camino a casa',
4: 'En casa',
5: 'No molestar',
6: 'Relax',
7: 'Hora de dormir',
8: 'Levantó',
9: 'Paseo nocturno',
10: 'Despierta',
11: 'Noche',
},
pl: {
0: 'Długość nieobecności',
1: 'Away',
2: 'Pet Care',
3: 'Strona domowa',
4: 'W domu',
5: 'Nie przeszkadzać',
6: 'Relaks',
7: 'Dobranoc',
8: 'W górę',
9: 'Nocny spacer',
10: 'Obudź się',
11: 'Noc',
},
uk: {
0: 'Розширена абсенція',
1: 'Проживання',
2: 'Сімейний догляд за домашніми тваринами',
3: 'Головна',
4: 'вдома',
5: 'Не турбувати',
6: 'розслабитися',
7: 'Час спати',
8: 'Встала',
9: 'Нічна прогулянка',
10: 'Прокинься.',
11: 'Ніч',
},
'zh-cn': {
0: '缺点',
1: 'A. 公路',
2: '家庭护理',
3: 'B. 家庭办法',
4: '在家',
5: '请勿打扰',
6: '缩减',
7: '就寝时间',
8: '起床了',
9: '夜行',
10: '唤醒',
11: '夜间',
},
};
const residentialLang = residentialStateTexts[this.language]
? residentialStateTexts[this.language]
: residentialStateTexts['en'];
const residentialStates = {
0: '',
};
for (const i in residentialLang) {
residentialLang[i] = {
text:
this.config.residentialStates != undefined &&
this.config.residentialStates[i].text != '' &&
this.config.residentialStates[i].text != residentialStateTexts['en'][i]
? this.config.residentialStates[i].text
: residentialLang[i],
};
residentialLang[i]['state'] = residentialLang[i].text;
if (this.config.residentialStates != undefined && this.config.residentialStates[i].icon != '') {
residentialLang[i]['icon'] = this.config.residentialStates[i].icon;
residentialLang[i]['state'] = this.config.residentialStates[i].icon + ' ' + residentialLang[i].text;
}
residentialStates[i] = residentialLang[i]['state'];
}
// Update common.states
let currentObject = await this.getObjectAsync('state');
if (currentObject) {
currentObject.common.states = residentialStates;
await this.setObjectAsync('state', currentObject);
}
const moodStateTexts = {
en: {
0: "Couldn't Get Worse",
1: 'Extraordinary Bad',
2: 'Extremely Bad',
3: 'Pretty Bad',
4: 'Somewhat Not Good',
5: 'Balanced',
6: 'Somewhat Okay',
7: 'Pretty Good',
8: 'Extremely Good',
9: 'Extraordinary Good',
10: "Couldn't Be Better",
},
de: {
0: 'Könnte nicht schlimmer werden',
1: 'Außerordentlich schlecht',
2: 'Äußerst schlecht',
3: 'Ziemlich schlecht',
4: 'Nicht so gut',
5: 'Ausgeglichen',
6: 'Einigermaßen okay',
7: 'Ziemlich gut',
8: 'Sehr gut',
9: 'Außerordentlich gut',
10: 'Könnte nicht besser sein',
},
ru: {
0: 'Не могли бы получить Worse',
1: 'Необычный Плохо',
2: 'Чрезвычайно плохо',
3: 'Очень плохо',
4: 'Что-то не хорошо',
5: 'сбалансированный',
6: 'Немного хорошо',
7: 'Довольно хорошо',
8: 'Чрезвычайно хорошо',
9: 'Необычный Хорошо',
10: 'Не может быть лучше',
},
pt: {
0: 'Não consegui ficar pior',
1: 'Mau Extraordinário',
2: 'Extremamente mau',
3: 'Muito mau',
4: 'Não é bom',
5: 'Equilibrado',
6: 'Alguma coisa bem',
7: 'Muito bem',
8: 'Extremamente bom',
9: 'Bem Extraordinário',
10: 'Não podia ser melhor',
},
nl: {
0: 'Kon Worse niet krijgen',
1: 'Buitengewoon slecht',
2: 'Extreem slecht',
3: 'Best',
4: 'Enigszins',
5: 'Gebalanceerd',
6: 'Enigszins oké',
7: 'Mooi',
8: 'Extreem goed',
9: 'Buitengewoon goed',
10: 'Kon niet beter',
},
fr: {
0: 'Ça ne pourrait pas être pire',
1: 'Extraordinairement mauvais',
2: 'Extrêmement mauvais',
3: 'Pas mal',
4: "C'est pas bon",
5: 'Équilibré',
6: 'Assez bien',
7: 'Plutôt bien',
8: 'Très bien',
9: 'Bien extraordinaire',
10: 'Ça ne pourrait pas être mieux',
},
it: {
0: 'Non potrebbe essere peggio',
1: 'Scarsa straordinaria',
2: 'Estremamente cattivo',
3: 'Abbastanza',
4: 'Qualcosa che non va"',
5: 'Equilibrato',
6: "Un po' ok",
7: 'Bello',
8: 'Estremamente buono',
9: 'Buono straordinario',
10: 'Non potrebbe essere meglio',
},
es: {
0: 'No podría ponerse peor',
1: 'Extraordinario malo',
2: 'Muy malo',
3: 'Bastante mal',
4: 'Algo que no es bueno',
5: 'Equilibrado',
6: 'Algo bien',
7: 'Muy bien',
8: 'Muy bueno',
9: 'Bien extraordinario',
10: 'No podría ser mejor',
},
pl: {
0: 'Nie mogło być gorzej',
1: 'Nadzwyczajny',
2: 'Badacze',
3: 'Całkiem źle',
4: 'Niedobrze',
5: 'Zrównoważony',
6: 'Trochę w porządku',
7: 'Całkiem dobrze',
8: 'Dobro',
9: 'Dobry nadzwyczajny',
10: 'Nie mogło być lepiej',
},
uk: {
0: 'Чи не побоюватися',
1: 'Поганий',
2: 'Надзвичайно Поганий',
3: 'Гарненька Поганий',
4: 'Що не добре',
5: 'збалансований',
6: 'Дещо нормально',
7: 'Гарненька Добре',
8: 'Надзвичайно Добре',
9: 'Надзвичайне добро',
10: 'Не можна краще',
},
'zh-cn': {
0: '没有比这更糟糕的了',
1: '特设包',
2: '极力包',
3: '序言',
4: '某些人',
5: '平衡',
6: '有点好',
7: '善意',
8: '极好',
9: '特 法',
10: '再好不过了',
},
};
const moodLang = moodStateTexts[this.language] ? moodStateTexts[this.language] : moodStateTexts['en'];
const moodStates = {
0: '',
};
for (const key in moodLang) {
moodLang[key] = {
text:
this.config.moodStates != undefined &&
this.config.moodStates[key].text != '' &&
this.config.moodStates[key].text != moodStateTexts['en'][key]
? this.config.moodStates[key].text
: moodLang[key],
};
moodLang[key]['state'] = moodLang[key].text;
if (this.config.moodStates != undefined && this.config.moodStates[key].icon != '') {
moodLang[key]['icon'] = this.config.moodStates[key].icon;
moodLang[key]['state'] = this.config.moodStates[key].icon + ' ' + moodLang[key].text;
}
moodStates[key] = moodLang[key]['state'];
}
// Update common.states
currentObject = await this.getObjectAsync('mood');
if (currentObject) {
currentObject.common.states = moodStates;
await this.setObjectAsync('mood', currentObject);
}
///////////////////////////
// Create/Update resident objects
const residentTypeName = {
roomie: {
en: 'Roommate Devices',
de: 'Mitbewohner Geräte',
ru: 'Устройства для соседей по комнате',
pt: 'Dispositivos para companheiros de quarto',
nl: 'Apparaten voor huisgenoten',
fr: 'Dispositifs de colocation',
it: 'Dispositivi per i coinquilini',
es: 'Dispositivos para compañeros de piso',
pl: 'Urządzenia dla współlokatorów',
uk: 'Сусідні пристрої',
'zh-cn': '室友设备',
},
pet: {
en: 'Pet Devices',
de: 'Haustier Geräte',
ru: 'Устройства для домашних животных',
pt: 'Dispositivos para animais',
nl: 'Apparaten voor huisdieren',
fr: 'Dispositifs pour animaux de compagnie',
it: 'Dispositivi per animali domestici',
es: 'Dispositivos para mascotas',
pl: 'Urządzenia dla zwierząt domowych',
uk: 'Пристрої для домашніх тварин',
'zh-cn': '',
},
guest: {
en: 'Guest Devices',
de: 'Gast Geräte',
ru: 'Гостевые устройства',
pt: 'Dispositivos Convidados',
nl: 'Gastapparaten',
fr: 'Appareils invités',
it: 'Dispositivi per gli ospiti',
es: 'Dispositivos para invitados',
pl: 'Urządzenia gościnne',
uk: 'Гостьові пристрої',
'zh-cn': '访客设备',
},
};
const activityStateTexts = {
en: {
// 000-0999: Not present at home / Away
0: 'Extended Absence',
1: 'On the Road for Today',
2: 'Way Home',
// 100-899: Not present at home / Away: Custom Focus states (e.g. to sync with Apple Focus modes)
100: 'Personal',
101: 'Work',
102: 'Mindfulness',
103: 'Fitness',
104: 'Reading',
105: 'Gaming',
106: 'Driving',
107: 'Shopping',
// 1000: WAKING TIME at home ///////////////////////////////////////////////////////////////////////
1000: 'Home',
// 1100-1899: WAKING TIME at home: Custom Focus states (e.g. to sync with Apple Focus modes)
1100: 'Personal',
1101: 'Work',
1102: 'Mindfulness',
1103: 'Fitness',
1104: 'Reading',
1105: 'Gaming',
1106: 'Driving',
1107: 'Shopping',
// 1900-1999: WAKING TIME at home: Transitioning to Sleeping Time
1900: 'Preparing Bedtime',
1901: 'Getting to Bed',
1902: 'In Bed',
// 2000-2999: SLEEPING TIME at home ////////////////////////////////////////////////////////////////
2000: 'Sleeping',
// 2000-2099: SLEEPING TIME at home: While I should be sleeping
2010: 'Awake during Night Time',
2020: 'Asleep again',
// 2100-2199: SLEEPING TIME at home: While I should get up
2100: 'Wake-up Alarm',
2101: '💤 Alarm Snooze',
2102: '💤 Alarm Snooze',
2103: '💤💤 Alarm Snooze',
2104: '💤💤 Alarm Snooze',
2105: '💤💤💤 Alarm Snooze',
// 2200-2299: SLEEPING TIME at home: Transitioning to Waking Time
2200: 'Awakening after Wake-up Alarm',
2210: 'Awakening',
},
de: {
// 000-0999: Not present at home / Away
0: 'Längere Abwesenheit',
1: 'Unterwegs für heute',
2: 'Nachhauseweg',
// 100-899: Not present at home / Away: Custom Focus states (e.g. to sync with Apple Focus modes)
100: 'Zeit für mich',
101: 'Arbeiten',
102: 'Achtsamkeit',
103: 'Fitness',
104: 'Lesen',
105: 'Spielen',
106: 'Fahren',
107: 'Shopping',
// 1000: WAKING TIME at home ///////////////////////////////////////////////////////////////////////
1000: 'zu Hause',
// 1100-1899: WAKING TIME at home: Custom Focus states (e.g. to sync with Apple Focus modes)
1100: 'Zeit für mich',
1101: 'Arbeiten',
1102: 'Achtsamkeit',
1103: 'Fitness',
1104: 'Lesen',
1105: 'Spielen',
1106: 'Fahren',
1107: 'Shopping',
// 1900-1999: WAKING TIME at home: Transitioning to Sleeping Time
1900: 'Auf Schlaf einstellen',
1901: 'Bettfertig machen',
1902: 'Im Bett',
// 2000-2999: SLEEPING TIME at home ////////////////////////////////////////////////////////////////
2000: 'Schlafen',
// 2000-2099: SLEEPING TIME at home: While I should be sleeping
2010: 'Wach während der Nacht',
2020: 'Wieder eingeschlafen',
// 2100-2199: SLEEPING TIME at home: While I should get up
2100: 'Weckalarm',
2101: '💤 Schlummern',
2102: '💤 Schlummern',
2103: '💤💤 Schlummern',
2104: '💤💤 Schlummern',
2105: '💤💤💤 Schlummern',
// 2200-2299: SLEEPING TIME at home: Transitioning to Waking Time
2200: 'Aufwachen nach Weckruf',
2210: 'Aufwachen',
},
};
const activityLang = activityStateTexts[this.language]
? activityStateTexts[this.language]
: activityStateTexts.en;
const activityStates = {
0: '',
};
// add Focus Modes
if (
this.config.focusStates != undefined &&
typeof this.config.focusStates == 'object' &&
this.config.focusStates.length > 0
) {
for (const key in this.config.focusStates) {
const awayFocusKey = Number(key) + 100;
const homeFocusKey = Number(key) + 100 + 1000;
if (
this.config.focusStates[key].enabled != undefined &&
this.config.focusStates[key].enabled == false
) {
delete activityLang[awayFocusKey];
delete activityLang[homeFocusKey];
continue;
}
if (
this.config.focusStates[key].text != '' &&
this.config.focusStates[key].text != activityStateTexts['en'][awayFocusKey]
) {
activityLang[awayFocusKey] = this.config.focusStates[key].text;
activityLang[homeFocusKey] = this.config.focusStates[key].text;
}
}
} else {
this.log.error('Configuration error: config.focusStates has invalid format');
}
// add Custom Focus Modes
if (this.config.customFocusStates != undefined && this.config.customFocusStates.length > 0) {
for (const key in this.config.customFocusStates) {
// Limit custom focus modes to maximum of 100
if (Number(key) > 99) {
this.log.error('Reached maximum limit of 100 Custom Focus Modes.');
break;
}
if (
// @ts-ignore
(this.config.customFocusStates[key].enabled != undefined &&
// @ts-ignore
this.config.customFocusStates[key].enabled == false) ||
// @ts-ignore
this.config.customFocusStates[key].text == undefined ||
// @ts-ignore
this.config.customFocusStates[key].text == ''
)
continue;
const awayFocusKey = Number(key) + 200;
const homeFocusKey = Number(key) + 200 + 1000;
// @ts-ignore
activityLang[awayFocusKey] = this.config.customFocusStates[key].text;
// @ts-ignore
activityLang[homeFocusKey] = this.config.customFocusStates[key].text;
}
}
const offStateTexts = {
en: 'Off',
de: 'Aus',
ru: 'С сайта',
pt: 'Desligado',
nl: 'Uit',
fr: 'Désactivé',
it: 'Spento',
es: 'Apagado',
pl: 'Wył.',
uk: 'Вимкнено',
'zh-cn': '关掉了',
};
let offLang = offStateTexts[this.language] ? offStateTexts[this.language] : offStateTexts.en;
if (this.config.stateTranslations != undefined && this.config.stateTranslations[0] != undefined) {
if (
this.config.stateTranslations[0].text != '' &&
this.config.stateTranslations[0].text != offStateTexts.en
)
offLang = this.config.stateTranslations[0].text;
if (this.config.stateTranslations[0].icon != '')
offLang = this.config.stateTranslations[0].icon + ' ' + offLang;
}
const nobodyStateTexts = {
en: 'Nobody',
de: 'Niemand',
};
let nobodyLang = nobodyStateTexts[this.language] ? nobodyStateTexts[this.language] : nobodyStateTexts.en;
if (this.config.stateTranslations != undefined && this.config.stateTranslations[1] != undefined) {
if (
this.config.stateTranslations[1].text != '' &&
this.config.stateTranslations[1].text != nobodyStateTexts.en
)
nobodyLang = this.config.stateTranslations[1].text;
if (this.config.stateTranslations[1].icon != '')
nobodyLang = this.config.stateTranslations[1].icon + ' ' + nobodyLang;
}
const focusStateTexts = {
en: 'Focus',
de: 'Fokus',
ru: 'Фокус',
pt: 'Foco',
nl: 'Focus',
fr: 'Focus',
it: 'Focus',
es: 'Focus',
pl: 'Focus',
uk: 'Фокус',
'zh-cn': '焦点',
};
const focusLang = focusStateTexts[this.language] ? focusStateTexts[this.language] : focusStateTexts.en;
const focusStates = {
away: {
0: offLang,
},
home: {
0: offLang,
},
};
for (const key in activityLang) {
let customActivityState;
if (this.config.activityStates != undefined)
customActivityState = this.config.activityStates.filter((obj) => {
return obj.id == Number(key);
})[0];
activityLang[key] = {
text:
customActivityState != undefined &&
customActivityState.text != '' &&
customActivityState.text != activityStateTexts['en'][key]
? customActivityState.text
: activityLang[key],
};
const regexp = /^([^:]*):\s*(.+)$/;
const match = activityLang[key].text.match(regexp);
// Extract custom prefix from text
if (match) {
if (match[1].trim() != '') activityLang[key].prefix = match[1].trim();
activityLang[key].text = match[2].trim();
}
// Add prefix from residential state
else {
// Away
if (Number(key) >= 0 && Number(key) < 1000) {
if (activityLang[key].text != residentialLang[1].text)
activityLang[key].prefix = residentialLang[1].text;
}
// Focus modes
else if (Number(key) >= 1100 && Number(key) < 1300) {
activityLang[key].prefix = focusLang;
}
// Wind Down
else if (Number(key) == 1900) {
if (activityLang[key].text != residentialLang[6].text)
activityLang[key].prefix = residentialLang[6].text;
}
// Bedtime
else if (Number(key) == 1901) {
if (activityLang[key].text != residentialLang[7].text)
activityLang[key].prefix = residentialLang[7].text;
}
// In Bed
else if (Number(key) == 1902) {
if (activityLang[key].text != residentialLang[11].text)
activityLang[key].prefix = residentialLang[11].text;
}
// Home
else if (Number(key) >= 1000 && Number(key) < 2000) {
if (activityLang[key].text != residentialLang[4].text)
activityLang[key].prefix = residentialLang[4].text;
}
// Wake-up
else if (Number(key) >= 2101 && Number(key) < 2200) {
if (activityLang[key].text != residentialLang[10].text)
activityLang[key].prefix = residentialLang[10].text;
}
// Awoken
else if (Number(key) >= 2200 && Number(key) < 2300) {
if (activityLang[key].text != residentialLang[8].text)
activityLang[key].prefix = residentialLang[8].text;
}
// Night
else if (Number(key) >= 2000) {
if (activityLang[key].text != residentialLang[11].text)
activityLang[key].prefix = residentialLang[11].text;
}
}
// Add custom activity icons
if (customActivityState != undefined && customActivityState.icon != '') {
activityLang[key].icon = customActivityState.icon;
}
// Add icons from residential state
else {
let focusIndex = Number(key) - 100;
if (focusIndex >= 1000) focusIndex -= 1000;
let customFocusIndex = Number(key) - 200;
if (customFocusIndex >= 1000) customFocusIndex -= 1000;
// Away
if (Number(key) == 0) {
activityLang[key].icon = residentialLang[0].icon;
} else if (Number(key) == 2) {
activityLang[key].icon = residentialLang[3].icon;
} else if (Number(key) >= 1 && Number(key) < 100) {
activityLang[key].icon = residentialLang[1].icon;
}
// Focus modes
else if ((Number(key) >= 100 && Number(key) < 200) || (Number(key) >= 1100 && Number(key) < 1200)) {
if (this.config.focusStates[focusIndex].icon != '')
activityLang[key].icon = this.config.focusStates[focusIndex].icon;
}
// Custom Focus modes
else if ((Number(key) >= 200 && Number(key) < 300) || (Number(key) >= 1200 && Number(key) < 1300)) {
// @ts-ignore
if (this.config.customFocusStates[customFocusIndex].icon != '')
// @ts-ignore
activityLang[key].icon = this.config.customFocusStates[customFocusIndex].icon;
}
// Wind Down
else if (Number(key) == 1900) {
activityLang[key].icon = residentialLang[6].icon;
}
// Bedtime
else if (Number(key) >= 1901 && Number(key) < 2000) {
activityLang[key].icon = residentialLang[7].icon;
}
// Home
else if (Number(key) >= 1000 && Number(key) < 2000) {
activityLang[key].icon = residentialLang[4].icon;
}
// Awake at night
else if (Number(key) == 2010) {
activityLang[key].icon = residentialLang[9].icon;
}
// Wake-up
else if (Number(key) >= 2100 && Number(key) < 2200) {
activityLang[key].icon = residentialLang[10].icon;
}
// Awoken
else if (Number(key) >= 2200 && Number(key) < 2300) {
activityLang[key].icon = residentialLang[8].icon;
}
// Night
else if (Number(key) >= 2000) {
activityLang[key].icon = residentialLang[11].icon;
}
}
activityLang[key].state =
(activityLang[key].icon ? activityLang[key].icon + ' ' : '') +
(activityLang[key].prefix ? activityLang[key].prefix + ': ' : '') +
activityLang[key].text;
activityStates[key] = activityLang[key].state;
// Consider no active focus as Off and
// map as 0 to comply with boolean standards
if (Number(key) == 1000) {
focusStates['away'][0] = offLang;
focusStates['home'][0] = offLang;
}
// Only numbers between 100-299 or 1100-1299 for activity.focus
else if ((Number(key) >= 100 && Number(key) < 300) || (Number(key) >= 1100 && Number(key) < 1300)) {
const stateVal = (activityLang[key].icon ? activityLang[key].icon + ' ' : '') + activityLang[key].text;
let focusIndex = Number(key) - 100;
if (focusIndex >= 1000) focusIndex -= 1000;
let customFocusIndex = Number(key) - 200;
if (customFocusIndex >= 1000) customFocusIndex -= 1000;
// Check away usage for Focus Modes
if (Number(key) >= 100 && Number(key) < 200 && this.config.focusStates[focusIndex].away == true) {
focusStates['away'][key] = stateVal;
}
// Check home usage for Focus Modes
else if (
Number(key) >= 1100 &&
Number(key) < 1200 &&
this.config.focusStates[focusIndex].home == true
) {
focusStates['home'][key] = stateVal;
}
// Check away usage Custom Focus Modes
else if (
Number(key) >= 200 &&
Number(key) < 300 &&
// @ts-ignore
this.config.customFocusStates[customFocusIndex].away == true
) {
focusStates['away'][key] = stateVal;
}
// Check home usage Custom Focus Modes
else if (
Number(key) >= 1200 &&
Number(key) < 1300 &&
// @ts-ignore
this.config.customFocusStates[customFocusIndex].home == true
) {
focusStates['home'][key] = stateVal;
}
}
// DND variants for activity.state
if (Number(key) < 1000 || Number(key) >= 2000) continue;
const dndKey = Number(key) + 10000;
activityStates[dndKey] =
(residentialLang[5].icon ? residentialLang[5].icon + ' ' : '') +
residentialLang[5].text +
(dndKey != 11000
? (activityLang[key].icon ? ': ' + activityLang[key].icon : ':') +
(activityLang[key].prefix ? ' ' + activityLang[key].prefix : '') +
(activityLang[key].prefix
? ' | ' + activityLang[key].text
: (activityLang[key].icon ? ' ' : '') + activityLang[key].text)
: '');
}
// TODO: also add roomies from other instances
const roomieIDsToNames = {};
this.roomies.forEach((roomie) => {
const name = roomie['name'].trim();
const roomieId = 'roomie.' + this.cleanNamespace(roomie['id'] ? roomie['id'] : name);
let icon = null;
if (
this.config.stateTranslations != undefined &&
this.config.stateTranslations[2] != undefined &&
this.config.stateTranslations[2].icon != ''
)
icon = this.config.stateTranslations[2].icon;
if (roomie.icon != undefined && roomie.icon != '') icon = roomie.icon;
roomieIDsToNames[this.namespace + '.' + roomieId] = icon ? icon + ' ' + name : name;
});
// TODO: also add guests from other instances
const guestIDsToNames = {};
this.guests.forEach((guest) => {
const name = guest['name'].trim();
const guestId = 'guest.' + this.cleanNamespace(guest['id'] ? guest['id'] : name);
let icon = null;
if (
this.config.stateTranslations != undefined &&
this.config.stateTranslations[2] != undefined &&
this.config.stateTranslations[2].icon != ''
)
icon = this.config.stateTranslations[2].icon;
if (guest.icon != undefined && guest.icon != '') icon = guest.icon;
guestIDsToNames[this.namespace + '.' + guestId] = icon ? icon + ' ' + name : name;
});
const residentTypes = ['roomie', 'pet', 'guest'];
for (const key1 in residentTypes) {
const residentType = residentTypes[key1];
if (this.config[residentType] == undefined) continue;
for (const key2 in this.config[residentType]) {
await this.setObjectNotExistsAsync(residentType, {
type: 'folder',
common: {
name: residentTypeName[residentType],
icon: residentType + '.svg',
},
native: {},
});
const resident = this.config[residentType][key2];
const name = resident['name'].trim();
const id = residentType + '.' + this.cleanNamespace(resident['id'] ? resident['id'] : name);
const fullId = this.namespace + '.' + id;
this.config[residentType][key2]['id'] = id;
let icon = null;
if (this.config.stateTranslations != undefined) {