diff --git a/modules/default/weather/providers/ukmetoffice.js b/modules/default/weather/providers/ukmetoffice.js index 27127806..d2443866 100755 --- a/modules/default/weather/providers/ukmetoffice.js +++ b/modules/default/weather/providers/ukmetoffice.js @@ -116,9 +116,7 @@ WeatherProvider.register("ukmetoffice", { } // determine the sunrise/sunset times - not supplied in UK Met Office data - let times = this.calcAstroData(location); - currentWeather.sunrise = times[0]; - currentWeather.sunset = times[1]; + currentWeather.updateSunTime(location.lat, location.lon); return currentWeather; }, @@ -154,20 +152,6 @@ WeatherProvider.register("ukmetoffice", { return days; }, - /* - * calculate the astronomical data - */ - calcAstroData(location) { - const sunTimes = []; - - // determine the sunrise/sunset times - let times = SunCalc.getTimes(new Date(), location.lat, location.lon); - sunTimes.push(moment(times.sunrise, "X")); - sunTimes.push(moment(times.sunset, "X")); - - return sunTimes; - }, - /* * Convert the Met Office icons to a more usable name. */ diff --git a/modules/default/weather/providers/weathergov.js b/modules/default/weather/providers/weathergov.js index ab46b4ee..9dc1fddf 100755 --- a/modules/default/weather/providers/weathergov.js +++ b/modules/default/weather/providers/weathergov.js @@ -167,9 +167,7 @@ WeatherProvider.register("weathergov", { currentWeather.weatherType = this.convertWeatherType(currentWeatherData.textDescription, 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]; + currentWeather.updateSunTime(this.config.lat, this.config.lon); return currentWeather; }, @@ -272,20 +270,6 @@ WeatherProvider.register("weathergov", { } }, - /* - * 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. */ diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 5bd5be9a..b6a840c4 100755 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -1,3 +1,5 @@ +/* global SunCalc */ + /* Magic Mirror * Module: Weather * @@ -113,4 +115,16 @@ class WeatherObject { return this.tempUnits === "imperial" ? feelsLike : ((feelsLike - 32) * 5) / 9; } + + /** + * Update the sunrise / sunset time depending on the location + * + * @param {number} lat latitude + * @param {number} lon longitude + */ + updateSunTime(lat, lon) { + let times = SunCalc.getTimes(new Date(), lat, lon); + this.sunrise = moment(times.sunrise, "X"); + this.sunset = moment(times.sunset, "X"); + } }