-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_helper.js
361 lines (310 loc) · 9.68 KB
/
smart_helper.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
module.exports = {
/**
* This functions converts a value into the given type.
* If a type is unknown the NodeRed function is used for conversation.
*
* Known types are:
* - "NOTHING", which will return always null
*/
evaluateNodeProperty(RED, value, type)
{
try
{
switch (type?.toUpperCase())
{
case null:
case "NOTHING":
return null;
default:
return RED.util.evaluateNodeProperty(value, type);
}
}
catch (error)
{
console.error({ value, type, error });
return null;
}
},
/**
* This functions returns the name that is set in a smart topic.
* The format is name#number
*
* If no name is found null is returned.
*/
getTopicName(topic)
{
if (typeof topic == "undefined" || topic == null || topic == "" || typeof topic != "string")
return null;
if (topic.indexOf("#") == -1)
return topic;
return topic.substring(0, topic.indexOf("#"));
},
/**
* This functions returns the number that is set in a smart topic.
* The format is name#number
*
* If no number is found null is returned.
*/
getTopicNumber(topic)
{
if (typeof topic == "undefined" || topic == null || topic == "")
return null;
if (typeof topic == "string")
{
if (topic.indexOf("#") != -1)
return parseInt(topic.substring(topic.indexOf("#") + 1), 10);
}
let result = parseInt(topic, 10);
if (isNaN(result) || !isFinite(result))
return null;
return result;
},
/**
* This functions extracts the topic name out of the given topic and checks if it is a valid topic.
* If not, the default topic is returned.
* @param {string} topic
* @param {string} defaultTopic
* @param {string[]} possibleTopics
* @returns The real topic that needs to be processes
*/
getRealTopic(topic, defaultTopic, possibleTopics)
{
let realTopic = this.getTopicName(topic);
if (possibleTopics.includes(realTopic))
return realTopic;
return defaultTopic;
},
/**
* This function converts the given value with the given unit into milli seconds.
*
* This units are available:
* - ms
* - s or sec
* - m or min
* - h
*/
getTimeInMs(value, unit, defaultValue = 0)
{
value = parseFloat(value);
if (isNaN(value) || value == 0)
return defaultValue;
switch (unit)
{
case "ms":
return value;
case "s":
case "sec":
return value * 1000;
case "min":
return value * 60 * 1000;
case "h":
return value * 3600 * 1000;
}
},
/**
* This function converts the given value with the given unit into seconds.
*
* This units are available:
* - ms
* - s or sec
* - m or min
* - h
*/
getTimeInS(value, unit)
{
value = parseInt(value, 10);
if (isNaN(value) || value == 0)
return 0;
switch (unit)
{
case "ms":
return value / 1000;
case "s":
return value;
case "min":
return value * 60;
case "h":
return value * 3600;
}
},
/**
* This function is parsing the given value and returns the time in milli seconds.
*
* The string can have a number followed by an optional unit.
* If no unit is given, the default is ms.
*
* This units are available:
* - ms
* - s or sec
* - m or min
* - h
*/
getTimeInMsFromString(value, defaultValue = 0)
{
// default in ms
if (typeof value == "number")
return value;
if (typeof value != "string")
return defaultValue;
// Split 123min into ["123", "min"]
let values = value.match(/^([0-9]+[,.]?[0-9]*)(ms|s|sec|m|min|h|)?$/);
// string doesn't match
if (values == null)
return defaultValue;
return this.getTimeInMs(values[1].replace(",", "."), values[2] || "ms", defaultValue);
},
/**
* Formats a string by replacing {n} with the n-th argument.
* Example: format("Hello {0}. {1} {0}.", "world", "Bye") => "Hello world. Bye world."
* @param {string} str The string to format
* @param {...string} args The arguments used for the replacement
* @returns The formatted string
*/
format(str, ...args)
{
if (!args.length)
return str;
for (var a in args)
{
str = str.replace(new RegExp("\\{" + a + "\\}", "gi"), args[a]);
}
return str
},
/**
* This function returns a string that represents the remaining time given in time_ms.
* If a timeConcatWord it set, it returns also this word including the target time.
*/
formatMsToStatus(time_ms, time_concat_word = null)
{
let result = "";
if (time_concat_word)
{
let date = (new Date(Date.now() + time_ms));
result = " " + time_concat_word + " " + date.getHours() + ":" + ("" + date.getMinutes()).padStart(2, "0") + ":" + ("" + date.getSeconds()).padStart(2, "0");
}
if (time_ms == 0)
return "0:00" + result;
// value in sec
time_ms = parseInt(time_ms / 1000, 10);
result = (time_ms % 60) + result;
if (time_ms % 60 < 10)
result = "0" + result;
// value in min
time_ms = parseInt(time_ms / 60, 10);
result = (time_ms % 60) + ":" + result;
if (time_ms % 60 < 10)
result = "0" + result;
// value in hour
time_ms = parseInt(time_ms / 60, 10);
result = (time_ms % 24) + ":" + result;
if (time_ms % 24 < 10)
result = "0" + result;
// value in days
time_ms = parseInt(time_ms / 24, 10);
if (time_ms > 0)
result = time_ms + "." + result;
return result;
},
/**
* This function returns a string that represents the remaining time until the given date.
* If a time_concat_word it set, it returns also this word including the target time.
*/
formatDateToStatus(date, time_concat_word = null)
{
let result = "";
if (time_concat_word)
result = " " + time_concat_word + " " + date.getHours() + ":" + ("" + date.getMinutes()).padStart(2, "0") + ":" + ("" + date.getSeconds()).padStart(2, "0");
let value = date - new Date();
if (value <= 0)
return "0:00" + result;
// value in sec
value = parseInt(value / 1000, 10);
result = (value % 60) + result;
if (value % 60 < 10)
result = "0" + result;
// value in min
value = parseInt(value / 60, 10);
result = (value % 60) + ":" + result;
if (value % 60 < 10)
result = "0" + result;
// value in hour
value = parseInt(value / 60, 10);
result = (value % 24) + ":" + result;
if (value % 24 < 10)
result = "0" + result;
// value in days
value = parseInt(value / 24, 10);
if (value > 0)
result = value + "." + result;
return result;
},
/**
* This function converts a number from a specific range into another range
* E.g:
* Input range = 0 to 10
* Target range = 100 to 200
*
* current number = 0
* scale(5, 0, 10, 100, 200) => 100
*
* current number = 5
* scale(5, 0, 10, 100, 200) => 150
*
* current number = 10
* scale(5, 0, 10, 100, 200) => 200
*
* current number = 20 (out of input range!)
* scale(5, 0, 10, 100, 200) => 300 (out of output range!)
*/
scale(number, inMin, inMax, outMin, outMax)
{
return (number - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
},
/**
* This function returns a short string that represents the current date and time.
*/
getCurrentDateForStatus()
{
let date = new Date();
return date.getDate().toString().padStart(2, "0") + "." +
(date.getMonth() + 1).toString().padStart(2, "0") + " - " +
date.getHours().toString().padStart(2, "0") + ":" +
date.getMinutes().toString().padStart(2, "0") + ":" +
date.getSeconds().toString().padStart(2, "0");
},
/**
* This function returns a short string that represents the current time.
*/
getCurrentTimeForStatus()
{
let date = new Date();
return date.getHours().toString().padStart(2, "0") + ":" +
date.getMinutes().toString().padStart(2, "0") + ":" +
date.getSeconds().toString().padStart(2, "0");
},
/**
* Creates a new independend object and copies all properties to the new one
* @param {*} obj The Object to copy
* @returns A new Object
*/
cloneObject(...obj)
{
return Object.assign({}, ...obj);
},
/**
* Forward all arguments to the console if it is enabled
*/
log()
{
// uncomment to see all log values
// console.log(...arguments);
},
/**
* Forward all arguments to the console if it is enabled
*/
warn(node, ...args)
{
// uncomment to see all warn values
console.warn(typeof node + " (" + node.id + "):", ...args);
}
};