-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-HK-Transport-ETA.js
231 lines (193 loc) · 5.65 KB
/
MMM-HK-Transport-ETA.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
/* global ETAProvider */
/* MagicMirror²
* Module: MMM-HK-Transport-ETA
*
* By Winston Ma https://github.com/winstonma
* AGPL-3.0 Licensed.
*/
Module.register("MMM-HK-Transport-ETA", {
// Default module config.
defaults: {
transportETAProvider: "mtr",
sta: "Hong Kong",
reloadInterval: 1 * 60 * 1000, // every 1 minute
updateInterval: 5 * 1000, // every 5 seconds
animationSpeed: 2500,
timeFormat: config.timeFormat,
lang: config.language,
initialLoadDelay: 0, // 0 seconds delay
tableClass: "small",
colored: false,
showHeader: false
},
// Module properties.
transportETAProvider: null,
// Can be used by the provider to display location of event if nothing else is specified
firstEvent: null,
// Define required scripts.
getStyles: function () {
return ["font-awesome.css", "MMM-HK-Transport-ETA.css"];
},
// Return the scripts that are necessary for the ETA module.
getScripts: function () {
return ["moment.js", this.file("../default/utils.js"), "hktransportetaprovider.js", "etaobject.js", this.file(`providers/${this.config.transportETAProvider.toLowerCase()}.js`)];
},
// Override getHeader method.
getHeader: function () {
return this.transportETAProvider.currentETA() ?
this.transportETAProvider.currentETA()[0].station : `${this.data.classes}-${this.config.transportETAProvider}`;
},
getTranslations() {
return {
en: "translations/en.json",
"zh-tw": "translations/zh-tw.json",
};
},
// Start the ETA module.
start: function () {
this.loaded = false;
this.displayRelativeTime = false;
this.error = null;
// Moment.js config
moment.locale(this.config.lang);
moment.relativeTimeThreshold('m', 60);
const momentLanguageConfigData = {
"en": {
relativeTime: {
s: "just now",
ss: "just now",
m: '%dm',
mm: '%dm',
h: '%dh',
hh: '%dh',
}
},
"zh-tw": {
relativeTime: {
s: "現在",
ss: "現在",
m: '%d分',
mm: '%d分',
h: '%d小時',
hh: '%d小時',
}
},
};
const lang = this.config.lang.startsWith("zh") ? "zh-tw" : "en";
moment.updateLocale(lang, momentLanguageConfigData[lang]);
// Initialize the ETA provider.
this.transportETAProvider = HKTransportETAProvider.initialize(this.config.transportETAProvider, this);
// Add custom filters
this.addFilters();
if (this.config.transportETAProvider === "kmb") {
this.sendSocketNotification("ADD_KMB_STOP", this.config.sta);
} else {
// Let the ETA provider know we are starting.
this.transportETAProvider.start();
// Schedule the first update.
this.scheduleUpdate(this.config.initialLoadDelay);
}
},
socketNotificationReceived: function (notification, payload) {
if (notification === "KMB_STOP_ITEM" && payload.station === this.config.sta) {
this.config.stops = payload.data;
const config = Object.assign({}, this.transportETAProvider.defaults, this.config);
this.transportETAProvider.setConfig(config);
// Let the ETA provider know we are starting.
this.transportETAProvider.start();
// Schedule the first update.
this.scheduleUpdate(this.config.initialLoadDelay);
}
},
// Override notification handler.
notificationReceived: function (notification, payload, sender) {
},
// Select the template depending on the display type.
getTemplate: function () {
return "eta.njk";
},
// Add all the data to the template.
getTemplateData: function () {
if (this.error) {
return {
error: this.error
};
}
return {
config: this.config,
currentETA: this.transportETAProvider.currentETA()
};
},
// What to do when the HK Transport ETA provider has new information available?
updateAvailable: function () {
Log.log("New ETA information available.");
if (this.config.updateInterval !== 0 && !this.loaded) {
this.scheduleUpdateInterval();
} else {
this.updateDom(this.config.animationSpeed);
}
this.scheduleUpdate();
this.loaded = true;
},
/**
* Schedule visual update.
*/
scheduleUpdateInterval: function () {
this.updateDom(this.config.animationSpeed);
const currentTime = Date.now();
const nextLoad = this.config.updateInterval - (currentTime % this.config.updateInterval);
this.config.displayRelativeTime = Math.round(currentTime / this.config.updateInterval) % 2;
if (this.timer) clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.scheduleUpdateInterval();
}, nextLoad);
},
scheduleUpdate: function (delay = null) {
let nextLoad = this.config.reloadInterval;
if (delay !== null && delay >= 0) {
nextLoad = delay;
}
setTimeout(() => {
this.transportETAProvider.fetchETA();
}, nextLoad);
},
addFilters() {
this.nunjucksEnvironment().addFilter(
"formatTime",
function (date) {
if (Array.isArray(date)) {
const retArray = date.map(singleDate => {
singleDate = moment(singleDate);
if (this.config.timeFormat !== 24) {
return singleDate.format("h:mm")
}
return singleDate.format("HH:mm");
});
return retArray.toString();
} else {
date = moment(date);
if (this.config.timeFormat !== 24) {
return date.format("h:mm");
}
return date.format("HH:mm");
}
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"fromNow",
function (dateArray) {
return dateArray.map(date => moment(date).fromNow(true)).toString();
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"json",
function (value, spaces) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString()
}
const jsonString = JSON.stringify(value, null, spaces).replace(/</g, '\\u003c')
return nunjucks.runtime.markSafe(jsonString)
}.bind(this)
);
}
});