iobroker.sprinklecontrol 0.2.19 → 1.0.0

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,148 +1,211 @@
1
- 'use strict';
2
-
3
-
4
- /**
5
- * func addTime (02:12:24 + 00:15) || (15)sek => 02:12:39
6
- *
7
- * @param time1 - z.B. 02:12:24 || 7944
8
- * @param time2 -z.B. 00:15 || 15 | Null
9
- * @returns - (h > 0) ? 02:12:39 : 12:39
10
- */
11
- function addTime(time1, time2){
12
- const wert = string2seconds(time1) + string2seconds(time2);
13
- return seconds2string(wert);
14
-
15
- // private functions
16
- function seconds2string(n){
17
- n = Math.abs(n);
18
- const h = Math.trunc(n / 3600);
19
- const m = Math.trunc((n / 60 ) % 60);
20
- const sec = Math.trunc(n % 60);
21
- return (h === 0)?(`${frmt(m)}:${frmt(sec)}`):(`${frmt(h)}:${frmt(m)}:${frmt(sec)}`);
22
- } // end function seconds2string
23
-
24
- function string2seconds(n) {
25
- if(!n || (n === '--:--')) return 0;
26
- if(Number.isInteger(n)) return n;
27
- const tmp = n.split(':').reverse();
28
- if(!tmp.length) tmp[0] = 0; // Sekunden
29
- if(tmp.length < 2) tmp[1] = 0; // Minuten
30
- if(tmp.length < 3) tmp[2] = 0; // Stunden
31
- while(tmp[0] > 59) {
32
- tmp[0] -= 60;
33
- ++tmp[1];
34
- }
35
- while(tmp[1] > 59) {
36
- tmp[1] -= 60;
37
- ++tmp[2];
38
- }
39
- return (tmp[2] * 3600 + tmp[1] * 60 + 1 * tmp[0]);
40
- } // string2seconds
41
-
42
- function frmt(n) {
43
- return n < 10 ? `0${n}` : n;
44
- }
45
-
46
- } // end - function addTime
47
-
48
-
49
- /**
50
- * func Format Time
51
- * hier wird der übergebene Zeitstempel, myDate, in das angegebene Format, timeFormat, umgewandelt.
52
- * Ist myDate nicht angegeben, so wird die aktuelle Zeit verwendet.
53
- *
54
- * @param adapter
55
- * @param myDate
56
- * @param timeFormat - 'kW': Rückgabe der KW;
57
- * - 'dayNr': Tag des Jahres (Tagesnummer)
58
- * - 'day': Wochentag
59
- * - 'dd.mm. hh:mm': Rückgabe Datum und Zeit
60
- * - 'hh:mm': Rückgabe der Zeit
61
- * - 'past': true => Heute schon vorbei
62
- * @returns
63
- */
64
- function formatTime(adapter, myDate, timeFormat) { // 'kW' 'dd.mm. hh:mm' 'hh:mm'
65
- /**
66
- * @param s
67
- */
68
- function zweiStellen (s) {
69
- while (s.toString().length < 2) {
70
- s = `0${s}`;
71
- }
72
- return s;
73
- }
74
-
75
- const d = (myDate) ? new Date(myDate) : new Date();
76
- const tag = zweiStellen(d.getDate());
77
- const monat = zweiStellen(d.getMonth() + 1);
78
- const stunde = zweiStellen(d.getHours());
79
- const minute = zweiStellen(d.getMinutes());
80
- const currentThursday = new Date(d.getTime() +(3-((d.getDay()+6) % 7)) * 86400000);
81
- // At the beginning or end of a year the thursday could be in another year.
82
- const yearOfThursday = currentThursday.getFullYear();
83
- // Get first Thursday of the year
84
- const firstThursday = new Date(new Date(yearOfThursday,0,4).getTime() +(3-((new Date(yearOfThursday,0,4).getDay()+6) % 7)) * 86400000);
85
-
86
-
87
- switch (timeFormat) {
88
- // Berechnung der Tagesnummer
89
- case 'dayNr': {
90
- const start = new Date(d.getFullYear(), 0, 0);
91
- // @ts-ignore
92
- const diff = (d - start) + ((start.getTimezoneOffset() - d.getTimezoneOffset()) * 60 * 1000);
93
- const oneDay = 1000 * 60 * 60 * 24;
94
- return Math.floor(diff / oneDay);
95
- }
96
- // formatTime('','kW');
97
- case 'kW': {
98
- // +1 we start with week number 1
99
- // +0.5 an easy and dirty way to round result (in combination with Math.floor)
100
- return Math.floor(1 + 0.5 + (currentThursday.getTime() - firstThursday.getTime()) / 86400000/7);
101
- }
102
- // Wochentag 0:So;1:Mo;...6:Sa
103
- case 'day': {
104
- return d.getDay();
105
- }
106
- // 'hh:mm'
107
- case 'hh:mm': {
108
- return `${stunde}:${minute}`;
109
- }
110
- // 'dd.mm. hh:mm'
111
- case 'dd.mm. hh:mm': {
112
- return `${tag}.${monat} ${stunde}:${minute}`;
113
- }
114
- // true Heute schon vorbei
115
- case 'past': {
116
- const cd = new Date();
117
- const cStunde = zweiStellen(cd.getHours());
118
- const cMinute = zweiStellen(cd.getMinutes());
119
- return ((stunde > cStunde) || ((stunde === cStunde) && (minute >= cMinute)));
120
- }
121
-
122
- default: {
123
- adapter.log.info('function formatTime: falsches Format angegeben');
124
- break;
125
- }
126
- }
127
- }
128
-
129
- /**
130
- * TREND - Zwischenwert ermitteln
131
- *
132
- * @param a1 - Istwert a1 der Zeitachse
133
- * @param a2 - Istwert a2 der Zeitachse
134
- * @param b1 - Sollwert b1 der Wertachse
135
- * @param b2 - Sollwert b2 der Wertachse
136
- * @param sollA3 - Sollwert soll_A3 der Zeitachse
137
- * @returns Zwischenwert
138
- */
139
- function trend (a1,a2,b1,b2,sollA3) {
140
- return (sollA3-a1)*(b2-b1)/(a2-a1)+b1;
141
- }
142
-
143
-
144
- module.exports = {
145
- addTime,
146
- formatTime,
147
- trend
148
- };
1
+ 'use strict';
2
+
3
+ /**
4
+ * func addTime (02:12:24 + 00:15) || (807) => 02:12:39
5
+ *
6
+ * @param time1 {string|number} z.B. 02:12:24 || 807 => 02:12:39
7
+ * @param time2 {string|number|undefined} z.B. 02:12:24 || 807 => 02:12:39 || undef.
8
+ * @returns {string} z.B. mm:ss || hh:mm:ss
9
+ */
10
+ function addTime(time1, time2){
11
+ const wert = string2seconds(time1) + string2seconds(time2);
12
+ return seconds2string(wert);
13
+
14
+ // private functions
15
+ function seconds2string(n){
16
+ n = Math.abs(n);
17
+ const h = Math.trunc(n / 3600);
18
+ const m = Math.trunc((n / 60 ) % 60);
19
+ const sec = Math.trunc(n % 60);
20
+ return (h === 0)?(`${frmt(m)}:${frmt(sec)}`):(`${frmt(h)}:${m}:${frmt(sec)}`);
21
+ } // end function seconds2string
22
+
23
+ function string2seconds(n) {
24
+ if(!n || (n === '--:--')) return 0;
25
+ if(Number.isInteger(n)) return n;
26
+ const tmp = n.split(':').reverse();
27
+ if(!tmp.length) tmp[0] = 0; // Sekunden
28
+ if(tmp.length < 2) tmp[1] = 0; // Minuten
29
+ if(tmp.length < 3) tmp[2] = 0; // Stunden
30
+ while(tmp[0] > 59) {
31
+ tmp[0] -= 60;
32
+ ++tmp[1];
33
+ }
34
+ while(tmp[1] > 59) {
35
+ tmp[1] -= 60;
36
+ ++tmp[2];
37
+ }
38
+ return (tmp[2] * 3600 + tmp[1] * 60 + 1 * tmp[0]);
39
+ } // string2seconds
40
+
41
+ function frmt(n) {
42
+ return n < 10 ? `0${ n }` : n;
43
+ }
44
+
45
+ } // end - function addTime
46
+
47
+
48
+ /**
49
+ * func Format Time
50
+ * hier wird der übergebene Zeitstempel, myDate, in das angegebene Format, timeFormat, umgewandelt.
51
+ * Ist myDate nicht angegeben, so wird die aktuelle Zeit verwendet.
52
+ *
53
+ * @param {Date=} myDate - wenn nicht angegeben; dann wird aktuelles Datum verwendet
54
+ * @returns {{dayNr: number; kW: number; day: number; dayTime: string; past: boolean}}
55
+ * - kW: (number) Rückgabe der KW;
56
+ * - dayNr: (number) Tag des Jahres (Tagesnummer)
57
+ * - day: (number) Wochentag
58
+ * - dd.mm. hh:mm: (string) Rückgabe Datum und Zeit
59
+ * - past: true => (boolean) Heute schon vorbei
60
+ */
61
+ function formatTime(myDate) { // 'kW' 'dd.mm. hh:mm'
62
+ function zweiStellen (s) {
63
+ while (s.toString().length < 2) {
64
+ s = `0${ s }`;
65
+ }
66
+ return s;
67
+ }
68
+ const d = (myDate)? new Date(myDate):new Date();
69
+ const tag = zweiStellen(d.getDate());
70
+ const monat = zweiStellen(d.getMonth() + 1);
71
+ const stunde = zweiStellen(d.getHours());
72
+ const minute = zweiStellen(d.getMinutes());
73
+ const currentThursday = new Date(d.getTime() +(3-((d.getDay()+6) % 7)) * 86400000);
74
+ // At the beginning or end of a year the thursday could be in another year.
75
+ const yearOfThursday = currentThursday.getFullYear();
76
+ // Get first Thursday of the year
77
+ const firstThursday = new Date(new Date(yearOfThursday,0,4).getTime() +(3-((new Date(yearOfThursday,0,4).getDay()+6) % 7)) * 86400000);
78
+
79
+ const start = new Date(d.getFullYear(), 0, 0);
80
+ const diff = (+d - +start) + ((start.getTimezoneOffset() - d.getTimezoneOffset()) * 60 * 1000);
81
+ const oneDay = 1000 * 60 * 60 * 24;
82
+ const dayNr = Math.floor(diff / oneDay);
83
+
84
+ // +1 we start with week number 1
85
+ // +0.5 an easy and dirty way to round result (in combination with Math.floor)
86
+ const kW = Math.floor(1 + 0.5 + (currentThursday.getTime() - firstThursday.getTime()) / 86400000/7);
87
+
88
+ // Wochentag 0:So;1:Mo;...6:Sa
89
+ const day = d.getDay();
90
+
91
+ // 'dd.mm. hh:mm':
92
+ const dayTime = `${tag}.${monat} ${stunde}:${minute}`;
93
+
94
+ // true → Heute schon vorbei
95
+ const cd = new Date();
96
+ const cStunde = zweiStellen(cd.getHours());
97
+ const cMinute = zweiStellen(cd.getMinutes());
98
+ const past = ((stunde > cStunde) || ((stunde === cStunde) && (minute > cMinute)));
99
+
100
+ return {
101
+ dayNr: dayNr,
102
+ kW: kW,
103
+ day: day,
104
+ dayTime: dayTime,
105
+ past: past
106
+ };
107
+ }
108
+
109
+ /**
110
+ * TREND - Zwischenwert ermitteln
111
+ *
112
+ * @param {number} a1 - Istwert a1 der Zeitachse
113
+ * @param {number} a2 - Istwert a2 der Zeitachse
114
+ * @param {number} b1 - Sollwert b1 der Wertachse
115
+ * @param {number} b2 - Sollwert b2 der Wertachse
116
+ * @param {number} sollA3 - Sollwert soll_A3 der Zeitachse
117
+ * @returns {number} Zwischenwert
118
+ */
119
+ function trend (a1,a2,b1,b2,sollA3) {
120
+ return (sollA3-a1)*(b2-b1)/(a2-a1)+b1;
121
+ }
122
+
123
+ // Signature of the callback
124
+ // type CallBackFind<T> = (
125
+ // value: T,
126
+ // index?: number,
127
+ // collection?: T[]
128
+ // ) => Promise<boolean>;
129
+
130
+ /**
131
+ * Async Find function
132
+ *
133
+ * You can use as follows
134
+ * const array = [1, 2, 3, 4];
135
+ * const output = await findAsync<number>(array, async (i) => {
136
+ * return Promise.resolve(i === 2);
137
+ * });
138
+ *
139
+ * @template T
140
+ * @param {T[]} elements
141
+ * @param {any} cb
142
+ * @returns {Promise<T | undefined>}
143
+ */
144
+
145
+ async function findAsync( elements, cb) {
146
+ for (const [index, element] of elements.entries()) {
147
+ if (await cb(element, index, elements)) {
148
+ return element;
149
+ }
150
+ }
151
+
152
+ return undefined;
153
+ }
154
+
155
+ // You can use as follows
156
+ // const array = [1, 2, 3, 4];
157
+
158
+ // const output = await findAsync<number>(array, async (i) => {
159
+ // return Promise.resolve(i === 2);
160
+ // });
161
+
162
+
163
+ /**
164
+ * sleep Funktion mit .cancel bzw .continues Token
165
+ *
166
+ * @param {number} ms - Zeit in Millisekunden
167
+ * @param {object} cancellationToken Objekt zur ansteuerung der inneren Funktion
168
+ * @returns {Promise <string>}
169
+ */
170
+ function sleep(ms, cancellationToken) {
171
+ return new Promise((resolve) => {
172
+ cancellationToken.clearEntireList = function() {
173
+ clearTimeout(timeout);
174
+ resolve('clearEntireList');
175
+ };
176
+ cancellationToken.boostKill = function() {
177
+ clearTimeout(timeout);
178
+ resolve('boostKill');
179
+ };
180
+ cancellationToken.ack = function() {
181
+ clearTimeout(timeout);
182
+ resolve('ack');
183
+ };
184
+ const timeout = setTimeout(() => {
185
+ resolve('resolved');
186
+ }, ms);
187
+ });
188
+ }
189
+ // async function beginTest() {
190
+ // try {
191
+ // const token = {};
192
+ // const promise = sleep(5000, token);
193
+ // await promise;
194
+ // // ... test code ...
195
+ // // ... return whatever;
196
+ // }
197
+ // catch(error) {
198
+ // console.log(error.message);
199
+ // // If button was clicked before the 5000 ms has expired,
200
+ // // and no other error has been thrown,
201
+ // // then the log will show "sleep() cancelled".
202
+ // throw error; // rethrow error to keep beginTest's caller informed.
203
+ // }
204
+
205
+ module.exports = {
206
+ addTime,
207
+ formatTime,
208
+ trend,
209
+ findAsync,
210
+ sleep
211
+ };