-
Notifications
You must be signed in to change notification settings - Fork 0
/
trArrServiceTriMetv2.js
290 lines (232 loc) · 8.57 KB
/
trArrServiceTriMetv2.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
/*
Copyright 2010-2011 Portland Transport
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
function trArrServiceTriMetCreateUpdaters(arrivals_object, service_requests, updaters) {
var max_stops_per_request = 10;
// TriMet allows a max of 10 stops in one request, so split things up into multiple updater objects if need be
while (service_requests.length > 0) {
if (service_requests.length > max_stops_per_request) {
updaters.push(new trArrTriMetUpdater(service_requests.slice(0,max_stops_per_request),arrivals_object));
service_requests = service_requests.slice(max_stops_per_request);
} else {
updaters.push(new trArrTriMetUpdater(service_requests,arrivals_object));
service_requests = [];
}
}
}
function trArrTriMetSupportsCors() {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Supports CORS
return true;
} else if (typeof XDomainRequest != "undefined") {
// IE
return true;
}
return false;
}
function trArrTriMetUpdater(service_requests,arrivals_object) {
var updater = this;
updater.access_method = "jsonp";
if (trArrTriMetSupportsCors()) {
updater.access_method = "json";
}
// every updater object needs to maintain a queue
this.arrivals_queue = [];
this.service_messages = [];
this.connection_health = [];
//this.unique_detour_messages = [];
this.update_interval = 60*1000;
this.health_limit = Math.floor(60*60*1000/this.update_interval);
var request_object = {}; // hash to use for testing arrivals against request
var stop_id_list = [];
for (var i = 0; i < service_requests.length; i++) {
stop_id_list.push(service_requests[i].stop_id);
request_object[service_requests[i].stop_id] = {};
for (var j = 0; j < service_requests[i].routes.length; j++) {
request_object[service_requests[i].stop_id][service_requests[i].routes[j].route_id] = true;
}
}
var stop_string = stop_id_list.join(',');
this.url = "http://developer.trimet.org/ws/v2/arrivals/?locIDs="+stop_string+"&appID=828B87D6ABC0A9DF142696F76&json=true";
// functions that will be polled by the arrivals object
this.arrivals = function() {
return this.arrivals_queue;
}
this.messages = function() {
//debug_alert(['read',updater.unique_detour_messages]);
var messages = [];
for (var key in updater.unique_detour_messages) {
messages.push(key);
}
updater.unique_detour_messages = []; //reinitialize
return messages;
}
this.connection = function() {
return this.connection_health;
}
this.update_connection_health = function(success_status) {
updater.connection_health.unshift( { success: success_status, timestamp: localTime().getTime() } );
if (updater.connection_health.length > this.health_limit) {
updater.connection_health.length = this.health_limit; // limit to last hour
}
}
this.trArrTriMetRequestLoop = function() {
var serviceClasses = {
"90": 1, // MAX
"100": 1,
"190" : 1,
"200": 1,
"203": 2, // commuter rail
"193": 3, // Streetcar
"194": 3, // Streetcar
"4": 5, // frequent service-Division/Fessenden
"6": 5,
"8": 5,
"9": 5,
"12": 5,
"14": 5,
"15": 5,
"33": 5,
"54": 5,
"56": 5,
"57": 5,
"72": 5,
"75": 5
}
updater.process_results = function(data) {
var detours = data.resultSet.detour;
updater.update_connection_health(true);
var local_queue = [];
var update_time = localTime().getTime();
if (data.resultSet.arrival) {
for (var i = 0; i < data.resultSet.arrival.length; i++){
var arrival = data.resultSet.arrival[i];
if (arrival.route == "193" || arrival.route == "194") {
continue; // Streetcar is handled by the NextBus adapter, so ignore TriMet info
}
if (request_object[arrival.locid] == undefined || request_object[arrival.locid][arrival.route] == undefined) {
continue; // don't process an arrival if it wasn't in the stop list
}
var entry = new transitArrival();
var arrival_time_raw = "";
if (arrival.status == "canceled") {
continue; // don't show canceled trips
}
if (arrival.status == "scheduled") {
entry.type = "scheduled";
arrival_time_raw = arrival.scheduled;
} else {
entry.type = "estimated";
arrival_time_raw = arrival.estimated;
}
if (0) {
var year = arrival_time_raw.slice(0, 4);
alert('about to call Number');
var mo = Number(arrival_time_raw.slice(5,7)) - 1; // Jan is 0 in JS
var day = arrival_time_raw.slice(8, 10)
var hour = arrival_time_raw.slice(11, 13);
var min = arrival_time_raw.slice(14, 16);
debug_alert([year,mo,day,hour,min]);
// Must be number or will be interpreted as tz
var sec = Number(arrival_time_raw.slice(17,18));
// Should get TriMet's TZ from GTFS agency defn, in case Oregon makes its own time
// (e.g. America/Portland)
var entry_date = new tzDate(year, mo, day, hour, min, sec, 'America/Los_Angeles');
entry.arrivalTime = entry_date.getTime(); // seconds since epoch for arrival
}
entry.arrivalTime = arrival_time_raw;
if (arrival.detour) {
for (var j = 0; j < detours.length; j++) {
var detour = detours[j];
var routes = detour.route;
for (var k = 0; k < routes.length; k++) {
var route = routes[k];
if (route.route == arrival.route) {
if (!updater.unique_detour_messages) {
updater.unique_detour_messages = [];
}
updater.unique_detour_messages[detour.desc] = true;
//debug_alert(['add',updater.unique_detour_messages]);
}
}
}
}
entry.headsign = arrival.fullSign;
entry.headsign = entry.headsign.replace(" "," ");
entry.stop_id = arrival.locid;
var stop_data = trStopCache().stopData('TriMet',entry.stop_id);
entry.stop_data = copyStopData(stop_data);
entry.route_id = arrival.route;
for (var j = 0; j < stop_data.routes.length; j++){
if (stop_data.routes[j].route_id == entry.route_id) {
entry.route_data = stop_data.routes[j];
if (serviceClasses[entry.route_id]) {
entry.route_data.service_class = serviceClasses[entry.route_id];
} else {
entry.route_data.service_class = 7; // local bus
}
}
}
entry.agency = "TriMet";
entry.avl_agency_id = "TriMet";
entry.alerts = ""; // need to figure this out later
entry.last_updated = update_time;
local_queue.push(entry);
}
}
// now copy to externally visble queue, making sure we're not in the middle of a query
updater.arrivals_queue = local_queue;
//trArrLog("<PRE>"+dump(updater.arrivals_queue)+"</PRE>");
}
jQuery.ajax({
url: updater.url,
dataType: updater.access_method,
cache: false,
error: function(data) {
// first retry
jQuery.ajax({
url: updater.url,
dataType: updater.access_method,
cache: false,
error: function(data) {
// second retry
jQuery.ajax({
url: updater.url,
dataType: updater.access_method,
cache: false,
error: function(data) {
updater.update_connection_health(false);
throw "TriMet Arrivals Error";
},
success: updater.process_results
});
},
success: updater.process_results
});
},
success: updater.process_results
});
}
updater.trArrTriMetRequestLoop(); // first time immediately
setInterval(updater.trArrTriMetRequestLoop,updater.update_interval);
}
function copyStopData(data) {
var out = {};
for (var element in data) {
// strip it down to just the GTFS elements
if (element != 'routes' && element != 'geometry' && element != 'doc_type' && element.substring(0,1) != '_') {
out[element] = data[element]
}
}
return out;
}