From d8d66f4ec8dfe909d70f143c4418bed86156ded3 Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 22 May 2018 17:10:38 +0100 Subject: [PATCH] Merged road incidents, just need to update backbone object to deal with them --- server.js | 26 +++++++++++++++++--------- server/directions.js | 6 ++++-- server/reducers/directions.js | 30 +++++++++++++++++++----------- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/server.js b/server.js index 6a644c3..9e0de52 100644 --- a/server.js +++ b/server.js @@ -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); - }); - logger.debug('m', m); - res.send(m); + 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!'); + }); + + else { + // throw new Error('Weather: LL missing'); + logger.warn('FS: oLat missing'); + res.status(500).send('oLat Missing'); + } })); app.listen(port, (err) => { diff --git a/server/directions.js b/server/directions.js index 6092746..ff3e317 100644 --- a/server/directions.js +++ b/server/directions.js @@ -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) { diff --git a/server/reducers/directions.js b/server/reducers/directions.js index 22240ec..611cf17 100644 --- a/server/reducers/directions.js +++ b/server/reducers/directions.js @@ -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 };