-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
122 lines (112 loc) · 4.22 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
/* Magic Mirror
* Module: MMM-Nantes-TAN
*
* By Mathieu Goulène (normyx)
* based on a script from da4throux (https://github.com/da4throux/MMM-Paris-RATP-PG)
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const unirest = require('unirest');
module.exports = NodeHelper.create({
start: function () {
this.started = false;
//this.lock = false;
},
socketNotificationReceived: function(notification, payload) {
const self = this;
if (notification === 'SET_CONFIG' && this.started == false) {
this.config = payload;
if (this.config.debug) {
console.log (' *** config set in node_helper: ');
console.log ( payload );
}
this.started = true;
self.scheduleUpdate(this.config.initialLoadDelay);
}
},
/* scheduleUpdate()
* Schedule next update.
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
if (this.config.debug) { console.log (' *** scheduleUpdate set next update in ' + nextLoad);}
this.updateTimer = setTimeout(function() {
self.updateTimetable();
}, nextLoad);
},
getResponse: function(_url, _processFunction, _stopConfig, _stopData) {
var self = this;
var retry = true;
if (this.config.debug) { console.log (' *** fetching: ' + _url);}
unirest.get(_url)
.header({
'Accept': 'application/json;charset=utf-8'
})
.end(function(response){
if (response && response.body) {
if (self.config.debug) {
console.log (' *** received answer for: ' + _url);
console.log (' *** stop config : ' + _stopConfig);
}
_processFunction(response.body, _stopConfig, _stopData);
} else {
if (self.config.debug) {
if (response) {
console.log (' *** partial response received');
console.log (response);
} else {
console.log (' *** no response received');
}
}
}
if (retry) {
self.scheduleUpdate((self.loaded) ? -1 : this.config.retryDelay);
}
})
},
/* updateTimetable(transports)
* Calls processTrains on successful response.
*/
updateTimetable: function() {
var self = this;
var urlArret, urlHoraire, stopConfig;
if (this.config.debug) { console.log (' *** fetching update');}
self.sendSocketNotification("UPDATE", { lastUpdate : new Date()});
for (var index in self.config.busStations) {
var stopData = {};
stopConfig = self.config.busStations[index];
urlArret = self.config.tanURL+'horairesarret.json/'+stopConfig.arret+'/'+stopConfig.ligne+'/'+stopConfig.sens;
self.getResponse(urlArret, self.processArret.bind(this), stopConfig, stopData);
urlHoraire = self.config.tanURL+'tempsattente.json/'+stopConfig.arret;
self.getResponse(urlHoraire, self.processHorairesLigne.bind(this), stopConfig, stopData);
}
},
processArret: function(data, stopConfig,stopData) {
if (this.config.debug) { console.log (' *** processArret request response'); console.log (data); }
//this.schedule = {};
stopData.id = stopConfig.arret+'/'+stopConfig.ligne+'/'+stopConfig.sens;
stopData.arret = data;
},
processHorairesLigne: function(data, stopConfig,stopData) {
if (this.config.debug) { console.log (' *** processHorairesLigne request response'); console.log (data); }
var self = this;
var numLigne = stopConfig.ligne;
var sens = stopConfig.sens;
stopData.schedules = [];
for (var i = 0; i < data.length; i++) {
if (data[i].ligne.numLigne == numLigne && data[i].sens == sens) {
stopData.schedules.push(data[i]);
}
}
if (this.config.debug) {console.log (' *** processHorairesLigne schedules data = '); console.log(stopData);}
stopData.lastUpdate = new Date();
this.loaded = true;
this.sendSocketNotification("BUS", stopData);
},
});