-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
144 lines (120 loc) · 4.73 KB
/
node_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
/* ---------------------------------------------------------------------
* Magic Mirror
* Module: MMM-CaravanPiPosition
*
* CaravanPi Module
* see https://github.com/spitzlbergerj/CaravanPi for more Information
* about the DIY project
*
* By Josef Spitzlberger http://spitzlberger.de
* MIT Licensed.
*/
const NodeHelper = require("node_helper")
var async = require('async');
var exec = require('child_process').exec;
var express = require('express');
var expressApp = express();
var myIntervalId;
var myUpdateIntervalLong;
var myUpdateIntervalShort;
//globale Variable, weil diese ansonsten in fillValueList unbekannt
valueListNHCaravanPiPosition = [];
module.exports = NodeHelper.create({
start: function() {
//console.error('Starting node helper: ' + this.name);
var self = this;
// Updateinterval ändern über Aufruf http://127.0.0.1:8080/MMM-CaravanPiPosition/changeUpdateInterval
this.expressApp.get('/' + this.name + '/changeUpdateInterval', function (req, res) {
self.changeUpdateInterval();
res.send('Change UpdateInterval from '+ myUpdateInterval);
});
// Kalibrierung anzeigen über Aufruf http://127.0.0.1:8080/MMM-CaravanPiPosition/setCalibration
this.expressApp.get('/' + this.name + '/setCalibration', function (req, res) {
valueListNHCaravanPiPosition[0]["cal"] = "1";
self.sendSocketNotification('VALUES', valueListNHCaravanPiPosition);
res.send('set Calibration hhhhhhh');
});
// Kalibrierung löschen über Aufruf http://127.0.0.1:8080/MMM-CaravanPiPosition/unsetCalibration
this.expressApp.get('/' + this.name + '/unsetCalibration', function (req, res) {
valueListNHCaravanPiPosition[0]["cal"] = "0";
self.sendSocketNotification('VALUES', valueListNHCaravanPiPosition);
res.send('unset Calibration');
});
},
socketNotificationReceived: function(notification, payload) {
var self = this;
// console.error('node_helper MMM-CaravanPiPosition: ' + notification);
switch(notification) {
case "CONFIG":
this.config = payload.config;
valueListNHCaravanPiPosition = payload.valueList;
myUpdateIntervalLong = this.config.updateIntervalLong;
myUpdateIntervalShort = this.config.updateIntervalShort;
myUpdateInterval = myUpdateIntervalLong;
// first call
self.getValues(valueListNHCaravanPiPosition);
// interval call
myIntervalId = setInterval(function() {
self.getValues(valueListNHCaravanPiPosition);
}, myUpdateInterval);
break
}
},
getValues: function(valueList) {
var self = this;
var cmdPart = "tail -1 " + self.config.valueDir + "/";
var cmd = "";
var i = 0;
while (i<valueList.length) {
cmd = cmdPart + valueList[i]["file"]
// console.error('node_helper MMM-CaravanPiPosition - cmd', cmd);
exec(cmd,"",this.fillValueList);
i+=1;
}
self.sendSocketNotification('VALUES', valueListNHCaravanPiPosition);
},
fillValueList: function (err, stdout, stderr) {
var i = 0;
if (err) {
console.error('node_helper MMM-CaravanPiPosition - fillValueList - Fehler:', err, stderr);
return;
}
var resSplit = stdout.split(' ');
var sensorID = "position";
while (i<valueListNHCaravanPiPosition.length) {
if (sensorID === valueListNHCaravanPiPosition[i]["file"]) {
valueListNHCaravanPiPosition[i]["datetime"] = resSplit[0].substring(6,8)+"."+resSplit[0].substring(4,6)+"."+resSplit[0].substring(0,4)+" "+resSplit[0].substring(8,10)+":"+resSplit[0].substring(10,12);
valueListNHCaravanPiPosition[i]["hl"] = resSplit[12];
valueListNHCaravanPiPosition[i]["hr"] = resSplit[13];
valueListNHCaravanPiPosition[i]["zl"] = resSplit[14];
valueListNHCaravanPiPosition[i]["zr"] = resSplit[15];
valueListNHCaravanPiPosition[i]["vl"] = resSplit[16];
valueListNHCaravanPiPosition[i]["vr"] = resSplit[17];
valueListNHCaravanPiPosition[i]["vo"] = resSplit[18];
valueListNHCaravanPiPosition[i]["cal"] = "08888989898989";
// console.error('node_helper MMM-CaravanPiPosition - valueList:', valueListNHCaravanPiPosition[i]);
}
i+=1;
}
return;
},
changeUpdateInterval: function() {
var self = this;
console.error('node_helper MMM-CaravanPiPosition changeUpdateInterval - actual: ', myUpdateInterval);
clearInterval(myIntervalId);
if (myUpdateInterval == myUpdateIntervalLong) {
myUpdateInterval = myUpdateIntervalShort;
}
else {
myUpdateInterval = myUpdateIntervalLong;
}
myIntervalId = setInterval(function() {
self.getValues(valueListNHCaravanPiPosition);
}, myUpdateInterval);
},
getDateTime: function(){
var currentDateTime = new Date();
var datetimestr = currentDateTime.toLocaleString()
return (datetimestr);
},
})