This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_helper.js
107 lines (93 loc) · 3.09 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
/* Timetable for Paris local transport Module */
/* Magic Mirror
* Module: MMM-Ratp
*
* By Louis-Guillaume MORAND
* based on a script from Benjamin Angst http://www.beny.ch and Georg Peters (https://lane6.de)
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const forge = require('node-forge');
const unirest = require('unirest');
module.exports = NodeHelper.create({
updateTimer: "",
start: function() {
this.started = false;
console.log("MMM-Ratp- NodeHelper started");
},
/* updateTimetable(transports)
* Calls processTransports on succesfull response.
*/
updateTimetable: function(url) {
var self = this;
var retry = false;
// calling this API
unirest.get(url)
.end(function(r) {
if (r.error) {
console.log(self.name + " : " + r.error);
retry = true;
} else {
self.processTransports(r.body, url);
}
if (retry) {
console.log("retrying");
// self.scheduleUpdate((self.loaded) ? -1 : this.config.retryDelay);
}
});
},
// Help to retrieve a type which can be directly displayed inside the title of the module
getSanitizedName: function(type) {
var t = "";
switch (type) {
case "bus":
t = "<i class='fa fa-bus' aria-hidden='true'></i> Bus";
break;
case "rers":
t = "<i class='fa fa-train' aria-hidden='true'></i> RER";
break;
case "tramways":
t = "<i class='fa fa-subway' aria-hidden='true'></i> Tramway";
break;
case "noctiliens":
t = "<i class='fa fa-bus' aria-hidden='true'></i> Noctilien";
break;
case "metros":
t = "<i class='fa fa-train' aria-hidden='true'></i> Metro";
break;
default:
t = "";
}
return t;
},
/* processTransports(data)
* Uses the received data to set the various values.
*/
processTransports: function(data, url) {
this.transports = [];
this.lineInfo = "";
for (var i = 0, count = data.result.schedules.length; i < count; i++) {
var nextTransport = data.result.schedules[i];
this.transports.push({
name: nextTransport.destination,
time: nextTransport.message
});
}
this.loaded = true;
this.sendSocketNotification("TRANSPORTS", {
transports: this.transports,
lineInfo: this.lineInfo,
uniqueID: url
});
},
socketNotificationReceived: function(notification, payload) {
if (payload.debugging) {
console.log("Notif received: " + notification);
console.log(payload);
}
const self = this;
if (notification === 'GETDATA') {
self.updateTimetable(payload.apiURL);
}
}
});