From df0f048ecc018d55119108d7bb7682f3ea3f80b0 Mon Sep 17 00:00:00 2001 From: eouia Date: Tue, 26 Apr 2022 13:55:39 +0200 Subject: [PATCH 1/5] Added outgoing notification --- CHANGELOG.md | 2 ++ modules/default/weather/weather.js | 9 +++++++++ modules/default/weather/weatherobject.js | 14 ++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f7e61de..5f306611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ _This release is scheduled to be released on 2022-07-01._ ### Added +- Added the notification emitting from the weather module on infromation updated. + ### Updated ### Fixed diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 350c4f2d..d86384f8 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -154,6 +154,15 @@ Module.register("weather", { if (this.weatherProvider.currentWeather()) { this.sendNotification("CURRENTWEATHER_TYPE", { type: this.weatherProvider.currentWeather().weatherType.replace("-", "_") }); } + + let notificationPayload = { + current: this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, + forecast: this.weatherProvider?.weatherForecastArray?.map((ar) => ar.simpleClone()) ?? [], + hourly: this.weatherProvider?.weatherHourlyArray?.map((ar) => ar.simpleClone()) ?? [], + location: this.weatherProvider?.fetchedLocationName, + provider: this.weatherProvider.providerName + }; + this.sendNotification("WEATHER_UPDATED", notificationPayload); }, scheduleUpdate: function (delay = null) { diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index e9359345..9b5a21a9 100755 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -146,6 +146,20 @@ class WeatherObject { this.sunrise = moment(times.sunrise, "X"); this.sunset = moment(times.sunset, "X"); } + + /** + * Clone to simple object to prevent mutating and deprecated legacy library. + * + * @return {object} simple object cloned. + */ + simpleClone() { + const toFlat = ["date", "sunrise", "sunset"]; + let clone = { ...this }; + for (const prop of toFlat) { + clone[prop] = clone?.[prop]?.valueOf() ?? clone?.[prop]; + } + return clone; + } } /*************** DO NOT EDIT THE LINE BELOW ***************/ From 58569a648c61ab56b742e1f3add181ec5422b40f Mon Sep 17 00:00:00 2001 From: eouia Date: Tue, 26 Apr 2022 14:51:12 +0200 Subject: [PATCH 2/5] Added ECMA Version of lint to 2020 --- .eslintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 751bf56d..9d40203b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -16,7 +16,7 @@ }, "parserOptions": { "sourceType": "module", - "ecmaVersion": 2018, + "ecmaVersion": 2020, "ecmaFeatures": { "globalReturn": true } From 038b6765e7c3f7e8d365c92fef9775e2de3e3f0c Mon Sep 17 00:00:00 2001 From: eouia Date: Tue, 26 Apr 2022 14:52:05 +0200 Subject: [PATCH 3/5] Change property name clearly --- modules/default/weather/weather.js | 10 +++++----- modules/default/weather/weatherobject.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index d86384f8..8a8dc90c 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -156,11 +156,11 @@ Module.register("weather", { } let notificationPayload = { - current: this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, - forecast: this.weatherProvider?.weatherForecastArray?.map((ar) => ar.simpleClone()) ?? [], - hourly: this.weatherProvider?.weatherHourlyArray?.map((ar) => ar.simpleClone()) ?? [], - location: this.weatherProvider?.fetchedLocationName, - provider: this.weatherProvider.providerName + currentWeather: this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, + forecastArray: this.weatherProvider?.weatherForecastArray?.map((ar) => ar.simpleClone()) ?? [], + hourlyArray: this.weatherProvider?.weatherHourlyArray?.map((ar) => ar.simpleClone()) ?? [], + locationName: this.weatherProvider?.fetchedLocationName, + providerName: this.weatherProvider.providerName }; this.sendNotification("WEATHER_UPDATED", notificationPayload); }, diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 9b5a21a9..0e57bfca 100755 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -126,7 +126,7 @@ class WeatherObject { /** * Checks if the weatherObject is at dayTime. * - * @returns {boolean} true if it is at dayTime + * @return {boolean} true if it is at dayTime */ isDayTime() { return this.date.isBetween(this.sunrise, this.sunset, undefined, "[]"); From 0f39b7733c4bbbe23f0a769150a035775c651b25 Mon Sep 17 00:00:00 2001 From: eouia Date: Tue, 26 Apr 2022 14:57:47 +0200 Subject: [PATCH 4/5] fix typo jsdom tag returns --- modules/default/weather/weatherobject.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 0e57bfca..08b79fef 100755 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -126,7 +126,7 @@ class WeatherObject { /** * Checks if the weatherObject is at dayTime. * - * @return {boolean} true if it is at dayTime + * @returns {boolean} true if it is at dayTime */ isDayTime() { return this.date.isBetween(this.sunrise, this.sunset, undefined, "[]"); @@ -150,7 +150,7 @@ class WeatherObject { /** * Clone to simple object to prevent mutating and deprecated legacy library. * - * @return {object} simple object cloned. + * @returns {object} simple object cloned. */ simpleClone() { const toFlat = ["date", "sunrise", "sunset"]; From 1d90c5e1fec6826f606de9809fd1ef9e176968c8 Mon Sep 17 00:00:00 2001 From: eouia Date: Tue, 26 Apr 2022 17:37:23 +0200 Subject: [PATCH 5/5] Add JSDoc description --- modules/default/weather/weather.js | 2 +- modules/default/weather/weatherobject.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/default/weather/weather.js b/modules/default/weather/weather.js index 8a8dc90c..6a02d182 100644 --- a/modules/default/weather/weather.js +++ b/modules/default/weather/weather.js @@ -155,7 +155,7 @@ Module.register("weather", { this.sendNotification("CURRENTWEATHER_TYPE", { type: this.weatherProvider.currentWeather().weatherType.replace("-", "_") }); } - let notificationPayload = { + const notificationPayload = { currentWeather: this.weatherProvider?.currentWeatherObject?.simpleClone() ?? null, forecastArray: this.weatherProvider?.weatherForecastArray?.map((ar) => ar.simpleClone()) ?? [], hourlyArray: this.weatherProvider?.weatherHourlyArray?.map((ar) => ar.simpleClone()) ?? [], diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 08b79fef..c2e6727b 100755 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -148,9 +148,12 @@ class WeatherObject { } /** - * Clone to simple object to prevent mutating and deprecated legacy library. + * Clone to simple object to prevent mutating and deprecation of legacy library. * - * @returns {object} simple object cloned. + * Before being handed to other modules, mutable values must be cloned safely. + * Especially 'moment' object is not immutable, so original 'date', 'sunrise', 'sunset' could be corrupted or changed by other modules. + * + * @returns {object} plained object clone of original weatherObject */ simpleClone() { const toFlat = ["date", "sunrise", "sunset"];