diff --git a/CHANGELOG.md b/CHANGELOG.md index 315851f850..4dd12cc955 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - use Feels Like temp from feed if present - optionally display probability of precipitation (PoP) in current weather (UK Met Office data) - automatically try to fix eslint errors by passing `--fix` option to it +- Added sunrise and sunset times to weathergov weather provider [#1705](https://github.com/MichMich/MagicMirror/issues/1705) ### Updated - English translation for "Feels" to "Feels like" diff --git a/modules/default/weather/providers/weathergov.js b/modules/default/weather/providers/weathergov.js index 77b36f1285..bfecaf24be 100755 --- a/modules/default/weather/providers/weathergov.js +++ b/modules/default/weather/providers/weathergov.js @@ -71,9 +71,14 @@ WeatherProvider.register("weathergov", { currentWeather.temperature = currentWeatherData.temperature; currentWeather.windSpeed = currentWeatherData.windSpeed.split(" ", 1); - currentWeather.windDirection = this.convertDirectiontoDegrees(currentWeatherData.windDirection); + currentWeather.windDirection = this.convertWindDirection(currentWeatherData.windDirection); currentWeather.weatherType = this.convertWeatherType(currentWeatherData.shortForecast, currentWeatherData.isDaytime); + // determine the sunrise/sunset times - not supplied in weather.gov data + let times = this.calcAstroData(this.config.lat, this.config.lon) + currentWeather.sunrise = times[0]; + currentWeather.sunset = times[1]; + return currentWeather; }, @@ -136,7 +141,7 @@ WeatherProvider.register("weathergov", { } // last day - // calculate minimum/maximum temperature, specify rain amount + // calculate minimum/maximum temperature weather.minTemperature = Math.min.apply(null, minTemp); weather.maxTemperature = Math.max.apply(null, maxTemp); @@ -145,6 +150,20 @@ WeatherProvider.register("weathergov", { return days.slice(1); }, + /* + * Calculate the astronomical data + */ + calcAstroData(lat, lon) { + const sunTimes = []; + + // determine the sunrise/sunset times + let times = SunCalc.getTimes(new Date(), lat, lon); + sunTimes.push(moment(times.sunrise, "X")); + sunTimes.push(moment(times.sunset, "X")); + + return sunTimes; + }, + /* * Convert the icons to a more usable name. */ @@ -218,39 +237,26 @@ WeatherProvider.register("weathergov", { /* Convert the direction into Degrees */ - convertDirectiontoDegrees(direction) { - if (direction === "NNE"){ - return 33.75; - } else if (direction === "NE") { - return 56.25; - } else if (direction === "ENE") { - return 78.75; - } else if (direction === "E") { - return 101.25; - } else if (direction === "ESE") { - return 123.75; - } else if (direction === "SE") { - return 146.25; - } else if (direction === "SSE") { - return 168.75; - } else if (direction === "S") { - return 191.25; - } else if (direction === "SSW") { - return 213.75; - } else if (direction === "SW") { - return 236.25; - } else if (direction === "WSW") { - return 258.75; - } else if (direction === "W") { - return 281.25; - } else if (direction === "WNW") { - return 303.75; - } else if (direction === "NW") { - return 326.25; - } else if (direction === "NNW") { - return 348.75; - } else { - return 0; - } + convertWindDirection(windDirection) { + const windCardinals = { + "N": 0, + "NNE": 22, + "NE": 45, + "ENE": 67, + "E": 90, + "ESE": 112, + "SE": 135, + "SSE": 157, + "S": 180, + "SSW": 202, + "SW": 225, + "WSW": 247, + "W": 270, + "WNW": 292, + "NW": 315, + "NNW": 337 + }; + + return windCardinals.hasOwnProperty(windDirection) ? windCardinals[windDirection] : null; } });