forked from CFenner/MMM-LocalTransport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocaltransport.js
More file actions
134 lines (134 loc) · 4.25 KB
/
Copy pathlocaltransport.js
File metadata and controls
134 lines (134 loc) · 4.25 KB
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
/* global Module */
/* Magic Mirror
* Module: localtransport
*
* By Christopher Fenner https://github.com/CFenner
* MIT Licensed.
*/
Module.register('localtransport', {
defaults: {
animationSpeed: 1,
updateInterval: 5,
mode: 'transit',
traffic_model: 'best_guess',
departure_time: 'now',
language: 'de',
units: 'metric',
alternatives: true,
maxAlternatives: 3,
absoluteTime: false,
showDuration: false,
apiBase: 'https://maps.googleapis.com/',
apiEndpoint: 'maps/api/directions/json'
},
start: function() {
Log.info('Starting module: ' + this.name);
this.loaded = false;
this.url = this.config.apiBase + this.config.apiEndpoint + this.getParams();
this.update();
// refresh every x minutes
setInterval(
this.update.bind(this),
this.config.updateInterval * 60 * 1000);
},
update: function() {
this.sendSocketNotification(
'LOCAL_TRANSPORT_REQUEST',
this.config.apiBase + this.config.apiEndpoint + this.getParams());
},
getParams: function() {
var params = '?';
params += 'mode=' + this.config.mode;
params += '&origin=' + this.config.origin;
params += '&destination=' + this.config.destination;
params += '&key=' + this.config.api_key;
params += '&traffic_model=' + this.config.traffic_model;
params += '&departure_time=' + this.config.departure_time;
params += '&alternatives=' + this.config.alternatives;
return params;
},
renderLeg: function(wrapper, leg){
var depature = leg.departure_time.value * 1000;
var arrival = leg.arrival_time.value * 1000;
var span = document.createElement("div");
if (!this.config.absoluteTime) {
span.innerHTML = moment(depature).fromNow()
} else {
if (config.timeFormat !== 24) {
span.innerHTML = this.translate('ABSOLUTE_PREFIX') + moment(depature).format('h:mm A')
} else {
span.innerHTML = this.translate('ABSOLUTE_PREFIX') + moment(depature).format('HH:mm')
}
}
if (this.config.showDuration) {
span.innerHTML += "(" + moment.duration(moment(arrival).diff(depature, 'minutes'), 'minutes').humanize() + ")";
}
// + this.translate('TRAVEL_TIME') + ": "
// + moment.duration(moment(arrival).diff(depature, 'minutes'), 'minutes').humanize()
;
wrapper.appendChild(span);
},
renderStep: function(wrapper, step){
if(step.travel_mode === "WALKING"){
return; // skip walking
}
var details = step.transit_details;
if(details) {
var img = document.createElement("img");
img.src = details.line.vehicle.local_icon || ("http:" + details.line.vehicle.icon);
wrapper.appendChild(img);
var span = document.createElement("span");
span.innerHTML = details.line.short_name || details.line.name;
span.className = "bright";
wrapper.appendChild(span);
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'LOCAL_TRANSPORT_RESPONSE') {
Log.info('received' + notification);
if(payload && payload.status === "OK"){
this.data = payload;
this.loaded = true;
this.updateDom(this.config.animationSpeed * 1000);
}
}
},
getStyles: function() {
return ["localtransport.css"];
},
getScripts: function() {
return ["moment.js"];
},
getTranslations: function() {
return {
de: "i18n/de.json",
en: "i18n/en.json"
};
},
getDom: function() {
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING_CONNECTIONS");
}else{
var ul = document.createElement("ul");
for(var routeKey in this.data.routes) {
if(Number(routeKey) >= this.config.maxAlternatives){
break;
}
var route = this.data.routes[routeKey];
var li = document.createElement("li");
for(var legKey in route.legs) {
var leg = route.legs[legKey];
for(var stepKey in leg.steps) {
var step = leg.steps[stepKey];
this.renderStep(li, step);
}
this.renderLeg(li, leg);
}
ul.appendChild(li);
}
wrapper.appendChild(ul);
}
return wrapper;
}
});