diff --git a/app/app.js b/app/app.js index d625a4f..62c5d2f 100644 --- a/app/app.js +++ b/app/app.js @@ -96,134 +96,19 @@ }); }; - let updateBTC = function(g, u) { - let title, total, elm = $('#btc'); - if (lastGBP !== 0) { - elm.removeClass(); - if (g > lastGBP) { - elm.addClass('up'); - } else if (g < lastGBP) { - elm.addClass('down'); - } - } else { - lows.gbp = g; - lows.usd = u; - - highs.gbp = g; - highs.usd = u; - } - - lastGBP = g; - lastUSD = u; - - if (g < lows.gbp) lows.gbp = g; - if (u < lows.usd) lows.usd = u; - - if (highs.gbp < g) highs.gbp = g; - if (highs.usd < u) highs.usd = u; - - total = myBTC * g; - - title = 'High: $' + parseFloat(highs.usd.toFixed(2)) + ' / Low $' + parseFloat(lows.usd.toFixed(2)); - elm.html('$' + parseFloat(u.toFixed(2)) + ' / £' + parseFloat(g.toFixed(2)) + ' (£' + parseFloat(total.toFixed(2)) + ')'); - elm.prop('title', title); - }; - - let updateFX = function(data) { - let elm = $('#fx'); - elm.html('£1 = $' + parseFloat(data.gpbe.toFixed(2)) + ' = ' + parseFloat(data.sekex.toFixed(2)) + ' SEK'); - }; - - this.bind('updateFX', function(data) { - $('#fx').html('£1 = $' + parseFloat(data.gpbe.toFixed(2)) + ' = ' + parseFloat(data.sekex.toFixed(2)) + ' SEK'); - }); - let btcValue = function() { - let url = '/btc'; - $.ajax({ - type: 'GET', - url: url, - data: '', - dataType: 'json', - timeout: 10000, - //contentType: ('application/json'), - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type' - }, - success: function(data) { - // console.log(data); - let gbp = data.bpi.GBP.rate_float, - usd = data.bpi.USD.rate_float; - - updateBTC(gbp, usd); - }, - error: function(xhr, type) { - console.log('ajax error'); - console.log(xhr); - console.log(type); - } - }); - }; - - /*this.bind('getBTC', function() { - btcValue(); - });*/ - - let getFX = function() { - let url = '/fx'; - - $.ajax({ - type: 'GET', - url: url, - data: '', - dataType: 'json', - - timeout: 10000, - - //contentType: ('application/json'), - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type' - - }, - success: function(data) { - let gpbex = (1 / data.rates.GBP); - let sekex = (gpbex * data.rates.SEK); - let fxdata = { - usd: 1, - gbp: data.rates.GBP, - sek: data.rates.SEK, - gpbe: gpbex, - sekex: sekex - }; - self.trigger('updateFX', fxdata); - }, - error: function(xhr, type) { - console.log('ajax error'); - console.log(xhr); - console.log(type); - } - }); - }; - - this.bind('getFX', function() { - getFX(); - }); let getNextTrainTime = function(toStation, fromStation) { let url = '/getnexttraintimes?from=' + fromStation + '&to=' + toStation; let target = fromStation + toStation; - $.ajax({ - type: 'GET', - url: url, + $.ajax({ 'GET', + u + type:rl: url, data: '', dataType: 'json', @@ -332,21 +217,19 @@ tick(); get_weather(); - self.trigger('getBTC'); - self.trigger('getFX'); getNextTrainTime('dbe', 'glq'); getNextTrainTime('glq', 'dbe'); // start 15 minute timer _fastTimer = setInterval(function() { - self.trigger('getBTC'); + getNextTrainTime('dbe', 'glq'); getNextTrainTime('glq', 'dbe'); }, (60000)); _slowTimer = setInterval(function() { - self.trigger('getFX'); + get_weather(); }, (60000 * 15)); diff --git a/app/js/modules/fx.js b/app/js/modules/fx.js new file mode 100644 index 0000000..8ed3647 --- /dev/null +++ b/app/js/modules/fx.js @@ -0,0 +1,72 @@ +/** + * Created by mdonnel on 22/03/2017. + */ + +let FxModel = Backbone.Model.extend({ + initialize: function () { + this.set('url', '/fx'); + this.set('fxdata', {}); + this.update(); + }, + update: function () { + this.getFX(); + const now = new Date; + const mod = 900000 - (now.getTime() % 900000); + + let fxUpdateFn = function() { + this.update(); + }; + setTimeout(fxUpdateFn.bind(this), mod + 10); + }, + getFX: function() { + let url = this.get('url'); + let self = this; + $.ajax({ + type: 'GET', + url: url, + data: '', + dataType: 'json', + + timeout: 10000, + + //contentType: ('application/json'), + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type' + + }, + success: function(data) { + let gpbex = (1 / data.rates.GBP); + let sekex = (gpbex * data.rates.SEK); + let fxdata = { + usd: 1, + gbp: data.rates.GBP, + sek: data.rates.SEK, + gpbe: gpbex, + sekex: sekex + }; + self.set('fxdata', fxdata); + }, + error: function(xhr, type) { + console.log('ajax error'); + console.log(xhr); + console.log(type); + } + }); + } +}); + + +let FxView = Backbone.View.extend({ + tagName: 'div', + initialize: function () { + _.bindAll(this, 'render'); + this.model.bind('change', this.render); + this.$fx = $('#fx'); + }, + render: function () { + let fxdata = this.model.get('fxdata'); + this.$fx.html(`£1 = $${parseFloat(fxdata.gpbe.toFixed(2))} = ${ parseFloat(fxdata.sekex.toFixed(2))} SEK`); + } +}); diff --git a/app/js/modules/train.js b/app/js/modules/train.js index 630b05c..b3597f1 100644 --- a/app/js/modules/train.js +++ b/app/js/modules/train.js @@ -6,9 +6,48 @@ * */ -var TrainModel = Backbone.Model.extend({}); +let TrainModel = Backbone.Model.extend({ + initialize: function() { + console.log(this.get('to')); + let fromStation = this.get('from'); + let toStation = this.get('to'); + let url = '/getnexttraintimes?from=' + fromStation + '&to=' + toStation; + let target = fromStation + toStation; + this.set('url', url); + this.set('target', target); + this.update(); + }, update: function() { + this.getTrain(); + }, + getTrain: function() { + let url = this.get('url'); + let self = this; + console.log(url); + $.ajax({ + type: 'GET', + url: url, + data: '', + dataType: 'json', + timeout: 10000, + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type' + }, + success: function(data) { + console.log(data); + self.set('trainData', data); + }, + error: function(xhr, type) { + console.log('ajax error'); + console.log(xhr); + console.log(type); + } + }); + } +}); -var Train = Backbone.View.extend({ +let Train = Backbone.View.extend({ tagName: 'div', initialize: function() { _.bindAll(this, 'render'); @@ -18,27 +57,21 @@ var Train = Backbone.View.extend({ }, render: function() { console.log('Train:Render'); - var ws =''; - var data = this.model.get('data'); + let ws = ''; + let data = this.model.get('data'); console.log(this.model); - if (data.length > 0) - { - for (var i = 0; i < data.length; i++) - { + if (data.length > 0) { + for (let i = 0; i < data.length; i++) { ws = ws + '
' + data[i].title + '
'; ws = ws + '
' + data[i].description + '
'; } - this.$traintext.empty().html(ws); - this.$traininfo.removeClass('mui--hide').addClass('mui--show'); - + this.$traintext.empty().html(ws); + this.$traininfo.removeClass('mui--hide').addClass('mui--show'); } else { this.$traininfo.removeClass('mui--show').addClass('mui--hide'); } - - - } }); diff --git a/lib/fx.js b/lib/fx.js index 4377cf5..d35bfd9 100644 --- a/lib/fx.js +++ b/lib/fx.js @@ -1,5 +1,5 @@ - var http = require('http'); -var fxCache = {}; +const http = require('http'); +let fxCache = {}; exports.doFx = function (req,res) { console.log('FX request'); function fxQuery(callback, r) { @@ -22,6 +22,7 @@ exports.doFx = function (req,res) { data += chunk; }); response.on('end', function () { + console.log('Data done...'); callback(JSON.parse(data), r); }); response.on('error', function(e) { @@ -43,6 +44,8 @@ exports.doFx = function (req,res) { else { console.log("Using cache"); res.setHeader('Content-Type', 'application/json'); + console.log('Cache length:', JSON.stringify(fxCache).length); + console.log(JSON.stringify(fxCache).substring(0,50)); res.end(JSON.stringify(fxCache)); } diff --git a/lib/train.js b/lib/train.js index 7a72418..e05627d 100644 --- a/lib/train.js +++ b/lib/train.js @@ -47,7 +47,7 @@ module.exports = { logger.debug(ts); //GLOBAL.lastcheck = now; logger.debug(ts.sta); - logger.debug(toSeconds(ts.sta)); + // logger.debug(toSeconds(ts.sta)); output.sta = ts.sta; output.eta = ts.eta; @@ -102,11 +102,12 @@ module.exports = { { // console.log(ts); //GLOBAL.lastcheck = now; - logger.debug(ts.sta); - logger.debug(toSeconds(ts.sta)); + logger.debug(ts.sta, ts.std); + // logger.debug(toSeconds(ts.sta)); + + output.sta = (ts.sta !== null) ? ts.sta : ts.std; + output.eta = (ts.eta !== null ? ts.eta : ts.etd); - output.sta = ts.sta; - output.eta = (ts.eta !== null ? ts.eta : ts.sta); // trainCache.last.glqdbe = toSeconds(ts.sta); // console.log(ts); } else @@ -146,8 +147,13 @@ module.exports = { }; function toSeconds(inval) { - const a = inval.split(':'); - return ((parseInt(a[0]) * (60 * 60)) + (parseInt(a[1]) * 60)); + console.log('inval', typeof inval); + if (typeof inval === 'string') { + const a = inval.split(':'); + return ((parseInt(a[0]) * (60 * 60)) + (parseInt(a[1]) * 60)); + } + + return ''; } diff --git a/views/pages/slackV2.ejs b/views/pages/slackV2.ejs index a936463..319d265 100644 --- a/views/pages/slackV2.ejs +++ b/views/pages/slackV2.ejs @@ -228,6 +228,7 @@
Travel
+
DBEGLQ: GLQDBE: