-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
254 lines (214 loc) · 8.38 KB
/
node_helper.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const NodeHelper = require('node_helper');
const Log = require("logger");
module.exports = NodeHelper.create({
start() {
Log.info(`Starting module: ${this.name} with identifier: ${this.identifier}`);
this.apiKey = null;
this.outstandingTrainTimeRequest = false;
this.outstandingBusTimeRequest = false;
this.outstandingTrainIncidentRequest = false;
this.outstandingBusIncidentRequest = false;
},
socketNotificationReceived(notification, payload) {
switch (notification) {
case "WMATA_INIT":
this.apiKey = payload.apiKey;
this.initComplete(payload);
break;
case "WMATA_TRAIN_TIMES_GET":
this.getTrainTimes(payload);
break;
case "WMATA_BUS_TIMES_GET":
this.getBusTimes(payload);
break;
case "WMATA_TRAIN_INCIDENTS_GET":
this.getTrainIncidents(payload);
break;
case "WMATA_BUS_INCIDENTS_GET":
this.getBusIncidents(payload);
break;
}
},
initComplete(payload) {
this.sendSocketNotification("WMATA_INITIALIZED", {
identifier: payload.identifier
});
},
getTrainTimes(payload) {
const trainQuery = payload.stations.join(",");
const url = `https://api.wmata.com/StationPrediction.svc/json/GetPrediction/${trainQuery}`;
// TODO: Error handling
fetch(url, {
method: "GET",
headers: {
"api_key": this.apiKey,
}
})
.then((response) => {
return response.json();
})
.then((data) => {
const trainDataRaw = data['Trains'];
const trainDataFormatted = trainDataRaw.map((trainData) => this.formatTrainData(trainData));
this.sendSocketNotification("WMATA_TRAIN_TIMES_DATA", {
identifier: payload.identifier,
trainData: trainDataFormatted,
});
})
;
},
formatTrainData(data) {
return {
...data,
...{'MinNumber': this.normalizeTrainMinutes(data['Min']) }
};
},
normalizeTrainMinutes(value) {
if (value === 'BRD' || value === 'ARR') {
return 0;
} else if (value === "---" || value === null) {
return -1;
} else {
return parseInt(value);
}
},
getBusTimes(payload) {
console.debug(payload.busStops);
const busPredictions = {};
const busFetches = payload.busStops.map(stopID => this.getBusStopPrediction(stopID));
Promise.all(busFetches)
.then(responses => {
responses.map((r) => {
busPredictions[r.stopID] = r;
});
})
.then(() => {
console.debug(busPredictions);
})
.then(() => {
this.sendSocketNotification("WMATA_BUS_TIMES_DATA", {
identifier: payload.identifier,
busPredictions
});
});
},
getBusStopPrediction(stopID) {
const url = `https://api.wmata.com/NextBusService.svc/json/jPredictions?StopID=${stopID}`;
return fetch(url, {
method: "GET",
headers: {
"api_key": this.apiKey,
}
})
.then((response) => {
return response.json();
})
.then((data) => {
const stopPredictions = data['Predictions'];
return { stopID: stopID,
predictions: stopPredictions,
locationName: data['StopName']};
});
},
getTrainIncidents(payload) {
const url = "https://api.wmata.com/Incidents.svc/json/Incidents";
console.log("Fetching train updates");
fetch(url, {
method: "GET",
headers: {
"api_key": this.apiKey,
}
})
.then((response) => {
return response.json();
})
.then((data) => {
const incidents = data['Incidents'];
const trainAlerts = new Set();
const trainDelays = new Set();
incidents
.filter((incident) => { return incident['IncidentType'] === 'Alert'; })
.map((incident) => {
console.log(`split from ${incident['LinesAffected']} is ${incident['LinesAffected'].split("; ")}`);
return incident['LinesAffected']
.split(";")
.map((line) => line.trim())
.filter((line) => line !== '');
})
.forEach((incidentLine) => {
trainAlerts.add(...incidentLine);
});
incidents
.filter((incident) => incident['IncidentType'] === 'Delay')
.map((incident) => {
return incident['LinesAffected']
.split(";")
.map((line) => line.trim())
.filter((line) => line !== '');
})
.forEach((incidentLine) => {
trainDelays.add(...incidentLine);
});
// TODO: WMATA claims that the incidents are *usually* either Alert or Delay, but it's subject to
// change at any time. It's probably worth doing a filter for incidents that are not alert / delay and
// pass them back to the frontend.
this.sendSocketNotification("WMATA_TRAIN_INCIDENTS_DATA", {
identifier: payload.identifier,
trainAlerts: Array.from(trainAlerts),
trainDelays: Array.from(trainDelays)
});
});
},
getBusIncidents(payload) {
console.log("Getting bus incidents");
const url = "https://api.wmata.com/Incidents.svc/json/BusIncidents";
fetch(url, {
method: "GET",
headers: {
"api_key": this.apiKey,
}
})
.then((response) => {
return response.json();
})
.then((data) => {
const incidents = data['BusIncidents'];
if (payload.busIncidentRoutes !== null) {
incidents.filter((incident) => {
for (const routeAffected of incident['RoutesAffected']) {
if (payload.busIncidents.includes(routeAffected)) {
return true;
}
}
return false;
});
}
const busAlerts = new Set();
const busDelays = new Set();
console.debug(incidents);
incidents
.filter((incident) => incident['IncidentType'] === 'Delay')
.forEach((incident) => {
busDelays.add(...incident['RoutesAffected']);
});
incidents
.filter((incident) => incident['IncidentType'] === 'Alert')
.map((incident) => incident['RoutesAffected'])
.forEach((incidentRoutes) => {
console.debug(incidentRoutes);
busAlerts.add(...incidentRoutes);
});
// TODO: WMATA claims that the incidents are *usually* either Alert or Delay, but it's subject to
// change at any time. It's probably worth doing a filter for incidents that are not alert / delay and
// pass them back to the frontend.
console.log("bus incidents");
console.debug(busAlerts);
console.debug(busDelays);
this.sendSocketNotification("WMATA_BUS_INCIDENTS_DATA", {
identifier: payload.identifier,
busAlerts: Array.from(busAlerts),
busDelays: Array.from(busDelays),
});
});
},
});