valenBisiFilter/index.js

62 lines
1.5 KiB
JavaScript

'use strict';
// Import required modules.
let express = require('express');
let axios = require('axios');
let bikeData = {};
// Initialize our Express app.
let app = express();
let requestDataTimer = setInterval(requestData, 3000);
const filterStations = (stations, stationId) => {
let returnObject;
stations.map((o,index)=>{
if(o.extra.uid == stationId){
returnObject = o;
}
});
let time = {
seconds: pad(new Date().getSeconds(), 2),
minutes: pad(new Date().getMinutes(), 2),
hours: pad(new Date().getHours(), 2)
};
returnObject.time = time;
return returnObject;
};
// Generate a simple API
app.get('/', function(req, res) {
res.send("Hey there! Thanks for visting the site!");
});
// Generate a simple dashboard page.
app.get('/stations', (req, res) => {
if(req.query.stationId){
let returnValue = {status:"not found"};
returnValue = filterStations(bikeData.network.stations, req.query.stationId);
res.json(returnValue);
}else{
res.json({status:"error"});
}
});
function requestData(){
axios.get('https://api.citybik.es/v2/networks/valenbisi',{headers: {'Accept-Encoding': 'gzip, deflate, br'}})
.then(function (response) {
//console.log(response);
bikeData = response.data;
})
.catch(function (error) {
console.log(error);
});
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
// Listen for incoming requests and serve them.
app.listen(process.env.PORT || 3000);