From cc274ffebe2736ac2ab07ca969d57ced911f5b9b Mon Sep 17 00:00:00 2001 From: fewieden Date: Sun, 30 Dec 2018 14:11:16 +0100 Subject: [PATCH 1/3] fixed darksky metric units --- modules/default/weather/providers/darksky.js | 35 ++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/modules/default/weather/providers/darksky.js b/modules/default/weather/providers/darksky.js index 4a049c70..e4cb78b3 100644 --- a/modules/default/weather/providers/darksky.js +++ b/modules/default/weather/providers/darksky.js @@ -16,10 +16,10 @@ WeatherProvider.register("darksky", { units: { imperial: 'us', - metric: 'ca' + metric: 'si' }, - fetchCurrentWeather: function() { + fetchCurrentWeather() { this.fetchData(this.getUrl()) .then(data => { if(!data || !data.currently || typeof data.currently.temperature === "undefined") { @@ -27,14 +27,14 @@ WeatherProvider.register("darksky", { return; } - var currentWeather = this.generateWeatherDayFromCurrentWeather(data); + const currentWeather = this.generateWeatherDayFromCurrentWeather(data); this.setCurrentWeather(currentWeather); }).catch(function(request) { Log.error("Could not load data ... ", request); }); }, - fetchWeatherForecast: function() { + fetchWeatherForecast() { this.fetchData(this.getUrl()) .then(data => { if(!data || !data.daily || !data.daily.data.length) { @@ -42,7 +42,7 @@ WeatherProvider.register("darksky", { return; } - var forecast = this.generateWeatherObjectsFromForecast(data.daily.data); + const forecast = this.generateWeatherObjectsFromForecast(data.daily.data); this.setWeatherForecast(forecast); }).catch(function(request) { Log.error("Could not load data ... ", request); @@ -50,14 +50,14 @@ WeatherProvider.register("darksky", { }, // Create a URL from the config and base URL. - getUrl: function() { - var units = this.units[this.config.units] || 'auto'; + getUrl() { + const units = this.units[this.config.units] || "auto"; return `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`; }, // Implement WeatherDay generator. - generateWeatherDayFromCurrentWeather: function(currentWeatherData) { - var currentWeather = new WeatherObject(); + generateWeatherDayFromCurrentWeather(currentWeatherData) { + const currentWeather = new WeatherObject(this.config.units); currentWeather.date = moment(); currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity); @@ -71,11 +71,11 @@ WeatherProvider.register("darksky", { return currentWeather; }, - generateWeatherObjectsFromForecast: function(forecasts) { - var days = []; + generateWeatherObjectsFromForecast(forecasts) { + const days = []; - for (var forecast of forecasts) { - var weather = new WeatherObject(); + for (const forecast of forecasts) { + const weather = new WeatherObject(this.config.units); weather.date = moment(forecast.time, "X"); weather.minTemperature = forecast.temperatureMin; @@ -83,15 +83,15 @@ WeatherProvider.register("darksky", { weather.weatherType = this.convertWeatherType(forecast.icon); weather.rain = forecast.precipAccumulation; - days.push(weather) + days.push(weather); } - return days + return days; }, // Map icons from Dark Sky to our icons. - convertWeatherType: function(weatherType) { - var weatherTypes = { + convertWeatherType(weatherType) { + const weatherTypes = { "clear-day": "day-sunny", "clear-night": "night-clear", "rain": "rain", @@ -103,6 +103,7 @@ WeatherProvider.register("darksky", { "partly-cloudy-day": "day-cloudy", "partly-cloudy-night": "night-cloudy" }; + return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null; } }); From 88d862303ddd7705529f99e99714c8f95a283ae8 Mon Sep 17 00:00:00 2001 From: fewieden Date: Sun, 30 Dec 2018 14:14:17 +0100 Subject: [PATCH 2/3] fixed beaufortwindspeed for imperial units --- modules/default/weather/weatherobject.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index b6ee9b95..a30a415c 100644 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -13,7 +13,8 @@ // As soon as we start implementing the forecast, mode properties will be added. class WeatherObject { - constructor() { + constructor(units) { + this.units = units; this.date = null; this.windSpeed = null; this.windDirection = null; @@ -26,7 +27,7 @@ class WeatherObject { this.humidity = null; } - cardinalWindDirection () { + cardinalWindDirection() { if (this.windDirection > 11.25 && this.windDirection <= 33.75){ return "NNE"; } else if (this.windDirection > 33.75 && this.windDirection <= 56.25) { @@ -62,20 +63,18 @@ class WeatherObject { } } - beaufortWindSpeed () { - var kmh = this.windSpeed * 60 * 60 / 1000; - var speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000]; - for (var beaufort in speeds) { - var speed = speeds[beaufort]; - if (speed > kmh) { - return beaufort; + beaufortWindSpeed() { + const windInKmh = this.units === "imperial" ? this.windSpeed * 1.609344 : this.windSpeed * 60 * 60 / 1000; + const speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000]; + for (const [index, speed] of speeds.entries()) { + if (speed > windInKmh) { + return index; } } return 12; } - nextSunAction () { - var now = new Date(); - return (this.sunrise < now && this.sunset > now) ? "sunset" : "sunrise"; + nextSunAction() { + return moment().isBetween(this.sunrise, this.sunset) ? "sunset" : "sunrise"; } } From 8dd7621f2937611bc9c344db3909dd7d186723c2 Mon Sep 17 00:00:00 2001 From: fewieden Date: Sun, 30 Dec 2018 14:17:13 +0100 Subject: [PATCH 3/3] add original feels like temperature and fixed it for imperial units --- modules/default/weather/README.md | 2 +- modules/default/weather/current.njk | 7 +++++ .../weather/providers/openweathermap.js | 30 +++++++++---------- modules/default/weather/weather.js | 3 +- modules/default/weather/weatherobject.js | 19 ++++++++++++ 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/modules/default/weather/README.md b/modules/default/weather/README.md index d8766824..ee9ec7b8 100644 --- a/modules/default/weather/README.md +++ b/modules/default/weather/README.md @@ -48,7 +48,6 @@ The following properties can be configured: | `lang` | The language of the days.

**Possible values:** `en`, `nl`, `ru`, etc ...
**Default value:** uses value of _config.language_ | `decimalSymbol` | The decimal symbol to use.

**Possible values:** `.`, `,` or any other symbol.
**Default value:** `.` | `initialLoadDelay` | The initial delay before loading. If you have multiple modules that use the same API key, you might want to delay one of the requests. (Milliseconds)

**Possible values:** `1000` - `5000`
**Default value:** `0` -| `retryDelay` | The delay before retrying after a request failure. (Milliseconds)

**Possible values:** `1000` - `60000`
**Default value:** `2500` | `appendLocationNameToHeader` | If set to `true`, the returned location name will be appended to the header of the module, if the header is enabled. This is mainly intresting when using calender based weather.

**Default value:** `true` | `calendarClass` | The class for the calender module to base the event based weather information on.

**Default value:** `'calendar'` @@ -63,6 +62,7 @@ The following properties can be configured: | `showHumidity` | Show the current humidity

**Possible values:** `true` or `false`
**Default value:** `false` | `showIndoorTemperature` | If you have another module that emits the `INDOOR_TEMPERATURE` notification, the indoor temperature will be displayed
**Default value:** `false` | `showIndoorHumidity` | If you have another module that emits the `INDOOR_HUMIDITY` notification, the indoor humidity will be displayed
**Default value:** `false` +| `showFeelsLike` | Shows the Feels like temperature weather.

**Possible values:**`true` or `false`
**Default value:** `true` #### Weather forecast options diff --git a/modules/default/weather/current.njk b/modules/default/weather/current.njk index a788bcc2..aed51ac6 100644 --- a/modules/default/weather/current.njk +++ b/modules/default/weather/current.njk @@ -51,6 +51,13 @@ {% endif %} + {% if config.showFeelsLike and not config.onlyTemp %} +
+ + {{ "FEELS" | translate }} {{ current.feelsLike() | roundValue | unit("temperature") }} + +
+ {% endif %} {% else %}
{{"LOADING" | translate}} diff --git a/modules/default/weather/providers/openweathermap.js b/modules/default/weather/providers/openweathermap.js index 8fd1e3be..caa88b21 100644 --- a/modules/default/weather/providers/openweathermap.js +++ b/modules/default/weather/providers/openweathermap.js @@ -17,7 +17,7 @@ WeatherProvider.register("openweathermap", { providerName: "OpenWeatherMap", // Overwrite the fetchCurrentWeather method. - fetchCurrentWeather: function() { + fetchCurrentWeather() { this.fetchData(this.getUrl()) .then(data => { if (!data || !data.main || typeof data.main.temp === "undefined") { @@ -28,7 +28,7 @@ WeatherProvider.register("openweathermap", { this.setFetchedLocation(`${data.name}, ${data.sys.country}`); - var currentWeather = this.generateWeatherObjectFromCurrentWeather(data); + const currentWeather = this.generateWeatherObjectFromCurrentWeather(data); this.setCurrentWeather(currentWeather); }) .catch(function(request) { @@ -37,7 +37,7 @@ WeatherProvider.register("openweathermap", { }, // Overwrite the fetchCurrentWeather method. - fetchWeatherForecast: function() { + fetchWeatherForecast() { this.fetchData(this.getUrl()) .then(data => { if (!data || !data.list || !data.list.length) { @@ -48,7 +48,7 @@ WeatherProvider.register("openweathermap", { this.setFetchedLocation(`${data.city.name}, ${data.city.country}`); - var forecast = this.generateWeatherObjectsFromForecast(data.list); + const forecast = this.generateWeatherObjectsFromForecast(data.list); this.setWeatherForecast(forecast); }) .catch(function(request) { @@ -62,15 +62,15 @@ WeatherProvider.register("openweathermap", { /* * Gets the complete url for the request */ - getUrl: function() { + getUrl() { return this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams(); }, /* * Generate a WeatherObject based on currentWeatherInformation */ - generateWeatherObjectFromCurrentWeather: function(currentWeatherData) { - var currentWeather = new WeatherObject(); + generateWeatherObjectFromCurrentWeather(currentWeatherData) { + const currentWeather = new WeatherObject(this.config.units); currentWeather.humidity = currentWeatherData.main.humidity; currentWeather.temperature = currentWeatherData.main.temp; @@ -86,11 +86,11 @@ WeatherProvider.register("openweathermap", { /* * Generate WeatherObjects based on forecast information */ - generateWeatherObjectsFromForecast: function(forecasts) { - var days = []; + generateWeatherObjectsFromForecast(forecasts) { + const days = []; - for (var forecast of forecasts) { - var weather = new WeatherObject(); + for (const forecast of forecasts) { + const weather = new WeatherObject(this.config.units); weather.date = moment(forecast.dt, "X"); weather.minTemperature = forecast.temp.min; @@ -107,8 +107,8 @@ WeatherProvider.register("openweathermap", { /* * Convert the OpenWeatherMap icons to a more usable name. */ - convertWeatherType: function(weatherType) { - var weatherTypes = { + convertWeatherType(weatherType) { + const weatherTypes = { "01d": "day-sunny", "02d": "day-cloudy", "03d": "cloudy", @@ -137,8 +137,8 @@ WeatherProvider.register("openweathermap", { * * return String - URL params. */ - getParams: function() { - var params = "?"; + getParams() { + let params = "?"; if(this.config.locationID) { params += "id=" + this.config.locationID; } else if(this.config.location) { diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 03e15bda..d2a64749 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -46,7 +46,8 @@ Module.register("weather",{ onlyTemp: false, showRainAmount: true, - colored: false + colored: false, + showFeelsLike: true }, // Module properties. diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index a30a415c..599708c3 100644 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -77,4 +77,23 @@ class WeatherObject { nextSunAction() { return moment().isBetween(this.sunrise, this.sunset) ? "sunset" : "sunrise"; } + + feelsLike() { + const windInMph = this.units === "imperial" ? this.windSpeed : this.windSpeed * 2.23694; + const tempInF = this.units === "imperial" ? this.temperature : this.temperature * 9 / 5 + 32; + let feelsLike = tempInF; + + if (windInMph > 3 && tempInF < 50) { + feelsLike = Math.round(35.74 + 0.6215 * tempInF - 35.75 * Math.pow(windInMph, 0.16) + 0.4275 * tempInF * Math.pow(windInMph, 0.16)); + } else if (tempInF > 80 && this.humidity > 40) { + feelsLike = -42.379 + 2.04901523 * tempInF + 10.14333127 * this.humidity + - 0.22475541 * tempInF * this.humidity - 6.83783 * Math.pow(10, -3) * tempInF * tempInF + - 5.481717 * Math.pow(10, -2) * this.humidity * this.humidity + + 1.22874 * Math.pow(10, -3) * tempInF * tempInF * this.humidity + + 8.5282 * Math.pow(10, -4) * tempInF * this.humidity * this.humidity + - 1.99 * Math.pow(10, -6) * tempInF * tempInF * this.humidity * this.humidity; + } + + return this.units === "imperial" ? feelsLike : (feelsLike - 32) * 5 / 9; + } }