From 2ababa521d2bd8cb491165f220d133b5db6acabe Mon Sep 17 00:00:00 2001 From: rejas Date: Sun, 21 Mar 2021 16:58:12 +0100 Subject: [PATCH] Use es6 notation in weather module and ukmet provider --- .../default/weather/providers/ukmetoffice.js | 37 ++++++++++--------- modules/default/weather/weather.js | 31 ++++++++-------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/modules/default/weather/providers/ukmetoffice.js b/modules/default/weather/providers/ukmetoffice.js index 11cee48f..27127806 100755 --- a/modules/default/weather/providers/ukmetoffice.js +++ b/modules/default/weather/providers/ukmetoffice.js @@ -81,6 +81,7 @@ WeatherProvider.register("ukmetoffice", { */ generateWeatherObjectFromCurrentWeather(currentWeatherData) { const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); + const location = currentWeatherData.SiteRep.DV.Location; // data times are always UTC let nowUtc = moment.utc(); @@ -88,8 +89,8 @@ WeatherProvider.register("ukmetoffice", { let timeInMins = nowUtc.diff(midnightUtc, "minutes"); // loop round each of the (5) periods, look for today (the first period may be yesterday) - for (var i in currentWeatherData.SiteRep.DV.Location.Period) { - let periodDate = moment.utc(currentWeatherData.SiteRep.DV.Location.Period[i].value.substr(0, 10), "YYYY-MM-DD"); + for (const period of location.Period) { + const periodDate = moment.utc(period.value.substr(0, 10), "YYYY-MM-DD"); // ignore if period is before today if (periodDate.isSameOrAfter(moment.utc().startOf("day"))) { @@ -97,17 +98,17 @@ WeatherProvider.register("ukmetoffice", { if (moment().diff(periodDate, "minutes") > 0) { // loop round the reports looking for the one we are in // $ value specifies the time in minutes-of-the-day: 0, 180, 360,...1260 - for (var j in currentWeatherData.SiteRep.DV.Location.Period[i].Rep) { - let p = currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].$; + for (const rep of period.Rep) { + const p = rep.$; if (timeInMins >= p && timeInMins - 180 < p) { // finally got the one we want, so populate weather object - currentWeather.humidity = currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].H; - currentWeather.temperature = this.convertTemp(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].T); - currentWeather.feelsLikeTemp = this.convertTemp(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].F); - currentWeather.precipitation = parseInt(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].Pp); - currentWeather.windSpeed = this.convertWindSpeed(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].S); - currentWeather.windDirection = this.convertWindDirection(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].D); - currentWeather.weatherType = this.convertWeatherType(currentWeatherData.SiteRep.DV.Location.Period[i].Rep[j].W); + currentWeather.humidity = rep.H; + currentWeather.temperature = this.convertTemp(rep.T); + currentWeather.feelsLikeTemp = this.convertTemp(rep.F); + currentWeather.precipitation = parseInt(rep.Pp); + currentWeather.windSpeed = this.convertWindSpeed(rep.S); + currentWeather.windDirection = this.convertWindDirection(rep.D); + currentWeather.weatherType = this.convertWeatherType(rep.W); } } } @@ -115,7 +116,7 @@ WeatherProvider.register("ukmetoffice", { } // determine the sunrise/sunset times - not supplied in UK Met Office data - let times = this.calcAstroData(currentWeatherData.SiteRep.DV.Location); + let times = this.calcAstroData(location); currentWeather.sunrise = times[0]; currentWeather.sunset = times[1]; @@ -130,21 +131,21 @@ WeatherProvider.register("ukmetoffice", { // loop round the (5) periods getting the data // for each period array, Day is [0], Night is [1] - for (var j in forecasts.SiteRep.DV.Location.Period) { + for (const period of forecasts.SiteRep.DV.Location.Period) { const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh); // data times are always UTC - const dateStr = forecasts.SiteRep.DV.Location.Period[j].value; + const dateStr = period.value; let periodDate = moment.utc(dateStr.substr(0, 10), "YYYY-MM-DD"); // ignore if period is before today if (periodDate.isSameOrAfter(moment.utc().startOf("day"))) { // populate the weather object weather.date = moment.utc(dateStr.substr(0, 10), "YYYY-MM-DD"); - weather.minTemperature = this.convertTemp(forecasts.SiteRep.DV.Location.Period[j].Rep[1].Nm); - weather.maxTemperature = this.convertTemp(forecasts.SiteRep.DV.Location.Period[j].Rep[0].Dm); - weather.weatherType = this.convertWeatherType(forecasts.SiteRep.DV.Location.Period[j].Rep[0].W); - weather.precipitation = parseInt(forecasts.SiteRep.DV.Location.Period[j].Rep[0].PPd); + weather.minTemperature = this.convertTemp(period.Rep[1].Nm); + weather.maxTemperature = this.convertTemp(period.Rep[0].Dm); + weather.weatherType = this.convertWeatherType(period.Rep[0].W); + weather.precipitation = parseInt(period.Rep[0].PPd); days.push(weather); } diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 50c73031..3bf1d774 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -48,6 +48,9 @@ Module.register("weather", { // Module properties. weatherProvider: null, + // Can be used by the provider to display location of event if nothing else is specified + firstEvent: null, + // Define required scripts. getStyles: function () { return ["font-awesome.css", "weather-icons.css", "weather.css"]; @@ -88,15 +91,13 @@ Module.register("weather", { // Override notification handler. notificationReceived: function (notification, payload, sender) { if (notification === "CALENDAR_EVENTS") { - var senderClasses = sender.data.classes.toLowerCase().split(" "); + const senderClasses = sender.data.classes.toLowerCase().split(" "); if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) { - this.firstEvent = false; - - for (var e in payload) { - var event = payload[e]; + this.firstEvent = null; + for (let event of payload) { if (event.location || event.geo) { this.firstEvent = event; - //Log.log("First upcoming event with location: ", event); + Log.debug("First upcoming event with location: ", event); break; } } @@ -114,15 +115,15 @@ Module.register("weather", { getTemplate: function () { switch (this.config.type.toLowerCase()) { case "current": - return `current.njk`; + return "current.njk"; case "hourly": - return `hourly.njk`; + return "hourly.njk"; case "daily": case "forecast": - return `forecast.njk`; + return "forecast.njk"; //Make the invalid values use the "Loading..." from forecast default: - return `forecast.njk`; + return "forecast.njk"; } }, @@ -152,7 +153,7 @@ Module.register("weather", { }, scheduleUpdate: function (delay = null) { - var nextLoad = this.config.updateInterval; + let nextLoad = this.config.updateInterval; if (delay !== null && delay >= 0) { nextLoad = delay; } @@ -176,8 +177,8 @@ Module.register("weather", { }, roundValue: function (temperature) { - var decimals = this.config.roundTemp ? 0 : 1; - var roundValue = parseFloat(temperature).toFixed(decimals); + const decimals = this.config.roundTemp ? 0 : 1; + const roundValue = parseFloat(temperature).toFixed(decimals); return roundValue === "-0" ? 0 : roundValue; }, @@ -272,8 +273,8 @@ Module.register("weather", { if (this.config.fadePoint < 0) { this.config.fadePoint = 0; } - var startingPoint = numSteps * this.config.fadePoint; - var numFadesteps = numSteps - startingPoint; + const startingPoint = numSteps * this.config.fadePoint; + const numFadesteps = numSteps - startingPoint; if (currentStep >= startingPoint) { return 1 - (currentStep - startingPoint) / numFadesteps; } else {