-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
57 lines (51 loc) · 1.48 KB
/
example.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
/* eslint-disable no-unused-vars */
import {
createThrottledClient as createThrottledHafas,
} from 'vbb-hafas/throttle.js'
import {createMonitor} from './index.js'
import {
addTripsFetchingToMonitor as fetchTrips,
} from './fetch-trips/index.js'
import {
createPriorityBasedTripFetchingStrategy as prioBasedTripFetching,
} from './fetch-trips/priority-based.js'
import {
createTimeBasedTripFetchingStrategy as timeBasedTripFetching,
} from './fetch-trips/time-based.js'
const potsdamerPlatz = {
north: 52.52,
west: 13.36,
south: 52.5,
east: 13.39,
}
const bbox = process.env.BBOX
? JSON.parse(process.env.BBOX)
: potsdamerPlatz
const userAgent = 'hafas-monitor-trips example'
const hafas = createThrottledHafas(userAgent, 5, 1000) // 5 req/s
const monitor = createMonitor(hafas, bbox, {
fetchTripsInterval: 10_000, // 10s
})
monitor.once('error', (err) => {
console.error(err)
process.exit(1)
})
monitor.on('hafas-error', console.error)
const prioStrategy = prioBasedTripFetching((movement) => {
if (movement.line.product === 'subway') return 0
return null
})
const timeBasedStrategy = timeBasedTripFetching((movement) => {
if (movement.line.product === 'subway') return 1
return null
})
fetchTrips(monitor, {
// strategy: prioStrategy,
strategy: timeBasedStrategy,
})
monitor.on('trip', (trip) => {
console.log('trip', trip.id, trip.line.name)
})
monitor.on('position', (loc, movement) => {
console.log('movement', movement.tripId, movement.line.name, 'pos', loc.latitude, loc.longitude)
})