From b46429c71397a8fd7d1e72e18a839bc14d811b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Mon, 12 Feb 2024 17:14:48 +0100 Subject: [PATCH] see issue #208: timeframe can be disabled by user (optionally) --- README.md | 1 + lib/semp/Gateway.js | 4 +- lib/semp/Planningrequest.js | 4 +- lib/semp/Timeframe.js | 73 +++++++++++++++++++++++++++--------- lib/semp/TimeframeWallbox.js | 4 +- 5 files changed, 63 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 3b18cea..52fe616 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ An description about general usage of energy requests see [SMA docu](docu/SMA/SS ### **WORK IN PROGRESS** * (René) bug fix: see issue #206 - wallbox OID's selectable * (René) bug fix: see issue #207 - wallbox maximum charge time adjustable +* (René) see issue #208: timeframe can be disabled by user (optionally) ### 1.3.15 (2024-02-03) * (René) bug fix: wallbox counter and status are not handled diff --git a/lib/semp/Gateway.js b/lib/semp/Gateway.js index 47533f7..fc335e8 100644 --- a/lib/semp/Gateway.js +++ b/lib/semp/Gateway.js @@ -286,7 +286,7 @@ class Gateway { return this.devices.get(id); } - getAllDevices() { + async getAllDevices() { const ds = []; try { this.parentAdapter.log.debug("get all devices "); @@ -304,7 +304,7 @@ class Gateway { let requests = []; if (dev.planningrequest != null) { - requests = dev.planningrequest.getPlanningrequestData(); + requests = await dev.planningrequest.getPlanningrequestData(); for (let r = 0; r < requests.length; r++) { requests[r].DeviceId = d.deviceInfo.Identification.DeviceId; diff --git a/lib/semp/Planningrequest.js b/lib/semp/Planningrequest.js index d5a44bf..33f413e 100644 --- a/lib/semp/Planningrequest.js +++ b/lib/semp/Planningrequest.js @@ -109,13 +109,13 @@ class Planningrequest { } - getPlanningrequestData() { + async getPlanningrequestData() { const PlanningrequestData = []; if (this.WallboxPlugConnected || this.settings.DeviceType != "EVCharger") { for (let t = 0; t < this.timeframes.length; t++) { - const timeframeData = this.timeframes[t].getTimeframeData(); + const timeframeData = await this.timeframes[t].getTimeframeData(); if (timeframeData != null) { PlanningrequestData.push(timeframeData); } diff --git a/lib/semp/Timeframe.js b/lib/semp/Timeframe.js index 5bb9f1f..dc7dca4 100644 --- a/lib/semp/Timeframe.js +++ b/lib/semp/Timeframe.js @@ -219,26 +219,41 @@ class Timeframe { } } - getTimeframeData() { + async getTimeframeData() { let timeframeData = null; - if (this.EarliestStart > 0 || this.LatestEnd > 0) { - timeframeData = { - - TimeframeId: this.settings.ID, - DeviceId: "", //to be filled later - EarliestStart: this.EarliestStart, - LatestEnd: this.LatestEnd, - MinRunningTime: this.MinRunningTime, - MaxRunningTime: this.MaxRunningTime, - }; - } - this.parentAdapter.log.debug(this.deviceName + " ( " + this.settings.ID + ") timeframe data " + JSON.stringify(timeframeData)); + const key = "Devices." + this.deviceName + ".TimeFrames." + this.settings.ID + ".active"; + + const current = await this.parentAdapter.getStateAsync(key); + + if (current != null) { + if (current.val == true) { + if (this.EarliestStart > 0 || this.LatestEnd > 0) { + timeframeData = { - const key = "Devices." + this.deviceName + ".TimeFrames." + this.settings.ID + ".LastSent"; + TimeframeId: this.settings.ID, + DeviceId: "", //to be filled later + EarliestStart: this.EarliestStart, + LatestEnd: this.LatestEnd, + MinRunningTime: this.MinRunningTime, + MaxRunningTime: this.MaxRunningTime, + }; + } - this.parentAdapter.setState(key, { ack: true, val: JSON.stringify(timeframeData) }); + this.parentAdapter.log.debug(this.deviceName + " ( " + this.settings.ID + ") timeframe data " + JSON.stringify(timeframeData)); + + const key = "Devices." + this.deviceName + ".TimeFrames." + this.settings.ID + ".LastSent"; + + await this.parentAdapter.setStateAsync(key, { ack: true, val: JSON.stringify(timeframeData) }); + } + else { + this.parentAdapter.log.debug(this.deviceName + " ( " + this.settings.ID + ") timeframe disabled by user "); + } + } + else { + this.parentAdapter.log.error(this.deviceName + " ( " + this.settings.ID + ") timeframe active not readable "); + } return timeframeData; @@ -298,8 +313,6 @@ class Timeframe { const now = new Date(); const dayOfWeek = now.getDay(); this.CanceledOnDay = dayOfWeek; - - } } @@ -367,6 +380,22 @@ class Timeframe { }; await this.CreateObject(key, obj); + key = "Devices." + this.deviceName + ".TimeFrames." + this.settings.ID + ".active"; + obj = { + type: "state", + common: { + name: "timeframe is active", + type: "boolean", + role: "value", + unit: "", + read: true, + write: true, + desc: "switch to enable / disable timeframe" + } + }; + await this.CreateObject(key, obj); + this.SetDefault(key, "true"); + } async CreateObject(key, obj) { @@ -404,6 +433,16 @@ class Timeframe { } } + async SetDefault(key, value) { + const current = await this.parentAdapter.getStateAsync(key); + //set default only if nothing was set before + if (current === null || current === undefined || current.val === undefined) { + this.parentAdapter.log.info("set default " + key + " to " + value); + await this.parentAdapter.setStateAsync(key, { ack: true, val: value }); + } + } + + UpdateObjects() { let Hour = Math.floor(this.CurrentOnTime / 60 / 60); let Minutes = Math.floor((this.CurrentOnTime - (Hour * 60 * 60)) / 60); diff --git a/lib/semp/TimeframeWallbox.js b/lib/semp/TimeframeWallbox.js index dad7756..bd59910 100644 --- a/lib/semp/TimeframeWallbox.js +++ b/lib/semp/TimeframeWallbox.js @@ -153,7 +153,7 @@ class TimeframeWallbox { } } - getTimeframeData() { + async getTimeframeData() { let timeframeData = null; if (this.EarliestStart > 0 || this.LatestEnd > 0) { @@ -176,7 +176,7 @@ class TimeframeWallbox { const key = "Devices." + this.deviceName + ".TimeFrames." + this.ID + ".LastSent"; - this.parentAdapter.setState(key, { ack: true, val: JSON.stringify(timeframeData) }); + await this.parentAdapter.setStateAsync(key, { ack: true, val: JSON.stringify(timeframeData) }); return timeframeData; }