From 7017d18ecc862492a97dcc6aa7e2eb4d23cc05f5 Mon Sep 17 00:00:00 2001 From: Jens Lody Date: Mon, 28 Dec 2015 09:35:48 +0100 Subject: [PATCH] Do not use weather-storm if forecast.io's icon is wind, but widnspeed is less than 39 mph. Use an icon based on the cloudcast instead in this case. Really fix for #83 . --- src/forecast_io.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/forecast_io.js b/src/forecast_io.js index 43b3ad9..92a7198 100644 --- a/src/forecast_io.js +++ b/src/forecast_io.js @@ -44,7 +44,7 @@ const ngettext = Gettext.ngettext; const OPENWEATHER_URL_HOST = 'api.forecast.io'; const OPENWEATHER_URL_BASE = 'https://' + OPENWEATHER_URL_HOST + '/forecast/'; -function getWeatherIcon(icon) { +function getWeatherIcon(icon, windspeed, cloudcover, night) { // clear-day weather-clear-day // clear-night weather-clear-night // rain weather-showers @@ -56,6 +56,15 @@ function getWeatherIcon(icon) { // partly-cloudy-day weather-few-clouds // partly-cloudy-night weather-few-clouds-night let iconname = ['weather-severe-alert']; + if ((icon == 'wind') && (windspeed < 39)) { + if (cloudcover < 0.3) + icon = 'clear-' + (night ? 'night' : 'day'); + else if (cloudcover < 0.7) + icon = 'partly-cloudy-' + (night ? 'night' : 'day'); + else + icon = 'cloudy'; + } + switch (icon) { case 'wind': iconname = ['weather-storm']; @@ -87,7 +96,7 @@ function getWeatherIcon(icon) { break; } for (let i = 0; i < iconname.length; i++) { - if (this.hasIcon(iconname[i])) + if (this.hasIcon(iconname[i]) + this.getIconType()) return iconname[i] + this.getIconType(); } return 'weather-severe-alert' + this.getIconType(); @@ -125,8 +134,6 @@ function parseWeatherCurrent() { let now = new Date(); - let iconname = this.getWeatherIcon(json.icon); - if (this.lastBuildId === undefined) this.lastBuildId = 0; @@ -155,8 +162,6 @@ function parseWeatherCurrent() { } } - this._currentWeatherIcon.icon_name = this._weatherIcon.icon_name = iconname; - let weatherInfoC = ""; let weatherInfoT = ""; if (this._comment_in_panel) @@ -178,6 +183,15 @@ function parseWeatherCurrent() { this._currentWeatherWind.text = this.formatWind(json.windSpeed, this.getWindDirection(json.windBearing)); this.parseWeatherForecast(); + + // get weather icon last, because we might need sunrise and sunset to know whether it's day or night + let iconname = this.getWeatherIcon(json.icon, + json.windSpeed, + json.cloudCover, + now < this._currentWeatherSunrise.text || now > this._currentWeatherSunset.text); + + this._currentWeatherIcon.icon_name = this._weatherIcon.icon_name = iconname; + this.recalcLayout(); } @@ -279,6 +293,9 @@ function parseWeatherForecast() { forecastUi.Day.text = date_string + ' (' + this.getLocaleDay(forecastDate.getDay()) + ')\n' + forecastDate.toLocaleDateString(); forecastUi.Temperature.text = '\u2193 ' + t_low + ' \u2191 ' + t_high; forecastUi.Summary.text = comment; - forecastUi.Icon.icon_name = this.getWeatherIcon(forecastData.icon); + + forecastUi.Icon.icon_name = this.getWeatherIcon(forecastData.icon, + forecastData.windSpeed, + forecastData.cloudCover); } }