forked from TTigges/MMM-Oiltank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
81 lines (71 loc) · 2.17 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
/* Magic Mirror
* Node Helper: MMM-Oiltank
*
* By Torben Tigges
*
*/
var NodeHelper = require("node_helper");
var request = require("request")
var fs = require("fs");
var timer = 0;
module.exports = NodeHelper.create({
start: function() {
},
socketNotificationReceived: function(notification, payload) {
if (notification === "GetChart") {
this.updateTimer(payload);
}
},
getChart: function(payload) {
var self = this;
var file = payload;
// console.log("gettingchart and sending it to the module");
request({
url: file,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
// console.log(body) // Print the json response
self.sendSocketNotification("ChartUpdate", body);
}
})
// var rawdata = fs.readFileSync("http://www.torben-tigges.de/div/history.json");
// var history = JSON.parse(rawdata);
// self.sendSocketNotification("ChartUpdate", history);
},
updateTimer: function(payload) {
// sofort abfragen, dann nur zur vollen Stunde
var self = this;
var d = new Date();
var h = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();
if (timer === 0) {
timer ++; // prevent unnecessary timer
// console.log("timer = " + timer);
this.getChart(payload);
if((min == '00') && (sec == '00')){
// console.log(h + ":" + min + ":" + sec + " - update calls price and waits 5 min");
// console.log("restart timer");
setTimeout(() => {
this.clearTimer(payload);
}, 60*1000*60); // 60 sec * 1000 ms = 1 min * 60 = hour
} else {
// console.log(h + ":" + min + ":" + sec + " - update waits for " + (59-min) + " min and " + (60-sec) + "sec");
// console.log("restart timer");
setTimeout(() => {
this.clearTimer(payload);
}, ((60-sec)*1000)+(60*1000*(59-min)));
}
}
else {
// console.log("timer already running, data displayed outside of timer run");
this.getChart(payload);
}
},
clearTimer: function(payload) {
// console.log("timer finished one circle");
timer --;
this.updateTimer(payload);
}
});