From 27c8d0a2385a27ae641bb2daa3f8b828f75c9cd0 Mon Sep 17 00:00:00 2001 From: simatec Date: Wed, 31 Jul 2024 23:33:38 +0200 Subject: [PATCH] (simatec) Fix sleep up when open the Window --- README.md | 1 + lib/shutterUpSleep.js | 75 ++++++++++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 89edcd6..56396c2 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ When the adapter crashes or an other Code error happens, this error message that * (simatec) Trigger changed * (simatec) Code revision and improvements * (simatec) many small fixes +* (simatec) Fix sleep up when open the Window ### 1.7.3 (2024-06-20) * (simatec) Fix Sunprotect End-Delay diff --git a/lib/shutterUpSleep.js b/lib/shutterUpSleep.js index d3a2c73..6c4e188 100644 --- a/lib/shutterUpSleep.js +++ b/lib/shutterUpSleep.js @@ -56,21 +56,26 @@ async function driveshutterUpSleep(adapter, upTimeSleep, autoSleepStr, shutterSe shutterSettings[s].alarmTriggerAction = shutterSettings[s].currentAction; if (pendingAlarm == false) { - let currentValue = ''; - const _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; let mustValue = ('' + shutterSettings[s].triggerState); let mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - if (typeof _triggerState != undefined && _triggerState != null) { - currentValue = ('' + _triggerState.val); - } + let currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; + + if (currentValue === mustValue || + currentValue === mustValueTilted || + (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].autoDrive != 'onlyDown' && + shutterSettings[s].autoDrive != 'off')) { - if (currentValue === mustValue || currentValue === mustValueTilted || (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyDown' && shutterSettings[s].autoDrive != 'off')) { const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); - if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight && Date.now() >= (_shutterState.lc + noGoDelay)) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight && + Date.now() >= (_shutterState.lc + noGoDelay)) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].lastAutoAction = 'up_SleepTime'; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger @@ -79,49 +84,73 @@ async function driveshutterUpSleep(adapter, upTimeSleep, autoSleepStr, shutterSe await setShutterState(adapter, shutterSettings, shutterSettings[s], shutterHeight, nameDevice, 'Sleep up #21'); adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); - adapter.log.debug('shutterUpSleep #1 ' + shutterSettings[s].shutterName + ' old height: ' + shutterSettings[s].oldHeight + '% new height: ' + shutterHeight + '%'); + adapter.log.debug(`shutterUpSleep #1 ${shutterSettings[s].shutterName} old height: ${shutterSettings[s].oldHeight}% new height: ${shutterHeight}%`); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight && Date.now() >= (_shutterState.lc + noGoDelay)) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight && + Date.now() >= (_shutterState.lc + noGoDelay)) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger shutterSettings[s].triggerAction = shutterSettings[s].currentAction; // reset Trigger await setShutterInfo(adapter, shutterSettings, shutterSettings[s], nameDevice); - adapter.log.debug('shutterUpSleep #1 ' + shutterSettings[s].shutterName + ' already up at: ' + shutterHeight + '% - setting current action: ' + shutterSettings[s].currentAction); + adapter.log.debug(`shutterUpSleep #1 ${shutterSettings[s].shutterName} already up at: ${shutterHeight}% - setting current action: ${shutterSettings[s].currentAction}`); } } else if (shutterSettings[s].triggerID == '') { const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); - if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight && Date.now() >= (_shutterState.lc + noGoDelay)) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight && + Date.now() >= (_shutterState.lc + noGoDelay)) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].lastAutoAction = 'up_SleepTime'; + await setShutterState(adapter, shutterSettings, shutterSettings[s], shutterHeight, nameDevice, 'Sleep up #22'); adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); - adapter.log.debug('shutterUpSleep #2 ' + shutterSettings[s].shutterName + ' old height: ' + shutterSettings[s].oldHeight + '% new height: ' + shutterHeight + '%'); + adapter.log.debug(`shutterUpSleep #2 ${shutterSettings[s].shutterName} old height: ${shutterSettings[s].oldHeight}% new height: ${shutterHeight}%`); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight && Date.now() >= (_shutterState.lc + noGoDelay)) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight && + Date.now() >= (_shutterState.lc + noGoDelay)) { + shutterSettings[s].currentHeight = shutterHeight; await setShutterInfo(adapter, shutterSettings, shutterSettings[s], nameDevice); - adapter.log.debug('shutterUpSleep #2 ' + shutterSettings[s].shutterName + ' already up at: ' + shutterHeight + '% - setting current action: ' + shutterSettings[s].currentAction); + adapter.log.debug(`shutterUpSleep #2 ${shutterSettings[s].shutterName} already up at: ${shutterHeight}% - setting current action: ${shutterSettings[s].currentAction}`); } - } else if (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].driveAfterClose == true) { + } else if (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].driveAfterClose == true) { + const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); - if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight) { + + shutterSettings[s].triggerHeight = shutterHeight; + shutterSettings[s].triggerAction = shutterSettings[s].currentAction; + + adapter.log.info(`#23 Will open ID: ${shutterSettings[s].shutterName} value: ${shutterHeight}% after the window has been closed`); + adapter.log.debug(`save new trigger height: ${shutterHeight}%`); + adapter.log.debug(`save new trigger action: ${shutterSettings[s].triggerAction}`); + } else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + shutterSettings[s].triggerHeight = shutterHeight; shutterSettings[s].triggerAction = shutterSettings[s].currentAction; - adapter.log.info('#23 Will open ID: ' + shutterSettings[s].shutterName + ' value: ' + shutterHeight + '%' + ' after the window has been closed '); - adapter.log.debug('save new trigger height: ' + shutterHeight + '%'); - adapter.log.debug('save new trigger action: ' + shutterSettings[s].triggerAction); + adapter.log.info(`#24 Will open ID: ${shutterSettings[s].shutterName} value: ${shutterHeight}% after is the value for sleep up`); + adapter.log.debug(`save new trigger height: ${shutterHeight}%`); + adapter.log.debug(`save new trigger action: ${shutterSettings[s].triggerAction}`); } } } else { - adapter.log.info('Sleep up not moving now due to active alarm: ' + shutterSettings[s].shutterName + ' value: ' + shutterHeight + '%'); + adapter.log.info(`Sleep up not moving now due to active alarm: ${shutterSettings[s].shutterName} value: ${shutterHeight}%`); } await sleep(driveDelayUpSleep); } @@ -129,9 +158,11 @@ async function driveshutterUpSleep(adapter, upTimeSleep, autoSleepStr, shutterSe } } adapter.log.debug(`Adapter Auto-Sleep is: ${autoSleepStr}`); - + // start sleep-auto after drive sleep shutter - if ((autoSleepStr === true || autoSleepStr === 'true') && sleepType === 'sleep') { + if ((autoSleepStr === true || autoSleepStr === 'true') && + sleepType === 'sleep') { + await sleep(timeoutSleepAuto); sleepType = 'sleep-auto'; driveshutterUpSleep(adapter, upTimeSleep, autoSleepStr, shutterSettings, sleepType, driveDelayUpSleep, timeoutSleepAuto, noGoDelay);