-
Notifications
You must be signed in to change notification settings - Fork 7
/
node_helper.js
61 lines (55 loc) · 1.37 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
/* Magic Mirror
* Module: MMM-RMV
*
* By Com-Lum
* MIT Licensed.
*/
const https = require("https");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start: function()
{
console.log("node helper: " + this.name);
},
//Built_Para - return URL parameter as string
Built_Para: function()
{
var para = this.config.apiKey;
para +="&id=" + this.config.stationId;
para +="&duration=" + this.config.maxT;
para +="&maxJourneys=" + this.config.maxJ;
para +="&format=json";
return para;
},
socketNotificationReceived: function(notification, payload)
{
if(notification === 'CONFIG')
{
this.config = payload;
var rmvUrl = this.config.apiUrl + this.Built_Para();
this.getData(rmvUrl, this.config.stationId);
//console.log(rmvUrl);
}
},
getData: function(url, stationId)
{
https.get(url, (res) => {
let data = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
let text = Buffer.concat(data).toString();
if (res.statusCode < 200 || res.statusCode > 299) {
console.error("Requesting " + url + " failed with status code " + res.statusCode + " " + text);
} else {
if (text !== "") text = JSON.parse(text);
this.sendSocketNotification("Trains" + stationId, text);
}
});
})
.on("error", (err) => {
console.error("Request failed: ", err.message);
});
}
});