121 lines
3.4 KiB
JavaScript
121 lines
3.4 KiB
JavaScript
const createClient = require('hafas-client')
|
|
const insaProfile = require('hafas-client/p/insa')
|
|
const legacyEncoder = require('legacy-encoding')
|
|
|
|
var express = require("express");
|
|
var app = express();
|
|
const port = 3000
|
|
|
|
const client = createClient(insaProfile, 'my-awesome-program')
|
|
|
|
function formatResult(departures,respdata) {
|
|
departures.forEach(element => {
|
|
let delay = ""
|
|
if (element['delay'] != 0) {
|
|
delay = element['delay'] / 60
|
|
}
|
|
respdata['departures'].push({
|
|
"line": element['line']['id'],
|
|
"direction": element['direction'],
|
|
"time": element['when'],
|
|
"delay": delay
|
|
})
|
|
|
|
});
|
|
return respdata;
|
|
}
|
|
|
|
function formatStboard(departures,respdata,num) {
|
|
let count = 0;
|
|
departures.forEach(departure => {
|
|
if (count<num) {
|
|
timediff = Math.floor(Math.max(0,(( ( Date.parse(departure['when']) - new Date().getTime() ) / 1000 ) / 60)))
|
|
respdata['departures'].push({
|
|
"line": departure['line']['id'].toUpperCase().substr(-3),
|
|
"dest": departure['direction'],
|
|
"timefromnow": timediff+"m"
|
|
})
|
|
}
|
|
count++
|
|
})
|
|
|
|
}
|
|
|
|
async function getNearestStation(lat, lon) {
|
|
var queryPromise = client.nearby({
|
|
type: 'location',
|
|
latitude: lat,
|
|
longitude: lon
|
|
}, {
|
|
distance: 400
|
|
})
|
|
var result = await queryPromise
|
|
return result[0]
|
|
}
|
|
async function getStationDepartures(id) {
|
|
var queryPromise2 = client.departures(id)
|
|
var departures = await queryPromise2
|
|
return departures
|
|
}
|
|
|
|
function cutPadString(string, length) {
|
|
let outstring = ""
|
|
if (string.length > length) {
|
|
outstring = string.substring(0, length - 3) + "..."
|
|
} else {
|
|
if (string.length < length) {
|
|
outstring = string.padEnd(length)
|
|
} else {
|
|
outstring = string
|
|
}
|
|
}
|
|
return outstring
|
|
}
|
|
|
|
app.get('/', (req, res) => res.send('Hello World!'))
|
|
|
|
|
|
// Parse JSON bodies (as sent by API clients)
|
|
app.use(express.json());
|
|
|
|
// Access the parse results as request.body
|
|
app.post('/departures/nearest', function (request, response) {
|
|
console.log(request.body.lat);
|
|
console.log(request.body.lon);
|
|
getNearestStation(request.body.lat, request.body.lon)
|
|
.then((station) => {
|
|
getStationDepartures(station)
|
|
.then((departures) => {
|
|
var respdata = {
|
|
"name": station['name'],
|
|
"departures": []
|
|
}
|
|
formatResult(departures, respdata)
|
|
response.send(respdata)
|
|
})
|
|
.catch(console.log("Error while getting departures."))
|
|
})
|
|
.catch(console.log("Error while getting Station ID."))
|
|
|
|
});
|
|
|
|
app.post('/radar', function (request, response) {
|
|
res.send("//TODO")
|
|
});
|
|
|
|
app.get('/stboard/:id/:num', function (req, res, next) {
|
|
console.log('id was '+ req.params.id)
|
|
getStationDepartures(req.params.id)
|
|
.then((departures) => {
|
|
var respdata = {
|
|
"departures": []
|
|
}
|
|
formatStboard(departures, respdata, req.params.num)
|
|
/* Yikes. */
|
|
res.write(legacyEncoder.encode(JSON.stringify(respdata),'cp850'))
|
|
res.end()
|
|
})
|
|
.catch(console.log("Error while getting departures."))
|
|
})
|
|
|
|
app.listen(port, () => console.log(`Example app listening on port ${port}!`)) |