Repository navigation
Expand file tree
/
Copy pathnode_helper.js
More file actions
415 lines (352 loc) · 16.8 KB
/
Copy pathnode_helper.js
File metadata and controls
415 lines (352 loc) · 16.8 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* MagicMirror²
* Module: Temperature
*
* By Tom Hirschberger
* MIT Licensed.
*/
const NodeHelper = require('node_helper')
const spawnSync = require('child_process').spawnSync
const fs = require('fs')
const path = require('path')
const scriptsDir = path.join(__dirname, '/scripts')
const json5 = require('json5')
module.exports = NodeHelper.create({
start: function () {
this.started = false
this.sensorValues = []
this.sensorValuesUseCount = []
this.notificationSensors = {}
},
updateSensorValues: function () {
const self = this
console.log(self.name+": Updating sensor values")
for(let curSensorId = 0; curSensorId < self.config.sensors.length; curSensorId++){
if (typeof self.sensorValues[curSensorId] === "undefined"){
self.sensorValues[curSensorId] = {"error":false}
}
if (
(typeof self.config.sensors[curSensorId].useValuesCnt === "undefined") ||
(typeof self.sensorValues[curSensorId].temperature === "undefined") ||
(typeof self.sensorValues[curSensorId].humidity === "undefined") ||
(typeof self.sensorValuesUseCount[curSensorId] === "undefined") ||
(self.sensorValuesUseCount[curSensorId] >= self.config.sensors[curSensorId].useValuesCnt)
){
if (typeof self.config.sensors[curSensorId].notificationId === "undefined") {
self.sensorValuesUseCount[curSensorId] = 1
let curScript = self.config.defaultScript
let curArgs = self.config.defaultArgs
if(typeof self.config.sensors[curSensorId].script !== "undefined"){
curScript = self.config.sensors[curSensorId].script
}
if(curScript.indexOf("/") !== 0){
curScript = scriptsDir+"/"+curScript
}
if(typeof self.config.sensors[curSensorId].args !== "undefined"){
curArgs = self.config.sensors[curSensorId].args
}
self.sensorValues[curSensorId] = {}
console.log(self.name+" Calling: "+curScript+" "+curArgs)
let output = null
let options = {}
if(typeof self.config.sensors[curSensorId].timeout !== "undefined"){
options["timeout"] = self.config.sensors[curSensorId].timeout
}
try {
if (!Array.isArray(curArgs)){
curArgs = curArgs.split(" ")
}
let spawnOutput = spawnSync(curScript, curArgs, options)
output = spawnOutput.stdout
errorOut = spawnOutput.stderr
} catch (err){
console.log(err)
output = null
}
if(errorOut != null){
console.log(self.name+" Error output of script "+curScript+":")
console.log(errorOut.toString())
}
if(output !== null){
output = output.toString().trim()
try {
curValues = JSON.parse(output)
console.log(JSON.stringify(curValues))
} catch (err) {
console.log(self.name+" Can not parse output of sensor with id "+curSensorId+" ("+self.config.sensors[curSensorId].name+"): "+output)
curValues = {}
curValues["error"] = true
}
if(curValues.error){
console.log(this.name+": Error while reading data of sensor with id "+curSensorId+" ("+self.config.sensors[curSensorId].name+")!")
} else {
if(self.config.useCelsius){
if(typeof curValues.temperature_c === "undefined"){
if(typeof curValues.temperature_f !== "undefined"){
curValues.temperature_c = (curValues.temperature_f - 32.0) / 1.8
curValues["temperature"] = curValues["temperature_c"].toFixed(self.config.fractionCount)
}
} else {
curValues["temperature"] = curValues["temperature_c"].toFixed(self.config.fractionCount)
}
} else {
if(typeof curValues.temperature_f === "undefined"){
if(typeof curValues.temperature_c !== "undefined"){
curValues.temperature_f = (curValues.temperature_c * 1.8) + 32
curValues["temperature"] = curValues["temperature_f"].toFixed(self.config.fractionCount)
}
} else {
curValues["temperature"] = curValues["temperature_f"].toFixed(self.config.fractionCount)
}
}
curValues["temperature_f"] = curValues["temperature_f"].toFixed(self.config.fractionCount)
curValues["temperature_c"] = curValues["temperature_c"].toFixed(self.config.fractionCount)
if(typeof curValues.humidity !== "undefined"){
curValues.humidity = curValues.humidity.toFixed(self.config.fractionCount)
}
self.sensorValues[curSensorId] = curValues
console.log(this.name+": New Values of sensor with id "+curSensorId+" ("+self.config.sensors[curSensorId].name+"): "+JSON.stringify(curValues))
}
}
}
// else {
// console.log("Skipping sensor with id: "+curSensorId+ " because it is an notification based sensor!")
// }
} else {
if (typeof self.config.sensors[curSensorId].name !== "undefined"){
console.log("Re-Using value of sensor with name: "+self.config.sensors[curSensorId].name)
} else {
console.log("Re-Using value of sensor with id: "+curSensorId)
}
if(typeof self.sensorValuesUseCount[curSensorId] !== "undefined"){
self.sensorValuesUseCount[curSensorId] += 1
} else {
self.sensorValuesUseCount[curSensorId] = 1
}
}
}
console.log("Sending temp update: "+JSON.stringify(self.sensorValues))
self.sendSocketNotification("TEMPERATURE_UPDATE", {values: self.sensorValues})
for(let curSensorId in self.notificationSensors){
curNotiId = self.notificationSensors[curSensorId];
if (
(typeof self.config.sensors[curNotiId].useValuesCnt === "undefined") ||
(self.sensorValuesUseCount[curNotiId] >= self.config.sensors[curNotiId].useValuesCnt)
){
if (typeof self.config.sensors[curNotiId].name !== "undefined"){
console.log("Removing values of sensor with name: "+self.config.sensors[curNotiId].name)
} else {
console.log("Removing values of sensor with id: "+curNotiId)
}
self.sensorValues[curNotiId] = {"error":false}
}
}
},
socketNotificationReceived: function (notification, payload) {
const self = this
if (notification === 'CONFIG' && self.started === false) {
self.config = payload
self.started = true
for(let curSensorId = 0; curSensorId < self.config.sensors.length; curSensorId++){
if (typeof self.config.sensors[curSensorId].notificationId !== "undefined") {
self.notificationSensors[self.config.sensors[curSensorId].notificationId] = curSensorId
}
}
} else if (notification === "UPDATE_SENSOR_VALUES"){
self.updateSensorValues()
} else if (notification.startsWith("TEMPERATURE_C_")){
cur_id = notification.substring(14)
cur_valuec = (payload*1).toFixed(self.config.fractionCount)
cur_valuef = ((cur_valuec * 1.8) + 32).toFixed(self.config.fractionCount)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["temperature_c"] = cur_valuec
self.sensorValues[cur_conf_id]["temperature_f"] = cur_valuef
if(self.config.useCelsius){
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_c"]
} else {
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_f"]
}
} else if (notification.startsWith("TEMPERATURE_F_")){
cur_id = notification.substring(14)
cur_valuef = payload
cur_valuec = ((cur_valuef - 32) / 1.8).toFixed(self.config.fractionCount)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["temperature_c"] = cur_valuec
self.sensorValues[cur_conf_id]["temperature_f"] = cur_valuef
if(self.config.useCelsius){
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_c"]
} else {
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_f"]
}
} else if (notification.startsWith("HUMIDITY_")){
cur_id = notification.substring(9)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["humidity"] = (payload*1).toFixed(self.config.fractionCount)
} else if (notification.startsWith("WIND_SPEED_AVG_")){
cur_id = notification.substring(15)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["wind_avg_m_s"] = payload
} else if (notification.startsWith("WIND_SPEED_MAX_")){
cur_id = notification.substring(15)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["wind_max_m_s"] = payload
} else if (notification.startsWith("WIND_DIRECTION_")){
cur_id = notification.substring(15)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["wind_dir_deg"] = payload
} else if (notification.startsWith("RAIN_")){
cur_id = notification.substring(5)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["rain_mm"] = payload
} else if (notification.startsWith("UV_")){
cur_id = notification.substring(3)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["uv"] = payload
} else if (notification.startsWith("LIGHT_")){
cur_id = notification.substring(6)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
self.sensorValues[cur_conf_id]["light_lux"] = payload
} else if (notification.startsWith("TEMPERATURE_VALUES_")){
cur_id = notification.substring(19)
cur_conf_id = self.notificationSensors[cur_id]
if(typeof self.sensorValues[cur_conf_id] === "undefined"){
self.sensorValues[cur_conf_id] = {"error":false}
}
self.sensorValuesUseCount[cur_conf_id] = 0
try {
curValues = json5.parse(payload)
// console.log("Parsed values: ")
// console.log(JSON.stringify(curValues))
for (var key in curValues){
curValues[key.toLowerCase()] = curValues[key]
}
// console.log("Parsed values after lowerCase transform: ")
// console.log(JSON.stringify(curValues))
if (typeof curValues["humidity"] !== "undefined"){
// console.log("Updating humidity")
self.sensorValues[cur_conf_id]["humidity"] = (curValues["humidity"]*1).toFixed(self.config.fractionCount)
} else {
// console.log("Missing humidity")
delete(self.sensorValues[cur_conf_id]["humidity"])
}
if (typeof curValues["temperature_c"] !== "undefined"){
// console.log("Updating temperature_c")
self.sensorValues[cur_conf_id]["temperature_c"] = (curValues["temperature_c"]*1).toFixed(self.config.fractionCount)
if (! ("temperature_f" in curValues)){
self.sensorValues[cur_conf_id]["temperature_f"] = ((self.sensorValues[cur_conf_id]["temperature_c"] * 1.8) + 32).toFixed(self.config.fractionCount)
}
if(self.config.useCelsius){
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_c"]
} else {
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_f"]
}
} else {
// console.log("Missing temperature_c")
delete(self.sensorValues[cur_conf_id]["temperature_c"])
}
if (typeof curValues["temperature_f"] !== "undefined"){
// console.log("Updating temperature_f")
self.sensorValues[cur_conf_id]["temperature_f"] = (curValues["temperature_f"]*1).toFixed(self.config.fractionCount)
if (! ("temperature_c" in curValues)){
self.sensorValues[cur_conf_id]["temperature_c"] = ((self.sensorValues[cur_conf_id]["temperature_f"] - 32) / 1.8).toFixed(self.config.fractionCount)
}
if(self.config.useCelsius){
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_c"]
} else {
self.sensorValues[cur_conf_id]["temperature"] = self.sensorValues[cur_conf_id]["temperature_f"]
}
} else {
// console.log("Missing temperature_f")
delete(self.sensorValues[cur_conf_id]["temperature_f"])
}
if (typeof curValues["wind_avg_m_s"] !== "undefined"){
// console.log("Updating wind_avg_m_s")
self.sensorValues[cur_conf_id]["wind_avg_m_s"] = (curValues["wind_avg_m_s"]*1).toFixed(self.config.fractionCount)
} else {
// console.log("Missing wind_avg_m_s")
delete(self.sensorValues[cur_conf_id]["wind_avg_m_s"])
}
if (typeof curValues["wind_max_m_s"] !== "undefined"){
// console.log("Updating wind_max_m_s")
self.sensorValues[cur_conf_id]["wind_max_m_s"] = (curValues["wind_max_m_s"]*1).toFixed(self.config.fractionCount)
} else {
// console.log("Missing wind_max_m_s")
delete(self.sensorValues[cur_conf_id]["wind_max_m_s"])
}
if (typeof curValues["wind_dir_deg"] !== "undefined"){
// console.log("Updating wind_dir_deg")
self.sensorValues[cur_conf_id]["wind_dir_deg"] = curValues["wind_dir_deg"]
} else {
// console.log("Missing wind_dir_deg")
delete(self.sensorValues[cur_conf_id]["wind_dir_deg"])
}
if (typeof curValues["rain_mm"] !== "undefined"){
// console.log("Updating rain_mm")
self.sensorValues[cur_conf_id]["rain_mm"] = (curValues["rain_mm"]*1).toFixed(self.config.fractionCount)
} else {
// console.log("Missing rain_mm")
delete(self.sensorValues[cur_conf_id]["rain_mm"])
}
if (typeof curValues["uv"] !== "undefined"){
// console.log("Updating uv")
self.sensorValues[cur_conf_id]["uv"] = curValues["uv"]
} else {
// console.log("Missing uv")
delete(self.sensorValues[cur_conf_id]["uv"])
}
if (typeof curValues["light_lux"] !== "undefined"){
// console.log("Updating light_lux")
self.sensorValues[cur_conf_id]["light_lux"] = curValues["light_lux"]
} else {
// console.log("Missing light_lux")
delete(self.sensorValues[cur_conf_id]["light_lux"])
}
} catch (err) {
console.log(self.name+" Can not parse output of notification of sensor with id "+cur_conf_id+" ("+self.config.sensors[cur_conf_id].name+"): "+payload)
console.log(err)
self.sensorValues[cur_conf_id] = {}
self.sensorValues[cur_conf_id]["error"] = true
}
// console.log("Saved new values of id: "+cur_id)
// console.log(JSON.stringify(self.sensorValues[cur_conf_id]))
}
else {
console.log(this.name + ': Received Notification: ' + notification)
}
}
})