Added road incidents, just need to update backbone object to deal with them
This commit is contained in:
parent
a70d76127a
commit
d5d129b58a
4348
package-lock.json
generated
4348
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
server.js
22
server.js
@ -22,6 +22,14 @@ const cache = apicache.middleware;
|
|||||||
app.use(express.static(path.join(__dirname, sitePath)));
|
app.use(express.static(path.join(__dirname, sitePath)));
|
||||||
|
|
||||||
// app.get('/weather', cache('15 minutes'), (req, res) => {
|
// app.get('/weather', cache('15 minutes'), (req, res) => {
|
||||||
|
|
||||||
|
const asyncMiddleware = fn =>
|
||||||
|
(req, res, next) => {
|
||||||
|
Promise.resolve(fn(req, res, next))
|
||||||
|
.catch(next);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
app.get('/weather', (req, res) => {
|
app.get('/weather', (req, res) => {
|
||||||
if (req.query.hasOwnProperty('ll'))
|
if (req.query.hasOwnProperty('ll'))
|
||||||
|
|
||||||
@ -196,6 +204,20 @@ app.get('/traffic', cache('5 minutes'), (req, res) => {
|
|||||||
res.status(500).send('oLat Missing');
|
res.status(500).send('oLat Missing');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('/incidents', cache('5 minutes'), (req, res) => {
|
||||||
|
logger.debug(req.query);
|
||||||
|
directions.getIncidents()
|
||||||
|
.then((d) => {
|
||||||
|
logger.debug(d);
|
||||||
|
res.send(d);
|
||||||
|
}).catch((e) => {
|
||||||
|
logger.error(e);
|
||||||
|
res.status(500).send('directions.getIncidents: There was an error!');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(port, (err) => {
|
app.listen(port, (err) => {
|
||||||
if (err)
|
if (err)
|
||||||
return logger.error('Server error:', err);
|
return logger.error('Server error:', err);
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
const request = require('request');
|
const request = require('request');
|
||||||
|
const FeedMe = require('feedme');
|
||||||
|
const http = require('http');
|
||||||
|
|
||||||
const logger = require('log4js').getLogger('Directions');
|
const logger = require('log4js').getLogger('Directions');
|
||||||
logger.level = 'debug';
|
logger.level = 'debug';
|
||||||
|
|
||||||
const { reduceEstDirections } = require('./reducers/directions');
|
const { reduceEstDirections, reduceIncidents } = require('./reducers/directions');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
'getTraffic': doGetEstDirections
|
'getTraffic': doGetEstDirections,
|
||||||
|
'getIncidents' : doGetTraffic
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const headers = {};
|
||||||
|
const lastGood = {};
|
||||||
|
|
||||||
//
|
//
|
||||||
// https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=AIzaSyBl7O9LHIthCagcqIaDkQ4um_hghYG5reE
|
// https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=AIzaSyBl7O9LHIthCagcqIaDkQ4um_hghYG5reE
|
||||||
|
|
||||||
@ -17,6 +23,7 @@ function doGetEstDirections(olat, olon, dlat, dlon) {
|
|||||||
const url = `https://sgws2.maps.yahoo.com/Directions?time=now&cache=n&flags=J&olat=${olat}&olon=${olon}&dlat=${dlat}&dlon=${dlon}`;
|
const url = `https://sgws2.maps.yahoo.com/Directions?time=now&cache=n&flags=J&olat=${olat}&olon=${olon}&dlat=${dlat}&dlon=${dlon}`;
|
||||||
|
|
||||||
logger.debug(url);
|
logger.debug(url);
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
request(url, function(err, resp, body) {
|
request(url, function(err, resp, body) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -29,6 +36,7 @@ function doGetEstDirections(olat, olon, dlat, dlon) {
|
|||||||
output.timestamp = new Date().getTime();
|
output.timestamp = new Date().getTime();
|
||||||
|
|
||||||
console.log(output);
|
console.log(output);
|
||||||
|
|
||||||
return resolve(output);
|
return resolve(output);
|
||||||
}, function(error, response, body) {
|
}, function(error, response, body) {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
@ -41,3 +49,61 @@ function doGetEstDirections(olat, olon, dlat, dlon) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function doGetTraffic() {
|
||||||
|
logger.debug('doGetTraffic');
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// https://trafficscotland.org/rss/feeds/currentincidents.aspx
|
||||||
|
const options = {
|
||||||
|
'hostname': 'trafficscotland.org',
|
||||||
|
'path': '/rss/feeds/currentincidents.aspx',
|
||||||
|
'method': 'GET',
|
||||||
|
'headers': headers
|
||||||
|
};
|
||||||
|
|
||||||
|
http.get(options, (res) => {
|
||||||
|
const { statusCode } = res;
|
||||||
|
const contentType = res.headers['content-type'];
|
||||||
|
const reqLastModified = res.headers['date'];
|
||||||
|
|
||||||
|
logger.debug(res.headers);
|
||||||
|
let error;
|
||||||
|
|
||||||
|
logger.debug('contentType', contentType);
|
||||||
|
if (statusCode !== 200 && statusCode !== 304)
|
||||||
|
error = new Error('Request Failed.\n' +
|
||||||
|
`Status Code: ${statusCode}`);
|
||||||
|
|
||||||
|
else if (!/^application\/rss\+xml/.test(contentType) && statusCode === 200)
|
||||||
|
error = new Error('Invalid content-type.\n' +
|
||||||
|
`Expected application/rss+xml but received ${contentType}`);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
logger.error(error.message);
|
||||||
|
// consume response data to free up memory
|
||||||
|
res.resume();
|
||||||
|
|
||||||
|
return reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( statusCode === 200) {
|
||||||
|
headers['If-Modified-Since'] = reqLastModified;
|
||||||
|
const parser = new FeedMe(true);
|
||||||
|
res.pipe(parser);
|
||||||
|
parser.on('end', () => {
|
||||||
|
lastGood.page = reduceIncidents(parser.done());
|
||||||
|
logger.info('Traffic Feed cached');
|
||||||
|
// return resolve(parser.done());
|
||||||
|
return resolve(lastGood.page);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (statusCode === 304) {
|
||||||
|
logger.info('Traffic Feed changed');
|
||||||
|
|
||||||
|
return resolve(lastGood.page);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const logger = require('log4js').getLogger('Directions 🔧');
|
const logger = require('log4js').getLogger('Directions 🔧');
|
||||||
|
|
||||||
const { get, isEmpty, has } = require('lodash');
|
const { get, isEmpty, has, uniq } = require('lodash');
|
||||||
const humanizeDuration = require('humanize-duration');
|
const humanizeDuration = require('humanize-duration');
|
||||||
|
|
||||||
logger.level = 'debug';
|
logger.level = 'debug';
|
||||||
@ -13,9 +13,11 @@ function reduceEstDirections(body = '') {
|
|||||||
const jBody = JSON.parse(body);
|
const jBody = JSON.parse(body);
|
||||||
const obj = {};
|
const obj = {};
|
||||||
const { ResultSet } = jBody;
|
const { ResultSet } = jBody;
|
||||||
|
const streets = [];
|
||||||
|
|
||||||
if (has(ResultSet, 'Result')) {
|
if (has(ResultSet, 'Result')) {
|
||||||
const directions = get(ResultSet, 'Result.yahoo_driving_directions');
|
const directions = get(ResultSet, 'Result.yahoo_driving_directions');
|
||||||
|
const route = get(directions, 'directions.route_leg');
|
||||||
|
|
||||||
obj.totalTime = parseFloat(get(directions, 'total_time'));
|
obj.totalTime = parseFloat(get(directions, 'total_time'));
|
||||||
obj.totalTimeWithTraffic = parseFloat(get(directions, 'total_time_with_traffic'));
|
obj.totalTimeWithTraffic = parseFloat(get(directions, 'total_time_with_traffic'));
|
||||||
@ -39,9 +41,37 @@ function reduceEstDirections(body = '') {
|
|||||||
obj.traffic = 'no traffic';
|
obj.traffic = 'no traffic';
|
||||||
obj.className = 'trafficNone';
|
obj.className = 'trafficNone';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const item of route) {
|
||||||
|
const street = item.street.split(',');
|
||||||
|
if (street[0] !== '') streets.push(street[0]);
|
||||||
|
}
|
||||||
|
obj.streets = uniq(streets);
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { reduceEstDirections };
|
function reduceIncidents(body = '') {
|
||||||
|
if (body === '') return [];
|
||||||
|
let workObj = Object.assign({}, body);
|
||||||
|
let incidents = [];
|
||||||
|
|
||||||
|
const items = get(workObj, 'items');
|
||||||
|
|
||||||
|
for (let item of items) {
|
||||||
|
|
||||||
|
let title = item.title.split(' ');
|
||||||
|
incidents.push({
|
||||||
|
title : item.title,
|
||||||
|
description: item.description,
|
||||||
|
road : title[0]
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return incidents;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = { reduceEstDirections, reduceIncidents };
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
const CACHE_VERSION = { 'version': '0.0.704' };
|
const CACHE_VERSION = { 'version': '0.0.707' };
|
||||||
const dataCacheName = 'jubileeData-v1';
|
const dataCacheName = 'jubileeData-v1';
|
||||||
const cacheName = 'jubilee-final-1';
|
const cacheName = 'jubilee-final-1';
|
||||||
const filesToCache = [
|
const filesToCache = [
|
||||||
|
@ -30,6 +30,8 @@ const TrafficModel = Backbone.Model.extend({
|
|||||||
if (day !== 0 && day !== 6)
|
if (day !== 0 && day !== 6)
|
||||||
doRequest = (((hour >= 7) && (hour <= 9)) || ((hour >= 17) && (hour <= 19)));
|
doRequest = (((hour >= 7) && (hour <= 9)) || ((hour >= 17) && (hour <= 19)));
|
||||||
|
|
||||||
|
doRequest = true;
|
||||||
|
|
||||||
const delay = doRequest ? 600000 : toHour();
|
const delay = doRequest ? 600000 : toHour();
|
||||||
|
|
||||||
if (this.has('latlong') && doRequest) {
|
if (this.has('latlong') && doRequest) {
|
||||||
@ -51,6 +53,7 @@ const TrafficModel = Backbone.Model.extend({
|
|||||||
'getTraffic': function() {
|
'getTraffic': function() {
|
||||||
console.log('Get Traffic');
|
console.log('Get Traffic');
|
||||||
// olat, olon, dlat, dlon
|
// olat, olon, dlat, dlon
|
||||||
|
const override = true;
|
||||||
const time = new Date().getTime() ;
|
const time = new Date().getTime() ;
|
||||||
const hour = (new Date()).getHours();
|
const hour = (new Date()).getHours();
|
||||||
const latlong = this.get('latlong');
|
const latlong = this.get('latlong');
|
||||||
@ -70,7 +73,7 @@ const TrafficModel = Backbone.Model.extend({
|
|||||||
'olat': latlong.lat, 'olon': latlong.lon, 'dlat': 55.872407, 'dlon': -3.549003
|
'olat': latlong.lat, 'olon': latlong.lon, 'dlat': 55.872407, 'dlon': -3.549003
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else if ((hour >= 17) && (hour <= 19)) {
|
else if (((hour >= 17) && (hour <= 19)) || override) {
|
||||||
mode = 1;
|
mode = 1;
|
||||||
this.set('dest', 'Home');
|
this.set('dest', 'Home');
|
||||||
qs = {
|
qs = {
|
||||||
|
@ -21,7 +21,7 @@ const { NearbyListModel, NearbyListView } = require('./NearbyList');
|
|||||||
const { NearbyPlacesView } = require('./NearbyPlaces');
|
const { NearbyPlacesView } = require('./NearbyPlaces');
|
||||||
var app = app || {};
|
var app = app || {};
|
||||||
|
|
||||||
const live = true;
|
const live = false;
|
||||||
|
|
||||||
if (live) {
|
if (live) {
|
||||||
window.loc = 'https://jubilee.silvrtree.co.uk';
|
window.loc = 'https://jubilee.silvrtree.co.uk';
|
||||||
|
Loading…
Reference in New Issue
Block a user