iobroker.device-watcher 2.12.3 → 2.12.4
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 +5 -0
- package/admin/jsonConfig.json5 +1 -1
- package/io-package.json +14 -14
- package/lib/arrApart.js +2 -0
- package/lib/crud.js +1942 -0
- package/lib/tools.js +153 -0
- package/main.js +217 -2243
- package/package.json +1 -1
package/lib/tools.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const cronParser = require('cron-parser');
|
|
2
|
+
async function isDisabledDevice(adaptr, unreachDP) {
|
|
3
|
+
let isDisabled = false;
|
|
4
|
+
|
|
5
|
+
const device = await adaptr.getForeignObject(unreachDP);
|
|
6
|
+
|
|
7
|
+
if (device.native.deviceRemoved == true || device.common.desc.includes('disabled') || device.common.desc.includes('Deaktiviert') || device.common.desc.includes('disabled')) {
|
|
8
|
+
isDisabled = true;
|
|
9
|
+
}
|
|
10
|
+
return isDisabled;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} data - object
|
|
16
|
+
*/
|
|
17
|
+
function parseData(data) {
|
|
18
|
+
if (!data) return {};
|
|
19
|
+
if (typeof data === 'object') return data;
|
|
20
|
+
if (typeof data === 'string') return JSON.parse(data);
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get previous run of cron job schedule
|
|
26
|
+
* Requires cron-parser!
|
|
27
|
+
* Inspired by https://stackoverflow.com/questions/68134104/
|
|
28
|
+
* @param {string} lastCronRun
|
|
29
|
+
*/
|
|
30
|
+
function getPreviousCronRun(adaptr, lastCronRun) {
|
|
31
|
+
try {
|
|
32
|
+
const interval = cronParser.parseExpression(lastCronRun);
|
|
33
|
+
const previous = interval.prev();
|
|
34
|
+
return Math.floor(Date.now() - previous.getTime()); // in ms
|
|
35
|
+
} catch (error) {
|
|
36
|
+
adaptr.log.error(`[getPreviousCronRun] - ${error}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {object} obj - State of datapoint
|
|
42
|
+
*/
|
|
43
|
+
async function getInitValue(adaptr, obj) {
|
|
44
|
+
//state can be null or undefinded
|
|
45
|
+
const foreignState = await adaptr.getForeignStateAsync(obj);
|
|
46
|
+
if (foreignState) return foreignState.val;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {string} id - id which should be capitalize
|
|
52
|
+
*/
|
|
53
|
+
function capitalize(id) {
|
|
54
|
+
//make the first letter uppercase
|
|
55
|
+
return id && id[0].toUpperCase() + id.slice(1);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {number} dpValue - get Time of this datapoint
|
|
60
|
+
*/
|
|
61
|
+
function getTimestamp(dpValue) {
|
|
62
|
+
const time = new Date();
|
|
63
|
+
return (dpValue = Math.round((time.getTime() - dpValue) / 1000 / 60));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {string} dp - get Time of this datapoint
|
|
68
|
+
* @param {number} ms - milliseconds
|
|
69
|
+
*/
|
|
70
|
+
async function getTimestampConnectionDP(adaptr, dp, ms) {
|
|
71
|
+
const time = new Date();
|
|
72
|
+
const dpValue = await adaptr.getForeignStateAsync(dp);
|
|
73
|
+
if (dpValue) {
|
|
74
|
+
if (!dpValue.val) return false;
|
|
75
|
+
|
|
76
|
+
const dpLastStateChange = Math.round(time.getTime() - dpValue.lc); // calculate in ms
|
|
77
|
+
if (dpLastStateChange >= ms) {
|
|
78
|
+
return true;
|
|
79
|
+
} else {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Count devices for each type
|
|
87
|
+
*/
|
|
88
|
+
async function countDevices(adaptr) {
|
|
89
|
+
// Count how many devices with link Quality
|
|
90
|
+
adaptr.linkQualityCount = adaptr.linkQualityDevices.length;
|
|
91
|
+
|
|
92
|
+
// Count how many devcies are offline
|
|
93
|
+
adaptr.offlineDevicesCount = adaptr.offlineDevices.length;
|
|
94
|
+
|
|
95
|
+
// Count how many devices are with battery
|
|
96
|
+
adaptr.batteryPoweredCount = adaptr.batteryPowered.length;
|
|
97
|
+
|
|
98
|
+
// 3d. Count how many devices are with low battery
|
|
99
|
+
adaptr.lowBatteryPoweredCount = adaptr.batteryLowPowered.length;
|
|
100
|
+
|
|
101
|
+
// Count how many devices are exists
|
|
102
|
+
adaptr.deviceCounter = adaptr.listAllDevices.length;
|
|
103
|
+
|
|
104
|
+
// Count how many devices has update available
|
|
105
|
+
adaptr.upgradableDevicesCount = adaptr.upgradableList.length;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* when was last contact of device
|
|
111
|
+
*/
|
|
112
|
+
async function checkLastContact(adaptr) {
|
|
113
|
+
for (const [deviceID, deviceData] of adaptr.listAllDevicesRaw.entries()) {
|
|
114
|
+
if (deviceData.instancedeviceConnected !== false) {
|
|
115
|
+
const oldContactState = deviceData.Status;
|
|
116
|
+
deviceData.UnreachState = await this.getInitValue(adaptr,deviceData.UnreachDP);
|
|
117
|
+
const contactData = await adaptr.getOnlineState(
|
|
118
|
+
deviceData.timeSelector,
|
|
119
|
+
deviceData.adapterID,
|
|
120
|
+
deviceData.UnreachDP,
|
|
121
|
+
deviceData.SignalStrength,
|
|
122
|
+
deviceData.UnreachState,
|
|
123
|
+
deviceData.deviceStateSelectorHMRPC,
|
|
124
|
+
deviceData.rssiPeerSelectorHMRPC,
|
|
125
|
+
);
|
|
126
|
+
if (contactData !== undefined && contactData !== null) {
|
|
127
|
+
deviceData.LastContact = contactData[0];
|
|
128
|
+
deviceData.Status = contactData[1];
|
|
129
|
+
deviceData.linkQuality = contactData[2];
|
|
130
|
+
}
|
|
131
|
+
if (adaptr.config.checkSendOfflineMsg && oldContactState !== deviceData.Status && !adaptr.blacklistNotify.includes(deviceData.Path)) {
|
|
132
|
+
// check if the generally deviceData connected state is for a while true
|
|
133
|
+
if (await this.getTimestampConnectionDP(adaptr, deviceData.instanceDeviceConnectionDP, 50000)) {
|
|
134
|
+
await adaptr.sendStateNotifications('Devices', 'onlineStateDevice', deviceID);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
module.exports = {
|
|
143
|
+
isDisabledDevice,
|
|
144
|
+
parseData,
|
|
145
|
+
getPreviousCronRun,
|
|
146
|
+
getInitValue,
|
|
147
|
+
capitalize,
|
|
148
|
+
getTimestamp,
|
|
149
|
+
getTimestampConnectionDP,
|
|
150
|
+
countDevices,
|
|
151
|
+
checkLastContact,
|
|
152
|
+
|
|
153
|
+
};
|