From 95adc0aec1b74167d79478be8627da7b75111e66 Mon Sep 17 00:00:00 2001 From: fewieden Date: Thu, 27 Dec 2018 17:14:03 +0100 Subject: [PATCH] forecast --- modules/default/weather/forecast.njk | 33 +++---- .../weather/providers/openweathermap.js | 87 +++++++++++-------- 2 files changed, 70 insertions(+), 50 deletions(-) diff --git a/modules/default/weather/forecast.njk b/modules/default/weather/forecast.njk index 66e7e427..edc11115 100644 --- a/modules/default/weather/forecast.njk +++ b/modules/default/weather/forecast.njk @@ -1,20 +1,23 @@ {% if forecast %} -
- - {% for f in forecast %} - - - - + {% endfor %} +
{{f.day}} - {{f.maxTemperature | roundValue | unit("temperature")}} + + {% for f in forecast %} + + + + + + {% if config.showRainAmount %} + - - - {% endfor %} -
{{f.date.format('ddd')}} + {{f.maxTemperature | roundValue | unit("temperature")}} + + {{f.minTemperature | roundValue | unit("temperature")}} + + {{f.rain | formatRain}} - {{f.minTemperature | roundValue | unit("temperature")}} -
- + {% endif %} +
{% else %} {{"LOADING" | translate}} {% endif %} diff --git a/modules/default/weather/providers/openweathermap.js b/modules/default/weather/providers/openweathermap.js index c9e56d2c..ff58547c 100644 --- a/modules/default/weather/providers/openweathermap.js +++ b/modules/default/weather/providers/openweathermap.js @@ -12,28 +12,22 @@ WeatherProvider.register("openweathermap", { // Set the name of the provider. - // This isn't strictly nessecery, since it will fallback to the provider identifier + // This isn't strictly necessary, since it will fallback to the provider identifier // But for debugging (and future alerts) it would be nice to have the real name. providerName: "OpenWeatherMap", // Overwrite the fetchCurrentWeather method. fetchCurrentWeather: function() { - var apiVersion = "2.5" - var apiBase = "http://api.openweathermap.org/data/" - var weatherEndpoint = "weather" - - var url = apiBase + apiVersion + "/" + weatherEndpoint + this.getParams() - - this.fetchData(url) + this.fetchData(this.getUrl()) .then(data => { - Log.log(data) - if (!data || !data.main || typeof data.main.temp === "undefined") { // Did not receive usable new data. // Maybe this needs a better check? return; } + this.setFetchedLocation(data.name + ', ' + data.sys.country) + var currentWeather = this.generateWeatherObjectFromCurrentWeather(data) this.setCurrentWeather(currentWeather) }) @@ -44,29 +38,33 @@ WeatherProvider.register("openweathermap", { // Overwrite the fetchCurrentWeather method. fetchWeatherForecast: function() { + this.fetchData(this.getUrl()) + .then(data => { + if (!data || !data.list || !data.list.length) { + // Did not receive usable new data. + // Maybe this needs a better check? + return; + } - // I haven't yet implemented the real api call, so let's just generate some random data. + this.setFetchedLocation(data.city.name + ', ' + data.city.country) - 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(forecast) + var forecast = this.generateWeatherObjectsFromForecast(data.list) + this.setWeatherForecast(forecast) + }) + .catch(function(request) { + Log.error("Could not load data ... ", request) + }) }, /** OpenWeatherMap Specific Methods - These are not part of the default provider methods */ - + /* + * Gets the complete url for the request + */ + getUrl: function() { + return this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams() + }, /* * Generate a WeatherObject based on currentWeatherInformation @@ -74,19 +72,38 @@ WeatherProvider.register("openweathermap", { generateWeatherObjectFromCurrentWeather: function(currentWeatherData) { var currentWeather = new WeatherObject() - currentWeather.date = new Date - - currentWeather.humidity = currentWeatherData.main.humidity ? parseFloat(currentWeatherData.main.humidity) : null - currentWeather.temperature = currentWeatherData.main.temp ? parseFloat(currentWeatherData.main.temp) : null - currentWeather.windSpeed = currentWeatherData.wind.speed ? parseFloat(currentWeatherData.wind.speed) : null - currentWeather.windDirection = currentWeatherData.wind.deg ? currentWeatherData.wind.deg : null - currentWeather.weatherType = currentWeatherData.weather[0].icon ? this.convertWeatherType(currentWeatherData.weather[0].icon) : null - currentWeather.sunrise = currentWeatherData.sys.sunrise ? new Date(currentWeatherData.sys.sunrise * 1000) : null - currentWeather.sunset = currentWeatherData.sys.sunset ? new Date(currentWeatherData.sys.sunset * 1000) : null + currentWeather.humidity = currentWeatherData.main.humidity + currentWeather.temperature = currentWeatherData.main.temp + currentWeather.windSpeed = currentWeatherData.wind.speed + currentWeather.windDirection = currentWeatherData.wind.deg + currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon) + currentWeather.sunrise = moment(currentWeatherData.sys.sunrise, "X") + currentWeather.sunset = moment(currentWeatherData.sys.sunset, "X") return currentWeather }, + /* + * Generate WeatherObjects based on forecast information + */ + generateWeatherObjectsFromForecast: function(forecasts) { + var days = [] + + for (var forecast of forecasts) { + var weather = new WeatherObject() + + weather.date = moment(forecast.dt, "X") + weather.minTemperature = forecast.temp.min + weather.maxTemperature = forecast.temp.max + weather.weatherType = this.convertWeatherType(forecast.weather[0].icon) + weather.rain = forecast.rain + + days.push(weather) + } + + return days + }, + /* * Convert the OpenWeatherMap icons to a more usable name. */