2017-09-18 22:06:11 +00:00
|
|
|
'use strict';
|
2017-09-18 16:45:21 +00:00
|
|
|
// Import required modules.
|
|
|
|
let express = require('express');
|
2017-09-18 22:06:11 +00:00
|
|
|
let axios = require('axios');
|
|
|
|
let bikeData = {};
|
2017-09-18 16:45:21 +00:00
|
|
|
|
|
|
|
// Initialize our Express app.
|
|
|
|
let app = express();
|
2017-09-18 22:06:11 +00:00
|
|
|
let requestDataTimer = setInterval(requestData, 3000);
|
2017-09-18 16:45:21 +00:00
|
|
|
|
|
|
|
const filterStations = (stations, stationId) => {
|
|
|
|
let returnObject;
|
|
|
|
stations.map((o,index)=>{
|
|
|
|
if(o.extra.uid == stationId){
|
|
|
|
returnObject = o;
|
|
|
|
}
|
|
|
|
});
|
2017-11-23 20:55:47 +00:00
|
|
|
let time = {
|
|
|
|
seconds: pad(new Date().getSeconds(), 2),
|
|
|
|
minutes: pad(new Date().getMinutes(), 2),
|
|
|
|
hours: pad(new Date().getHours(), 2)
|
|
|
|
};
|
|
|
|
returnObject.time = time;
|
2017-09-18 16:45:21 +00:00
|
|
|
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) => {
|
2017-09-18 22:06:11 +00:00
|
|
|
if(req.query.stationId){
|
2017-09-18 16:45:21 +00:00
|
|
|
let returnValue = {status:"not found"};
|
2017-09-18 22:06:11 +00:00
|
|
|
returnValue = filterStations(bikeData.network.stations, req.query.stationId);
|
2017-09-18 16:45:21 +00:00
|
|
|
res.json(returnValue);
|
|
|
|
}else{
|
|
|
|
res.json({status:"error"});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-18 22:06:11 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2017-11-23 20:55:47 +00:00
|
|
|
|
|
|
|
function pad(n, width, z) {
|
|
|
|
z = z || '0';
|
|
|
|
n = n + '';
|
|
|
|
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
|
|
|
|
}
|
2017-09-18 16:45:21 +00:00
|
|
|
// Listen for incoming requests and serve them.
|
|
|
|
app.listen(process.env.PORT || 3000);
|