From 2156aac0469418a95dc5507b9ba0c716fd7f3c35 Mon Sep 17 00:00:00 2001 From: fwitte Date: Fri, 4 Jan 2019 12:07:02 +0100 Subject: [PATCH 1/4] fixed typos, fetching forecast parameters by day --- .../weather/providers/openweathermap.js | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/modules/default/weather/providers/openweathermap.js b/modules/default/weather/providers/openweathermap.js index 89ccfcf5..c156d787 100644 --- a/modules/default/weather/providers/openweathermap.js +++ b/modules/default/weather/providers/openweathermap.js @@ -88,24 +88,48 @@ WeatherProvider.register("openweathermap", { */ generateWeatherObjectsFromForecast(forecasts) { const days = []; + var minTemp = []; + var maxTemp = []; + var rain = 0; + let date = ""; + var weather = new WeatherObject(this.config.units); for (const forecast of forecasts) { - const weather = new WeatherObject(this.config.units); - - weather.date = moment(forecast.dt, "X"); - weather.minTemperature = forecast.temp.min; - weather.maxTemperature = forecast.temp.max; - weather.weatherType = this.convertWeatherType(forecast.weather[0].icon); - if (this.config.units === "imperial" && !isNaN(forecast.rain)) { - weather.rain = forecast.rain / 25.4 + + if (date === moment(forecast.dt, "X").format("YYYY-MM-DD")) { + minTemp.push(forecast.main.temp_min); + maxTemp.push(forecast.main.temp_max); + if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) { + rain += forecast.rain["3h"] / 25.4; + } else { + rain += forecast.rain["3h"]; + } } else { - weather.rain = forecast.rain; + weather.minTemperature = Math.min.apply(null, minTemp); + weather.maxTemperature = Math.max.apply(null, maxTemp); + weather.rain = rain; + days.push(weather); + weather = new WeatherObject(this.config.units); + + minTemp = []; + maxTemp = []; + rain *= 0; + date = moment(forecast.dt, "X").format("YYYY-MM-DD"); + + weather.date = moment(forecast.dt, "X"); + weather.weatherType = this.convertWeatherType(forecast.weather[0].icon); + + minTemp.push(forecast.main.temp_min); + maxTemp.push(forecast.main.temp_max); + if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) { + rain += forecast.rain["3h"] / 25.4; + } else { + rain += forecast.rain["3h"]; + } } - - days.push(weather); } - - return days; + + return days.slice(1); }, /* From b55685d610019a354763a5215ecc02bd59c0873c Mon Sep 17 00:00:00 2001 From: fwitte Date: Fri, 4 Jan 2019 12:13:39 +0100 Subject: [PATCH 2/4] added comments --- .../default/weather/providers/openweathermap.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/default/weather/providers/openweathermap.js b/modules/default/weather/providers/openweathermap.js index c156d787..64df4104 100644 --- a/modules/default/weather/providers/openweathermap.js +++ b/modules/default/weather/providers/openweathermap.js @@ -87,16 +87,21 @@ WeatherProvider.register("openweathermap", { * Generate WeatherObjects based on forecast information */ generateWeatherObjectsFromForecast(forecasts) { + // initial variable declaration const days = []; + // variables for temperature range and rain var minTemp = []; var maxTemp = []; var rain = 0; + // variable for date let date = ""; var weather = new WeatherObject(this.config.units); for (const forecast of forecasts) { if (date === moment(forecast.dt, "X").format("YYYY-MM-DD")) { + // the same day as before + // add values from forecast to corresponding variables minTemp.push(forecast.main.temp_min); maxTemp.push(forecast.main.temp_max); if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) { @@ -105,20 +110,30 @@ WeatherProvider.register("openweathermap", { rain += forecast.rain["3h"]; } } else { + // a new day + // calculate minimum/maximum temperature, specify rain amount weather.minTemperature = Math.min.apply(null, minTemp); weather.maxTemperature = Math.max.apply(null, maxTemp); weather.rain = rain; + // push weather information to days array days.push(weather); + // create new weather-object weather = new WeatherObject(this.config.units); minTemp = []; maxTemp = []; rain *= 0; + + // set new date date = moment(forecast.dt, "X").format("YYYY-MM-DD"); + // specify date weather.date = moment(forecast.dt, "X"); + + // select weather type by first forecast value of a day, is this reasonable? weather.weatherType = this.convertWeatherType(forecast.weather[0].icon); + // add values from first forecast of this day to corresponding variables minTemp.push(forecast.main.temp_min); maxTemp.push(forecast.main.temp_max); if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) { From 9e394ea349c23dfaa67984601aeafc2dcfe60234 Mon Sep 17 00:00:00 2001 From: fwitte Date: Fri, 4 Jan 2019 12:34:12 +0100 Subject: [PATCH 3/4] updated CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b990c080..913e95c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Updated ### Fixed +- Weather forecast now works with openweathermap in new weather module. Daily data are displayed, see issue [#1504](https://github.com/MichMich/MagicMirror/issues/1504). ## [2.6.0] - 2019-01-01 From 1df2de92026ddcd68b8f1250b9fa205a8b843fde Mon Sep 17 00:00:00 2001 From: fwitte Date: Fri, 4 Jan 2019 12:34:29 +0100 Subject: [PATCH 4/4] updated README --- modules/default/weather/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/default/weather/README.md b/modules/default/weather/README.md index 89e65de1..d10297d1 100644 --- a/modules/default/weather/README.md +++ b/modules/default/weather/README.md @@ -78,7 +78,7 @@ The following properties can be configured: | ---------------------------- | ----------- | `apiVersion` | The OpenWeatherMap API version to use.

**Default value:** `2.5` | `apiBase` | The OpenWeatherMap base URL.

**Default value:** `'http://api.openweathermap.org/data/'` -| `weatherEndpoint` | The OpenWeatherMap API endPoint.

**Possible values:** `/weather` or `/forecast/daily`
**Default value:** `'/weather'` +| `weatherEndpoint` | The OpenWeatherMap API endPoint.

**Possible values:** `/weather`, `/forecast` or `/forecast/daily` (paying users only)
**Default value:** `'/weather'` | `locationID` | Location ID from [OpenWeatherMap](https://openweathermap.org/find) **This will override anything you put in location.**
Leave blank if you want to use location.
**Example:** `1234567`
**Default value:** `false`

**Note:** When the `location` and `locationID` are both not set, the location will be based on the information provided by the calendar module. The first upcoming event with location data will be used. | `location` | The location used for weather information.

**Example:** `'Amsterdam,Netherlands'`
**Default value:** `false`

**Note:** When the `location` and `locationID` are both not set, the location will be based on the information provided by the calendar module. The first upcoming event with location data will be used. | `apiKey` | The [OpenWeatherMap](https://home.openweathermap.org) API key, which can be obtained by creating an OpenWeatherMap account.

This value is **REQUIRED**