diff --git a/modules/default/weather/providers/darksky.js b/modules/default/weather/providers/darksky.js index 3164d7c8..4ce0c895 100644 --- a/modules/default/weather/providers/darksky.js +++ b/modules/default/weather/providers/darksky.js @@ -13,49 +13,76 @@ WeatherProvider.register("darksky", { // Set the name of the provider. // Not strictly required, but helps for debugging. providerName: "Dark Sky", - // Implement fetchCurrentWeather. + fetchCurrentWeather: function() { - // Create a URL from the config and base URL. - var url = `https://api.darksky.net/forecast/${this.config.apiKey}/${this.config.latLong}`; - // Run the request. - this.fetchData(url).then(data => { - Log.log(data); - if(!data || !data.main || typeof data.main.temp === "undefined") { - // No usable data? - return; - } - var currentWeather = this.generateWeatherDayFromCurrentWeather(data); - this.setCurrentWeather(currentWeather); - }).catch(function(request) { - Log.error("Could not load data!", request); - }); + this.fetchData(this.getUrl()) + .then(data => { + Log.log(data); + if(!data || !data.currently || typeof data.currently.temperature === "undefined") { + // No usable data? + return; + } + var currentWeather = this.generateWeatherDayFromCurrentWeather(data); + this.setCurrentWeather(currentWeather); + }).catch(function(request) { + Log.error("Could not load data!", request); + }); }, + fetchWeatherForecast: function() { - // Also, fake data. - var forecast = []; - var today = moment(); - for(var i = 0; i < 5; i++) { - var weatherObject = new WeatherObject(); - weatherObject.date = moment(today).add(i, "days"); - weatherObject.minTemperature = Math.random() * 10 + 10; - weatherObject.maxTemperature = Math.random() * 15 + 10; - forecast.push(weatherObject); - } - this.setWeatherForecast(); + this.fetchData(this.getUrl()) + .then(data => { + Log.log(data); + if(!data || !data.daily || !data.daily.data.length) { + // No usable data? + return; + } + var forecast = this.generateWeatherObjectsFromForecast(data.daily.data); + this.setWeatherForecast(forecast); + }).catch(function(request) { + Log.error("Could not load data!", request); + }); }, + + // Create a URL from the config and base URL. + getUrl: function() { + return `https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/${this.config.apiKey}/${this.config.lat},${this.config.lon}`; + }, + // Implement WeatherDay generator. generateWeatherDayFromCurrentWeather: function(currentWeatherData) { var currentWeather = new WeatherObject(); - currentWeather.date = new Date(); + + currentWeather.date = moment(); currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity); currentWeather.temperature = parseFloat(currentWeatherData.currently.temperature); currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed); currentWeather.windDirection = currentWeatherData.currently.windBearing; - currentWeather.weatherType = this.currentWeatherType(currentWeatherData.currently.icon); - currentWeather.sunrise = new Date(currentWeatherData.daily.data[0].sunriseTime); - currentWeather.sunset = new Date(currentWeatherData.daily.data[0].sunsetTime); + currentWeather.weatherType = this.convertWeatherType(currentWeatherData.currently.icon); + currentWeather.sunrise = moment(currentWeatherData.daily.data[0].sunriseTime, "X"); + currentWeather.sunset = moment(currentWeatherData.daily.data[0].sunsetTime, "X"); + return currentWeather; }, + + generateWeatherObjectsFromForecast: function(forecasts) { + var days = []; + + for (var forecast of forecasts) { + var weather = new WeatherObject(); + + weather.date = moment(forecast.time, "X"); + weather.minTemperature = forecast.temperatureMin; + weather.maxTemperature = forecast.temperatureMax; + weather.weatherType = this.convertWeatherType(forecast.icon); + weather.rain = forecast.precipAccumulation; + + days.push(weather) + } + + return days + }, + // Map icons from Dark Sky to our icons. convertWeatherType: function(weatherType) { var weatherTypes = {