-
Notifications
You must be signed in to change notification settings - Fork 2
/
station.ts
613 lines (575 loc) · 20.8 KB
/
station.ts
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import * as Logger from 'bunyan'
import { Response } from 'express'
import moment from 'moment-timezone'
import { DBStopTime, WakaRequest, WakaTripUpdate } from '../../types'
import BaseStops from '../../types/BaseStops'
import { prefixToTimezone } from '../../utils'
import WakaRedis from '../../waka-realtime/Redis'
import Connection from '../db/connection'
import Lines from '../lines'
import StopsDataAccess from '../dataAccess/stopsDataAccess'
import RedisDataAccess from '../dataAccess/redisDataAccess'
interface StationProps {
logger: Logger
connection: Connection
prefix: string
stopsExtras?: BaseStops
lines: Lines
redis: WakaRedis
version: string
realtimeTimes(
trips: string[],
stop_id: string,
train: boolean
):
| Promise<{
[tripId: string]: WakaTripUpdate
}>
| undefined
}
class Station {
logger: Logger
connection: Connection
prefix: string
regionSpecific?: BaseStops
lines: Lines
redis: WakaRedis
realtimeTimes: (
trips: string[],
stop_id: string,
train: boolean
) =>
| Promise<{
[tripId: string]: WakaTripUpdate
}>
| undefined
dataAccess: StopsDataAccess
redisDataAccess: RedisDataAccess
version: string
constructor(props: StationProps) {
const {
logger,
connection,
prefix,
version,
stopsExtras,
lines,
realtimeTimes,
redis,
} = props
this.redis = redis
this.logger = logger
this.connection = connection
this.prefix = prefix
this.version = version
this.regionSpecific = stopsExtras
this.lines = lines
this.realtimeTimes = realtimeTimes
this.dataAccess = new StopsDataAccess({ connection, prefix })
this.redisDataAccess = new RedisDataAccess({ logger, prefix, redis })
}
transfers = async () => {
const transfers = await this.dataAccess.getTransfers()
Object.keys(transfers).forEach(stopId => {
try {
const stopTransfers = transfers[stopId].toString()
this.redis.client.set(
`waka-worker:${this.prefix}:stop-transfers:${stopId}`,
stopTransfers
)
} catch (error) {
console.log(error)
}
})
const { logger } = this
logger.info({ stopCount: Object.keys(transfers).length }, 'Got Transfers')
}
getBounds = async () => {
return this.dataAccess.getBounds()
}
/**
* @api {get} /:region/station/:stop_id Info - by stop_id
* @apiName GetStation
* @apiGroup Station
* @apiDescription This returns data on a single station.
*
* @apiParam {String} region Region of Worker
* @apiParam {String} stop_id Station Stop ID, find using All Stations or Stations by Location routes.
*
* @apiSuccess {String} stop_id Unique Stop Id for this station
* @apiSuccess {String} stop_name Station Name
* @apiSuccess {String} stop_desc Station Description, if any
* @apiSuccess {String} stop_lat Station Latitude
* @apiSuccess {String} stop_lon Station Longitude
* @apiSuccess {String} zone_id Fare zone - See GTFS.
* @apiSuccess {String} location_type If the station is a parent station or not - see GTFS.
* @apiSuccess {String} parent_station Parent Station, if any
* @apiSuccess {String} stop_timezone Timezone of station, usually null & assumed to be agency timezone.
* @apiSuccess {String} wheelchair_boarding Wheelchair Boarding - see GTFS.
* @apiSuccess {String} route_type GTFS Route Type from this Station
* @apiSuccess {String} prefix Worker Region of station
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "stop_id": "133",
* "stop_name": "Britomart Train Station",
* "stop_desc": null,
* "stop_lat": -36.84429,
* "stop_lon": 174.76848,
* "zone_id": "merged_20",
* "location_type": 0,
* "parent_station": null,
* "stop_timezone": null,
* "wheelchair_boarding": null,
* "route_type": 2,
* "prefix": "nz-akl"
* }
*
*/
stopInfo = async (
req: WakaRequest<null, { station: string }>,
res: Response
) => {
const { prefix, dataAccess, regionSpecific } = this
if (!req.params.station) {
return res.status(404).send({
message: 'Please specify a station.',
})
}
let stopCode = req.params.station.trim()
let override = ''
if (
prefix === 'nz-wlg' &&
(regionSpecific?.badStops?.indexOf(stopCode) ?? -1) > -1
) {
override = stopCode
stopCode = `${stopCode}1`
}
const notFound = { message: 'Station not found.' }
try {
const [stopData, linesObject] = await Promise.all([
dataAccess.getStopInfo(stopCode),
this.redisDataAccess.getLinesForStop(stopCode),
])
const data = {
...stopData,
lines: linesObject.map(l => ({
...l,
route_color: this.lines.getColor(l.agency_id, l.route_short_name),
})),
}
if (override) {
data.stop_id = override
}
if (data.location_type === 1) {
const platforms = await dataAccess.getPlatforms(data.stop_id)
return res.send({ ...data, platforms })
}
return res.send(data)
} catch (err) {
// TODO: make this more generic
if (prefix === 'nz-akl') {
try {
const getSingle = regionSpecific?.getSingle
if (getSingle) {
const data = getSingle(stopCode)
return res.send(data)
}
} catch (err) {
// couldn't get any carpark
}
}
return res.status(404).send(notFound)
}
}
/**
* @api {get} /:region/station/:stop_id/times/:time Stop Times - by stop_id
* @apiName GetTimes
* @apiGroup Station
* @apiDescription Shows services at a particular station.
*
* @apiParam {String} region Region of Worker
* @apiParam {String} stop_id Station Stop ID, find using All Stations or Stations by Location routes.
* @apiParam {String} [time] Find services at a particular time, defaults to current time.
*
* @apiSuccess {String} provider The data source. Usually "sql-server"
* @apiSuccess {Number} current_time Server time, in seconds.
* @apiSuccess {Object[]} trips List of all the trips for the station.
* @apiSuccess {String} trips.trip_id GTFS trip_id
* @apiSuccess {Number} trips.stop_sequence What stop of the line this station corresponds to.
* @apiSuccess {String} trips.trip_headsign General direction of where the service is going - usually displayed on vehicle
* @apiSuccess {String} trips.shape_id Shape_id of route
* @apiSuccess {Number} trips.direction_id 0 for outbound, 1 for inbound.
* @apiSuccess {Date} trips.start_date When this trip is valid from - server filters out invalid ones automatically
* @apiSuccess {Date} trips.end_date When this trip is valid unti - server filters out invalid ones automatically
* @apiSuccess {String} trips.route_short_name Short service name.
* @apiSuccess {String} trips.route_long_name Long service name - usually origin & destination. Sometimes "Eastern Line"
* @apiSuccess {Number} trips.route_type GTFS Route Transport Type
* @apiSuccess {String} trips.agency_id Agency that operates this service
* @apiSuccess {String} trips.route_color Colour for the route
* @apiSuccess {String} trips.route_icon Icon for the route (optional)
* @apiSuccess {Number} trips.departure_time_seconds When the service is due to depart from this station, in seconds.
* @apiSuccess {Number} trips.arrival_time_seconds When the service is due to arrive at this station, in seconds.
* @apiSuccess {Date} trips.departure_time UTC date when the service is due to depart from this station.
* @apiSuccess {Date} trips.arrival_time UTC date when the service is due to arrive at this station.
* @apiSuccess {Number} trips.dwell Time between arrival and departure, in seconds.
* @apiSuccess {Number} trips.drop_off_type Indicates whether riders are dropped off at a stop as part of the normal schedule or whether a dropoff at the stop is unavailable.
* @apiSuccess {Number} trips.pickup_type Indicates whether riders are picked up at a stop as part of the normal schedule or whether a pickup at the stop isn't available.
* @apiSuccess {Object[]} realtime Realtime Info, only provided for some services. If empty, call the realtime API.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "provider": "sql-server",
* "currentTime": 45960,
* "trips": [
* {
* "trip_id": "50051071268-20171113160906_v60.12",
* "stop_sequence": 11,
* "trip_headsign": "Britomart",
* "shape_id": "1198-20171113160906_v60.12",
* "direction_id": 0,
* "start_date": "2017-11-16T00:00:00.000Z",
* "end_date": "2017-12-09T00:00:00.000Z",
* "route_short_name": "EAST",
* "route_long_name": "Manukau Train Station to Britomart Train Station",
* "route_type": 2,
* "agency_id": "AM",
* "route_color": "#f39c12",
* "route_icon": "nz/at-metro-eastern",
* "departure_time_seconds": 44280
* },
* ],
* "realtime": {
* "50051071268-20171113160906_v60.12": {
* "stop_sequence": 41,
* "delay": -35,
* "timestamp": 1511222480.044,
* "v_id": "2CA9",
* "double_decker": false
* }
* }
* }
*/
stopTimes = async (
req: WakaRequest<{}, { time: string; station: string }>,
res: Response
) => {
const {
prefix,
dataAccess,
logger,
lines,
regionSpecific,
realtimeTimes,
} = this
if (!req.params.station) {
return res.status(404).send({
message: 'Please specify a stop.',
})
}
const station = req.params.station.trim()
// carparks
if (prefix === 'nz-akl') {
const getTimes = regionSpecific?.getTimes
if (getTimes) {
const data = getTimes(station)
if (data !== null) {
return res.send(data)
}
}
}
const timezone = prefixToTimezone(prefix)
const time = moment().tz(timezone)
let currentTime = new Date(Date.UTC(1970, 0, 1, time.hour(), time.minute()))
let midnightOverride = false
if (req.params.time) {
const split = req.params.time.split(':')
const tentativeDate = new Date(
Date.UTC(
1970,
0,
1,
Number.parseInt(split[0], 10),
Number.parseInt(split[1], 10)
)
)
if (tentativeDate.toString() !== 'Invalid Date') {
currentTime = tentativeDate
midnightOverride = true
}
}
const sendingCurrentTime = currentTime.getTime() / 1000
const today = new Date(0)
today.setUTCFullYear(time.year())
today.setUTCMonth(time.month())
today.setUTCDate(time.date())
// now is 00:00 in region's local timezone
const now = moment().tz(timezone)
now.seconds(0)
now.hours(0)
now.minutes(0)
// midnight fix
// the big issue that it probably needs to handle a sliding window
if (time.hour() < 5 && midnightOverride === false) {
today.setTime(today.getTime() - 1000 * 60 * 60 * 24)
now.subtract(1, 'day')
}
let procedure = 'GetStopTimes'
let trips: DBStopTime[] = []
let allRoutesWithColors = {}
const realtimeTrips: string[] = []
try {
let allRoutes = {}
;[trips, allRoutes] = await Promise.all([
dataAccess.getStopTimes(station, currentTime, today, procedure),
dataAccess.getRoutesForMultipleStops([station]),
])
allRoutesWithColors = (allRoutes[station] || []).map(l => ({
...l,
route_color: this.lines.getColor(l.agency_id, l.route_short_name),
route_text_color: '#ffffff', // TODO!
}))
// use last week's timetable if no trips
if (trips.length === 0) {
logger.warn({ station }, 'Used timetable for last week!')
today.setDate(today.getDate() - 7)
trips = await dataAccess.getStopTimes(
station,
currentTime,
today,
procedure
)
}
} catch (err) {
logger.error({ err }, 'Could not get stop times.')
return res.status(500).send({ message: 'Could not get stop times.' })
}
const sendingTrips = trips.map(r => {
// fully formed arrival and departure times in region's local tz
const arrivalTime = moment.unix(now.unix() + r.new_arrival_time)
const departureTime = moment.unix(now.unix() + r.new_departure_time)
// time between arrival and departure
const dwell = r.new_departure_time - r.new_arrival_time
let departure_time_seconds = new Date(r.departure_time).getTime() / 1000
if (r.departure_time_24) {
departure_time_seconds += 86400
}
let arrival_time_seconds = new Date(r.arrival_time).getTime() / 1000
if (r.arrival_time_24) {
arrival_time_seconds += 86400
}
const route_icon = lines.getIcon(r.agency_id, r.route_short_name)
// 30mins of realtime
if (
departure_time_seconds < sendingCurrentTime + 1800 ||
r.departure_time_24
) {
realtimeTrips.push(r.trip_id)
}
let platform: string
let { stop_name } = r
if (r.parent_station) {
const split = r.stop_name.split(' Platform ')
if (split.length === 2) {
;[stop_name, platform] = split
}
}
const record = {
arrival_time_seconds,
departure_time_seconds,
arrival_time: arrivalTime,
departure_time: departureTime,
route_icon,
dwell,
trip_headsign: r.trip_headsign || r.route_long_name,
route_id: r.route_id,
route_short_name: r.route_short_name,
route_long_name: r.route_long_name,
agency_id: r.agency_id,
stop_sequence: r.stop_sequence,
direction_id: r.direction_id,
route_color: `#${r.route_color}`,
route_text_color: `#${r.route_text_color}`,
trip_id: r.trip_id,
pickup_type: r.pickup_type,
drop_off_type: r.drop_off_type,
shape_id: r.shape_id,
platform,
stop_name,
stop_id: r.stop_id,
}
return record
})
let realtime = {}
try {
realtime = await realtimeTimes(realtimeTrips, station, false)
} catch (err) {
logger.error({ err }, 'Could not get realtime')
}
const sending: {
provider: string
currentTime: number
trips: {
drop_off_type: 0 | 1
pickup_type: 0 | 1
stop_sequence: number
trip_id: string
direction_id: number
shape_id: string
trip_headsign: string
route_color: string
route_long_name: string
route_short_name: string
agency_id: string
stop_name?: string
platform?: string
}[]
realtime?: { [tripId: string]: WakaTripUpdate }
allRoutes?: any
} = {
provider: 'sql-server',
currentTime: sendingCurrentTime,
trips: sendingTrips,
allRoutes: allRoutesWithColors,
realtime,
}
return res.send(sending)
}
/**
* @api {get} /:region/station/:stop_id/timetable/:route/:direction/:offset Timetable - by stop_id
* @apiName GetTimetable
* @apiGroup Station
* @apiDescription Shows timetable for a particular service at a particular station.
*
* @apiParam {String} region Region of Worker
* @apiParam {String} stop_id Station Stop ID, find using All Stations or Stations by Location routes.
* @apiParam {String} route route_short_name to look up.
* @apiParam {Number} direction 0 for inbound, 1 for outbound, 2 for both directions.
* @apiParam {Number} [offset] The number of days from today to get the timetable for
*
* @apiSuccess {Object[]} trips List of all the trips for the station - just in root array, no actual object
* @apiSuccess {String} trips.trip_id GTFS trip_id
* @apiSuccess {String} trips.service_id GTFS service_id
* @apiSuccess {String} trips.shape_id GTFS shape_id
* @apiSuccess {String} trips.trip_headsign General direction of where the service is going - usually displayed on vehicle
* @apiSuccess {Number} trips.direction_id 0 for outbound, 1 for inbound.
* @apiSuccess {Number} trips.stop_sequence What stop of the line this station corresponds to.
* @apiSuccess {String} trips.route_id GTFS route_id
* @apiSuccess {String} trips.route_long_name Long service name - usually origin & destination. Sometimes "Eastern Line"
* @apiSuccess {String} trips.agency_id Agency that operates this service
* @apiSuccess {Number} trips.departure_time_seconds When the service is due to depart from this station, in seconds.
* @apiSuccess {Number} trips.arrival_time_seconds When the service is due to arrive at this station, in seconds.
* @apiSuccess {Date} trips.departure_time UTC time of when the service is due to depart from this station.
* @apiSuccess {Date} trips.arrival_time UTC time of when the service is due to arrive at this station.
* @apiSuccess {String} trips.route_color Colour for the route
* @apiSuccess {String} trips.route_icon Icon for the route (optional)
* @apiSuccess {Number} trips.currentTime Server Time, in Seconds
* @apiSuccess {Date} trips.date Date of the Trip, in local time
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* [
* {
* "trip_id": "50051071494-20171113160906_v60.12",
* "service_id": "50051071494-20171113160906_v60.12",
* "shape_id": "1198-20171113160906_v60.12",
* "trip_headsign": "Britomart",
* "direction_id": 0,
* "stop_sequence": 11,
* "route_id": "50151-20171113160906_v60.12",
* "route_long_name": "Manukau Train Station to Britomart Train Station",
* "agency_id": "AM",
* "departure_time_seconds": 20880,
* "route_color": "#f39c12",
* "route_icon": "nz/at-metro-eastern",
* "currentTime": 47760,
* "date": "2017-12-08T00:00:00.000Z"
* }
* ]
*/
timetable = async (
req: WakaRequest<
null,
{ station: string; route: string; direction: string; offset: string }
>,
res: Response
) => {
const { prefix, dataAccess, logger, regionSpecific, lines } = this
const { station, route, direction, offset } = req.params
if (parseInt(direction, 10) > 2 || parseInt(direction, 10) < 0) {
return res.status(400).send({ error: 'Direction is not valid.' })
}
let dateOffset = 0
if (!Number.isNaN(parseInt(offset, 10))) {
dateOffset = parseInt(offset, 10)
}
const timezone = prefixToTimezone(prefix)
const time = moment().tz(timezone)
const currentTime = new Date(
Date.UTC(1970, 0, 1, time.hour(), time.minute())
)
const today = new Date(Date.UTC(1970, 0, 1, 0, 0))
today.setUTCFullYear(time.year())
today.setUTCMonth(time.month())
today.setUTCDate(time.date() + dateOffset)
// now is 00:00 in region's local timezone
const now = moment(today).tz(timezone)
now.seconds(0)
now.hours(0)
now.minutes(0)
// combines train stations platforms together
const procedure = 'GetTimetable'
let trips = []
try {
trips = await dataAccess.getTimetable(
station,
route,
today,
direction,
procedure
)
// use last week's timetable if no trips
if (trips.length === 0) {
logger.warn({ station }, 'Used timetable for last week!')
today.setDate(today.getDate() - 7)
trips = await dataAccess.getTimetable(
station,
route,
today,
direction,
procedure
)
}
} catch (err) {
logger.error({ err }, 'Could not get timetable.')
return res.status(500).send(err)
}
const sending = trips.map(oldRecord => {
const record = JSON.parse(JSON.stringify(oldRecord))
// fully formed arrival and departure times in region's local tz
record.departure_time = moment.unix(
now.unix() + record.new_departure_time
)
record.arrival_time = moment.unix(now.unix() + record.new_arrival_time)
// old for compat
record.departure_time_seconds = record.new_departure_time
record.arrival_time_seconds = record.new_arrival_time
record.route_color = lines.getColor(record.agency_id, req.params.route)
record.route_icon = lines.getIcon(record.agency_id, req.params.route)
record.currentTime = currentTime.getTime() / 1000
record.date = today
if (record.trip_headsign === null) {
logger.warn('This dataset has a null trip_headsign.')
record.trip_headsign = record.route_long_name
}
delete record.departure_time_24
delete record.new_departure_time
delete record.new_arrival_time
return record
})
return res.send(sending)
}
}
export default Station