iobroker.device-watcher 2.13.1 → 2.14.5

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/lib/tools.js CHANGED
@@ -1,155 +1,158 @@
1
-
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 cronParser = require('cron-parser');
33
- const interval = cronParser.parseExpression(lastCronRun);
34
- const previous = interval.prev();
35
- return Math.floor(Date.now() - previous.getTime()); // in ms
36
- } catch (error) {
37
- adaptr.log.error(`[getPreviousCronRun] - ${error}`);
38
- }
39
- }
40
-
41
- /**
42
- * @param {object} obj - State of datapoint
43
- */
44
- async function getInitValue(adaptr, obj) {
45
- //state can be null or undefinded
46
- const foreignState = await adaptr.getForeignStateAsync(obj);
47
- if (foreignState) return foreignState.val;
48
- }
49
-
50
-
51
- /**
52
- * @param {string} id - id which should be capitalize
53
- */
54
- function capitalize(id) {
55
- //make the first letter uppercase
56
- return id && id[0].toUpperCase() + id.slice(1);
57
- }
58
-
59
- /**
60
- * @param {number} dpValue - get Time of this datapoint
61
- */
62
- function getTimestamp(dpValue) {
63
- const time = new Date();
64
- return (dpValue = Math.round((time.getTime() - dpValue) / 1000 / 60));
65
- }
66
-
67
- /**
68
- * @param {string} dp - get Time of this datapoint
69
- * @param {number} ms - milliseconds
70
- */
71
- async function getTimestampConnectionDP(adaptr, dp, ms) {
72
- const time = new Date();
73
- const dpValue = await adaptr.getForeignStateAsync(dp);
74
- if (dpValue) {
75
- if (!dpValue.val) return false;
76
-
77
- const dpLastStateChange = Math.round(time.getTime() - dpValue.lc); // calculate in ms
78
- if (dpLastStateChange >= ms) {
79
- return true;
80
- } else {
81
- return false;
82
- }
83
- }
84
- }
85
-
86
- /**
87
- * Count devices for each type
88
- */
89
- async function countDevices(adaptr) {
90
- // Count how many devices with link Quality
91
- adaptr.linkQualityCount = adaptr.linkQualityDevices.length;
92
-
93
- // Count how many devcies are offline
94
- adaptr.offlineDevicesCount = adaptr.offlineDevices.length;
95
-
96
- // Count how many devices are with battery
97
- adaptr.batteryPoweredCount = adaptr.batteryPowered.length;
98
-
99
- // 3d. Count how many devices are with low battery
100
- adaptr.lowBatteryPoweredCount = adaptr.batteryLowPowered.length;
101
-
102
- // Count how many devices are exists
103
- adaptr.deviceCounter = adaptr.listAllDevices.length;
104
-
105
- // Count how many devices has update available
106
- adaptr.upgradableDevicesCount = adaptr.upgradableList.length;
107
- }
108
-
109
-
110
- /**
111
- * when was last contact of device
112
- */
113
- async function checkLastContact(adaptr) {
114
- for (const [deviceID, deviceData] of adaptr.listAllDevicesRaw.entries()) {
115
- if (deviceData.instancedeviceConnected !== false) {
116
- const oldContactState = deviceData.Status;
117
- deviceData.UnreachState = await this.getInitValue(adaptr,deviceData.UnreachDP);
118
- const contactData = await adaptr.getOnlineState(
119
- deviceData.timeSelector,
120
- deviceData.adapterID,
121
- deviceData.UnreachDP,
122
- deviceData.SignalStrength,
123
- deviceData.UnreachState,
124
- deviceData.deviceStateSelectorHMRPC,
125
- deviceData.rssiPeerSelectorHMRPC,
126
- );
127
- if (contactData !== undefined && contactData !== null) {
128
- deviceData.LastContact = contactData[0];
129
- deviceData.Status = contactData[1];
130
- deviceData.linkQuality = contactData[2];
131
- }
132
- if (adaptr.config.checkSendOfflineMsg && oldContactState !== deviceData.Status && !adaptr.blacklistNotify.includes(deviceData.Path)) {
133
- // check if the generally deviceData connected state is for a while true
134
- if (await this.getTimestampConnectionDP(adaptr, deviceData.instanceDeviceConnectionDP, 50000)) {
135
- await adaptr.sendStateNotifications('Devices', 'onlineStateDevice', deviceID);
136
- }
137
- }
138
- }
139
- }
140
- }
141
-
142
-
143
- module.exports = {
144
- isDisabledDevice,
145
- parseData,
146
- getPreviousCronRun,
147
- getInitValue,
148
- capitalize,
149
- getTimestamp,
150
- getTimestampConnectionDP,
151
- countDevices,
152
- checkLastContact,
153
-
154
- };
155
-
1
+ const adapterArray = require('./adapterArray');
2
+
3
+ /**
4
+ *
5
+ * @param adaptr
6
+ * @param unreachDP
7
+ * @param treeDP
8
+ */
9
+ async function isDisabledDevice(adaptr, treeDP) {
10
+ let isDisabled = false;
11
+
12
+ const device = await adaptr.getForeignObject(treeDP);
13
+
14
+ if (device.native.deviceRemoved == true || device.common.desc.includes('disabled') || device.common.desc.includes('Deaktiviert') || device.common.desc.includes('disabled')) {
15
+ isDisabled = true;
16
+ }
17
+ return isDisabled;
18
+ }
19
+
20
+ /**
21
+ * @param {object} data - object
22
+ */
23
+ function parseData(data) {
24
+ if (!data) {
25
+ return {};
26
+ }
27
+ if (typeof data === 'object') {
28
+ return data;
29
+ }
30
+ if (typeof data === 'string') {
31
+ return JSON.parse(data);
32
+ }
33
+ return {};
34
+ }
35
+
36
+ /**
37
+ * @param adaptr
38
+ * @param {object} obj - State of datapoint
39
+ */
40
+ async function getInitValue(adaptr, obj) {
41
+ //state can be null or undefinded
42
+ const foreignState = await adaptr.getForeignStateAsync(obj);
43
+ if (foreignState) {
44
+ return foreignState.val;
45
+ }
46
+ }
47
+
48
+ /**
49
+ * @param {string} id - id which should be capitalize
50
+ */
51
+ function capitalize(id) {
52
+ //make the first letter uppercase
53
+ return id && id[0].toUpperCase() + id.slice(1);
54
+ }
55
+
56
+ /**
57
+ * @param {number} dpValue - get Time of this datapoint
58
+ */
59
+ function getTimestamp(dpValue) {
60
+ const time = new Date();
61
+ return (dpValue = Math.round((time.getTime() - dpValue) / 1000));
62
+ }
63
+
64
+ /**
65
+ * @param adaptr
66
+ * @param {string} dp - get Time of this datapoint
67
+ * @param {number} ms - milliseconds
68
+ */
69
+ async function getTimestampConnectionDP(adaptr, dp, ms) {
70
+ const time = new Date();
71
+ const dpValue = await adaptr.getForeignStateAsync(dp);
72
+ if (dpValue) {
73
+ if (!dpValue.val) {
74
+ return false;
75
+ }
76
+
77
+ const dpLastStateChange = Math.round(time.getTime() - dpValue.lc); // calculate in ms
78
+ if (dpLastStateChange >= ms) {
79
+ return true;
80
+ }
81
+ return false;
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Count devices for each type
87
+ *
88
+ * @param adaptr
89
+ */
90
+ async function countDevices(adaptr) {
91
+ // Count how many devices with link Quality
92
+ adaptr.linkQualityCount = adaptr.linkQualityDevices.length;
93
+
94
+ // Count how many devcies are offline
95
+ adaptr.offlineDevicesCount = adaptr.offlineDevices.length;
96
+
97
+ // Count how many devices are with battery
98
+ adaptr.batteryPoweredCount = adaptr.batteryPowered.length;
99
+
100
+ // 3d. Count how many devices are with low battery
101
+ adaptr.lowBatteryPoweredCount = adaptr.batteryLowPowered.length;
102
+
103
+ // Count how many devices are exists
104
+ adaptr.deviceCounter = adaptr.listAllDevices.length;
105
+
106
+ // Count how many devices has update available
107
+ adaptr.upgradableDevicesCount = adaptr.upgradableList.length;
108
+ }
109
+
110
+ /**
111
+ * when was last contact of device
112
+ *
113
+ * @param adaptr
114
+ */
115
+ async function checkLastContact(adaptr) {
116
+ for (const [deviceID, deviceData] of adaptr.listAllDevicesRaw.entries()) {
117
+ if (deviceData.instanceDeviceConnected !== false && deviceData.instanceDeviceConnected != undefined) {
118
+ deviceData.UnreachState = await this.getInitValue(adaptr, deviceData.UnreachDP);
119
+
120
+ const gefundenerAdapter = Object.values(adapterArray).find((adapter) => adapter.adapterID === deviceData.adapterID);
121
+ const silentEnabled = Object.values(adaptr.config.tableDevices).find((adapter) => adapter.adapterKey === gefundenerAdapter.adapterKey);
122
+
123
+ const oldContactState = deviceData.Status;
124
+
125
+ const contactData = await adaptr.getOnlineState(
126
+ deviceData.timeSelector,
127
+ deviceData.adapterID,
128
+ deviceData.UnreachDP,
129
+ deviceData.SignalStrength,
130
+ deviceData.UnreachState,
131
+ deviceData.deviceStateSelectorHMRPC,
132
+ deviceData.rssiPeerSelectorHMRPC,
133
+ );
134
+ if (contactData !== undefined || contactData !== null) {
135
+ deviceData.LastContact = contactData[0];
136
+ deviceData.Status = contactData[1];
137
+ deviceData.linkQuality = contactData[2];
138
+ }
139
+ if (adaptr.config.checkSendOfflineMsg && oldContactState !== deviceData.Status && !adaptr.blacklistNotify.includes(deviceData.Path)) {
140
+ // check if the generally deviceData connected state is for a while true
141
+ if (await this.getTimestampConnectionDP(adaptr, deviceData.instanceDeviceConnectionDP, 50000)) {
142
+ await adaptr.sendStateNotifications('Devices', 'onlineStateDevice', deviceID, silentEnabled.telegramSilent);
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+
149
+ module.exports = {
150
+ isDisabledDevice,
151
+ parseData,
152
+ getInitValue,
153
+ capitalize,
154
+ getTimestamp,
155
+ getTimestampConnectionDP,
156
+ countDevices,
157
+ checkLastContact,
158
+ };
@@ -542,6 +542,32 @@ const translations = {
542
542
  uk: 'годин',
543
543
  'zh-cn': '小时',
544
544
  },
545
+ minits: {
546
+ en: 'minutes',
547
+ de: 'Minuten',
548
+ ru: 'протокол',
549
+ pt: 'ata',
550
+ nl: 'minuten',
551
+ fr: 'minutes',
552
+ it: 'minuti',
553
+ es: 'minutos',
554
+ pl: 'minuty',
555
+ uk: 'хвилини',
556
+ 'zh-cn': '分钟',
557
+ },
558
+ secs: {
559
+ en: 'seconds',
560
+ de: 'Sekunden',
561
+ ru: 'секунды',
562
+ pt: 'segundos',
563
+ nl: 'seconden',
564
+ fr: 'secondes',
565
+ it: 'secondi',
566
+ es: 'segundos',
567
+ pl: 'sekundy',
568
+ uk: 'секунд',
569
+ 'zh-cn': '秒',
570
+ },
545
571
  Link_quality_devices: {
546
572
  en: 'Link quality devices',
547
573
  de: 'Signalstärke Geräte',