-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-NexusMetroRTI.js
217 lines (179 loc) · 6.31 KB
/
MMM-NexusMetroRTI.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
/* Copyright (C) 2024 jcktwd */
Module.register("MMM-NexusMetroRTI", {
// Module config defaults.
defaults: {
updateCacheOnStart: false,
updateInterval: 15000,
colorize: true,
showStation: true,
showPlatform: true,
platformFormat: "{{helperText}}",
showDestination: true,
showLocation: true,
locationFormat: "{{eventTypeString}} {{eventStation}}",
locationTimeFormat: "HH:mm",
showDueIn: true,
minETA: -1,
maxETA: -2,
maxTrains: 4,
station: null,
platform: null,
},
baseURL: "https://metro-rti.nexus.org.uk/api",
rti: [],
_platforms: null,
_stations: null,
templateData: {
platform: "Platform",
station: "Station",
trains: [],
},
// Translating API response to friendly format
locationEventStringTrans: {
"APPROACHING": "Approaching",
"ARRIVED": "Arrived at",
"DEPARTED": "Departed from",
"READY_TO_START": "Starting at",
},
// Translating API response to emoji format
locationEventEmojiTrans: {
"APPROACHING": "🛤️➡️",
"ARRIVED": "🚉🛑",
"DEPARTED": "🚉➡️",
"READY_TO_START": "🛤️🛑",
},
// Define required scripts.
getScripts () {
return ["moment.js"];
},
getStyles () {
return ["MMM-NexusMetroRTI.css", "font-awesome.css",];
},
// Define start sequence.
async start () {
Log.info(`Starting module: ${this.name}`);
if (this.config.updateCacheOnStart) {
this.sendSocketNotification("UPDATE_CACHE", {});
};
Object.defineProperty(this, "platforms", {
get: function() {
return (this._platforms ?? true) ? this.getPlatforms() : this._platforms
}
});
Object.defineProperty(this, "stations", {
get: function() {
return (this._stations ?? true) ? this.getStations() : this._stations
}
});
// Process the config properties
await this.processConfig()
await this.updateTrains();
this.updateDom()
// Schedule update timer.
setInterval(async () => {
await this.updateTrains();
this.updateDom();
}, this.config.updateInterval);
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "UPDATED_CACHE":
this._stations = null;
this._platforms = null;
console.log("Updating cached stations and platforms.")
break;
}
},
async getStations () {
return this._stations = await (await fetch(this.file("public/cache/stations.json"))).json();
},
async getPlatforms () {
return this._platforms = await (await fetch(this.file("public/cache/platforms.json"))).json();
},
async updateTrains() {
let response = await fetch(
`cors?url=${this.baseURL}/times/${this.config.station}/${this.config.platform}`
);
this.rti = await response.json();
// process data
let trainTemplateData = [];
for (const train of this.rti) {
// Skip processing trains that are not within specified ETA range
if ((train.dueIn > this.config.maxETA && this.config.maxETA != -2)
|| train.dueIn < this.config.minETA){
continue;
}
// Stop processing trains once we have enough
if (trainTemplateData.length >= this.config.maxTrains) {
break;
}
data = {
trainNumber: train.trn,
event: train.lastEvent,
eventTypeString: this.locationEventStringTrans[train.lastEvent] ?? train.lastEvent,
eventStation: train.lastEventLocation.substring(
0,
train.lastEventLocation.lastIndexOf(" Platform")
),
eventPlatform: train.lastEventLocation.substring(
train.lastEventLocation.lastIndexOf("Platform")
),
eventTime: moment(train.lastEventTime).format(
this.config.locationTimeFormat
),
destination: train.destination,
dueIn: train.dueIn,
line: train.line,
}
data.locationString = this.nunjucksEnvironment().renderString(
this.config.locationFormat, data
);
trainTemplateData.push(data);
}
this.templateData.trains = trainTemplateData;
},
async processConfig() {
if (this.config.station === null){
this.sendSocketNotification("ERR", {
msg: `'station' is a required config property`
});
};
stations = (await this.stations)
if (!Object.hasOwn(stations, this.config.station)){
this.sendSocketNotification("ERR", {
msg: `Station with code ${this.config.station} does not exist.`
});
};
if (this.config.platform === null){
this.sendSocketNotification("ERR", {
msg: `'platform' is a required config property`
});
};
platforms = (await this.platforms)
const possibleNums = platforms[this.config.station].map(({platformNumber}) => platformNumber);
if (!possibleNums.includes(this.config.platform)) {
this.sendSocketNotification(
"ERR",
{
msg: `platform "${this.config.platform}" is not at station "${this.config.station}" Possible values were ${platforms[this.config.station]}`
}
);
};
this.templateData.platform = this.nunjucksEnvironment().renderString(
this.config.platformFormat,
platforms[this.config.station].find(
({platformNumber}) => this.config.platform === platformNumber
)
);
this.templateData.station = stations[this.config.station];
},
getTemplate () {
return "MMM-NexusMetroRTI.njk";
},
getTemplateData () {
return {
config: this.config,
data: this.templateData,
}
},
})