forked from da4throux/MMM-Paris-RATP-PG
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-Toulouse-Transports.js
304 lines (256 loc) · 12.7 KB
/
MMM-Toulouse-Transports.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// config is expected as follows:
/*
{
module: 'MMM-Toulouse-Transports',
position: 'top_right',
header: 'Horaires de passage',
config: {
apiKey: 'APIKEY',
stopSchedules: [
{
lineNumber: 22, // shall be >0
stopCode: 6601, // shall be >0
maxEntries:2 // shall be >1
},
{
lineNumber: 22, // shall be >0
stopCode: 431, // shall be >0
maxEntries:2 // shall be >1
}
],
debug: true,
updateInterval: 120000
}
}
*/
Module.register( "MMM-Toulouse-Transports", {
defaults: {
maxTimeOffset: 200, // Max time in the future for entries
useRealtime: true,
updateInterval: 1 * 60 * 1000, //time in ms between pulling request for new times (update request)
animationSpeed: 2000,
convertToWaitingTime: true, // messages received from API can be 'hh:mm' in that case convert it in the waiting time 'x mn'
initialLoadDelay: 0, // start delay seconds.
apiBaseSchedules: 'https://api.tisseo.fr/v2/stops_schedules.json?',
maxLettersForDestination: 22, //will limit the length of the destination string
concatenateArrivals: true, //if for a transport there is the same destination and several times, they will be displayed on one line
showSecondsToNextUpdate: true, // display a countdown to the next update pull (should I wait for a refresh before going ?)
showLastUpdateTime: false, //display the time when the last pulled occured (taste & color...)
oldUpdateOpacity: 0.5, //when a displayed time age has reached a threshold their display turns darker (i.e. less reliable)
oldThreshold: 0.1, //if (1+x) of the updateInterval has passed since the last refresh... then the oldUpdateOpacity is applied
retryDelay: 1000,//10 * 1000, // delay before data update retrys (ms)
debug: false
},
/* Start Sequence*/
start: function ( ) {
Log.info("start - Starting module: " + this.name );
Log.info("start - Send notification SET_CONFIG");
// give config to node_helper
this.sendSocketNotification( 'SET_CONFIG', this.config );
// for bus schedules
// ask to get all lines defininitions from API
// only one call should be necessary for now
Log.info("start - Send notification UPDATE_GLOBAL_TISSEO_DATA");
this.sendSocketNotification( 'UPDATE_GLOBAL_TISSEO_DATA', null);
this.busScheduleData = [];
this.currentUpdateInterval = this.config.updateInterval
this.lastUpdate = new Date();
// test to avoid first request delayed of update interval
//this.log(TRACE, "start - lastUpdate " + this.lastUpdate);
//this.lastUpdate.setHours(this.lastUpdate.getHours() - 1)
//this.log(TRACE, "start - lastUpdate minus 01h00 " + this.lastUpdate);
//this.config.lastUpdate = this.lastUpdate
// end
this.loaded = false;
this.updateTimer = null;
var self = this;
setInterval( function ( ) {
self.updateDom( );
}, 1000 );
Log.info("start - End fo start for module: " + this.name );
},
getHeader: function ( ) {
//Log.info("Start getHeader");
var header = this.data.header;
if ( this.config.showSecondsToNextUpdate ) {
//var timeDifference = Math.round( ( this.config.updateInterval - new Date( ) + Date.parse( this.config.lastUpdate ) ) / 1000 );
var timeDifference = Math.round( ( this.currentUpdateInterval - new Date( ) + Date.parse( this.config.lastUpdate ) ) / 1000 );
if ( timeDifference > 0 ) {
header += ', next update in ' + timeDifference + 's';
} else {
header += ', update requested ' + Math.abs( timeDifference ) + 's ago';
}
}
if ( this.config.showLastUpdateTime ) {
var now = this.config.lastUpdate;
header += ( now ? ( ' @ ' + now.getHours( ) + ':' + ( now.getMinutes( ) > 9 ? '' : '0' ) + now.getMinutes( ) + ':' + ( now.getSeconds( ) > 9 ? '' : '0' ) + now.getSeconds( ) ) : '' );
}
//Log.info("End getHeader (before return statement)");
return header;
},
getStyles: function () {
//Log.info("Start getStyles");
//Log.info("End getStyles (before return statement)");
return ['font-awesome.css'];
},
getDom: function ( ) {
Log.info("getDom - Start");
var now = new Date(); // moment(); // moment package cannot be invoked in here ... only in nodehelper ?! not very handy
var minutesLeft;
var wrapper = document.createElement( "div" );
wrapper.classList.add("small");
if ( !this.loaded ) {
Log.info("getDom - not loaded yet");
wrapper.innerHTML = "Loading stop schedules ...";
wrapper.className = "dimmed light small";
return wrapper;
}
var table = document.createElement("table");
table.className = "small";
wrapper.appendChild(table);
var row;
var lineIdCell, destCell, stopNameCell, timeLeftCell;
// cells:
// | icon NUmber | | stopName (code) |
// | | Direction | timeleft |
// | | Direction | timeleft |
// | icon NUmber | | stopName (code) |
// | | Direction | timeleft |
// | | Direction | timeleft |
//var icon;
Log.info("getDom - start loop over all schedule data");
//Log.info("getDom - this.busScheduleData: "+ JSON.stringify(this.busScheduleData));
//Log.info(this.busScheduleData);
for ( var scheduleDataIndex = 0; scheduleDataIndex < this.busScheduleData.length; scheduleDataIndex++ ) {
row = document.createElement("tr");
lineIdCell = document.createElement("td");
lineIdCell.className = "align-right bright";
destCell = document.createElement("td");
destCell.className = "align-right bright";
timeLeftCell = document.createElement("td");
timeLeftCell.className = "align-right bright";
stopNameCell = document.createElement("td");
stopNameCell.className = "align-right bright";
var currentDataToDisplay = this.busScheduleData[ scheduleDataIndex ];
var lineNumber = currentDataToDisplay.lineShortName;
var data = currentDataToDisplay.scheduleData;
rgbString = data.departure[0].line.color;
var lineColor = 'rgb'+rgbString
var stopName = data.stop.name;
var stopCode = data.stop.operatorCode;
// put line N°
lineIdCell.innerHTML += '<i class="fa fa-bus bright" style="color:'+lineColor+'"></i>' // put icon
lineIdCell.innerHTML += lineNumber;
lineIdCell.style.color = lineColor
row.appendChild(lineIdCell);
// put empty dest cell
destCell.innerHTML = "";
row.appendChild(destCell);
// put stopname cell
stopNameCell.innerHTML = stopName + " ("+stopCode+")";
stopNameCell.style.color = lineColor
row.appendChild(stopNameCell);
// create section row
table.appendChild(row);
// display the next scheduled times
for(var index = 0; index < data.departure.length ; index++) {
Log.info("getDom - start loop over a given stop schedule data ( lineNumber"+ lineNumber+")");
row = document.createElement("tr");
lineIdCell = document.createElement("td");
lineIdCell.className = "align-right bright";
destCell = document.createElement("td");
destCell.className = "align-right bright";
timeLeftCell = document.createElement("td");
timeLeftCell.className = "align-right bright";
stopNameCell = document.createElement("td");
stopNameCell.className = "align-right bright";
var currentElement = data.departure[index];
// put empty icon
span = document.createElement("span");
icon = document.createElement("i");
span.appendChild(icon);
lineIdCell.appendChild(span);
// put empty line N°
lineIdCell.innerHTML = "";
row.appendChild(lineIdCell);
// put dest cell
destCell.innerHTML = currentElement.destination[0].name;
row.appendChild(destCell);
// put timeleft cell
minutesLeft = this.computeWaitingTime(now, currentElement.dateTime);
//listElement.innerHTML += minutesLeft + " minutes";
//uList.appendChild( listElement );
timeLeftCell.innerHTML = minutesLeft;
row.appendChild(timeLeftCell);
// put row
table.appendChild(row);
};// end loop over all schedule times
};// end loop over all buses numbers
//wrapper.appendChild( uList );
//Log.info("End getDom (before return statement)");
return wrapper;
},
computeWaitingTime: function(now, string) {
Log.info("computeWaitingTime - start - params: now: " + now + " - string: " + string);
//var scheduledDate = moment(string);
////var now = moment();
//var minutesLeft = scheduledDate.diff(now, 'minutes');
//if(minutesLeft < 0) {
// Log.debug("Difference betweens moments is negative, are we trying to display a schedule time that is in the past ?");
// Log.debug(" Now was : " + now.toISOString());
// Log.debug(" scheduledDate was: " + scheduledDate.toISOString());
//};
//return minutesLeft;
// inpout string is in the form "2017-06-03 15:11"
// now is in the form
var endTime = string.split(' ')[1].split(':');
//Log.info("computeWaitingTime - endTime="+endTime);
var endDate = new Date(0, 0, 0, endTime[0], endTime[1]);
var startDate = new Date(0, 0, 0, now.getHours(), now.getMinutes(), now.getSeconds());
//Log.info("computeWaitingTime - startDate = "+startDate);
//Log.info("computeWaitingTime - endDate = "+endDate);
var waitingTime = endDate - startDate;
if (startDate > endDate) {
Log.info("computeWaitingTime - startDate > endDate !!!");
waitingTime += 1000 * 60 * 60 * 24;
};
if (waitingTime > 60 * 60 * 1000) {
// return time in hours
waitingTime = "" + Math.ceil( ((waitingTime / 1000) / 60) / 60) + " h";
}
else if (waitingTime > 60 * 1000) {
// return time in minutes
waitingTime = "" + Math.ceil( (waitingTime / 1000) / 60) + " m";
}
else {
waitingTime = "" + Math.ceil( waitingTime / 1000) + " s";
};
Log.info("computeWaitingTime - end - waitingTime="+waitingTime);
return waitingTime;
},
socketNotificationReceived: function ( notification, payload ) {
Log.info("socketNotificationReceived - Module received notification: " + notification);
switch ( notification ) {
case "JOURNEYS":
break;
case "BUS_SCHEDULES":
this.busScheduleData = payload.data;
this.lastUpdate = payload.lastUpdate;
this.loaded = payload.loaded;
this.currentUpdateInterval = payload.currentUpdateInterval
//Log.info("socketNotificationReceived - bus schedule data received is: " + this.busScheduleData);
//Log.info("socketNotificationReceived - bus schedule data received is: " + JSON.stringify(this.busScheduleData));
this.updateDom( );
break;
case "UPDATE_JOURNEYS":
break;
case "UPDATE_BUS_SCHEDULES":
this.config.lastUpdate = payload.lastUpdate;
this.updateDom( );
break;
case "ALL_LINES_AVAILABLE":
this.sendSocketNotification("UPDATE_BUS_SCHEDULES");
}
Log.info("socketNotificationReceived - End of Module received notification: " + notification);
}
} );