Skip to content

Commit

Permalink
Merge Weather Underground provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrossman committed Jul 9, 2019
2 parents 068afde + 38dd70d commit ef0d593
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/pkjs/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var config = require('./config.js');
var DarkSkyProvider = require('./weather/darksky.js');
// var DarkSkyProvider = require('./weather/darksky.js');
var WundergroundProvider = require('./weather/wunderground.js');

var provider = new DarkSkyProvider(config.apiKey);
// var provider = new DarkSkyProvider(config.darkSkyApiKey);
var provider = new WundergroundProvider(config.wundergroundApiKey);

// Listen for when the watchface is opened
Pebble.addEventListener('ready',
Expand Down
48 changes: 48 additions & 0 deletions src/pkjs/weather/wunderground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var WeatherProvider = require('./provider.js');

function request(url, type, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
callback(this.responseText);
};
xhr.open(type, url);
xhr.send();
}

var WundergroundProvider = function(apiKey) {
this._super.call(this);
this.apiKey = apiKey;
}

WundergroundProvider.prototype = Object.create(WeatherProvider.prototype);
WundergroundProvider.prototype.constructor = WundergroundProvider;
WundergroundProvider.prototype._super = WeatherProvider;

WundergroundProvider.prototype.withWundergroundResponse = function(lat, lon, callback) {
// callback(wundergroundResponse)
var url = 'https://api.weather.com/v1/geocode/' + lat + '/' + lon + '/forecast/hourly/48hour.json?apiKey=' + this.apiKey;
request(url, 'GET', function (response) {
var weatherData = JSON.parse(response);
callback(weatherData);
});
}

// ============== IMPORTANT OVERRIDE ================

WundergroundProvider.prototype.withProviderData = function(lat, lon, callback) {
// callBack expects that this.hasValidData() will be true
this.withWundergroundResponse(lat, lon, (function(wundergroundResponse) {
var forecast = wundergroundResponse.forecasts;
this.tempTrend = forecast.map(function(entry) {
return entry.temp;
})
this.precipTrend = forecast.map(function(entry) {
return entry.pop / 100.0
})
this.startHour = new Date(forecast[0].fcst_valid * 1000).getHours()
this.currentTemp = this.tempTrend[0];
callback();
}).bind(this));
}

module.exports = WundergroundProvider;

0 comments on commit ef0d593

Please sign in to comment.