forked from domsen123/mrx-work-traffic
-
Notifications
You must be signed in to change notification settings - Fork 26
/
MMM-MyCommute.js
496 lines (377 loc) · 13.7 KB
/
MMM-MyCommute.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*********************************
Magic Mirror Module:
MMM-MyCommute
By Jeff Clarke
Fork of mrx-work-traffic
By Dominic Marx
https://github.com/domsen123/mrx-work-traffic
MIT Licensed
*********************************/
Module.register('MMM-MyCommute', {
defaults: {
apikey: '',
origin: '65 Front St W, Toronto, ON M5J 1E6',
startTime: '00:00',
endTime: '23:59',
hideDays: [],
showSummary: true,
colorCodeTravelTime: true,
moderateTimeThreshold: 1.1,
poorTimeThreshold: 1.3,
nextTransitVehicleDepartureFormat: "[next at] h:mm a",
travelTimeFormat: "m [min]",
travelTimeFormatTrim: "left",
pollFrequency: 10 * 60 * 1000, //every ten minutes, in milliseconds
destinations: [
{
destination: '40 Bay St, Toronto, ON M5J 2X2',
label: 'Air Canada Centre',
mode: 'walking',
time: null
},
{
destination: '317 Dundas St W, Toronto, ON M5T 1G4',
label: 'Art Gallery of Ontario',
mode: 'transit',
time: null
},
{
destination: '55 Mill St, Toronto, ON M5A 3C4',
label: 'Distillery',
mode: 'bicycling',
time: null
},
{
destination: '6301 Silver Dart Dr, Mississauga, ON L5P 1B2',
label: 'Pearson Airport',
time: null
}
]
},
// Define required scripts.
getScripts: function() {
return ["moment.js", this.file("node_modules/moment-duration-format/lib/moment-duration-format.js")];
},
// Define required styles.
getStyles: function () {
return ["MMM-MyCommute.css", "font-awesome.css"];
},
travelModes: [
'driving',
'walking',
'bicycling',
'transit'
],
transitModes: [
'bus',
'subway',
'train',
'tram',
'rail'
],
avoidOptions: [
'tolls',
'highways',
'ferries',
'indoor'
],
// Icons to use for each transportation mode
symbols: {
'driving': 'car',
'walking': 'walk',
'bicycling': 'bike',
'transit': 'streetcar',
'tram': 'streetcar',
'bus': 'bus',
'subway': 'subway',
'train': 'train',
'rail': 'train',
'metro_rail': 'subway',
'monorail': 'train',
'heavy_rail': 'train',
'commuter_train': 'train',
'high_speed_train': 'train',
'intercity_bus': 'bus',
'trolleybus': 'streetcar',
'share_taxi': 'taxi',
'ferry': 'boat',
'cable_car': 'gondola',
'gondola_lift': 'gondola',
'funicular': 'gondola',
'other': 'streetcar'
},
start: function() {
Log.info('Starting module: ' + this.name);
this.predictions = new Array();
this.loading = true;
this.inWindow = true;
this.isHidden = false;
//start data poll
this.getData();
var self = this;
setInterval(function() {
self.getData();
}, this.config.pollFrequency);
},
/*
function isInWindow()
@param start
STRING display start time in 24 hour format e.g.: 06:00
@param end
STRING display end time in 24 hour format e.g.: 10:00
@param hideDays
ARRAY of numbers representing days of the week during which
this tested item shall not be displayed. Sun = 0, Sat = 6
e.g.: [3,4] to hide the module on Wed & Thurs
returns TRUE if current time is within start and end AND
today is not in the list of days to hide.
*/
isInWindow: function(start, end, hideDays) {
var now = moment();
var startTimeSplit = start.split(":");
var endTimeSplit = end.split(":");
var startTime = moment().hour(startTimeSplit[0]).minute(startTimeSplit[1]);
var endTime = moment().hour(endTimeSplit[0]).minute(endTimeSplit[1]);
if ( now.isBefore(startTime) || now.isAfter(endTime) ) {
return false;
} else if ( hideDays.indexOf( now.day() ) != -1) {
return false;
}
return true;
},
getData: function() {
//only poll if in window
if ( this.isInWindow( this.config.startTime, this.config.endTime, this.config.hideDays ) ) {
//build URLs
var destinations = new Array();
for(var i = 0; i < this.config.destinations.length; i++) {
var d = this.config.destinations[i];
var destStartTime = d.startTime || '00:00';
var destEndTime = d.endTime || '23:59';
var destHideDays = d.hideDays || [];
if ( this.isInWindow( destStartTime, destEndTime, destHideDays ) ) {
var url = 'https://maps.googleapis.com/maps/api/directions/json' + this.getParams(d);
destinations.push({ url:url, config: d});
console.log(url);
}
}
this.inWindow = true;
if (destinations.length > 0) {
this.sendSocketNotification("GOOGLE_TRAFFIC_GET", {destinations: destinations, instanceId: this.identifier});
} else {
this.hide(1000, {lockString: this.identifier});
this.inWindow = false;
this.isHidden = true;
}
} else {
this.hide(1000, {lockString: this.identifier});
this.inWindow = false;
this.isHidden = true;
}
},
getParams: function(dest) {
var params = '?';
params += 'origin=' + encodeURIComponent(this.config.origin);
params += '&destination=' + encodeURIComponent(dest.destination);
params += '&key=' + this.config.apikey;
//travel mode
var mode = 'driving';
if (dest.mode && this.travelModes.indexOf(dest.mode) != -1) {
mode = dest.mode;
}
params += '&mode=' + mode;
//transit mode if travelMode = 'transit'
if (mode == 'transit' && dest.transitMode) {
var tModes = dest.transitMode.split("|");
var sanitizedTransitModes = '';
for (var i = 0; i < tModes.length; i++) {
if (this.transitModes.indexOf(tModes[i]) != -1) {
sanitizedTransitModes += (sanitizedTransitModes == '' ? tModes[i] : "|" + tModes[i]);
}
}
if (sanitizedTransitModes.length > 0) {
params += '&transit_mode=' + sanitizedTransitModes;
}
}
if (dest.alternatives == true) {
params += '&alternatives=true';
}
if (dest.waypoints) {
var waypoints = dest.waypoints.split("|");
for (var i = 0; i < waypoints.length; i++) {
waypoints[i] = "via:" + encodeURIComponent(waypoints[i]);
}
params += '&waypoints=' + waypoints.join("|");
}
//avoid
if (dest.avoid) {
var a = dest.avoid.split("|");
var sanitizedAvoidOptions = '';
for (var i = 0; i < a.length; i++) {
if (this.avoidOptions.indexOf(a[i]) != -1) {
sanitizedAvoidOptions += (sanitizedAvoidOptions == '' ? a[i] : "|" + a[i]);
}
}
if (sanitizedAvoidOptions.length > 0) {
params += '&avoid=' + sanitizedAvoidOptions;
}
}
params += '&departure_time=now'; //needed for time based on traffic conditions
return params;
},
svgIconFactory: function(glyph) {
var svg = document.createElementNS('http://www.w3.org/2000/svg','svg');
svg.setAttributeNS(null, "class", "transit-mode-icon");
var use = document.createElementNS('http://www.w3.org/2000/svg', "use");
use.setAttributeNS("http://www.w3.org/1999/xlink", "href", "modules/MMM-MyCommute/icon_sprite.svg#" + glyph);
svg.appendChild(use);
return(svg);
},
formatTime: function(time, timeInTraffic) {
var timeEl = document.createElement("span");
timeEl.classList.add("travel-time");
if (timeInTraffic != null) {
timeEl.innerHTML = moment.duration(Number(timeInTraffic), "seconds").format(this.config.travelTimeFormat, {trim: this.config.travelTimeFormatTrim});
var variance = timeInTraffic / time;
if (this.config.colorCodeTravelTime) {
if (variance > this.config.poorTimeThreshold) {
timeEl.classList.add("status-poor");
} else if (variance > this.config.moderateTimeThreshold) {
timeEl.classList.add("status-moderate");
} else {
timeEl.classList.add("status-good");
}
}
} else {
timeEl.innerHTML = moment.duration(Number(time), "seconds").format(this.config.travelTimeFormat, {trim: this.config.travelTimeFormatTrim});
timeEl.classList.add("status-good");
}
return timeEl;
},
getTransitIcon: function(dest, route) {
var transitIcon;
if (dest.transitMode) {
var transitIcon = dest.transitMode.split("|")[0];
if (this.symbols[transitIcon] != null) {
transitIcon = this.symbols[transitIcon];
} else {
transitIcon = this.symbols[route.transitInfo[0].vehicle.toLowerCase()];
}
} else {
transitIcon = this.symbols[route.transitInfo[0].vehicle.toLowerCase()];
}
return transitIcon;
},
buildTransitSummary: function(transitInfo, summaryContainer) {
for (var i = 0; i < transitInfo.length; i++) {
var transitLeg = document.createElement("span");
transitLeg.classList.add('transit-leg');
transitLeg.appendChild(this.svgIconFactory(this.symbols[transitInfo[i].vehicle.toLowerCase()]));
var routeNumber = document.createElement("span");
routeNumber.innerHTML = transitInfo[i].routeLabel;
if (transitInfo[i].arrivalTime) {
routeNumber.innerHTML = routeNumber.innerHTML + " (" + moment(transitInfo[i].arrivalTime).format(this.config.nextTransitVehicleDepartureFormat) + ")";
}
transitLeg.appendChild(routeNumber);
summaryContainer.appendChild(transitLeg);
}
},
getDom: function() {
var wrapper = document.createElement("div");
if (this.loading) {
var loading = document.createElement("div");
loading.innerHTML = this.translate("LOADING");
loading.className = "dimmed light small";
wrapper.appendChild(loading);
return wrapper
}
for (var i = 0; i < this.predictions.length; i++) {
var p = this.predictions[i];
var row = document.createElement("div");
row.classList.add("row");
var destination = document.createElement("span");
destination.className = "destination-label bright";
destination.innerHTML = p.config.label;
row.appendChild(destination);
var icon = document.createElement("span");
icon.className = "transit-mode bright";
var symbolIcon = 'car';
if (this.config.destinations[i].color) {
icon.setAttribute("style", "color:" + p.config.color);
}
if (p.config.mode && this.symbols[p.config.mode]) {
symbolIcon = this.symbols[p.config.mode];
}
//different rendering for single route vs multiple
if (p.error) {
//no routes available. display an error instead.
var errorTxt = document.createElement("span");
errorTxt.classList.add("route-error");
errorTxt.innerHTML = "Error";
row.appendChild(errorTxt);
} else if (p.routes.length == 1 || !this.config.showSummary) {
var r = p.routes[0];
row.appendChild( this.formatTime(r.time, r.timeInTraffic) );
//summary?
if (this.config.showSummary) {
var summary = document.createElement("div");
summary.classList.add("route-summary");
if (r.transitInfo) {
symbolIcon = this.getTransitIcon(p.config,r);
this.buildTransitSummary(r.transitInfo, summary);
} else {
summary.innerHTML = r.summary;
}
row.appendChild(summary);
}
} else {
row.classList.add("with-multiple-routes");
for (var j = 0; j < p.routes.length; j++) {
var routeSummaryOuter = document.createElement("div");
routeSummaryOuter.classList.add("route-summary-outer");
var r = p.routes[j];
routeSummaryOuter.appendChild( this.formatTime(r.time, r.timeInTraffic) );
var summary = document.createElement("div");
summary.classList.add("route-summary");
if (r.transitInfo) {
symbolIcon = this.getTransitIcon(p.config,r);
this.buildTransitSummary(r.transitInfo, summary);
} else {
summary.innerHTML = r.summary;
}
routeSummaryOuter.appendChild(summary);
row.appendChild(routeSummaryOuter);
}
}
var svg = this.svgIconFactory(symbolIcon);
icon.appendChild(svg);
row.appendChild(icon);
wrapper.appendChild(row);
}
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
if ( notification === 'GOOGLE_TRAFFIC_RESPONSE' + this.identifier ) {
this.predictions = payload;
if (this.loading) {
this.loading = false;
if (this.isHidden) {
this.updateDom();
this.show(1000, {lockString: this.identifier});
} else {
this.updateDom(1000);
}
} else {
this.updateDom();
this.show(1000, {lockString: this.identifier});
}
this.isHidden = false;
}
},
notificationReceived: function(notification, payload, sender) {
if ( notification == 'DOM_OBJECTS_CREATED' && !this.inWindow) {
this.hide(0, {lockString: this.identifier});
this.isHidden = true;
}
}
});