// train.js var http = require('http'); var trainCache = { last: {}, data: {} }; module.exports = { dbe_glq: function (req, res) { console.log('DBE:GLQ request'); 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) { var ts = a.departures[0].service; var output = {}; console.log(ts); console.log(ts.sta); output.sta = ts.sta; output.eta = ts.eta; trainCache.data.dbeglq = output; res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(trainCache.data.dbeglq)); }, res, 'huxley.apphb.com', '/next/dbe/to/glq/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8'); } }, glq_dbe: function (req, res) { console.log('GLQ:DBE request'); 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) { var ts = a.departures[0].service; var output = {}; console.log(ts); //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, 'huxley.apphb.com', '/next/glq/to/dbe/1?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8'); } }, getTrainTimes: function(req, res) { console.log(req); console.log('---------'); console.log(req.query); if (req.query.hasOwnProperty('from') && req.query.hasOwnProperty('from')) { var url = '/all/' + req.query.from + '/to/' + req.query.to + '/10?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8'; Query(function (a, b) { res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(a)); }, res, 'huxley.apphb.com', url); } else{ res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({})); } } }; function toSeconds(inval) { var a = inval.split(':'); return ((parseInt(a[0]) * (60 * 60)) + (parseInt(a[1]) * 60)); } function Query(callback, r, host, path) { console.log(path); var req = r; var options = { host: host, // port: 80, path: path, //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); } }