Merged road incidents, just need to update backbone object to deal with them

This commit is contained in:
martin 2018-05-22 17:10:38 +01:00
parent 32dd4a0b24
commit d8d66f4ec8
3 changed files with 40 additions and 22 deletions

View File

@ -29,7 +29,6 @@ const asyncMiddleware = fn =>
.catch(next);
};
app.get('/weather', (req, res) => {
if (req.query.hasOwnProperty('ll'))
@ -186,7 +185,7 @@ app.get('/agenda', cache('15 minutes'), (req, res) => {
});
});
app.get('/traffic', cache('5 minutes'), (req, res) => {
app.get('/oldtraffic', cache('5 minutes'), (req, res) => {
logger.debug(req.query);
if (req.query.hasOwnProperty('olat'))
@ -215,17 +214,26 @@ app.get('/incidents', cache('5 minutes'), (req, res) => {
logger.error(e);
res.status(500).send('directions.getIncidents: There was an error!');
});
});
app.get('/comb', asyncMiddleware(async (req, res, next) => {
app.get('/traffic', cache('5 minutes'), asyncMiddleware(async (req, res, next) => {
logger.debug(req.query);
let m = await directions.doGetEstDirectionsWithIncidents(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon).then((b) => {
logger.debug('b', b);
if (req.query.hasOwnProperty('olat'))
await directions.doGetEstDirectionsWithIncidents(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon).then((b) => {
//logger.debug('b', b);
res.send(b);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
logger.debug('m', m);
res.send(m);
else {
// throw new Error('Weather: LL missing');
logger.warn('FS: oLat missing');
res.status(500).send('oLat Missing');
}
}));
app.listen(port, (err) => {

View File

@ -5,7 +5,7 @@ const http = require('http');
const logger = require('log4js').getLogger('Directions');
logger.level = 'debug';
const { reduceEstDirections, reduceIncidents } = require('./reducers/directions');
const { reduceEstDirections, reduceIncidents, combine } = require('./reducers/directions');
module.exports = {
'getTraffic': doGetEstDirections,
@ -24,7 +24,9 @@ async function doGetEstDirectionsWithIncidents(olat, olon, dlat, dlon) {
let incid = await doGetTraffic();
logger.debug('after...');
return {traff, incid};
let combined = combine(traff, incid);
return combined;
}
async function doGetEstDirections(olat, olon, dlat, dlon) {

View File

@ -54,24 +54,32 @@ function reduceEstDirections(body = '') {
function reduceIncidents(body = '') {
if (body === '') return [];
let workObj = Object.assign({}, body);
let incidents = [];
const workObj = Object.assign({}, body);
const incidents = [];
const items = get(workObj, 'items');
for (let item of items) {
let title = item.title.split(' ');
for (const item of items) {
const title = item.title.split(' ');
incidents.push({
title : item.title,
description: item.description,
road : title[0]
})
'title' : item.title,
'description': item.description,
'road' : title[0]
});
}
return incidents;
}
function combine(traffic, incidents) {
const workObj = Object.assign({ 'incidents':[] }, traffic);
module.exports = { reduceEstDirections, reduceIncidents };
for (const item of incidents)
if (workObj.streets.indexOf(item.road) > -1)
workObj.incidents.push(item);
return workObj;
}
module.exports = { reduceEstDirections, reduceIncidents, combine };