mqtt_server/app/js/modules/weather.js
martind2000 c9b90f2221 updates
2017-01-06 16:59:39 +00:00

76 lines
2.0 KiB
JavaScript

/**
*
* User: Martin Donnelly
* Date: 2016-10-03
* Time: 14:20
*
*/
let WeatherModel = Backbone.Model.extend({
initialize: function () {
this.set('url', 'https://api.darksky.net/forecast/9ad2a41d420f3cf4960571bb886f710c/' + this.get('lat').toString() + ',' + this.get('long').toString() + '?units=uk2&exclude=minutely,hourly,daily,alerts,flags');
console.log(this.get('url'));
// this.update();
},
update: function () {
this.getWeather();
const now = new Date;
let mod = 1800000 - (now.getTime() % 1800000);
let weatherTrigger = function () {
this.update();
};
},
getWeather: function () {
const self = this;
$.ajax({
type: 'GET',
url: self.get('url'),
data: '',
dataType: 'json',
timeout: 10000,
context: $('body'),
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) {
const stored = {
temperature: data.currently.temperature,
icon: data.currently.icon,
summary: data.currently.summary
};
self.set(stored);
},
error: function (xhr, type) {
console.error('ajax error');
console.error(xhr);
console.error(type);
}
});
}
});
let Weather = Backbone.View.extend({
tagName: 'div',
initialize: function () {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.$weatherText = $('#weatherDescription');
this.$weatherTemp = $('#temp');
this.$weatherIcon = $('#weatherIcon');
},
render: function () {
console.log('Weather:Render');
const ws = '<i class="wi wi-forecast-io-' + this.model.get('icon') + '"></i>';
this.$weatherTemp.empty().html(parseInt(this.model.get('temperature')) + '&deg;c&nbsp;');
this.$weatherText.empty().html(this.model.get('summary'));
this.$weatherIcon.empty().html(ws);
}
});