// train.js var http = require('http'); var trainCache = { last: {}, data: {} }; module.exports = { dbe_glq: function(req, res) { // http://huxley.apphb.com/all/dbe/to/glq/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8 // http://huxley.apphb.com/all/dbe/from/glq/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8 console.log('DBE:GLQ request'); function Query(callback, r) { var req = r; var options = { host: 'huxley.apphb.com', // port: 80, path: '/next/dbe/to/glq/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8', //method: 'GET', headers: { } }; try { http.request(options).on('response', function(response) { var data = ''; response.on("data", function(chunk) { data += chunk; }); response.on('end', function() { callback(JSON.parse(data), r); }); }).end(); } catch (e) { console.log(e); } } var now = new Date(); var nowSeconds = (now.getHours() * (60 * 60)) + (now.getMinutes() * 60); console.log('Now Seconds: ' + nowSeconds); if (trainCache.last.dbeglq == null || nowSeconds != trainCache.last.dbeglq) { Query(function(a, b) { //console.log(a); var ts = a.departures[0].service; var output = {}; //GLOBAL.lastcheck = now; console.log(ts.sta); // console.log(toSeconds(ts.sta)); output.sta = ts.sta; output.eta = ts.eta; trainCache.data.dbeglq = output; // trainCache.last.dbeglq = toSeconds(ts.sta); // console.log(ts); res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(trainCache.data.dbeglq)); }, res); } }, glq_dbe: function(req, res) { // http://huxley.apphb.com/all/dbe/to/glq/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8 // http://huxley.apphb.com/all/dbe/from/glq/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8 console.log('GLQ:DBE request'); function Query(callback, r) { var req = r; var options = { host: 'huxley.apphb.com', // port: 80, path: '/next/dbe/from/glq/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8', //method: 'GET', headers: { } }; try { http.request(options).on('response', function(response) { var data = ''; response.on("data", function(chunk) { data += chunk; }); response.on('end', function() { callback(JSON.parse(data), r); }); }).end(); } catch (e) { console.log(e); } } var now = new Date(); var nowSeconds = (now.getHours() * (60 * 60)) + (now.getMinutes() * 60); console.log('Now Seconds: ' + nowSeconds); if (trainCache.last.glqdbe == null || nowSeconds != trainCache.last.dbeglq) { Query(function(a, b) { //console.log(a); var ts = a.departures[0].service; var output = {}; //GLOBAL.lastcheck = now; console.log(ts.sta); console.log(toSeconds(ts.sta)); output.sta = ts.sta; output.eta = ts.eta; trainCache.data.glqdbe = output; // trainCache.last.glqdbe = toSeconds(ts.sta); // console.log(ts); res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(trainCache.data.glqdbe)); }, res); } } }; function toSeconds(inval) { var a = inval.split(':'); return ((parseInt(a[0]) * (60 * 60)) + (parseInt(a[1]) * 60)); }