-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (34 loc) · 1.2 KB
/
index.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
#!/usr/bin/env node
'use strict'
const anybar = require('anybar')
const createHafas = require('vbb-hafas')
const {departures} = createHafas('vbb-anybar')
const setColor = (color) => {
anybar(color, {port: 1738})
}
const colors = [
'red', // < 2m
'yellow', // < 4m
'green', // < 6m
'yellow', // < 8m
'red' // < 10m
]
const minute = 1000 * 60
module.exports = (origin, direction, timeToStation = 0) => {
if (Number.isNaN(timeToStation)) throw new Error('invalid when parameter')
return departures(origin, {
direction,
results: 3,
when: Date.now() + timeToStation * minute
})
.then((deps) => {
const dep = deps.find(dep => !dep.cancelled)
const msToDepature = new Date(dep.when) - Date.now()
const minutesToDeparture = Math.floor(msToDepature / minute)
const spareTimeBeforeDeparture = minutesToDeparture - timeToStation
// Comment left in for future debugging as required
// console.log(`Next departure is at ${dep.when}, in ${minutesToDeparture} minutes time. This gives ${spareTimeBeforeDeparture} minutes spare, after spending ${timeToStation} minutes on the way to the station.`)
const timeColor = colors[spareTimeBeforeDeparture / 2 | 0]
return setColor(timeColor || 'question')
})
}