-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransitAlerts.js
executable file
·326 lines (304 loc) · 15.8 KB
/
transitAlerts.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Written by Joshua Fabian, [email protected]
// MBTA alerts (updates once every 120 seconds)
var staleAlertThreshold = 800000000;
var currentTransitAlertsIds = [];
var stopsFilter = '';
if (transitStops != '') {
stopsFilter = '&filter[stop]=' + transitStops;
}
var routesFilter = '';
if (transitRoutes != '') {
routesFilter = '&filter[route]=' + transitRoutes;
}
var causeDisplayLookup = {
UNKNOWN_CAUSE: '',
AUTOS_IMPEDING_SERVICE: 'due to impeding auto',
POLICE_ACTION: 'due to police action',
POLICE_ACTIVITY: 'due to police action',
TRAFFIC: 'due to traffic',
CONGESTION: 'due to congestion',
CONSTRUCTION: 'due to construction',
MAINTENANCE: 'due to maintenance',
SPECIAL_EVENT: 'due to special event',
FIRE: 'due to fire',
ACCIDENT: 'due to crash',
DEMONSTRATION: 'due to unrest',
DISABLED_BUS: 'due to disabled bus',
DISABLED_TRAIN: 'due to disabled train',
HEAVY_RIDERSHIP: 'due to heavy crowds',
MECHANICAL_PROBLEM: 'due to mech issue',
MEDICAL_EMERGENCY: 'due to EMS activity',
POWER_PROBLEM: 'due to power problem',
SEVERE_WEATHER: 'due to weather',
SIGNAL_PROBLEM: 'due to signal issue',
SWITCH_PROBLEM: 'due to switch issue',
TECHNICAL_PROBLEM: 'due to mech issue',
UNRULY_PASSENGER: 'due to unrest',
WEATHER: 'due to weather'
};
var effectDisplayLookup = {
SUSPENSION: 'Suspension',
DELAY: 'Delays',
STATION_ISSUE: 'Station issue',
SHUTTLE: 'Bus shuttle',
SERVICE_CHANGE: 'Service change',
DETOUR: 'Detour',
TRACK_CHANGE: 'Track change',
STOP_CLOSURE: 'Stop closure'
};
var severityDisplayLookup = {
3: 'up to 10m',
4: 'up to 15m',
5: 'up to 20m',
6: 'up to 25m',
7: 'up to 30m',
8: 'of 30m+',
9: 'of 60m+'
}
var severityCategoryLookup = {
3: 'minor',
4: 'moderate',
5: 'moderate',
6: 'severe',
7: 'severe',
8: 'severe',
9: 'severe'
}
var transitAlertsUpdate = function nextServiceUpdate() {
// Testing 19 May
console.log("((( CHECKING TRANSIT ALERTS NOW - PREVIOUS OPEN:")
console.log(currentTransitAlertsIds)
console.log("((( - - - - - - - - - - -")
jQuery(document).ready(function($) {
$.ajax({
url : "https://api-v3.mbta.com/alerts?include=routes&" + routesFilter,
dataType : "json",
success : function(parsed_json) {
// Testing 19 May
console.log("((( GOOD RESPONSE RE ALERTS")
console.log("((( - - - - - - - - - - -")
var allAlerts = parsed_json['data'];
var allIncluded = parsed_json['included'];
var predictions = [];
var infoAboutRoutes = {};
var infoAboutAlerts = [];
var parsedAlerts = [];
for (i = 0; i < allIncluded.length; i++) {
if (allIncluded[i]['type'] == 'route') {
var routeId = allIncluded[i]['id'];
var textColor = allIncluded[i]['attributes']['text_color'];
var backgroundColor = allIncluded[i]['attributes']['color'];
var longName = allIncluded[i]['attributes']['long_name'];
var shortName = allIncluded[i]['attributes']['short_name'];
if (routeId.startsWith("Green-")) {
shortName = "Green Line " + shortName;
}
infoAboutRoutes[routeId] = {};
infoAboutRoutes[routeId]['textColor'] = textColor;
infoAboutRoutes[routeId]['backgroundColor'] = backgroundColor;
infoAboutRoutes[routeId]['longName'] = longName;
infoAboutRoutes[routeId]['shortName'] = shortName;
}
}
for (i = 0; i < allAlerts.length; i++) {
var alertId = allAlerts[i]['id'];
var routeId = allAlerts[i]['attributes']['informed_entity'][0]['route'];
var cause = causeDisplayLookup[allAlerts[i]['attributes']['cause']];
var effect = allAlerts[i]['attributes']['effect'];
var effectDisplay = effectDisplayLookup[effect];
var header = allAlerts[i]['attributes']['header'];
var severity = allAlerts[i]['attributes']['severity'];
var severityDisplay = severityDisplayLookup[severity];
var severityCategory = severityCategoryLookup[severity];
newAlert = {};
newAlert['alertId'] = alertId;
newAlert['routeId'] = routeId;
newAlert['cause'] = cause;
newAlert['causeDisplay'] = cause;
newAlert['effect'] = effect;
newAlert['effectDisplay'] = effectDisplay;
newAlert['description'] = header;
newAlert['severity'] = severity;
newAlert['severityDisplay'] = severityDisplay;
newAlert['severityCategory'] = severityCategory;
if (allAlerts[i]['attributes']['lifecycle'] != 'NEW' && allAlerts[i]['attributes']['lifecycle'] != 'ONGOING') {
continue;
}
for (j = 0; j < allAlerts[i]['attributes']['active_period'].length; j++) {
// Remove stale alerts having currently-active period more than 5 days running
var currentPeriod = allAlerts[i]['attributes']['active_period'][j];
if (currentPeriod['end'] == null) {
var startTime = (new Date(currentPeriod['start'])).getTime();
if (startTime < Date.now() - staleAlertThreshold) {
continue;
} else {
infoAboutAlerts.push(newAlert);
break;
}
// IF START TIME IS MORE THAN 5 DAYS BEFORE Date.now(), CONTINUE
} else {
var startTime = (new Date(currentPeriod['start'])).getTime();
var endTime = (new Date(currentPeriod['end'])).getTime();
if (startTime > Date.now() || endTime < Date.now()) {
continue;
} else if (startTime < Date.now() - staleAlertThreshold) {
continue;
} else {
infoAboutAlerts.push(newAlert);
break;
}
}
}
}
for (i = 0; i < infoAboutAlerts.length; i++) {
var routeId = infoAboutAlerts[i]['routeId'];
if (!routeId) {
continue;
}
var routeDisplay = infoAboutRoutes[routeId]['shortName'];
if (routeDisplay == '') {
routeDisplay = infoAboutRoutes[routeId]['longName'];
}
var textColor = infoAboutRoutes[routeId]['textColor'];
var backgroundColor = infoAboutRoutes[routeId]['backgroundColor'];
var effectDisplay = infoAboutAlerts[i]['effectDisplay'];
if (!effectDisplay) {
effectDisplay = '';
}
var severityDisplay = infoAboutAlerts[i]['severityDisplay'];
var causeDisplay = infoAboutAlerts[i]['causeDisplay'];
if (!causeDisplay) {
causeDisplay = '';
}
if (!severityDisplay || effectDisplay != 'Delays') {
severityDisplay = '';
}
var description = infoAboutAlerts[i]['description'];
if (!description) {
description = '';
}
if (effectDisplay != 'Delays' && effectDisplay != 'Bus shuttle' && effectDisplay != 'Detour' && effectDisplay != 'Suspension') {
continue;
}
if (infoAboutAlerts[i]['severityCategory'] == 'severe' && displayAlertsSevere == false && displayAlertsMinor == false) {
console.log("Removing alert due to displayAlertsSevere == false")
console.log(infoAboutAlerts[i]['alertId'])
continue;
}
if (infoAboutAlerts[i]['severityCategory'] != 'severe' && displayAlertsMinor == false) {
console.log("Removing alert due to displayAlertsMinor == false")
console.log(infoAboutAlerts[i]['alertId'])
continue;
}
if (overnightMode == true) {
console.log("Removing alert due to overnightMode == true")
console.log(infoAboutAlerts[i]['alertId'])
continue;
}
htmlForAlert = '';
htmlForAlert += '<div class="transit-alert-container ' + 'normal-colors' + '"><h2 class="transitAlert">'
htmlForAlert += '<span class="transitAlertType">';
htmlForAlert += '<span class="transit-alert-route" style="color: #' + textColor;
htmlForAlert += '; background-color: #' + backgroundColor + '"> ';
htmlForAlert += routeDisplay + ' </span><br>';
htmlForAlert += '<span class="transit-alert-title">' + effectDisplay + ' ';
htmlForAlert += severityDisplay + ' ' + causeDisplay + '</span>'
htmlForAlert += '</h2>' + description + '</div>';
parsedAlert = {};
parsedAlert['alertId'] = infoAboutAlerts[i]['alertId'];
parsedAlert['severityCategory'] = infoAboutAlerts[i]['severityCategory'];
parsedAlert['html'] = htmlForAlert;
parsedAlerts.push(parsedAlert);
}
for (i = 0; i < parsedAlerts.length; i++) {
var divId = 'transit-alert-' + parsedAlerts[i]['alertId'];
console.log('Currently working with TRANSIT div of ID:')
console.log(divId)
if (document.getElementById(divId) == null) {
// Testing 19 May
console.log("((( ABOVE ALERT NEW - CREATING NEW DIV")
console.log("((( - - - - - - - - - - -")
$('#main').append('<div id=' + divId + '></div>');
currentTransitAlertsIds.push(parsedAlerts[i]['alertId']);
document.getElementById(divId).innerHTML = parsedAlerts[i]['html'];
$('.rotation-group').slick('slickAdd', '#' + divId);
} else {
// Testing 19 May
console.log("((( ABOVE ALERT ALREADY OPEN, NO ACTION")
console.log("((( - - - - - - - - - - -")
document.getElementById(divId).innerHTML = parsedAlerts[i]['html'];
}
}
// Check if all currentTransitAlertsDivIds are still active alerts
console.log(currentTransitAlertsIds);
currentTransitAlertsIdsCopy = Object.assign([], currentTransitAlertsIds);
// Testing 19 May
console.log("((( - - - - - - - - - - -")
for (i = 0; i < currentTransitAlertsIdsCopy.length; i++) {
// Testing 19 May
console.log("((( BEGIN TO CHECK FOR ALERT ID...")
console.log(currentTransitAlertsIdsCopy[i])
console.log(" ")
var alertStillActive = false;
for (j = 0; j < parsedAlerts.length; j++) {
// Testing 19 May
console.log("((( CURRENTLY CHECKING AGAINST...")
console.log(parsedAlerts[j]['alertId'])
console.log(" ")
if (parsedAlerts[j]['alertId'] == currentTransitAlertsIdsCopy[i]) {
alertStillActive = true;
break;
}
}
// Testing 19 May
console.log(alertStillActive)
if (alertStillActive == false) {
// This means alert is not active: remove from list, rotation, html
console.log("")
console.log(" !!! REMOVING TRANSIT ALERT")
console.log(currentTransitAlertsIdsCopy[i])
console.log($('#transit-alert-' + currentTransitAlertsIdsCopy[i]).attr('data-slick-index'))
console.log(document.getElementById('transit-alert-' + currentTransitAlertsIdsCopy[i]))
console.log("")
if ($('#transit-alert-' + currentTransitAlertsIdsCopy[i]).attr('data-slick-index') != null) {
// Testing 19 May
console.log("((( APPARENTLY IT IS BEING REMOVED NOW.......")
console.log("((( REMOVING INDEX......")
console.log($('#transit-alert-' + currentTransitAlertsIdsCopy[i]).attr('data-slick-index'))
$('.rotation-group').slick('slickRemove', $('#transit-alert-' + currentTransitAlertsIdsCopy[i]).attr('data-slick-index'))
// Testing 19 May
if (document.getElementById('transit-alert-' + currentTransitAlertsIdsCopy[i]) == null) {
console.log("((( BEFORE SPLICE")
console.log(currentTransitAlertsIds)
indexToSplice = currentTransitAlertsIds.indexOf(currentTransitAlertsIdsCopy[i])
console.log("((( SPLICING AT INDEX...")
console.log(indexToSplice)
currentTransitAlertsIds.splice(indexToSplice, 1);
// Testing 19 May
console.log("((( AFTER SPLICE")
console.log(currentTransitAlertsIds)
} else {
console.log("((( ERROR : DID NOT ACTUALLY REMOVE SLIDE .........")
console.log(currentTransitAlertsIds)
}
} else {
// Testing 19 May
console.log("((( !!! NOT ACTUALLY REMOVED ALERT ALERT...PROBABLY ALREADY GONE")
console.log("((( BEFORE SPLICE")
console.log(currentTransitAlertsIds)
indexToSplice = currentTransitAlertsIds.indexOf(currentTransitAlertsIdsCopy[i])
console.log("((( SPLICING AT INDEX...")
console.log(indexToSplice)
currentTransitAlertsIds.splice(indexToSplice, 1);
// Testing 19 May
console.log("((( AFTER SPLICE")
console.log(currentTransitAlertsIds)
}
}
}
}
});
});
};
transitAlertsUpdate();
setInterval(transitAlertsUpdate,120000);