-
Notifications
You must be signed in to change notification settings - Fork 11
/
MMM-Entur-tavle.js
156 lines (146 loc) · 5.4 KB
/
MMM-Entur-tavle.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
Module.register("MMM-Entur-tavle", {
defaults: {
ETApiUrl: "https://api.entur.io/journey-planner/v3/graphql",
ETClientName: "MMM-Entur-tavle-v3api",
stopId: "12345",
stopType: "StopPlace", // StopPlace or Quay - case sensitive.
numResults: 5,
authorityId: "NSR",
highlightRealtime: false,
showHeader: true,
updateSpeed: 1000,
size: "medium",
refresh: 30,
showTransportMode: false,
timeOffset: [0, "seconds"],
exclusions: [],
whiteListedTransportModes: []
},
getStyles() {
return ["font-awesome.css"];
},
getScripts() {
return ["moment.js"];
},
getTranslations() {
return {
en: "translations/en.json",
nb: "translations/nb.json",
nn: "translations/nn.json"
};
},
start() {
const self = this;
this.fullId = `NSR:${this.config.stopType}:${this.config.stopId}`;
this.journeys = [];
this.getDepartures();
setInterval(() => {
self.getDepartures();
}, this.config.refresh * 1000);
},
getDepartures() {
const startTime = moment().add(moment.duration(this.config.timeOffset[0], this.config.timeOffset[1]));
const payload = {
url: this.config.ETApiUrl,
ETClientName: this.config.ETClientName,
id: this.config.stopId,
stopType: this.config.stopType,
authorityId: this.config.authorityId,
numResults: this.config.numResults,
whiteListedTransportModes: this.config.whiteListedTransportModes,
startTime: startTime.toISOString()
};
this.sendSocketNotification("GET_DEPARTURES", payload);
},
getCell(cellText, className) {
const cell = document.createElement("td");
if (className) {
cell.className = className;
}
cell.innerHTML = cellText;
return cell;
},
getDom() {
const wrapper = document.createElement("div");
wrapper.className = `align-left light bright ${this.config.size}`;
if (this.journeys.length > 0) {
const table = document.createElement("table");
if (this.config.showHeader) {
const hrow = document.createElement("div");
hrow.className = "light small align-right";
hrow.innerHTML = this.quayName;
wrapper.appendChild(hrow);
}
for (const journey of this.journeys) {
const exclusions = this.config.exclusions.map(excl => excl.toLowerCase());
const { publicCode } = journey.serviceJourney.journeyPattern.line;
if (exclusions.includes(publicCode.toLowerCase())) {
continue;
}
const row = document.createElement("tr");
if (this.config.highlightRealtime && journey.realtime === true) {
row.className += " regular";
}
if (this.config.showTransportMode) {
const icon = document.createElement("i");
icon.className = this.getTransportIcon(journey.serviceJourney.journeyPattern.line.transportMode);
icon.innerHTML = " ";
row.appendChild(icon);
row.appendChild(this.getCell(" "));
}
row.appendChild(this.getCell(publicCode, "align-left"));
row.appendChild(this.getCell(" "));
row.appendChild(this.getCell(journey.destinationDisplay.frontText));
row.appendChild(this.getCell(" "));
row.appendChild(this.getCell(this.getDepartureTime(moment().local().toISOString(), journey.expectedDepartureTime), "align-right"));
table.appendChild(row);
}
wrapper.appendChild(table);
}
else {
wrapper.innerHTML = this.translate("LOADING");
}
return wrapper;
},
socketNotificationReceived(message, payload) {
if (message === "DEPARTURE_LIST" && payload.id === this.fullId) {
this.quayName = payload.name;
this.journeys = payload.estimatedCalls;
this.updateDom(this.config.updateSpeed);
}
},
getTransportIcon(type) {
switch (type) {
case "bus":
return "fa fa-bus";
case "bike":
return "fa fa-bicycle";
case "water":
return "fa fa-ship";
case "metro":
return "fa fa-subway";
case "rail":
return "fas fa-train";
case "tram":
return "fa fa-subway";
default:
return null;
}
},
getDepartureTime(queryTime, departureTime) {
const diffSeconds = moment(departureTime).diff(queryTime, "seconds");
const diffMinutes = moment(departureTime).diff(queryTime, "minutes");
if (diffSeconds < 0) {
return this.translate("departed");
}
else if (diffSeconds < 60) {
return this.translate("now");
}
else if (diffSeconds < 600) {
const min = this.translate("min");
return `${diffMinutes} ${min}`;
}
return moment(departureTime).local()
.format("HH:mm");
}
});