-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-Nantes-TAN.js
219 lines (196 loc) · 7.2 KB
/
MMM-Nantes-TAN.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
/* Timetable for Nantes local transport Module */
/* 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.
*/
Module.register("MMM-Nantes-TAN",{
// Define module defaults
defaults: {
maximumEntries: 2, //if the APIs sends several results for the incoming transport how many should be displayed
updateInterval: 1 * 60 * 1000, //time in ms between pulling request for new times (update request)
initialLoadDelay: 0, // start delay seconds.
maxLettersForDestination: 12, //will limit the length of the destination string
maxLettersForStop: 12, //will limit the length of the destination string
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...)
debug: false, //console.log more things to help debugging
tanURL: 'http://open.tan.fr/ewp/',
colorBlue: "rgb(0,121,188)",
colorGreen: "rgb(0, 118,125)",
colorYellow: "rgb(253,197,16)",
colorPurple: "rgb(189,169,208)",
colorWhite: "rgb(255,255,255)",
colorOrange: "rgb(236,114,0)",
useColor: true,
defaultSymbol: 'bus',
},
// Define required scripts.
getStyles: function() {
return ["MMM-Nantes-TAN.css", "font-awesome.css"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.sendSocketNotification('SET_CONFIG', this.config);
this.busSchedules = {};
this.arretData = {};
this.busLastUpdate = {};
this.loaded = false;
this.updateTimer = null;
var self = this;
setInterval(function () {
self.caller = 'updateInterval';
self.updateDom();
}, 1000);
},
getHeader: function () {
var header = this.data.header;
if (this.config.showSecondsToNextUpdate) {
var timeDifference = Math.round((this.config.updateInterval - 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()) : '');
}
return header;
},
setColor: function(element, codeColor) {
if (this.config.useColor && codeColor != null) {
var color = null;
switch(codeColor) {
case 'blue':
color = this.config.colorBlue;
break;
case 'green':
color = this.config.colorGreen;
break;
case 'yellow':
color = this.config.colorYellow;
break;
case 'purple':
color = this.config.colorPurple;
break;
case 'white':
color = this.config.colorWhite;
break;
case 'orange':
color = this.config.colorOrange;
break;
default :
}
if (color != null) {
element.style="color:"+color+";";
}
}
},
timeCleaning: function(time) {
return time.replace(" mn ","'").replace(" mn","'00").replace("horaire.proche","Proche");
},
transitSymbol: function(typeLigne) {
var symbol = 'bus';
switch(typeLigne) {
case 1:
symbol = 'subway';
break;
case 2:
case 3:
symbol = 'bus';
break;
case 4:
symbol = 'ship';
break;
default :
console.log("unknown type "+typeLigne);
break;
}
return symbol;
},
// Override dom generator.
getDom: function() {
var now = new Date();
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = "Loading connections ...";
wrapper.className = "dimmed light small";
return wrapper;
} else {
wrapper.className = "nantestan";
}
var table = document.createElement("table");
var stopIndex;
var previousRow, previousDestination, previousMessage, row, comingBus;
var firstCell, secondCell;
wrapper.appendChild(table);
table.className = "small";
for (var busIndex = 0; busIndex < this.config.busStations.length; busIndex++) {
var firstLine = true;
var stop = this.config.busStations[busIndex];
stopIndex=stop.arret+'/'+stop.ligne+'/'+stop.sens;
var comingBuses = this.busSchedules[stopIndex];
var comingBusLastUpdate = this.busLastUpdate[stopIndex];
var arretData = this.arretData[stopIndex];
row = document.createElement("tr");
this.setColor(row,stop.color);
var symbol = document.createElement("span");
if (stop.symbol != null) {
symbol.className = "fa fa-fw fa-"+stop.symbol;
} else {
symbol.className = "fa fa-fw fa-"+this.config.defaultSymbol;
}
var symbolTd = document.createElement("td");
symbolTd.appendChild(symbol);
row.appendChild(symbolTd);
var busNameCell = document.createElement("td");
busNameCell.className = "align-right bold";
//busNameCell.style="color:rgb(255, 99, 71)";
busNameCell.innerHTML = arretData.ligne.numLigne;
row.appendChild(busNameCell);
var trip = document.createElement("td");
trip.innerHTML = arretData.arret.libelle.substr(0, this.config.maxLettersForStop);
if (comingBuses.length>0) {
trip.innerHTML = trip.innerHTML + " → " + comingBuses[0].terminus.substr(0, this.config.maxLettersForDestination);
}
trip.className = "align-left";
row.appendChild(trip);
var depCell = document.createElement("td");
//depCell.className = "bright";
for (var comingIndex = 0; (comingIndex < this.config.maximumEntries) && (comingIndex < comingBuses.length); comingIndex++) {
comingBus = comingBuses[comingIndex];
var time = this.timeCleaning(comingBus.temps);
if (comingIndex == 0) {
depCell.innerHTML=time;
} else {
depCell.innerHTML=depCell.innerHTML+" / " + time;
}
}
row.appendChild(depCell);
table.appendChild(row);
}
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
var now = new Date();
this.caller = notification;
switch (notification) {
case "BUS":
this.busSchedules[payload.id] = payload.schedules;
this.busLastUpdate[payload.id] = payload.lastUpdate;
this.arretData[payload.id] = payload.arret;
this.loaded = true;
this.updateDom();
break;
case "UPDATE":
this.config.lastUpdate = payload.lastUpdate;
this.updateDom();
break;
}
}
});