From e791a663c845342175e969664c2c8c3e231a9618 Mon Sep 17 00:00:00 2001 From: tobias Date: Mon, 21 Feb 2022 14:54:35 +0100 Subject: [PATCH 1/6] add weatherprovider for weatherflow --- .../default/weather/providers/weatherflow.js | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 modules/default/weather/providers/weatherflow.js diff --git a/modules/default/weather/providers/weatherflow.js b/modules/default/weather/providers/weatherflow.js new file mode 100644 index 00000000..0ac27b6b --- /dev/null +++ b/modules/default/weather/providers/weatherflow.js @@ -0,0 +1,86 @@ +/* global WeatherProvider, WeatherObject */ + +/* MagicMirror² + * Module: Weather + * Provider: Dark Sky + * + * By Tobias Dreyem https://github.com/10bias + * MIT Licensed + * + * This class is a provider for Weatherflow. + * Note that the Weatherflow API does not provide snowfall. + */ + +WeatherProvider.register("weatherflow", { + // Set the name of the provider. + // Not strictly required, but helps for debugging + providerName: "WeatherFlow", + + // Set the default config properties that is specific to this provider + defaults: { + apiBase: "https://swd.weatherflow.com/swd/rest/", + token: "", + stationid: "", + }, + + units: { + imperial: "us", + metric: "si" + }, + + + fetchCurrentWeather() { + this.fetchData(this.getUrl()) + .then((data) => { + const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); + currentWeather.date = moment(); + + currentWeather.humidity = data.current_conditions.relative_humidity; + currentWeather.temperature = data.current_conditions.air_temperature; + currentWeather.windSpeed = data.current_conditions.wind_avg; + currentWeather.windDirection = data.current_conditions.wind_direction; + currentWeather.weatherType = data.forecast.daily[0].icon; + currentWeather.sunrise = moment(data.forecast.daily[0].sunrise), "X"; + currentWeather.sunset = moment(data.forecast.daily[0].sunset, "X"); + this.setCurrentWeather(currentWeather); + + }) + .catch(function (request) { + Log.error("Could not load data ... ", request); + }) + .finally(() => this.updateAvailable()); + }, + + fetchWeatherForecast() { + this.fetchData(this.getUrl()) + .then((data) => { + const days = []; + + for(const forecast of data.forecast.daily) { + + const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); + + weather.date = moment(forecast.day_start_local, "X"); + weather.minTemperature = forecast.air_temp_low; + weather.maxTemperature = forecast.air_temp_high; + weather.weatherType = forecast.icon; + weather.snow = 0; + + + days.push(weather); + } + + this.setWeatherForecast(days); + + }) + .catch(function (request) { + Log.error("Could not load data ... ", request); + }) + .finally(() => this.updateAvailable()); + }, + + // Create a URL from the config and base URL. + getUrl() { + return this.config.apiBase + "better_forecast?station_id=" + this.config.stationid + "&token=" + this.config.token + } + }); \ No newline at end of file From 45142bc1bc5038efa8dfaeb58812e445c05e985e Mon Sep 17 00:00:00 2001 From: tobias Date: Mon, 21 Feb 2022 14:58:48 +0100 Subject: [PATCH 2/6] updated the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 588889e5..6e4997c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ _This release is scheduled to be released on 2022-04-01._ - The modules get a class hidden added/removed if they get hidden/shown - Added new config option `showTitleAsUrl` to newsfeed module. If set, the diplayed title is a link to the article which is useful when running in a browser and you want to read this article. - Added internal cors proxy to get weather providers working without public proxies (fixes #2714). The new url `http(s)://address:port/cors?url=https://whatever-to-proxy` can be used in other modules too. +- Added a WeatherProvider for Weatherflow ### Updated From 212e60c12d184c0f96184d29003b07da8b88f278 Mon Sep 17 00:00:00 2001 From: tobias Date: Tue, 22 Feb 2022 11:12:17 +0100 Subject: [PATCH 3/6] fix copy and paste mistake in comment --- modules/default/weather/providers/weatherflow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/default/weather/providers/weatherflow.js b/modules/default/weather/providers/weatherflow.js index 0ac27b6b..294d4191 100644 --- a/modules/default/weather/providers/weatherflow.js +++ b/modules/default/weather/providers/weatherflow.js @@ -2,7 +2,7 @@ /* MagicMirror² * Module: Weather - * Provider: Dark Sky + * Provider: Weatherflow * * By Tobias Dreyem https://github.com/10bias * MIT Licensed From 4048d79fc54dae8b0e2ae227784541816ec2871d Mon Sep 17 00:00:00 2001 From: tobias Date: Tue, 22 Feb 2022 11:14:28 +0100 Subject: [PATCH 4/6] run lint:prettier --- .../default/weather/providers/weatherflow.js | 97 +++++++++---------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/modules/default/weather/providers/weatherflow.js b/modules/default/weather/providers/weatherflow.js index 294d4191..af332000 100644 --- a/modules/default/weather/providers/weatherflow.js +++ b/modules/default/weather/providers/weatherflow.js @@ -14,73 +14,68 @@ WeatherProvider.register("weatherflow", { // Set the name of the provider. // Not strictly required, but helps for debugging - providerName: "WeatherFlow", + providerName: "WeatherFlow", - // Set the default config properties that is specific to this provider - defaults: { + // Set the default config properties that is specific to this provider + defaults: { apiBase: "https://swd.weatherflow.com/swd/rest/", token: "", - stationid: "", + stationid: "" }, - units: { + units: { imperial: "us", metric: "si" }, + fetchCurrentWeather() { + this.fetchData(this.getUrl()) + .then((data) => { + const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); + currentWeather.date = moment(); - fetchCurrentWeather() { - this.fetchData(this.getUrl()) - .then((data) => { - const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); - currentWeather.date = moment(); - - currentWeather.humidity = data.current_conditions.relative_humidity; - currentWeather.temperature = data.current_conditions.air_temperature; - currentWeather.windSpeed = data.current_conditions.wind_avg; - currentWeather.windDirection = data.current_conditions.wind_direction; - currentWeather.weatherType = data.forecast.daily[0].icon; - currentWeather.sunrise = moment(data.forecast.daily[0].sunrise), "X"; - currentWeather.sunset = moment(data.forecast.daily[0].sunset, "X"); - this.setCurrentWeather(currentWeather); - - }) - .catch(function (request) { + currentWeather.humidity = data.current_conditions.relative_humidity; + currentWeather.temperature = data.current_conditions.air_temperature; + currentWeather.windSpeed = data.current_conditions.wind_avg; + currentWeather.windDirection = data.current_conditions.wind_direction; + currentWeather.weatherType = data.forecast.daily[0].icon; + (currentWeather.sunrise = moment(data.forecast.daily[0].sunrise)), "X"; + currentWeather.sunset = moment(data.forecast.daily[0].sunset, "X"); + this.setCurrentWeather(currentWeather); + }) + .catch(function (request) { Log.error("Could not load data ... ", request); }) .finally(() => this.updateAvailable()); - }, - - fetchWeatherForecast() { - this.fetchData(this.getUrl()) - .then((data) => { - const days = []; + }, - for(const forecast of data.forecast.daily) { + fetchWeatherForecast() { + this.fetchData(this.getUrl()) + .then((data) => { + const days = []; - const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); + for (const forecast of data.forecast.daily) { + const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); - weather.date = moment(forecast.day_start_local, "X"); - weather.minTemperature = forecast.air_temp_low; - weather.maxTemperature = forecast.air_temp_high; - weather.weatherType = forecast.icon; - weather.snow = 0; + weather.date = moment(forecast.day_start_local, "X"); + weather.minTemperature = forecast.air_temp_low; + weather.maxTemperature = forecast.air_temp_high; + weather.weatherType = forecast.icon; + weather.snow = 0; + days.push(weather); + } - days.push(weather); - } + this.setWeatherForecast(days); + }) + .catch(function (request) { + Log.error("Could not load data ... ", request); + }) + .finally(() => this.updateAvailable()); + }, - this.setWeatherForecast(days); - - }) - .catch(function (request) { - Log.error("Could not load data ... ", request); - }) - .finally(() => this.updateAvailable()); - }, - - // Create a URL from the config and base URL. - getUrl() { - return this.config.apiBase + "better_forecast?station_id=" + this.config.stationid + "&token=" + this.config.token - } - }); \ No newline at end of file + // Create a URL from the config and base URL. + getUrl() { + return this.config.apiBase + "better_forecast?station_id=" + this.config.stationid + "&token=" + this.config.token; + } +}); From 3c27fd10b6a128fe5ed0815e8eb5d89fbff1e856 Mon Sep 17 00:00:00 2001 From: tobias Date: Fri, 25 Feb 2022 18:30:01 +0100 Subject: [PATCH 5/6] fix a problem with sunrise --- modules/default/weather/providers/weatherflow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/default/weather/providers/weatherflow.js b/modules/default/weather/providers/weatherflow.js index af332000..03bdbdab 100644 --- a/modules/default/weather/providers/weatherflow.js +++ b/modules/default/weather/providers/weatherflow.js @@ -39,7 +39,7 @@ WeatherProvider.register("weatherflow", { currentWeather.windSpeed = data.current_conditions.wind_avg; currentWeather.windDirection = data.current_conditions.wind_direction; currentWeather.weatherType = data.forecast.daily[0].icon; - (currentWeather.sunrise = moment(data.forecast.daily[0].sunrise)), "X"; + currentWeather.sunrise = moment(data.forecast.daily[0].sunrise, "X"); currentWeather.sunset = moment(data.forecast.daily[0].sunset, "X"); this.setCurrentWeather(currentWeather); }) From 9056abaf4a9efd3d2282c682113977b9706061ca Mon Sep 17 00:00:00 2001 From: tobias Date: Sat, 26 Feb 2022 11:09:41 +0100 Subject: [PATCH 6/6] add function to switch units --- .../default/weather/providers/weatherflow.js | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/modules/default/weather/providers/weatherflow.js b/modules/default/weather/providers/weatherflow.js index 03bdbdab..dc3dc19c 100644 --- a/modules/default/weather/providers/weatherflow.js +++ b/modules/default/weather/providers/weatherflow.js @@ -24,8 +24,20 @@ WeatherProvider.register("weatherflow", { }, units: { - imperial: "us", - metric: "si" + imperial: { + temp: "f", + wind: "mph", + pressure: "hpa", + precip: "in", + distance: "mi" + }, + metric: { + temp: "c", + wind: "kph", + pressure: "mb", + precip: "mm", + distance: "km" + } }, fetchCurrentWeather() { @@ -76,6 +88,22 @@ WeatherProvider.register("weatherflow", { // Create a URL from the config and base URL. getUrl() { - return this.config.apiBase + "better_forecast?station_id=" + this.config.stationid + "&token=" + this.config.token; + return ( + this.config.apiBase + + "better_forecast?station_id=" + + this.config.stationid + + "&units_temp=" + + this.units[this.config.units].temp + + "&units_wind=" + + this.units[this.config.units].wind + + "&units_pressure=" + + this.units[this.config.units].pressure + + "&units_precip=" + + this.units[this.config.units].precip + + "&units_distance=" + + this.units[this.config.units].distance + + "&token=" + + this.config.token + ); } });