added fx and now trains

This commit is contained in:
martind2000 2017-03-22 17:01:58 +00:00
parent 82c3cce796
commit 24aa88ecba
6 changed files with 151 additions and 145 deletions

View File

@ -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('&#36;' + parseFloat(u.toFixed(2)) + ' / &pound;' + parseFloat(g.toFixed(2)) + ' (&pound;' + parseFloat(total.toFixed(2)) + ')');
elm.prop('title', title);
};
let updateFX = function(data) {
let elm = $('#fx');
elm.html('&pound;1 = &#36;' + parseFloat(data.gpbe.toFixed(2)) + ' = ' + parseFloat(data.sekex.toFixed(2)) + ' SEK');
};
this.bind('updateFX', function(data) {
$('#fx').html('&pound;1 = &#36;' + 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));

72
app/js/modules/fx.js Normal file
View File

@ -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(`&pound;1 = &#36;${parseFloat(fxdata.gpbe.toFixed(2))} = ${ parseFloat(fxdata.sekex.toFixed(2))} SEK`);
}
});

View File

@ -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 + '<div class="mui-row"><div class="mui-col-md-12"><strong>' + data[i].title + '</strong></div></div>';
ws = ws + '<div class="mui-row"><div class="mui-col-md-12">' + data[i].description + '</div></div>';
}
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');
}
}
});

View File

@ -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));
}

View File

@ -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 '';
}

View File

@ -228,6 +228,7 @@
<div class="mui-col-md-4">
<div class="mui--text-title mui-text-black">Travel <span id="fx"></div>
<!-- Travel -->
<div id="trains"></div>
<span>DBEGLQ: <button class="mui-btn mui-btn--flat" id="dbeglq"></button></span> <span>GLQDBE: <button class="mui-btn mui-btn--flat" id="glqdbe"></button></span>
<div id='trainResults' style='display:none'></div>
<ul>
@ -370,10 +371,18 @@
<script src="libs/underscore.js"></script>
<script src="libs/backbone.js"></script>
<script src="js/modules/bitcoin.js"></script>
<script src="js/modules/fx.js"></script>
<script src="js/modules/train.js"></script>
<script src="app.js"></script>
<script>
this.bitcoin = new Bitcoin({model: new BitcoinModel()});
this.fx = new FxView({model: new FxModel()});
dbeglqModel = new TrainModel({from:'dbe', to:'glq'});
glqdbeModel = new TrainModel({from:'glq', to:'dbe'});
glqhymModel = new TrainModel({from:'glq', to:'hym'});
hymglqModel = new TrainModel({from:'hym', to:'glq'});
</script>