forked from CatoAntonsen/MMM-Ruter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
executable file
·120 lines (97 loc) · 2.75 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
/* Magic Mirror
* Node Helper: Vindsiden
*
* By Erik Mohn
*
* Forked from https://github.com/CatoAntonsen/MMM-Ruter
*
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var http = require("http");
var async = require("async");
var crypto = require("crypto");
var moment = require("moment");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting module: " + this.name);
},
socketNotificationReceived: function(notification, config) {
if (notification === "CONFIG") {
this.config = config;
this.allLocations = [];
this.lastMD5 = [];
this.initPolling();
return;
}
},
initPolling: function() {
var self = this;
for(var i=0; i < this.config.locations.length; i++) {
this.allLocations.push(this.config.locations[i]);
}
this.startPolling();
setInterval(function() {
self.startPolling();
}, this.config.serviceReloadInterval);
},
startPolling: function() {
var self = this;
async.map(this.allLocations, this.getLocationInfo, function(err, result) {
var locations = [];
for(var i=0; i < result.length; i++) {
locations = locations.concat(result[i]);
}
locations = locations.slice(0, self.config.maxItems);
if (self.hasChanged("locations", locations)) {
self.sendSocketNotification("VINDSIDEN_UPDATE", locations);
}
});
},
hasChanged: function(key, value) {
var md5sum = crypto.createHash("md5");
md5sum.update(JSON.stringify(value));
var md5Hash = md5sum.digest("hex");
if (md5Hash != this.lastMD5[key]) {
this.lastMD5[key] = md5Hash;
return true;
} else {
return false;
}
},
getLocationInfo: function(locationItem, callback) {
var str = "";
var responseCallback = function(response) {
response.on("data", function(chunk) {
str += chunk;
});
response.on("end", function() {
var locations = JSON.parse(str);
var allLocationItems = new Array();
var location = locations;
allLocationItems.push({
locationId: locationItem.locationId,
locationName: location.Name,
time: location.LastMeasurementTime,
windspeed: Math.round(location.Data[0].WindAvg),
windgust: Math.round(location.Data[0].WindMax),
winddirection: degreesToDirection(location.Data[0].DirectionAvg)
});
callback(null, allLocationItems);
});
response.on("error", function(error) {
console.error("------------->" + error)
});
}
var options = {
host: "vindsiden.no",
path: "/api/stations/" + locationItem.locationId + "?n=1"
};
http.request(options, responseCallback).end();
}
});
function degreesToDirection(num) {
var val = Math.floor((num / 22.5) + 0.5);
var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
return arr[(val % 16)];
}