-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-TrainTimesRTT.js
93 lines (79 loc) · 2.16 KB
/
MMM-TrainTimesRTT.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
/* eslint-disable */
Module.register("MMM-TrainTimesRTT", {
defaults: {
origin: "LDS",
username: undefined,
password: undefined,
amount: 5,
refreshTime: 5
},
__getData() {
this.sendSocketNotification("GetTrains", {
origin: this.config.origin,
username: this.config.username,
password: this.config.password
});
},
start() {
this.nunjucksEnvironment().addFilter("trainTime", (text) => {
if (text) {
return `${text.substr(0, 2)}:${text.substr(2)}`;
}
});
this.nunjucksEnvironment().addFilter(
"compareTime",
(bookedTime, realTime, type) => {
const parseTime = (timeStr) => {
const [hoursStr, minutesStr] = timeStr.match(/\d{2}/g);
const hours = parseInt(hoursStr, 10);
const minutes = parseInt(minutesStr, 10);
return hours * 60 + minutes;
};
if (bookedTime && realTime) {
const bookedMinutes = parseTime(bookedTime);
const realMinutes = parseTime(realTime);
const timeDifference = realMinutes - bookedMinutes;
if (timeDifference > 0) {
return {
text: "Late",
class: timeDifference < 10 ? "orange" : "red"
};
} else if (timeDifference < 0) {
return { text: "Early", class: "lightblue" };
} else {
return { text: "On Time", class: "green" };
}
} else {
return { text: "BUS", class: "darkred" };
}
}
);
setTimeout(() => {
setInterval(() => {
this.__getData();
}, this.config.refreshTime * 60000);
this.__getData();
}, 1000);
},
socketNotificationReceived(id, payload) {
if (id === "GetTrainsResult") {
this.payload = payload;
this.updateDom(300);
}
},
getTemplateData() {
return {
data: this.payload,
config: this.config
};
},
getScripts() {
return ["moment.js"];
},
getTemplate() {
return "TrainInfo.njk";
},
getStyles() {
return ["trains.css"];
}
});