-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.js
952 lines (812 loc) · 49 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
'use strict';
const utils = require('@iobroker/adapter-core');
const SourceIcal = require('./lib/source/ical');
const SourceApiJumomind = require('./lib/source/api-jumomind');
const SourceApiAbfallIo = require('./lib/source/api-abfallio');
const SourceApiAwido = require('./lib/source/api-awido');
class Trashschedule extends utils.Adapter {
constructor(options) {
super({
...options,
name: 'trashschedule',
useFormatDate: true,
});
this.sources = {};
/**
* @type {import('./lib/source/base') | null}
*/
this.source = null;
this.refreshEverythingTimeout = null;
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
await this.setStateAsync('info.connection', { val: false, ack: true });
const trashTypesConfig = this.getTrashTypes();
const typesAll = [];
const typesKeep = [];
try {
const typeChannels = await this.getChannelsOfAsync('type');
// Collect all existing types
if (typeChannels) {
for (let i = 0; i < typeChannels.length; i++) {
const idNoNamespace = this.removeNamespace(typeChannels[i]._id);
// Check if the state is a direct child (e.g. type.YourTrashType)
if (idNoNamespace.split('.').length === 2) {
this.log.debug(`[onReady] found existing trash type with ID "${idNoNamespace}"`);
typesAll.push(idNoNamespace);
}
}
}
} catch (err) {
this.log.warn(err);
}
// Create states and channels
if (trashTypesConfig.length > 0) {
for (const trashType of trashTypesConfig) {
const trashName = trashType.name;
const trashNameClean = trashType.nameClean;
if (trashNameClean && !!trashType.match) {
typesKeep.push(`type.${trashNameClean}`);
this.log.debug(`[onReady] found configured trash type: "${trashName}" with ID "type.${trashNameClean}"`);
if (trashType.match != trashType.match.trim()) {
this.log.info(
`attention: trash type "${trashName}" contains leading or trailing whitespaces in the match pattern - this could lead to an unexpected behavior! -> "${trashType.match}"`,
);
}
await this.setObjectNotExistsAsync(`type.${trashNameClean}`, {
type: 'channel',
common: {
name: trashName,
icon: 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyBmb2N1c2FibGU9ImZhbHNlIiB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgoJPHBhdGggZmlsbD0icmdiYSgwLCAwLCAwLCAwLjU0KSIgZD0iTTYgMTljMCAxLjEuOSAyIDIgMmg4YzEuMSAwIDItLjkgMi0yVjdINnYxMnpNMTkgNGgtMy41bC0xLTFoLTVsLTEgMUg1djJoMTRWNHoiPjwvcGF0aD4KPC9zdmc+',
},
native: {},
});
if (trashType.color) {
await this.extendObjectAsync(`type.${trashNameClean}`, {
common: {
color: `${trashType.color}FF`.toUpperCase(),
},
});
}
await this.setObjectNotExistsAsync(`type.${trashNameClean}.completed`, {
type: 'state',
common: {
name: {
en: 'Completed',
de: 'Erledigt',
ru: 'Завершено',
pt: 'Completada',
nl: 'Gecompliceerd',
fr: 'Complété',
it: 'Completato',
es: 'Completado',
pl: 'Completed',
uk: 'Виконаний',
'zh-cn': '完成',
},
type: 'boolean',
role: 'switch.enable',
read: true,
write: true,
def: false,
},
native: {},
});
// Abfall-Handling: 'actionNeeded' zeigt an, ob Tonne zur Abholung bereit gestellt werden muss
await this.setObjectNotExistsAsync(`type.${trashNameClean}.actionNeeded`, {
type: 'state',
common: {
name: {
en: 'Action needed',
de: 'Tätigwerden erforderlich',
},
type: 'boolean',
role: 'switch.enable',
read: true,
write: false,
def: false,
},
native: {},
});
await this.setObjectNotExistsAsync(`type.${trashNameClean}.nextDate`, {
type: 'state',
common: {
name: {
en: 'Next pickup - date (' + trashName + ')',
de: 'Nächste Abholung - Datum (' + trashName + ')',
ru: 'Следующий пикап - дата (' + trashName + ')',
pt: 'Próxima coleta - data (' + trashName + ')',
nl: 'Volgende afhaling - datum (' + trashName + ')',
fr: 'Prochaine collecte - date (' + trashName + ')',
it: 'Prossimo ritiro - data (' + trashName + ')',
es: 'Próxima recogida: fecha (' + trashName + ')',
pl: 'Następny odbiór — data (' + trashName + ')',
uk: 'Наступний пікап - дата (' + trashName + ')',
'zh-cn': '下次取件 - 日期 (' + trashName + ')',
},
type: 'number',
role: 'date',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync(`type.${trashNameClean}.nextDateFormat`, {
type: 'state',
common: {
name: {
en: 'Next pickup - date formatted (' + trashName + ')',
de: 'Nächste Abholung - Datum formatiert (' + trashName + ')',
ru: 'Следующее получение - дата в формате (' + trashName + ')',
pt: 'Próxima coleta - data formatada (' + trashName + ')',
nl: 'Volgende afhaling - datum geformatteerd (' + trashName + ')',
fr: 'Prochaine collecte - date formatée (' + trashName + ')',
it: 'Prossimo ritiro - data formattata (' + trashName + ')',
es: 'Próxima recogida: fecha formateada (' + trashName + ')',
pl: 'Następny odbiór — sformatowana data (' + trashName + ')',
uk: 'Наступний пікап - дата форматування (' + trashName + ')',
'zh-cn': '下次取件 - 日期格式化 (' + trashName + ')',
},
type: 'string',
role: 'text',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync(`type.${trashNameClean}.nextDescription`, {
type: 'state',
common: {
name: {
en: 'Next pickup - description (' + trashName + ')',
de: 'Nächste Abholung - Beschreibung (' + trashName + ')',
ru: 'Следующий пикап - описание (' + trashName + ')',
pt: 'Próxima coleta - descrição (' + trashName + ')',
nl: 'Volgende afhaling - beschrijving (' + trashName + ')',
fr: 'Prochain ramassage - description (' + trashName + ')',
it: 'Prossimo ritiro - descrizione (' + trashName + ')',
es: 'Próxima recogida - descripción (' + trashName + ')',
pl: 'Następny odbiór — opis (' + trashName + ')',
uk: 'Наступний пікап - опис (' + trashName + ')',
'zh-cn': '下次取件 - 描述 (' + trashName + ')',
},
type: 'string',
role: 'text',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync(`type.${trashNameClean}.nextWeekday`, {
type: 'state',
common: {
name: {
en: 'Next pickup - weekday (' + trashName + ')',
de: 'Nächste Abholung - Wochentag (' + trashName + ')',
ru: 'Следующий пикап - будний день (' + trashName + ')',
pt: 'Próxima coleta - dia da semana (' + trashName + ')',
nl: 'Volgende afhaling - weekdag (' + trashName + ')',
fr: 'Prochain ramassage - jour de la semaine (' + trashName + ')',
it: 'Prossimo ritiro - giorno della settimana (' + trashName + ')',
es: 'Próxima recogida: día de la semana (' + trashName + ')',
pl: 'Następny odbiór — dzień powszedni (' + trashName + ')',
uk: 'Наступний пікап - тиждень (' + trashName + ')',
'zh-cn': '下一个取件 - 工作日 (' + trashName + ')',
},
type: 'number',
role: 'value',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync(`type.${trashNameClean}.daysLeft`, {
type: 'state',
common: {
name: {
en: 'Next pickup - days left (' + trashName + ')',
de: 'Nächste Abholung - verbleibende Tage (' + trashName + ')',
ru: 'Следующий самовывоз - осталось дней (' + trashName + ')',
pt: 'Próxima coleta - faltam dias (' + trashName + ')',
nl: 'Volgende afhaling - resterende dagen (' + trashName + ')',
fr: 'Prochain ramassage - jours restants (' + trashName + ')',
it: 'Prossimo ritiro - giorni rimasti (' + trashName + ')',
es: 'Próxima recogida: quedan días (' + trashName + ')',
pl: 'Następny odbiór — pozostały dni (' + trashName + ')',
uk: 'Наступний пікап - дні зліва (' + trashName + ')',
'zh-cn': '下次取件 - 剩余天数 (' + trashName + ')',
},
type: 'number',
role: 'value',
unit: 'days',
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync(`type.${trashNameClean}.nextDateFound`, {
type: 'state',
common: {
name: {
en: 'Next pickup - date found (' + trashName + ')',
de: 'Nächste Abholung - Termin gefunden (' + trashName + ')',
ru: 'Следующий пикап - дата нахождения (' + trashName + ')',
pt: 'Próxima coleta - data encontrada (' + trashName + ')',
nl: 'Volgende afhaling - datum gevonden (' + trashName + ')',
fr: 'Prochain ramassage - date trouvée (' + trashName + ')',
it: 'Prossimo ritiro - data trovata (' + trashName + ')',
es: 'Próxima recogida: fecha encontrada (' + trashName + ')',
pl: 'Następny odbiór — znaleziono datę (' + trashName + ')',
uk: 'Наступний пікап - дата знайдено (' + trashName + ')',
'zh-cn': '下次取件 - 找到日期 (' + trashName + ')',
},
type: 'boolean',
role: 'indicator',
def: false,
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync(`type.${trashNameClean}.color`, {
type: 'state',
common: {
name: {
en: 'Next pickup - color (' + trashName + ')',
de: 'Nächste Abholung - Farbe (' + trashName + ')',
ru: 'Следующий пикап - цвет (' + trashName + ')',
pt: 'Próxima coleta - cor (' + trashName + ')',
nl: 'Volgende afhaling - kleur (' + trashName + ')',
fr: 'Prochain ramassage - couleur (' + trashName + ')',
it: 'Prossimo ritiro - colore (' + trashName + ')',
es: 'Siguiente recogida - color (' + trashName + ')',
pl: 'Następny odbiór — kolor (' + trashName + ')',
uk: 'Наступний пікап - колір (' + trashName + ')',
'zh-cn': '下一个拾音器 - 颜色 (' + trashName + ')',
},
type: 'string',
role: 'level.color.rgb',
read: true,
write: false,
},
native: {},
});
} else {
this.log.warn(`[onReady] skipping invalid/empty trash name or match: ${trashName}`);
}
}
} else {
this.log.warn('[onReady] no trash types configured');
}
// Delete non existent trash types
for (let i = 0; i < typesAll.length; i++) {
const id = typesAll[i];
if (typesKeep.indexOf(id) === -1) {
this.log.debug(`[onReady] deleting existing but unconfigured trash type with ID "${id}"`);
await this.delObjectAsync(id, { recursive: true });
}
}
// Subscribe for changes
await this.subscribeStatesAsync('*');
this.sources = {
ical: new SourceIcal(this),
'api-jumomind': new SourceApiJumomind(this),
'api-abfallio': new SourceApiAbfallIo(this),
'api-awido': new SourceApiAwido(this),
};
// Set active source
const configSource = this.config.source;
if (this.sources[configSource]) {
this.source = this.sources[configSource];
}
if (this.source !== null) {
this.log.info(`[onReady] starting with source "${configSource}" -> ${this.source.getType()}`);
await this.setStateChangedAsync('source', { val: this.source.getType(), ack: true });
const isValid = await this.source.validate();
if (isValid) {
this.refreshEverything(); // start data refresh
} else {
this.log.info(`[onReady] instance configuration is invalid or incomplete - please configure a valid source`);
}
} else {
this.log.error('[onReady] source is not defined');
typeof this.terminate === 'function' ? this.terminate(utils.EXIT_CODES.INVALID_ADAPTER_CONFIG) : process.exit(utils.EXIT_CODES.INVALID_ADAPTER_CONFIG);
return;
}
}
async refreshEverything() {
if (this.source) {
await this.source.removeOldCacheFiles();
const data = await this.source.getPickupDates();
if (data && data.length) {
this.updateAll(data);
} else {
this.log.error('[refreshEverything] no pickup dates found - check configuration and restart instance');
}
}
// Clear existing timeout
if (this.refreshEverythingTimeout) {
this.log.debug('clearing old refresh timeout');
this.clearTimeout(this.refreshEverythingTimeout);
}
// Next Timeout
const nexTimeoutMilli = this.getMillisecondsToNextFullHour();
this.setStateAsync('type.nextRefresh', { val: new Date().getTime() + nexTimeoutMilli, ack: true });
this.log.debug(`re-creating refresh timeout in ${nexTimeoutMilli}ms (in ${this.convertMillisecondsToDuration(nexTimeoutMilli)})`);
this.refreshEverythingTimeout = this.setTimeout(() => {
this.log.debug('started automatic refresh (every full hour)');
this.refreshEverythingTimeout = null;
this.refreshEverything();
}, nexTimeoutMilli);
}
/**
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (id && state) {
const idNoNamespace = this.removeNamespace(id);
if (id == `${this.config.ical}.data.table`) {
this.refreshEverything();
} else if (idNoNamespace == 'type.resetCompleted' && state.val && !state.ack) {
this.log.info(`Setting "completed" flag for all types to false (RESET_ALL)`);
const trashTypesConfig = this.getTrashTypes();
for (const trashType of trashTypesConfig) {
const trashNameClean = trashType.nameClean;
this.log.debug(`Setting "completed" flag for type.${trashNameClean}.completed to false (RESET_ALL)`);
await this.setStateAsync(`type.${trashNameClean}.completed`, { val: false, ack: true, c: 'RESET_ALL' });
}
this.refreshEverything();
} else if (idNoNamespace == 'type.completedAll' && state.val && !state.ack) {
this.log.info(`Setting "completed" flag for all types to true (COMPLETE_ALL)`);
const trashTypesConfig = this.getTrashTypes();
for (const trashType of trashTypesConfig) {
const trashNameClean = trashType.nameClean;
const daysLeft = await this.getStateAsync(`type.${trashNameClean}.daysLeft`);
if (daysLeft && Number(daysLeft.val) <= this.config.daysuntilaction) {
this.log.debug(`Setting "completed" flag for type.${trashNameClean}.completed to true (COMPLETE_ALL)`);
await this.setStateAsync(`type.${trashNameClean}.completed`, { val: true, ack: true, c: 'COMPLETE_ALL' });
}
}
this.refreshEverything();
} else if (idNoNamespace == 'type.completedToday' && state.val && !state.ack) {
this.log.info(`Setting "completed" flag for all types of today to true (COMPLETE_TODAY)`);
const trashTypesConfig = this.getTrashTypes();
for (const trashType of trashTypesConfig) {
const trashNameClean = trashType.nameClean;
const daysLeft = await this.getStateAsync(`type.${trashNameClean}.daysLeft`);
if (daysLeft && Number(daysLeft.val) == 0) {
this.log.debug(`Setting "completed" flag for type.${trashNameClean}.completed to true (COMPLETE_TODAY)`);
await this.setStateAsync(`type.${trashNameClean}.completed`, { val: true, ack: true, c: 'COMPLETE_TODAY' });
}
}
this.refreshEverything();
} else if (idNoNamespace == 'type.completedTomorrow' && state.val && !state.ack) {
this.log.info(`Setting "completed" flag for all types of today and tomorrow to true (COMPLETE_TOMORROW)`);
const trashTypesConfig = this.getTrashTypes();
for (const trashType of trashTypesConfig) {
const trashNameClean = trashType.nameClean;
const daysLeft = await this.getStateAsync(`type.${trashNameClean}.daysLeft`);
if (daysLeft && Number(daysLeft.val) <= 1) {
this.log.debug(`Setting "completed" flag for type.${trashNameClean}.completed to true (COMPLETE_TOMORROW)`);
await this.setStateAsync(`type.${trashNameClean}.completed`, { val: true, ack: true, c: 'COMPLETE_TOMORROW' });
}
}
this.refreshEverything();
} else if (idNoNamespace.endsWith('.completed') && !state.ack) {
this.log.debug(`Setting "completed" flag for ${idNoNamespace} to ${state.val} (MANUALLY_CHANGED)`);
this.refreshEverything();
await this.setStateAsync(idNoNamespace, { val: state.val, ack: true, c: 'MANUALLY_CHANGED' });
}
}
}
getTrashTypes() {
const trashTypesConfig = this.config.trashtypes;
if (trashTypesConfig && Array.isArray(trashTypesConfig)) {
return trashTypesConfig.map((trashType) => ({ ...trashType, name: trashType.name.trim(), nameClean: this.cleanNamespace(trashType.name.trim()) }));
}
return [];
}
getMillisecondsToNextFullHour() {
const now = new Date();
const nextHour = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours() + 1, 0, 5, 0); // add 5 seconds to ensure we are in the next hour
return nextHour.getTime() - now.getTime();
}
convertMillisecondsToDuration(duration) {
const seconds = Math.floor((duration / 1000) % 60);
const minutes = Math.floor((duration / (1000 * 60)) % 60);
const hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
return `${hours < 10 ? '0' + hours : hours}:${minutes < 10 ? '0' + minutes : minutes}:${seconds < 10 ? '0' + seconds : seconds}`;
}
getDateWithoutTime(date, offset) {
const d = new Date(date.getFullYear(), date.getMonth(), date.getDate());
if (offset != 0) {
d.setTime(d.getTime() + offset * 24 * 60 * 60 * 1000);
}
return d;
}
cleanNamespace(id) {
return id
.trim()
.replace(/\s/g, '_') // Replace whitespaces with underscores
.replace(/[^\p{Ll}\p{Lu}\p{Nd}]+/gu, '_') // Replace not allowed chars with underscore
.replace(/[_]+$/g, '') // Remove underscores end
.replace(/^[_]+/g, '') // Remove underscores beginning
.replace(/_+/g, '_') // Replace multiple underscores with one
.toLowerCase()
.replace(/_([a-z])/g, (m, w) => {
return w.toUpperCase();
});
}
removeNamespace(id) {
const re = new RegExp(this.namespace + '*\\.', 'g');
return id.replace(re, '');
}
async updateAll(data) {
this.log.debug('(0) updating data');
if (data && data.length > 0) {
await this.setStateAsync('info.connection', { val: true, ack: true });
// Sort by date
data.sort((a, b) => {
const aD = new Date(a.date);
const bD = new Date(b.date);
return aD.getTime() - bD.getTime();
});
this.log.debug(`(0) start processing ${data.length} events`);
const dateNow = this.getDateWithoutTime(new Date(), 0);
const hourNow = new Date().getHours();
const trashTypesConfig = this.getTrashTypes();
const globalOffset = this.config.globaloffset || 0;
const skipsamedayathour = this.config.skipsamedayathour || 18;
if (skipsamedayathour > 23 || skipsamedayathour < 0) {
this.log.warn(`Skip same day at hour has an invalid configuration: ${skipsamedayathour}. Select a value between 0 and 23 in instance settings`);
}
const jsonSummary = [];
const filledTypes = [];
const next = {
minDays: 999,
minDate: null,
minTypes: [],
};
const nextAfter = {
minDays: 999,
minDate: null,
minTypes: [],
};
this.log.debug(`(0) offset (config): ${globalOffset}`);
for (const entry of data) {
const date = this.getDateWithoutTime(new Date(entry.date), globalOffset);
this.log.debug(`(1) parsing next event ${JSON.stringify(entry)} // originalDate: ${entry.date} // calculated date (with offset): ${date}`);
// Just future events
if (date.getTime() >= dateNow.getTime()) {
const dayDiff = Math.round((date.getTime() - dateNow.getTime()) / (24 * 60 * 60 * 1000));
this.log.debug(`(2) processing: "${entry.name}" (${date.getTime()}) // dayDiff: ${dayDiff} // current hour (date): ${hourNow} // skipsamedayathour (config): ${skipsamedayathour}`);
// Check if event matches trash type and fill information
for (const trashType of trashTypesConfig) {
const trashName = trashType.name;
const trashNameClean = trashType.nameClean;
if (trashNameClean && !!trashType.match) {
if (dayDiff > 0 || hourNow < skipsamedayathour) {
// Fill type if event matches
const lcName = entry.name.toLowerCase();
const lcMatch = trashType.match.toLowerCase();
if ((!trashType.exactmatch && lcName.includes(lcMatch)) || (trashType.exactmatch && entry.name == trashType.match)) {
this.log.debug(`(3) event match: "${entry.name}" matches type "${trashName}" with pattern "${trashType.match}"${trashType.exactmatch ? ' (exact match)' : ''}`);
if (!filledTypes.includes(trashName)) {
filledTypes.push(trashName);
// Complete handling (reset)
const oldNextDateState = await this.getStateAsync(`type.${trashNameClean}.nextDate`);
if (oldNextDateState && oldNextDateState.val) {
const oldNextDate = Number(oldNextDateState.val);
if (oldNextDate < date.getTime()) {
this.log.debug(`Setting "completed" flag for type.${trashNameClean}.completed to false (RESET_NEXT_EVENT)`);
await this.setStateAsync(`type.${trashNameClean}.completed`, { val: false, ack: true, c: 'RESET_NEXT_EVENT' });
}
}
await this.setStateChangedAsync(`type.${trashNameClean}.nextDate`, { val: date.getTime(), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`type.${trashNameClean}.nextDateFormat`, { val: this.formatDate(date), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`type.${trashNameClean}.nextWeekday`, { val: date.getDay(), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`type.${trashNameClean}.daysLeft`, { val: dayDiff, ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`type.${trashNameClean}.nextDateFound`, { val: true, ack: true });
await this.setStateChangedAsync(`type.${trashNameClean}.color`, { val: trashType.color, ack: true });
// Do not store objects as value
await this.setStateChangedAsync(`type.${trashNameClean}.nextDescription`, { val: entry.description, ack: true, c: this.source?.getType() });
const isCompletedState = await this.getStateAsync(`type.${trashNameClean}.completed`);
if (dayDiff <= this.config.daysuntilaction && isCompletedState && !isCompletedState.val) {
this.log.debug(`Setting "actionNeeded" flag for type.${trashNameClean}.actionNeeded to true`);
await this.setStateChangedAsync(`type.${trashNameClean}.actionNeeded`, { val: true, ack: true });
}
if (isCompletedState && isCompletedState.val) {
this.log.debug(`Setting "actionNeeded" flag for type.${trashNameClean}.actionNeeded to false`);
await this.setStateChangedAsync(`type.${trashNameClean}.actionNeeded`, { val: false, ack: true });
}
jsonSummary.push({
name: trashName,
daysLeft: dayDiff,
nextDate: date.getTime(),
_completed: isCompletedState ? isCompletedState.val : false,
_description: entry.description,
_color: trashType.color,
});
this.log.debug(`(4) filled type: "${trashName}"`);
}
// Set next type
if (next.minTypes.length == 0) {
next.minDays = dayDiff;
next.minDate = date;
} else if (nextAfter.minTypes.length == 0) {
nextAfter.minDays = dayDiff;
nextAfter.minDate = date;
}
if (!next.minTypes.includes(trashName) && next.minDays == dayDiff) {
next.minTypes.push(trashName);
} else if (!nextAfter.minTypes.includes(trashName) && nextAfter.minDays == dayDiff) {
nextAfter.minTypes.push(trashName);
}
}
}
}
}
} else {
this.log.debug(`skipped event (is in the past) ${JSON.stringify(entry)}`);
}
}
// Check for "unmatched" types
const notFoundTypes = [];
for (const trashType of trashTypesConfig) {
const trashName = trashType.name;
const trashNameClean = trashType.nameClean;
if (trashNameClean && !!trashType.match) {
const hideWarnings = trashType.hidewarnings || false;
if (!filledTypes.includes(trashName)) {
if (!hideWarnings) {
this.log.warn(`no events matches type "${trashName}" with match "${trashType.match}"${trashType.exactmatch ? ' (exact match)' : ''}`);
}
// reset values
await this.setStateChangedAsync(`type.${trashNameClean}.nextDate`, { val: 0, ack: true, q: 0x02 });
await this.setStateChangedAsync(`type.${trashNameClean}.nextDateFormat`, { val: '', ack: true, q: 0x02 });
await this.setStateChangedAsync(`type.${trashNameClean}.nextWeekday`, { val: null, ack: true, q: 0x02 });
await this.setStateChangedAsync(`type.${trashNameClean}.daysLeft`, { val: null, ack: true, q: 0x02 });
await this.setStateChangedAsync(`type.${trashNameClean}.nextDescription`, { val: '', ack: true, q: 0x02 });
await this.setStateChangedAsync(`type.${trashNameClean}.completed`, { val: false, ack: true, q: 0x02 });
await this.setStateChangedAsync(`type.${trashNameClean}.nextDateFound`, { val: false, ack: true });
notFoundTypes.push({
name: trashName,
hideWarnings,
});
}
}
}
// Sort summary by days left
jsonSummary.sort((a, b) => {
return a.daysLeft - b.daysLeft;
});
await this.setStateAsync('type.json', { val: JSON.stringify(jsonSummary), ack: true });
await this.setStateAsync('type.jsonNotFound', { val: JSON.stringify(notFoundTypes), ack: true });
await this.setStateAsync('type.lastRefresh', { val: new Date().getTime(), ack: true });
await this.fillNext(next, 'next');
await this.fillNext(nextAfter, 'nextAfter');
} else {
this.log.error('[updateAll] no pickup dates found - check configuration and restart instance');
await this.setStateAsync('info.connection', { val: false, ack: true });
}
}
async fillNext(obj, statePrefix) {
this.log.debug(`(5) filling "${statePrefix}" event with data: ${JSON.stringify(obj)}`);
if (obj.minDays < 999 && obj.minTypes.length > 0) {
await this.setStateChangedAsync(`${statePrefix}.date`, { val: obj.minDate.getTime(), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`${statePrefix}.dateFormat`, { val: this.formatDate(obj.minDate), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`${statePrefix}.weekday`, { val: obj.minDate.getDay(), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`${statePrefix}.daysLeft`, { val: obj.minDays, ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`${statePrefix}.types`, { val: obj.minTypes.join(','), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`${statePrefix}.typesText`, { val: obj.minTypes.join(this.config.nextseparator), ack: true, c: this.source?.getType() });
await this.setStateChangedAsync(`${statePrefix}.dateFound`, { val: true, ack: true });
} else {
this.log.warn(`(5) ${statePrefix} has no entries. Check configuration of trashschedule!`);
await this.setStateChangedAsync(`${statePrefix}.date`, { val: 0, ack: true, q: 0x02 });
await this.setStateChangedAsync(`${statePrefix}.dateFormat`, { val: '', ack: true, q: 0x02 });
await this.setStateChangedAsync(`${statePrefix}.weekday`, { val: null, ack: true, q: 0x02 });
await this.setStateChangedAsync(`${statePrefix}.daysLeft`, { val: null, ack: true, q: 0x02 });
await this.setStateChangedAsync(`${statePrefix}.types`, { val: 'n/a', ack: true, q: 0x02 });
await this.setStateChangedAsync(`${statePrefix}.typesText`, { val: 'n/a', ack: true, q: 0x02 });
await this.setStateChangedAsync(`${statePrefix}.dateFound`, { val: false, ack: true });
}
}
/**
* @param {ioBroker.Message} obj
*/
async onMessage(obj) {
if (obj) {
this.log.debug(`[onMessage] ${obj.command}, ${obj.from}, ${JSON.stringify(obj.message)}`);
if (obj.command === 'getApiProviders') {
try {
const source = this.sources[obj.message?.source];
if (source) {
const response = await source.getApiProviders();
const providers = response.map((p) => ({ value: p.id, label: `${p.title} (${p.url})` }));
if (providers) {
this.log.debug(`[onMessage] ${obj.command} result: ${JSON.stringify(providers)}`);
obj.callback && this.sendTo(obj.from, obj.command, providers, obj.callback);
} else {
obj.callback && this.sendTo(obj.from, obj.command, 'Unable to get providers', obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: Source "${obj.message?.source}" not defined/found` }], obj.callback);
}
} catch (err) {
this.log.error(`[onMessage] ${obj.command} err: ${err}`);
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: ${err}` }], obj.callback);
}
} else if (obj.command === 'getApiCities') {
try {
const source = this.sources[obj.message?.source];
if (source) {
const provider = obj.message?.provider;
if (provider) {
const response = await source.getApiCities(provider);
const cities = response.map((c) => ({ value: c.id, label: c.name }));
if (cities) {
this.log.debug(`[onMessage] ${obj.command} result: ${JSON.stringify(cities)}`);
obj.callback && this.sendTo(obj.from, obj.command, cities, obj.callback);
} else {
obj.callback && this.sendTo(obj.from, obj.command, 'Unable to get cities', obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: 'No provider selected' }], obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: Source "${obj.message?.source}" not defined/found` }], obj.callback);
}
} catch (err) {
this.log.error(`[onMessage] ${obj.command} err: ${err}`);
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: ${err}` }], obj.callback);
}
} else if (obj.command === 'getApiDistricts') {
try {
const source = this.sources[obj.message?.source];
if (source) {
const provider = obj.message?.provider;
const cityId = obj.message?.cityId;
if (provider && cityId) {
const response = await source.getApiDistricts(provider, cityId);
const districts = response.map((d) => ({ value: d.id, label: d.name }));
if (districts) {
this.log.debug(`[onMessage] ${obj.command} result: ${JSON.stringify(districts)}`);
obj.callback && this.sendTo(obj.from, obj.command, districts, obj.callback);
} else {
obj.callback && this.sendTo(obj.from, obj.command, 'Unable to get districts', obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: 'Missing provider or cityId' }], obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: Source "${obj.message?.source}" not defined/found` }], obj.callback);
}
} catch (err) {
this.log.error(`[onMessage] ${obj.command} err: ${err}`);
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: ${err}` }], obj.callback);
}
} else if (obj.command === 'getApiStreets') {
try {
const source = this.sources[obj.message?.source];
if (source) {
const provider = obj.message?.provider;
const cityId = obj.message?.cityId;
const districtId = obj.message?.districtId;
if (provider && cityId) {
const response = await source.getApiStreets(provider, cityId, districtId);
const streets = response.map((s) => ({ value: s.id, label: s.name }));
if (streets) {
this.log.debug(`[onMessage] ${obj.command} result: ${JSON.stringify(streets)}`);
obj.callback && this.sendTo(obj.from, obj.command, streets, obj.callback);
} else {
obj.callback && this.sendTo(obj.from, obj.command, 'Unable to get streets', obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Missing provider or cityId` }], obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: Source "${obj.message?.source}" not defined/found` }], obj.callback);
}
} catch (err) {
this.log.error(`[onMessage] ${obj.command} err: ${err}`);
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: ${err}` }], obj.callback);
}
} else if (obj.command === 'getApiHouseNumbers') {
try {
const source = this.sources[obj.message?.source];
if (source) {
const provider = obj.message?.provider;
const cityId = obj.message?.cityId;
const districtId = obj.message?.districtId;
const streetId = obj.message?.streetId;
if (provider && cityId && districtId && streetId) {
const response = await source.getApiHouseNumbers(provider, cityId, districtId, streetId);
const houseNumbers = response.map((h) => ({ value: h.id, label: h.name }));
if (houseNumbers) {
this.log.debug(`[onMessage] ${obj.command} result: ${JSON.stringify(houseNumbers)}`);
obj.callback && this.sendTo(obj.from, obj.command, houseNumbers, obj.callback);
} else {
obj.callback && this.sendTo(obj.from, obj.command, 'Unable to get houseNumbers', obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Missing provider or cityId` }], obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: Source "${obj.message?.source}" not defined/found` }], obj.callback);
}
} catch (err) {
this.log.error(`[onMessage] ${obj.command} err: ${err}`);
obj.callback && this.sendTo(obj.from, obj.command, [{ value: 'err', label: `Error: ${err}` }], obj.callback);
}
} else if (obj.command === 'getApiTypesText') {
try {
const source = this.sources[obj.message?.source];
if (source) {
const provider = obj.message?.provider;
const cityId = this.isUUID(obj.message?.cityId) ? obj.message?.cityId : parseInt(obj.message?.cityId);
const districtId = obj.message?.districtId;
const streetId = obj.message?.streetId;
const houseNumber = obj.message?.houseNumber;
if (provider && cityId && (this.isUUID(obj.message?.cityId) || cityId > 0)) {
const source = this.sources[obj.message?.source];
const response = await source.getApiTypes(provider, cityId, districtId, streetId, houseNumber);
const types = response.map((c) => c.title ?? c.name).join(', ');
if (types) {
this.log.debug(`[onMessage] ${obj.command} result: ${JSON.stringify(types)}`);
obj.callback && this.sendTo(obj.from, obj.command, types, obj.callback);
} else {
obj.callback && this.sendTo(obj.from, obj.command, 'Unable to get types', obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, 'Missing provider or cityId', obj.callback);
}
} else {
obj.callback && this.sendTo(obj.from, obj.command, `Error: Source "${obj.message?.source}" not defined/found`, obj.callback);
}
} catch (err) {
this.log.error(`[onMessage] ${obj.command} err: ${err}`);
obj.callback && this.sendTo(obj.from, obj.command, `Error: ${err}`, obj.callback);
}
}
}
}
/**
* validates a UUID
*
* @param uuid a potential UUID
* @returns {boolean} whether uuid is a valid UUID
*/
isUUID(uuid) {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid);
}
/**
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.log.info('cleaned everything up...');
if (this.refreshEverythingTimeout) {
this.log.debug('clearing refresh timeout');
this.clearTimeout(this.refreshEverythingTimeout);
}
callback();
} catch {
callback();
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Trashschedule(options);
} else {
// otherwise start the instance directly
new Trashschedule();
}