iobroker.device-watcher 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/admin/i18n/de/translations.json +17 -10
- package/admin/i18n/en/translations.json +8 -3
- package/admin/i18n/es/translations.json +8 -1
- package/admin/i18n/fr/translations.json +8 -1
- package/admin/i18n/it/translations.json +8 -1
- package/admin/i18n/nl/translations.json +8 -1
- package/admin/i18n/pl/translations.json +8 -1
- package/admin/i18n/pt/translations.json +8 -1
- package/admin/i18n/ru/translations.json +8 -1
- package/admin/i18n/zh-cn/translations.json +8 -1
- package/admin/jsonConfig.json +109 -3
- package/io-package.json +32 -8
- package/main.js +598 -334
- package/package.json +5 -5
package/main.js
CHANGED
|
@@ -16,236 +16,355 @@ class DeviceWatcher extends utils.Adapter {
|
|
|
16
16
|
useFormatDate: true,
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
this.refreshEverythingTimeout = null;
|
|
20
|
-
|
|
21
19
|
this.on('ready', this.onReady.bind(this));
|
|
22
20
|
//this.on('stateChange', this.onStateChange.bind(this));
|
|
23
21
|
// this.on('objectChange', this.onObjectChange.bind(this));
|
|
24
22
|
// this.on('message', this.onMessage.bind(this));
|
|
25
23
|
this.on('unload', this.onUnload.bind(this));
|
|
24
|
+
|
|
25
|
+
// arrays
|
|
26
|
+
this.offlineDevices = [],
|
|
27
|
+
this.linkQualityDevices = [];
|
|
28
|
+
this.batteryPowered = [];
|
|
29
|
+
this.batteryLowPowered = [];
|
|
30
|
+
this.listAllDevices = [];
|
|
31
|
+
this.blacklistArr = [];
|
|
32
|
+
this.arrDev = [];
|
|
33
|
+
|
|
34
|
+
// counts
|
|
35
|
+
this.offlineDevicesCount = 0;
|
|
36
|
+
this.deviceCounter = 0;
|
|
37
|
+
this.linkQualityCount = 0;
|
|
38
|
+
this.batteryPoweredCount = 0;
|
|
39
|
+
this.lowBatteryPoweredCount = 0;
|
|
40
|
+
|
|
41
|
+
// arrays of supported adapters
|
|
42
|
+
this.arrApart = {
|
|
43
|
+
//**** This Datapoints are only for the dev ****//
|
|
44
|
+
test: {'Selektor':'0_userdata.*.UNREACH', 'adapter':'test', 'rssiState':'.RSSI_DEVICE', 'battery':'.OPERATING_VOLTAGE', 'reach':'.UNREACH'},
|
|
45
|
+
test2: {'Selektor':'0_userdata.*.reachable', 'adapter':'test2', 'battery':'none', 'reach':'none', 'isLowBat':'none'},
|
|
46
|
+
test3: {'Selektor':'0_userdata.*.link_quality', 'adapter':'test3', 'battery':'.battery', 'reach':'none', 'isLowBat':'none'},
|
|
47
|
+
//**** End of Dev Datapoints ****//
|
|
48
|
+
ble: {'Selektor':'ble.*.rssi', 'adapter':'Ble', 'battery':'.battery', 'reach':'none', 'isLowBat':'none'},
|
|
49
|
+
zigbee: {'Selektor':'zigbee.*.link_quality', 'adapter':'zigbee', 'battery':'.battery', 'reach':'none', 'isLowBat':'none'},
|
|
50
|
+
sonoff: {'Selektor':'sonoff.*.Wifi_RSSI', 'adapter':'sonoff', 'battery':'.battery', 'reach':'none', 'isLowBat':'none'},
|
|
51
|
+
shelly: {'Selektor':'shelly.*.rssi', 'adapter':'shelly', 'battery':'.sensor.battery', 'reach':'none', 'isLowBat':'none'},
|
|
52
|
+
homematic: {'Selektor':'hm-rpc.*.UNREACH', 'adapter':'homematic', 'rssiState':'.RSSI_DEVICE', 'battery':'.OPERATING_VOLTAGE', 'reach':'.UNREACH', 'isLowBat':'.LOW_BAT', 'isLowBat2':'.LOWBAT'},
|
|
53
|
+
deconz: {'Selektor':'deconz.*.reachable', 'adapter':'deconz', 'battery':'.battery', 'reach':'.reachable', 'isLowBat':'none'},
|
|
54
|
+
zwave: {'Selektor':'zwave2.*.ready', 'adapter':'zwave', 'battery':'.Battery.level', 'reach':'.ready', 'isLowBat':'.Battery.isLow'},
|
|
55
|
+
dect: {'Selektor':'fritzdect.*.present', 'adapter':'fritzDect', 'battery':'.battery', 'reach':'.present', 'isLowBat':'.batterylow'},
|
|
56
|
+
hue: {'Selektor':'hue.*.reachable', 'adapter':'hue', 'battery':'.battery', 'reach':'.reachable', 'isLowBat':'none'},
|
|
57
|
+
hueExt: {'Selektor':'hue-extended.*.reachable', 'adapter':'hue extended', 'battery':'.config.battery', 'reach':'.reachable', 'isLowBat':'none'},
|
|
58
|
+
ping: {'Selektor':'ping.*.alive', 'adapter':'ping', 'battery':'none', 'reach':'.alive', 'isLowBat':'none'},
|
|
59
|
+
switchbotBle: {'Selektor':'switchbot-ble.*.rssi', 'adapter':'switchbot ble', 'battery':'.battery', 'reach':'none', 'isLowBat':'none', 'id':'.id'},
|
|
60
|
+
sonos: {'Selektor':'sonos.*.alive', 'adapter':'sonos', 'battery':'none', 'reach':'.alive', 'isLowBat':'none'},
|
|
61
|
+
mihome: {'Selektor':'mihome.*.percent', 'adapter':'miHome', 'battery':'.percent', 'reach':'none', 'isLowBat':'none'},
|
|
62
|
+
mihomeGW: {'Selektor':'mihome.*.connected', 'adapter':'miHome', 'battery':'none', 'reach':'.connected', 'isLowBat':'none'}
|
|
63
|
+
};
|
|
26
64
|
}
|
|
27
65
|
|
|
28
66
|
async onReady() {
|
|
29
|
-
this.log.debug(
|
|
67
|
+
this.log.debug(`Adapter ${adapterName} was started`);
|
|
30
68
|
|
|
31
69
|
try {
|
|
32
70
|
await this.main();
|
|
71
|
+
await this.sendNotifications();
|
|
72
|
+
await this.writeDatapoints();
|
|
33
73
|
this.log.debug('all done, exiting');
|
|
34
74
|
this.terminate ? this.terminate('Everything done. Going to terminate till next schedule', 11) : process.exit(0);
|
|
35
75
|
} catch (e) {
|
|
36
|
-
this.log.error(
|
|
76
|
+
this.log.error(`Error while running Device-Watcher. Error Message: ${e}`);
|
|
37
77
|
this.terminate ? this.terminate(15) : process.exit(15);
|
|
38
78
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
79
|
}
|
|
43
80
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
async function capitalize(sentence)
|
|
50
|
-
{
|
|
51
|
-
return sentence && sentence[0].toUpperCase() + sentence.slice(1);
|
|
52
|
-
}*/
|
|
53
|
-
|
|
54
|
-
const pushover = {
|
|
55
|
-
instance: this.config.instancePushover,
|
|
56
|
-
title: this.config.titlePushover,
|
|
57
|
-
device: this.config.devicePushover
|
|
58
|
-
|
|
59
|
-
};
|
|
60
|
-
const telegram = {
|
|
61
|
-
instance: this.config.instanceTelegram,
|
|
62
|
-
user: this.config.deviceTelegram
|
|
63
|
-
};
|
|
64
|
-
const email = {
|
|
65
|
-
instance: this.config.instanceEmail,
|
|
66
|
-
subject: this.config.subjectEmail,
|
|
67
|
-
sendTo: this.config.sendToEmail
|
|
68
|
-
|
|
69
|
-
};
|
|
70
|
-
const jarvis = {
|
|
71
|
-
instance: this.config.instanceJarvis,
|
|
72
|
-
title: this.config.titleJarvis
|
|
81
|
+
//Helpfunctions
|
|
82
|
+
async capitalize(sentence)
|
|
83
|
+
{
|
|
84
|
+
return sentence && sentence[0].toUpperCase() + sentence.slice(1);
|
|
85
|
+
}
|
|
73
86
|
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
wednesday: this.config.checkWednesday,
|
|
79
|
-
thursday: this.config.checkThursday,
|
|
80
|
-
friday: this.config.checkFriday,
|
|
81
|
-
saturday: this.config.checkSaturday,
|
|
82
|
-
sunday: this.config.checkSunday,
|
|
83
|
-
};
|
|
87
|
+
async getInitValue(obj) {
|
|
88
|
+
const foreignState = await this.getForeignStateAsync(obj);
|
|
89
|
+
if (foreignState) return foreignState.val;
|
|
90
|
+
}
|
|
84
91
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
device: pushover.device
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
+
async getOwnInitValue(obj) {
|
|
93
|
+
const stateVal = await this.getStateAsync(obj);
|
|
94
|
+
if (stateVal) return stateVal.val;
|
|
95
|
+
}
|
|
92
96
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
async createDPsForEachAdapter(adptName) {
|
|
98
|
+
await this.setObjectNotExistsAsync(`${adptName}.offlineCount`, {
|
|
99
|
+
'type': 'state',
|
|
100
|
+
'common': {
|
|
101
|
+
'name': 'Quantity devices offline',
|
|
102
|
+
'type': 'number',
|
|
103
|
+
'role': 'value',
|
|
104
|
+
'read': true,
|
|
105
|
+
'write': false,
|
|
106
|
+
'def': 0
|
|
107
|
+
},
|
|
108
|
+
'native': {}
|
|
109
|
+
});
|
|
110
|
+
await this.setObjectNotExistsAsync(`${adptName}.offlineList`, {
|
|
111
|
+
'type': 'state',
|
|
112
|
+
'common': {
|
|
113
|
+
'name': 'List devices offline',
|
|
114
|
+
'type': 'array',
|
|
115
|
+
'role': 'json',
|
|
116
|
+
'read': true,
|
|
117
|
+
'write': false,
|
|
118
|
+
'def': [{Device: '--keine--', Adapter: '', Last_contact: ''}]
|
|
119
|
+
},
|
|
120
|
+
'native': {}
|
|
121
|
+
});
|
|
122
|
+
await this.setObjectNotExistsAsync(`${adptName}.listAll`, {
|
|
123
|
+
'type': 'state',
|
|
124
|
+
'common': {
|
|
125
|
+
'name': 'List all devices',
|
|
126
|
+
'type': 'array',
|
|
127
|
+
'role': 'json',
|
|
128
|
+
'read': true,
|
|
129
|
+
'write': false,
|
|
130
|
+
'def': [{Device: '--keine--', Adapter: '', Battery: '', Last_contact: '', Link_quality: ''}]
|
|
131
|
+
},
|
|
132
|
+
'native': {}
|
|
133
|
+
});
|
|
134
|
+
await this.setObjectNotExistsAsync(`${adptName}.linkQualityList`, {
|
|
135
|
+
'type': 'state',
|
|
136
|
+
'common': {
|
|
137
|
+
'name': 'List devices with qualitiy strength',
|
|
138
|
+
'type': 'array',
|
|
139
|
+
'role': 'json',
|
|
140
|
+
'read': true,
|
|
141
|
+
'write': false,
|
|
142
|
+
'def': [{Device: '--keine--', Adapter: '', Link_quality: ''}]
|
|
143
|
+
},
|
|
144
|
+
'native': {}
|
|
145
|
+
});
|
|
146
|
+
await this.setObjectNotExistsAsync(`${adptName}.countAll`, {
|
|
147
|
+
'type': 'state',
|
|
148
|
+
'common': {
|
|
149
|
+
'name': 'Quantity devices all',
|
|
150
|
+
'type': 'number',
|
|
151
|
+
'role': 'value',
|
|
152
|
+
'read': true,
|
|
153
|
+
'write': false,
|
|
154
|
+
'def': 0
|
|
155
|
+
},
|
|
156
|
+
'native': {}
|
|
157
|
+
});
|
|
158
|
+
await this.setObjectNotExistsAsync(`${adptName}.batteryList`, {
|
|
159
|
+
'type': 'state',
|
|
160
|
+
'common': {
|
|
161
|
+
'name': 'List devices with battery state',
|
|
162
|
+
'type': 'array',
|
|
163
|
+
'role': 'json',
|
|
164
|
+
'read': true,
|
|
165
|
+
'write': false,
|
|
166
|
+
'def': [{Device: '--keine--', Adapter: '', Battery: ''}]
|
|
167
|
+
},
|
|
168
|
+
'native': {}
|
|
169
|
+
});
|
|
170
|
+
await this.setObjectNotExistsAsync(`${adptName}.lowBatteryList`, {
|
|
171
|
+
'type': 'state',
|
|
172
|
+
'common': {
|
|
173
|
+
'name': 'List devices with low battery state',
|
|
174
|
+
'type': 'array',
|
|
175
|
+
'role': 'json',
|
|
176
|
+
'read': true,
|
|
177
|
+
'write': false,
|
|
178
|
+
'def': [{Device: '--keine--', Adapter: '', Battery: ''}]
|
|
179
|
+
},
|
|
180
|
+
'native': {}
|
|
181
|
+
});
|
|
182
|
+
await this.setObjectNotExistsAsync(`${adptName}.lowBatteryCount`, {
|
|
183
|
+
'type': 'state',
|
|
184
|
+
'common': {
|
|
185
|
+
'name': 'Quantity devices with low battery',
|
|
186
|
+
'type': 'number',
|
|
187
|
+
'role': 'value',
|
|
188
|
+
'read': true,
|
|
189
|
+
'write': false,
|
|
190
|
+
'def': 0
|
|
191
|
+
},
|
|
192
|
+
'native': {}
|
|
193
|
+
});
|
|
194
|
+
await this.setObjectNotExistsAsync(`${adptName}.batteryCount`, {
|
|
195
|
+
'type': 'state',
|
|
196
|
+
'common': {
|
|
197
|
+
'name': 'Quantity devices with battery',
|
|
198
|
+
'type': 'number',
|
|
199
|
+
'role': 'value',
|
|
200
|
+
'read': true,
|
|
201
|
+
'write': false,
|
|
202
|
+
'def': 0
|
|
203
|
+
},
|
|
204
|
+
'native': {}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
99
207
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
208
|
+
async mainForAdapter(adptName) {
|
|
209
|
+
for (let i = 0; i < this.arrDev.length; i++) {
|
|
210
|
+
switch (this.arrDev[i].adapter) {
|
|
211
|
+
case adptName:
|
|
212
|
+
await this.setStateAsync(`${adptName}.offlineCount`, {val: 2, ack: true});
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
107
217
|
|
|
108
|
-
|
|
109
|
-
|
|
218
|
+
async main() {
|
|
219
|
+
this.log.debug(`Function started: ${this.main.name}`);
|
|
220
|
+
|
|
221
|
+
const supAdapter = {
|
|
222
|
+
zigbee: this.config.zigbeeDevices,
|
|
223
|
+
ble: this.config.bleDevices,
|
|
224
|
+
sonoff: this.config.sonoffDevices,
|
|
225
|
+
shelly: this.config.shellyDevices,
|
|
226
|
+
homematic: this.config.homematicDevices,
|
|
227
|
+
deconz: this.config.deconzDevices,
|
|
228
|
+
zwave: this.config.zwaveDevices,
|
|
229
|
+
dect: this.config.dectDevices,
|
|
230
|
+
hue: this.config.hueDevices,
|
|
231
|
+
hueExt: this.config.hueExtDevices,
|
|
232
|
+
nukiExt: this.config.nukiExtDevices,
|
|
233
|
+
ping: this.config.pingDevices,
|
|
234
|
+
switchbotBle: this.config.switchbotBleDevices,
|
|
235
|
+
sonos: this.config.sonosDevices,
|
|
236
|
+
mihome: this.config.mihomeDevices,
|
|
237
|
+
mihomeGW: this.config.mihomeDevices,
|
|
238
|
+
test: false, // Only for Developer
|
|
239
|
+
test2: false, // Only for Developer
|
|
240
|
+
test3: false // Only for Developer
|
|
110
241
|
};
|
|
111
242
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
243
|
+
if (!supAdapter.zigbee &&
|
|
244
|
+
!supAdapter.ble &&
|
|
245
|
+
!supAdapter.sonoff &&
|
|
246
|
+
!supAdapter.shelly &&
|
|
247
|
+
!supAdapter.homematic &&
|
|
248
|
+
!supAdapter.deconz &&
|
|
249
|
+
!supAdapter.zwave &&
|
|
250
|
+
!supAdapter.dect &&
|
|
251
|
+
!supAdapter.hue &&
|
|
252
|
+
!supAdapter.hueExt &&
|
|
253
|
+
!supAdapter.nukiExt &&
|
|
254
|
+
!supAdapter.ping &&
|
|
255
|
+
!supAdapter.switchbotBle &&
|
|
256
|
+
!supAdapter.sonos &&
|
|
257
|
+
!supAdapter.mihome
|
|
258
|
+
) {
|
|
127
259
|
this.log.warn('No devices selected. Pleased check the instance configuration');
|
|
128
260
|
}
|
|
129
261
|
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
if (this.config.zigbeeDevices) {
|
|
145
|
-
myArrDev.push({'Selektor':'zigbee.*.link_quality', 'adapter':'Zigbee', 'battery':'.battery', 'reach':'.available', 'isLowBat':'none'});
|
|
146
|
-
this.log.info('Zigbee Devices wurden ausgewählt. Lade Daten...');
|
|
147
|
-
}
|
|
148
|
-
if (this.config.sonoffDevices) {
|
|
149
|
-
myArrDev.push({'Selektor':'sonoff.*.Wifi_RSSI', 'adapter':'Sonoff', 'battery':'.battery', 'reach':'none', 'isLowBat':'none'});
|
|
150
|
-
myArrDev.push({'Selektor':'sonoff.*.Wifi_Signal', 'adapter':'Sonoff', 'battery':'.battery', 'reach':'none', 'isLowBat':'none'});
|
|
151
|
-
myArrDev.push({'Selektor':'sonoff.*.RSSI', 'adapter':'Sonoff', 'battery':'.battery', 'reach':'none', 'isLowBat':'none'});
|
|
152
|
-
this.log.info('Sonoff Devices wurden ausgewählt. Lade Daten...');
|
|
153
|
-
}
|
|
154
|
-
if (this.config.shellyDevices) {
|
|
155
|
-
myArrDev.push({'Selektor':'shelly.*.rssi', 'adapter':'Shelly', 'battery':'.sensor.battery', 'reach':'none', 'isLowBat':'none'});
|
|
156
|
-
this.log.info('Shelly Devices wurden ausgewählt. Lade Daten...');
|
|
157
|
-
}
|
|
158
|
-
if (this.config.homematicDevices) {
|
|
159
|
-
myArrDev.push({'Selektor':'hm-rpc.*.RSSI_DEVICE', 'adapter':'Homematic', 'battery':'.OPERATING_VOLTAGE', 'reach':'.UNREACH', 'isLowBat':'none'});
|
|
160
|
-
this.log.info('Homematic Devices wurden ausgewählt. Lade Daten...');
|
|
161
|
-
}
|
|
162
|
-
if (this.config.deconzDevices) {
|
|
163
|
-
myArrDev.push({'Selektor':'deconz.*.reachable', 'adapter':'Deconz', 'battery':'.battery', 'reach':'.reachable', 'isLowBat':'none'});
|
|
164
|
-
this.log.info('Deconz Devices wurden ausgewählt. Lade Daten...');
|
|
165
|
-
}
|
|
166
|
-
if (this.config.zwaveDevices) {
|
|
167
|
-
myArrDev.push({'Selektor':'zwave.*.ready', 'adapter':'Zwave', 'battery':'.battery.level', 'reach':'.ready', 'isLowBat':'.battery.isLow'});
|
|
168
|
-
this.log.info('Zwave Devices wurden ausgewählt. Lade Daten...');
|
|
262
|
+
for(const [id] of Object.entries(this.arrApart)) {
|
|
263
|
+
const idAdapter = supAdapter[id];
|
|
264
|
+
if (idAdapter) {
|
|
265
|
+
this.log.info(`${await this.capitalize(id)} was selected. Loading data...`);
|
|
266
|
+
this.arrDev.push(this.arrApart[id]);
|
|
267
|
+
/*try {
|
|
268
|
+
await this.createDPsForEachAdapter(id);
|
|
269
|
+
this.log.debug(`Created datapoints for ${await this.capitalize(id)}`);
|
|
270
|
+
await this.mainForAdapter(id);
|
|
271
|
+
} catch (e) {
|
|
272
|
+
this.log.warn(`Error at creating datapoints for each adapter: ${e}`);
|
|
273
|
+
}*/
|
|
274
|
+
}
|
|
169
275
|
}
|
|
170
276
|
|
|
171
|
-
this.log.debug(JSON.stringify(
|
|
277
|
+
this.log.debug(JSON.stringify(this.arrDev));
|
|
172
278
|
|
|
173
279
|
/*=============================================
|
|
174
280
|
= Start of main loop =
|
|
175
281
|
=============================================*/
|
|
176
|
-
for (let i = 0; i <
|
|
177
|
-
const devices
|
|
178
|
-
const deviceAdapterName =
|
|
179
|
-
|
|
180
|
-
this.log.debug(JSON.stringify(devices));
|
|
181
|
-
|
|
182
|
-
const myBlacklist = this.config.tableBlacklist;
|
|
183
|
-
const myBlacklistArr = [];
|
|
282
|
+
for (let i = 0; i < this.arrDev.length; i++) {
|
|
283
|
+
const devices = await this.getForeignStatesAsync(this.arrDev[i].Selektor);
|
|
284
|
+
const deviceAdapterName = this.arrDev[i].adapter;
|
|
285
|
+
const myBlacklist = this.config.tableBlacklist;
|
|
184
286
|
|
|
185
287
|
/*---------- Loop for blacklist ----------*/
|
|
186
288
|
for(const i in myBlacklist){
|
|
187
|
-
|
|
188
|
-
this.log.debug(
|
|
289
|
+
this.blacklistArr.push(myBlacklist[i].device);
|
|
290
|
+
this.log.debug(`Found items on the blacklist: ${this.blacklistArr}`);
|
|
189
291
|
}
|
|
190
292
|
|
|
191
293
|
/*---------- Start of second main loop ----------*/
|
|
192
294
|
for(const [id] of Object.entries(devices)) {
|
|
193
|
-
if (!
|
|
295
|
+
if (!this.blacklistArr.includes(id)) {
|
|
194
296
|
|
|
195
|
-
const currDeviceString
|
|
297
|
+
const currDeviceString = id.slice(0, (id.lastIndexOf('.') + 1) - 1);
|
|
298
|
+
const shortCurrDeviceString = currDeviceString.slice(0, (currDeviceString.lastIndexOf('.') + 1) - 1);
|
|
196
299
|
|
|
197
300
|
//Get device name
|
|
198
301
|
const deviceObject = await this.getForeignObjectAsync(currDeviceString);
|
|
302
|
+
const shortDeviceObject = await this.getForeignObjectAsync(shortCurrDeviceString);
|
|
199
303
|
let deviceName;
|
|
200
304
|
|
|
201
305
|
if (deviceObject && typeof deviceObject === 'object') {
|
|
202
306
|
deviceName = deviceObject.common.name;
|
|
203
307
|
}
|
|
204
308
|
|
|
309
|
+
if (shortDeviceObject && typeof shortDeviceObject === 'object') {
|
|
310
|
+
if (this.arrDev[i].adapter === 'hue extended') {
|
|
311
|
+
deviceName = shortDeviceObject.common.name;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
205
314
|
|
|
206
|
-
//Get
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
this.log.warn(currRoom);
|
|
214
|
-
}*/
|
|
315
|
+
//Get ID for Switchbot Devices
|
|
316
|
+
if (this.arrDev[i].adapter === 'switchbot ble') {
|
|
317
|
+
const switchbotID = await this.getForeignStateAsync(currDeviceString + this.arrDev[i].id);
|
|
318
|
+
if (switchbotID) {
|
|
319
|
+
deviceName = switchbotID.val;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
215
322
|
|
|
216
323
|
// 1. Get link quality
|
|
217
|
-
|
|
324
|
+
let deviceQualityState;
|
|
218
325
|
let linkQuality;
|
|
219
326
|
|
|
220
|
-
|
|
327
|
+
switch (this.arrDev[i].adapter) {
|
|
328
|
+
case 'homematic':
|
|
329
|
+
deviceQualityState = await this.getForeignStateAsync(currDeviceString + this.arrDev[i].rssiState);
|
|
330
|
+
break;
|
|
331
|
+
default:
|
|
332
|
+
deviceQualityState = await this.getForeignStateAsync(id);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if ((deviceQualityState) && (typeof deviceQualityState.val === 'number')){
|
|
221
336
|
if (this.config.trueState) {
|
|
222
337
|
linkQuality = deviceQualityState.val;
|
|
223
|
-
} else
|
|
338
|
+
} else {
|
|
224
339
|
if (deviceQualityState.val < 0) {
|
|
225
340
|
linkQuality = Math.min(Math.max(2 * (deviceQualityState.val + 100), 0), 100) + '%';
|
|
226
341
|
} else if ((deviceQualityState.val) >= 0) {
|
|
227
342
|
linkQuality = parseFloat((100/255 * deviceQualityState.val).toFixed(0)) + '%';
|
|
228
343
|
}
|
|
229
344
|
}
|
|
345
|
+
this.linkQualityDevices.push(
|
|
346
|
+
{
|
|
347
|
+
Device: deviceName,
|
|
348
|
+
Adapter: deviceAdapterName,
|
|
349
|
+
Link_quality: linkQuality
|
|
350
|
+
}
|
|
351
|
+
);
|
|
352
|
+
} else {
|
|
353
|
+
// no linkQuality available for powered devices
|
|
354
|
+
linkQuality = ' - ';
|
|
230
355
|
}
|
|
231
|
-
jsonLinkQualityDevices.push(
|
|
232
|
-
{
|
|
233
|
-
Device: deviceName,
|
|
234
|
-
Adapter: deviceAdapterName,
|
|
235
|
-
Link_quality: linkQuality
|
|
236
|
-
}
|
|
237
|
-
);
|
|
238
356
|
|
|
239
|
-
// 1b. Count how many devices
|
|
240
|
-
|
|
357
|
+
// 1b. Count how many devices with link Quality
|
|
358
|
+
this.linkQualityCount = this.linkQualityDevices.length;
|
|
241
359
|
|
|
242
360
|
// 2. When was the last contact to the device?
|
|
361
|
+
let lastContactString;
|
|
362
|
+
|
|
243
363
|
if (deviceQualityState) {
|
|
244
364
|
try {
|
|
245
365
|
const time = new Date();
|
|
246
366
|
const lastContact = Math.round((time.getTime() - deviceQualityState.ts) / 1000 / 60);
|
|
247
|
-
const
|
|
248
|
-
const deviceUnreachState = await this.getForeignStateAsync(currDeviceUnreachString);
|
|
367
|
+
const deviceUnreachState = await this.getInitValue(currDeviceString + this.arrDev[i].reach);
|
|
249
368
|
|
|
250
369
|
// 2b. wenn seit X Minuten kein Kontakt mehr besteht, nimm Gerät in Liste auf
|
|
251
370
|
//Rechne auf Tage um, wenn mehr als 48 Stunden seit letztem Kontakt vergangen sind
|
|
@@ -257,9 +376,10 @@ class DeviceWatcher extends utils.Adapter {
|
|
|
257
376
|
if (Math.round(lastContact/60) > 48) {
|
|
258
377
|
lastContactString = Math.round(lastContact/60/24) + ' Tagen';
|
|
259
378
|
}
|
|
260
|
-
|
|
379
|
+
|
|
380
|
+
if (this.arrDev[i].reach === 'none') {
|
|
261
381
|
if (lastContact > this.config.maxMinutes) {
|
|
262
|
-
|
|
382
|
+
this.offlineDevices.push(
|
|
263
383
|
{
|
|
264
384
|
Device: deviceName,
|
|
265
385
|
Adapter: deviceAdapterName,
|
|
@@ -268,71 +388,104 @@ class DeviceWatcher extends utils.Adapter {
|
|
|
268
388
|
);
|
|
269
389
|
}
|
|
270
390
|
} else {
|
|
271
|
-
if (deviceUnreachState) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
);
|
|
288
|
-
}
|
|
391
|
+
if ((deviceUnreachState) && (this.arrDev[i].adapter === 'homematic')) {
|
|
392
|
+
this.offlineDevices.push(
|
|
393
|
+
{
|
|
394
|
+
Device: deviceName,
|
|
395
|
+
Adapter: deviceAdapterName,
|
|
396
|
+
Last_contact: lastContactString
|
|
397
|
+
}
|
|
398
|
+
);
|
|
399
|
+
} else if ((!deviceUnreachState) && (this.arrDev[i].adapter != 'homematic')) {
|
|
400
|
+
this.offlineDevices.push(
|
|
401
|
+
{
|
|
402
|
+
Device: deviceName,
|
|
403
|
+
Adapter: deviceAdapterName,
|
|
404
|
+
Last_contact: lastContactString
|
|
405
|
+
}
|
|
406
|
+
);
|
|
289
407
|
}
|
|
290
408
|
}
|
|
291
409
|
} catch (e) {
|
|
292
|
-
this.log.error(
|
|
410
|
+
this.log.error(`(03) Error while getting timestate ${e}`);
|
|
293
411
|
}
|
|
294
412
|
}
|
|
295
413
|
|
|
296
|
-
|
|
297
414
|
// 2c. Count how many devcies are offline
|
|
298
|
-
offlineDevicesCount =
|
|
415
|
+
this.offlineDevicesCount = this.offlineDevices.length;
|
|
299
416
|
|
|
300
417
|
// 3. Get battery states
|
|
301
|
-
const
|
|
302
|
-
const
|
|
418
|
+
const deviceBatteryState = await this.getInitValue(currDeviceString + this.arrDev[i].battery);
|
|
419
|
+
const shortDeviceBatteryState = await this.getInitValue(shortCurrDeviceString + this.arrDev[i].battery);
|
|
303
420
|
let batteryHealth;
|
|
304
421
|
|
|
305
|
-
if (!deviceBatteryState) {
|
|
306
|
-
batteryHealth = ' - ';
|
|
307
|
-
} else if ((myArrDev[i].adapter === 'Homematic') && (deviceBatteryState).val === 0) {
|
|
422
|
+
if ((!deviceBatteryState) && (!shortDeviceBatteryState)) {
|
|
308
423
|
batteryHealth = ' - ';
|
|
309
|
-
} else
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
424
|
+
} else {
|
|
425
|
+
this.log.debug(`Adapter ${this.arrDev[i].adapter}`);
|
|
426
|
+
|
|
427
|
+
switch (this.arrDev[i].adapter) {
|
|
428
|
+
case 'homematic':
|
|
429
|
+
if (deviceBatteryState === 0) {
|
|
430
|
+
batteryHealth = ' - ';
|
|
431
|
+
} else {
|
|
432
|
+
batteryHealth = deviceBatteryState + 'V';
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
this.batteryPowered.push(
|
|
436
|
+
{
|
|
437
|
+
Device: deviceName,
|
|
438
|
+
Adapter: deviceAdapterName,
|
|
439
|
+
Battery: batteryHealth
|
|
440
|
+
}
|
|
441
|
+
);
|
|
442
|
+
break;
|
|
443
|
+
case 'hue extended':
|
|
444
|
+
if (shortDeviceBatteryState) {
|
|
445
|
+
batteryHealth = shortDeviceBatteryState + '%';
|
|
446
|
+
this.batteryPowered.push(
|
|
447
|
+
{
|
|
448
|
+
Device: deviceName,
|
|
449
|
+
Adapter: deviceAdapterName,
|
|
450
|
+
Battery: batteryHealth
|
|
451
|
+
}
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
break;
|
|
455
|
+
default:
|
|
456
|
+
batteryHealth = (deviceBatteryState) + '%';
|
|
457
|
+
this.batteryPowered.push(
|
|
458
|
+
{
|
|
459
|
+
Device: deviceName,
|
|
460
|
+
Adapter: deviceAdapterName,
|
|
461
|
+
Battery: batteryHealth
|
|
462
|
+
}
|
|
463
|
+
);
|
|
464
|
+
}
|
|
327
465
|
}
|
|
466
|
+
|
|
328
467
|
// 3b. Count how many devices are with battery
|
|
329
|
-
batteryPoweredCount =
|
|
468
|
+
this.batteryPoweredCount = this.batteryPowered.length;
|
|
330
469
|
|
|
331
470
|
// 3c. Count how many devices are with low battery
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
471
|
+
const batteryWarningMin = this.config.minWarnBatterie;
|
|
472
|
+
const deviceLowBatState = await this.getInitValue(currDeviceString + this.arrDev[i].isLowBat);
|
|
473
|
+
const deviceLowBatStateHM = await this.getInitValue(currDeviceString + this.arrDev[i].isLowBat2);
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
if (this.arrDev[i].isLowBat === 'none') {
|
|
477
|
+
if (deviceBatteryState && (deviceBatteryState < batteryWarningMin)) {
|
|
478
|
+
this.batteryLowPowered.push(
|
|
479
|
+
{
|
|
480
|
+
Device: deviceName,
|
|
481
|
+
Adapter: deviceAdapterName,
|
|
482
|
+
Battery: batteryHealth
|
|
483
|
+
}
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
} else {
|
|
487
|
+
if (deviceLowBatState || deviceLowBatStateHM) {
|
|
488
|
+
this.batteryLowPowered.push(
|
|
336
489
|
{
|
|
337
490
|
Device: deviceName,
|
|
338
491
|
Adapter: deviceAdapterName,
|
|
@@ -340,84 +493,178 @@ class DeviceWatcher extends utils.Adapter {
|
|
|
340
493
|
}
|
|
341
494
|
);
|
|
342
495
|
}
|
|
343
|
-
|
|
344
496
|
}
|
|
497
|
+
|
|
345
498
|
// 3d. Count how many devices are with low battery
|
|
346
|
-
lowBatteryPoweredCount =
|
|
499
|
+
this.lowBatteryPoweredCount = this.batteryLowPowered.length;
|
|
347
500
|
|
|
348
501
|
// 4. Add all devices in the list
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
502
|
+
// only pusk if available
|
|
503
|
+
if (this.config.listOnlyBattery) {
|
|
504
|
+
if (deviceBatteryState !== null || shortDeviceBatteryState !== null) {
|
|
505
|
+
this.listAllDevices.push(
|
|
506
|
+
{
|
|
507
|
+
Device: deviceName,
|
|
508
|
+
Adapter: deviceAdapterName,
|
|
509
|
+
Battery: batteryHealth,
|
|
510
|
+
Last_contact: lastContactString,
|
|
511
|
+
Link_quality: linkQuality
|
|
512
|
+
}
|
|
513
|
+
);
|
|
356
514
|
}
|
|
357
|
-
)
|
|
515
|
+
} else if (!this.config.listOnlyBattery) {
|
|
516
|
+
this.listAllDevices.push(
|
|
517
|
+
{
|
|
518
|
+
Device: deviceName,
|
|
519
|
+
Adapter: deviceAdapterName,
|
|
520
|
+
Battery: batteryHealth,
|
|
521
|
+
Last_contact: lastContactString,
|
|
522
|
+
Link_quality: linkQuality
|
|
523
|
+
}
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
// 4a. Count how many devices are exists
|
|
529
|
+
this.deviceCounter = this.listAllDevices.length;
|
|
358
530
|
}
|
|
359
531
|
} //<--End of second loop
|
|
360
532
|
} //<---End of main loop
|
|
533
|
+
this.log.debug(`Function finished: ${this.main.name}`);
|
|
534
|
+
}
|
|
361
535
|
|
|
536
|
+
async sendNotifications() {
|
|
362
537
|
/*=============================================
|
|
363
538
|
= Notifications =
|
|
364
539
|
=============================================*/
|
|
540
|
+
this.log.debug(`Start the function: ${this.sendNotifications.name}`);
|
|
541
|
+
|
|
542
|
+
const pushover = {
|
|
543
|
+
instance: this.config.instancePushover,
|
|
544
|
+
title: this.config.titlePushover,
|
|
545
|
+
device: this.config.devicePushover
|
|
546
|
+
|
|
547
|
+
};
|
|
548
|
+
const telegram = {
|
|
549
|
+
instance: this.config.instanceTelegram,
|
|
550
|
+
user: this.config.deviceTelegram,
|
|
551
|
+
chatId: this.config.chatIdTelegram
|
|
552
|
+
};
|
|
553
|
+
const email = {
|
|
554
|
+
instance: this.config.instanceEmail,
|
|
555
|
+
subject: this.config.subjectEmail,
|
|
556
|
+
sendTo: this.config.sendToEmail
|
|
557
|
+
|
|
558
|
+
};
|
|
559
|
+
const jarvis = {
|
|
560
|
+
instance: this.config.instanceJarvis,
|
|
561
|
+
title: this.config.titleJarvis
|
|
562
|
+
|
|
563
|
+
};
|
|
564
|
+
const lovelace = {
|
|
565
|
+
instance: this.config.instanceLovelace,
|
|
566
|
+
title: this.config.titleLovelace
|
|
567
|
+
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
const choosedDays = {
|
|
571
|
+
monday: this.config.checkMonday,
|
|
572
|
+
tuesday: this.config.checkTuesday,
|
|
573
|
+
wednesday: this.config.checkWednesday,
|
|
574
|
+
thursday: this.config.checkThursday,
|
|
575
|
+
friday: this.config.checkFriday,
|
|
576
|
+
saturday: this.config.checkSaturday,
|
|
577
|
+
sunday: this.config.checkSunday,
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
const sendPushover = async (text) => {
|
|
581
|
+
await this.sendToAsync(pushover.instance, 'send', {
|
|
582
|
+
message: text,
|
|
583
|
+
title: pushover.title,
|
|
584
|
+
device: pushover.device
|
|
585
|
+
});
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
const sendTelegram = async (text) => {
|
|
589
|
+
await this.sendToAsync(telegram.instance, 'send', {
|
|
590
|
+
text: text,
|
|
591
|
+
user: telegram.user,
|
|
592
|
+
chatId: telegram.chatId
|
|
593
|
+
});
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
const sendEmail = async (text) => {
|
|
597
|
+
await this.sendToAsync(email.instance, 'send', {
|
|
598
|
+
sendTo: email.sendTo,
|
|
599
|
+
text: text,
|
|
600
|
+
subject: email.subject
|
|
601
|
+
});
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
const sendJarvis = async (text) => {
|
|
605
|
+
await this.setForeignStateAsync(`${jarvis.instance}.addNotification`, text);
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
const sendLovelace = async (text) => {
|
|
609
|
+
await this.setForeignStateAsync(`${lovelace.instance}.notifications.add`, text);
|
|
610
|
+
};
|
|
365
611
|
|
|
366
612
|
/*---------- oflline notification ----------*/
|
|
367
613
|
if(this.config.checkSendOfflineMsg) {
|
|
368
614
|
try {
|
|
369
615
|
let msg = '';
|
|
370
|
-
const offlineDevicesCountOld = await this.
|
|
371
|
-
|
|
372
|
-
if ((
|
|
373
|
-
if (
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
616
|
+
const offlineDevicesCountOld = await this.getOwnInitValue('offlineCount');
|
|
617
|
+
|
|
618
|
+
if ((this.offlineDevicesCount != offlineDevicesCountOld) && (this.offlineDevicesCount != 0)) {
|
|
619
|
+
if (this.offlineDevicesCount == 1) {
|
|
620
|
+
msg = 'Folgendes Gerät ist seit einiger Zeit nicht erreichbar: \n';
|
|
621
|
+
} else if (this.offlineDevicesCount >= 2) {
|
|
622
|
+
msg = 'Folgende ' + this.offlineDevicesCount + ' Geräte sind seit einiger Zeit nicht erreichbar: \n';
|
|
623
|
+
}
|
|
624
|
+
for (const id of this.offlineDevices) {
|
|
625
|
+
msg = msg + '\n' + id['Device'] + ' ' + /*id['room'] +*/ ' (' + id['Last_contact'] + ')';
|
|
626
|
+
}
|
|
627
|
+
this.log.info(msg);
|
|
628
|
+
await this.setStateAsync('lastNotification', msg, true);
|
|
629
|
+
if (pushover.instance) {
|
|
630
|
+
try {
|
|
631
|
+
await sendPushover(msg);
|
|
632
|
+
} catch (e) {
|
|
633
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
381
634
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
this.log.warn ('Getting error at sending notification' + (e));
|
|
389
|
-
}
|
|
635
|
+
}
|
|
636
|
+
if (telegram.instance) {
|
|
637
|
+
try {
|
|
638
|
+
await sendTelegram(msg);
|
|
639
|
+
} catch (e) {
|
|
640
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
390
641
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
642
|
+
}
|
|
643
|
+
if (email.instance) {
|
|
644
|
+
try {
|
|
645
|
+
await sendEmail(msg);
|
|
646
|
+
} catch (e) {
|
|
647
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
397
648
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
}
|
|
649
|
+
}
|
|
650
|
+
if (jarvis.instance) {
|
|
651
|
+
try {
|
|
652
|
+
await sendJarvis('{"title":"'+ jarvis.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')","message":" ' + this.offlineDevicesCount + ' Geräte sind nicht erreichbar","display": "drawer"}');
|
|
653
|
+
} catch (e) {
|
|
654
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
404
655
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
656
|
+
}
|
|
657
|
+
if (lovelace.instance) {
|
|
658
|
+
try {
|
|
659
|
+
await sendLovelace('{"message":" ' + this.offlineDevicesCount + ' Geräte sind nicht erreichbar", "title":"'+ lovelace.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')"}');
|
|
660
|
+
} catch (e) {
|
|
661
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
411
662
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
663
|
}
|
|
415
664
|
}
|
|
416
|
-
|
|
417
665
|
} catch (e) {
|
|
418
|
-
this.log.debug(
|
|
666
|
+
this.log.debug(`Getting error at sending offline notification ${e}`);
|
|
419
667
|
}
|
|
420
|
-
|
|
421
668
|
}
|
|
422
669
|
|
|
423
670
|
/*---------- Low battery Notification ----------*/
|
|
@@ -444,122 +691,139 @@ class DeviceWatcher extends utils.Adapter {
|
|
|
444
691
|
|
|
445
692
|
if (this.config.checkSendBatteryMsg) {
|
|
446
693
|
try {
|
|
447
|
-
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
if ((
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
if (id['
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
infotext = infotext + '\n' + id['Device'] + ' ' + /*id['room'] +*/ ' (' + id['Battery'] + ')'.split(', ');
|
|
462
|
-
++batteryMinCount;
|
|
463
|
-
}
|
|
694
|
+
const lastBatteryNotifyIndicator = await this.getOwnInitValue('info.lastBatteryNotification');
|
|
695
|
+
const batteryWarningMin = this.config.minWarnBatterie;
|
|
696
|
+
|
|
697
|
+
if (now.getHours() < 11) {await this.setStateAsync('info.lastBatteryNotification', false, true);} //Nur einmal abfragen
|
|
698
|
+
if ((now.getHours() > 11) && (!lastBatteryNotifyIndicator) && (checkToday != undefined)){
|
|
699
|
+
let batteryMinCount = 0;
|
|
700
|
+
let infotext = '';
|
|
701
|
+
|
|
702
|
+
for (const id of this.batteryPowered) {
|
|
703
|
+
if (id['Battery']) {
|
|
704
|
+
const batteryValue = parseFloat(id['Battery'].replace('%', ''));
|
|
705
|
+
if ((batteryValue < batteryWarningMin) && (id['Adapter'] != 'homematic')) {
|
|
706
|
+
infotext = infotext + '\n' + id['Device'] + ' ' + /*id['room'] +*/ ' (' + id['Battery'] + ')'.split(', ');
|
|
707
|
+
++batteryMinCount;
|
|
464
708
|
}
|
|
465
709
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
710
|
+
}
|
|
711
|
+
if (batteryMinCount > 0) {
|
|
712
|
+
this.log.info(`Batteriezustände: ${infotext}`);
|
|
713
|
+
await this.setStateAsync('lastNotification', infotext, true);
|
|
714
|
+
|
|
715
|
+
if (pushover.instance) {
|
|
716
|
+
try {
|
|
717
|
+
await sendPushover(`Batteriezustände: ${infotext}`);
|
|
718
|
+
} catch (e) {
|
|
719
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
475
720
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
}
|
|
721
|
+
}
|
|
722
|
+
if (telegram.instance) {
|
|
723
|
+
try {
|
|
724
|
+
await sendTelegram(`Batteriezustände: ${infotext}`);
|
|
725
|
+
} catch (e) {
|
|
726
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
482
727
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
728
|
+
}
|
|
729
|
+
if (email.instance) {
|
|
730
|
+
try {
|
|
731
|
+
await sendEmail(`Batteriezustände: ${infotext}`);
|
|
732
|
+
} catch (e) {
|
|
733
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
if (jarvis.instance) {
|
|
737
|
+
try {
|
|
738
|
+
await sendJarvis('{"title":"'+ jarvis.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')","message":" ' + batteryMinCount + ' Geräte mit schwacher Batterie","display": "drawer"}');
|
|
739
|
+
} catch (e) {
|
|
740
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
489
741
|
}
|
|
490
|
-
await this.setStateAsync('info.lastBatteryNotification', true, true);
|
|
491
742
|
}
|
|
743
|
+
if (lovelace.instance) {
|
|
744
|
+
try {
|
|
745
|
+
await sendLovelace('{"message":" ' + batteryMinCount + ' Geräte mit schwacher Batterie", "title":"'+ lovelace.title +' (' + this.formatDate(new Date(), 'DD.MM.YYYY - hh:mm:ss') + ')"}');
|
|
746
|
+
} catch (e) {
|
|
747
|
+
this.log.warn (`Getting error at sending notification ${e}`);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
await this.setStateAsync('info.lastBatteryNotification', true, true);
|
|
492
752
|
}
|
|
493
753
|
}
|
|
494
754
|
} catch (e) {
|
|
495
|
-
this.log.debug(
|
|
755
|
+
this.log.debug(`Getting error at sending battery notification ${e}`);
|
|
496
756
|
}
|
|
497
757
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
758
|
/*===== End of Section notifications ======*/
|
|
759
|
+
this.log.debug(`Function finished: ${this.sendNotifications.name}`);
|
|
760
|
+
}
|
|
501
761
|
|
|
502
|
-
|
|
762
|
+
async writeDatapoints() {
|
|
503
763
|
/*=============================================
|
|
504
764
|
= Write Datapoints =
|
|
505
765
|
=============================================*/
|
|
506
|
-
this.log.debug(
|
|
766
|
+
this.log.debug(`Start the function: ${this.writeDatapoints.name}`);
|
|
507
767
|
|
|
508
768
|
try {
|
|
509
|
-
await this.setStateAsync('offlineCount', {val: offlineDevicesCount, ack: true});
|
|
510
|
-
await this.setStateAsync('countAll', {val: deviceCounter, ack: true});
|
|
511
|
-
await this.setStateAsync('batteryCount', {val: batteryPoweredCount, ack: true});
|
|
512
|
-
await this.setStateAsync('lowBatteryCount', {val: lowBatteryPoweredCount, ack: true});
|
|
769
|
+
await this.setStateAsync('offlineCount', {val: this.offlineDevicesCount, ack: true});
|
|
770
|
+
await this.setStateAsync('countAll', {val: this.deviceCounter, ack: true});
|
|
771
|
+
await this.setStateAsync('batteryCount', {val: this.batteryPoweredCount, ack: true});
|
|
772
|
+
await this.setStateAsync('lowBatteryCount', {val: this.lowBatteryPoweredCount, ack: true});
|
|
513
773
|
|
|
514
|
-
if (deviceCounter == 0) {
|
|
515
|
-
|
|
516
|
-
arrListAllDevices = [{Device: '--keine--', Adapter: '', Battery: '', Last_contact: '', Link_quality: ''}]; //JSON-Info Gesamtliste mit Info je Gerät
|
|
774
|
+
if (this.deviceCounter == 0) {
|
|
775
|
+
this.listAllDevices = [{Device: '--keine--', Adapter: '', Battery: '', Last_contact: '', Link_quality: ''}]; //JSON-Info Gesamtliste mit Info je Gerät
|
|
517
776
|
|
|
518
|
-
await this.setStateAsync('
|
|
519
|
-
await this.setStateAsync('listAll', {val: JSON.stringify(arrListAllDevices), ack: true});
|
|
777
|
+
await this.setStateAsync('listAll', {val: JSON.stringify(this.listAllDevices), ack: true});
|
|
520
778
|
} else {
|
|
521
|
-
await this.setStateAsync('
|
|
522
|
-
await this.setStateAsync('listAll', {val: JSON.stringify(arrListAllDevices), ack: true});
|
|
779
|
+
await this.setStateAsync('listAll', {val: JSON.stringify(this.listAllDevices), ack: true});
|
|
523
780
|
}
|
|
524
781
|
|
|
525
|
-
if (
|
|
526
|
-
|
|
782
|
+
if (this.linkQualityCount == 0) {
|
|
783
|
+
this.linkQualityDevices = [{Device: '--keine--', Adapter: '', Link_quality: ''}]; //JSON-Info alle mit LinkQuality
|
|
527
784
|
|
|
528
|
-
await this.setStateAsync('
|
|
785
|
+
await this.setStateAsync('linkQualityList', {val: JSON.stringify(this.linkQualityDevices), ack: true});
|
|
529
786
|
} else {
|
|
530
|
-
await this.setStateAsync('
|
|
787
|
+
await this.setStateAsync('linkQualityList', {val: JSON.stringify(this.linkQualityDevices), ack: true});
|
|
531
788
|
}
|
|
532
789
|
|
|
533
|
-
if (batteryPoweredCount == 0) {
|
|
534
|
-
arrBatteryPowered = [{Device: '--keine--', Adapter: '', Battery: ''}]; //JSON-Info alle batteriebetriebenen Geräte
|
|
535
790
|
|
|
536
|
-
|
|
791
|
+
if (this.offlineDevicesCount == 0) {
|
|
792
|
+
this.offlineDevices = [{Device: '--keine--', Adapter: '', Last_contact: ''}]; //JSON-Info alle offline-Geräte = 0
|
|
793
|
+
|
|
794
|
+
await this.setStateAsync('offlineList', {val: JSON.stringify(this.offlineDevices), ack: true});
|
|
795
|
+
} else {
|
|
796
|
+
await this.setStateAsync('offlineList', {val: JSON.stringify(this.offlineDevices), ack: true});
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
if (this.batteryPoweredCount == 0) {
|
|
800
|
+
this.batteryPowered = [{Device: '--keine--', Adapter: '', Battery: ''}]; //JSON-Info alle batteriebetriebenen Geräte
|
|
801
|
+
|
|
802
|
+
await this.setStateAsync('batteryList', {val: JSON.stringify(this.batteryPowered), ack: true});
|
|
537
803
|
} else {
|
|
538
|
-
await this.setStateAsync('batteryList', {val: JSON.stringify(
|
|
804
|
+
await this.setStateAsync('batteryList', {val: JSON.stringify(this.batteryPowered), ack: true});
|
|
539
805
|
}
|
|
540
806
|
|
|
541
|
-
if (lowBatteryPoweredCount == 0) {
|
|
542
|
-
|
|
807
|
+
if (this.lowBatteryPoweredCount == 0) {
|
|
808
|
+
this.batteryLowPowered = [{Device: '--keine--', Adapter: '', Battery: ''}]; //JSON-Info alle batteriebetriebenen Geräte
|
|
543
809
|
|
|
544
|
-
await this.setStateAsync('lowBatteryList', {val: JSON.stringify(
|
|
810
|
+
await this.setStateAsync('lowBatteryList', {val: JSON.stringify(this.batteryLowPowered), ack: true});
|
|
545
811
|
} else {
|
|
546
|
-
await this.setStateAsync('lowBatteryList', {val: JSON.stringify(
|
|
812
|
+
await this.setStateAsync('lowBatteryList', {val: JSON.stringify(this.batteryLowPowered), ack: true});
|
|
547
813
|
}
|
|
548
814
|
|
|
549
815
|
//Zeitstempel wann die Datenpunkte zuletzt gecheckt wurden
|
|
550
816
|
const lastCheck = this.formatDate(new Date(), 'DD.MM.YYYY') + ' - ' + this.formatDate(new Date(), 'hh:mm:ss');
|
|
551
817
|
await this.setStateAsync('lastCheck', lastCheck, true);
|
|
552
|
-
|
|
553
|
-
this.log.debug('write the datapoints finished ' + this.main.name);
|
|
554
818
|
}
|
|
555
819
|
catch (e) {
|
|
556
|
-
this.log.error(
|
|
820
|
+
this.log.error(`(05) Error while writing the states ${e}`);
|
|
557
821
|
}
|
|
558
822
|
/*===== End of writing Datapoints ======*/
|
|
559
|
-
|
|
560
|
-
this.log.debug('Function finished: ' + this.main.name);
|
|
823
|
+
this.log.debug(`Function finished: ${this.writeDatapoints.name}`);
|
|
561
824
|
}
|
|
562
825
|
|
|
826
|
+
|
|
563
827
|
onUnload(callback) {
|
|
564
828
|
try {
|
|
565
829
|
this.log.info('cleaned everything up...');
|
|
@@ -579,4 +843,4 @@ if (require.main !== module) {
|
|
|
579
843
|
} else {
|
|
580
844
|
// otherwise start the instance directly
|
|
581
845
|
new DeviceWatcher();
|
|
582
|
-
}
|
|
846
|
+
}
|