forked from klein0r/ioBroker.trashschedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
215 lines (185 loc) · 7.73 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
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core');
class Trashschedule extends utils.Adapter {
constructor(options) {
super({
...options,
name: 'trashschedule',
useFormatDate: true
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
const self = this;
const iCalInstance = this.config.ical;
const trashTypesConfig = this.config.trashtypes;
// Create states and channels
if (trashTypesConfig && Array.isArray(trashTypesConfig)) {
for (const t in trashTypesConfig) {
const trashType = trashTypesConfig[t];
const trashName = trashType.name.trim();
this.setObjectNotExists('type.' + trashName, {
type: 'channel',
common: {
name: 'Type ' + trashName
},
native: {}
});
this.setObjectNotExists('type.' + trashName + '.nextdate', {
type: 'state',
common: {
name: 'Next date',
type: 'string',
role: 'date.start',
read: true,
write: false
},
native: {}
});
this.setObjectNotExists('type.' + trashName + '.nextdateformat', {
type: 'state',
common: {
name: 'Next date format',
type: 'string',
role: 'value',
read: true,
write: false
},
native: {}
});
this.setObjectNotExists('type.' + trashName + '.nextweekday', {
type: 'state',
common: {
name: 'Next week day',
type: 'string',
role: 'value',
read: true,
write: false
},
native: {}
});
this.setObjectNotExists('type.' + trashName + '.daysleft', {
type: 'state',
common: {
name: 'Days left',
type: 'number',
role: 'value',
unit: 'days',
read: true,
write: false
},
native: {}
});
}
}
if (iCalInstance) {
this.subscribeStates('*');
this.subscribeForeignStates(this.config.ical + '.data.table');
this.getForeignState(this.config.ical + '.data.table', function (err, state) {
self.updateByCalendarTable(state.val);
});
} else {
this.setState('info.connection', false, true);
}
}
onStateChange(id, state) {
if (id && state && id == this.config.ical + '.data.table') {
this.updateByCalendarTable(state.val);
}
}
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;
}
updateByCalendarTable(data) {
this.log.debug('updating data');
// Array should be sorted by date (done by ical)
if (data && Array.isArray(data)) {
this.setState('info.connection', true, true);
const dateNow = this.getDateWithoutTime(new Date(), 0);
const hourNow = (new Date()).getHours();
const trashTypesConfig = this.config.trashtypes;
const globalOffset = this.config.globaloffset || 0;
const skipsamedayathour = this.config.skipsamedayathour || 18;
let minDays = 999;
let minDate = null;
const minTypes = [];
const filledTypes = [];
for (const i in data) {
const entry = data[i];
const date = this.getDateWithoutTime(new Date(entry._date), globalOffset);
this.log.debug('parsing event ' + JSON.stringify(entry));
// Just future events
if (date.getTime() >= dateNow.getTime()) {
const dayDiff = (date.getTime() - dateNow.getTime()) / (1000 * 3600 * 24);
// Check if event matches trash type and fill information
for (const t in trashTypesConfig) {
const trashType = trashTypesConfig[t];
const trashName = trashType.name.trim();
if (dayDiff > 0 || hourNow < skipsamedayathour) {
// Fill type if event matches
if (
!filledTypes.includes(trashName) &&
(
(!trashType.exactmatch && entry.event.indexOf(trashType.match) > -1) ||
(trashType.exactmatch && entry.event == trashType.match)
)
) {
filledTypes.push(trashName);
this.setState('type.' + trashName + '.nextdate', {val: date, ack: true});
this.setState('type.' + trashName + '.nextdateformat', {val: this.formatDate(date), ack: true});
this.setState('type.' + trashName + '.nextweekday', {val: date.getDay(), ack: true});
this.setState('type.' + trashName + '.daysleft', {val: dayDiff, ack: true});
// Set next type
if (minTypes.length == 0) {
minDays = dayDiff;
minDate = date;
}
if (minDays == dayDiff) {
minTypes.push(trashName);
}
}
}
}
}
}
if (minDays < 999 && minTypes.length > 0) {
this.setState('next.daysleft', {val: minDays, ack: true});
this.setState('next.date', {val: minDate, ack: true});
this.setState('next.dateformat', {val: this.formatDate(minDate), ack: true});
this.setState('next.weekday', {val: minDate.getDay(), ack: true});
this.setState('next.types', {val: minTypes.join(','), ack: true});
this.setState('next.typestext', {val: minTypes.join(' ' + this.config.nextseparator + ' '), ack: true});
}
} else {
this.setState('info.connection', false, true);
}
}
onUnload(callback) {
try {
this.log.info('cleaned everything up...');
callback();
} catch (e) {
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();
}