From aef7479c98aa80aa134e10a94e6e291cd110f9cb Mon Sep 17 00:00:00 2001 From: simatec Date: Wed, 7 Aug 2024 10:37:48 +0200 Subject: [PATCH] (simatec) Code revision and improvements --- lib/shutterDownLiving.js | 49 ++++++++++++------- lib/shutterDownSleep.js | 49 ++++++++++++------- lib/shutterGoldenHour.js | 93 ++++++++++++++++++++++++------------- lib/shutterState.js | 12 +++-- lib/shutterSunriseSunset.js | 77 +++++++++++++++++++----------- lib/shutterUpChildren.js | 54 ++++++++++++++------- lib/shutterUpLiving.js | 45 ++++++++++++------ lib/shutterUpSleep.js | 18 +++---- lib/tools.js | 2 + lib/triggerChange.js | 25 +++++++--- 10 files changed, 281 insertions(+), 143 deletions(-) diff --git a/lib/shutterDownLiving.js b/lib/shutterDownLiving.js index a176685c..ca62d9fb 100644 --- a/lib/shutterDownLiving.js +++ b/lib/shutterDownLiving.js @@ -72,7 +72,10 @@ async function driveshutterDownLiving(adapter, downTimeLiving, autoLivingStr, sh shutterSettings[s].alarmTriggerAction = downAction; // overwrite target position and downAction if frost alarm is set. - if (statusAlarmFrost == true && shutterSettings[s].enableAlarmFrost == true && downAction != 'Xmas') { + if (statusAlarmFrost == true && + shutterSettings[s].enableAlarmFrost == true && + downAction != 'Xmas') { + targetLevel2Set = parseFloat(adapter.config.alarmFrostLevel); downAction = 'frost'; } @@ -81,21 +84,25 @@ async function driveshutterDownLiving(adapter, downTimeLiving, autoLivingStr, sh const nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); const _autoDownState = await adapter.getStateAsync(`shutters.autoDown.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoDownState && _autoDownState === true || _autoDownState && _autoDownState.val === true) { + if (_autoDownState?.val === true) { if (pendingAlarm == false) { - let currentValue = ''; - const _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; + const mustValue = ('' + shutterSettings[s].triggerState); + const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; + + if (currentValue === mustValue || + currentValue === mustValueTilted || + (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].autoDrive != 'onlyUp' && + shutterSettings[s].autoDrive != 'off')) { - 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); - } - if (currentValue === mustValue || currentValue === mustValueTilted || (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyUp' && 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; shutterSettings[s].lastAutoAction = 'Down_LivingTime'; @@ -107,7 +114,9 @@ async function driveshutterDownLiving(adapter, downTimeLiving, autoLivingStr, sh adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterDownLiving #1 ' + shutterSettings[s].shutterName + ' old height: ' + shutterSettings[s].oldHeight + '% new height: ' + targetLevel2Set + '%'); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; @@ -118,7 +127,9 @@ async function driveshutterDownLiving(adapter, downTimeLiving, autoLivingStr, sh } 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; shutterSettings[s].lastAutoAction = 'Down_LivingTime'; @@ -128,7 +139,9 @@ async function driveshutterDownLiving(adapter, downTimeLiving, autoLivingStr, sh adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterDownLiving #2 ' + shutterSettings[s].shutterName + ' old height: ' + shutterSettings[s].oldHeight + '% new height: ' + targetLevel2Set + '%'); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; @@ -136,10 +149,14 @@ async function driveshutterDownLiving(adapter, downTimeLiving, autoLivingStr, sh adapter.log.debug('shutterDownLiving #2 ' + shutterSettings[s].shutterName + ' already down at: ' + targetLevel2Set + '% - 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { shutterSettings[s].triggerHeight = targetLevel2Set; shutterSettings[s].triggerAction = downAction; diff --git a/lib/shutterDownSleep.js b/lib/shutterDownSleep.js index 03d5bcfa..618fa5d4 100644 --- a/lib/shutterDownSleep.js +++ b/lib/shutterDownSleep.js @@ -71,7 +71,10 @@ async function driveshutterDownSleep(adapter, downTimeSleep, autoSleepStr, shutt shutterSettings[s].alarmTriggerAction = downAction; // overwrite target position and downAction if frost alarm is set. - if (statusAlarmFrost == true && shutterSettings[s].enableAlarmFrost == true && downAction != 'Xmas') { + if (statusAlarmFrost == true && + shutterSettings[s].enableAlarmFrost == true && + downAction != 'Xmas') { + targetLevel2Set = parseFloat(adapter.config.alarmFrostLevel); downAction = 'frost'; } @@ -80,23 +83,26 @@ async function driveshutterDownSleep(adapter, downTimeSleep, autoSleepStr, shutt const nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); const _autoDownState = await adapter.getStateAsync(`shutters.autoDown.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoDownState && _autoDownState === true || _autoDownState && _autoDownState.val === true) { + if (_autoDownState?.val === true) { if (pendingAlarm == false) { - let currentValue = ''; - const _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; - const mustValue = ('' + shutterSettings[s].triggerState); const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; - if (typeof _triggerState != undefined && _triggerState != null) { - currentValue = ('' + _triggerState.val); - } - if (currentValue === mustValue || currentValue === mustValueTilted || (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyUp' && shutterSettings[s].autoDrive != 'off')) { + if (currentValue === mustValue || + currentValue === mustValueTilted || + (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].autoDrive != 'onlyUp' && + shutterSettings[s].autoDrive != 'off')) { + const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); - if (typeof _shutterState != undefined && _shutterState != null && _shutterState.val != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; shutterSettings[s].lastAutoAction = 'Down_SleepTime'; @@ -108,7 +114,9 @@ async function driveshutterDownSleep(adapter, downTimeSleep, autoSleepStr, shutt adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterDownSleep #1 ' + shutterSettings[s].shutterName + ' old height: ' + shutterSettings[s].oldHeight + '% new height: ' + targetLevel2Set + '%'); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; shutterSettings[s].triggerHeight = targetLevel2Set; // reset Trigger @@ -121,7 +129,9 @@ async function driveshutterDownSleep(adapter, downTimeSleep, autoSleepStr, shutt } 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; shutterSettings[s].lastAutoAction = 'Down_SleepTime'; @@ -131,7 +141,9 @@ async function driveshutterDownSleep(adapter, downTimeSleep, autoSleepStr, shutt adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterDownSleep #2 ' + shutterSettings[s].shutterName + ' old height: ' + shutterSettings[s].oldHeight + '% new height: ' + targetLevel2Set + '%'); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; @@ -139,14 +151,19 @@ async function driveshutterDownSleep(adapter, downTimeSleep, autoSleepStr, shutt adapter.log.debug('shutterDownSleep #2 ' + shutterSettings[s].shutterName + ' already down at: ' + targetLevel2Set + '% - 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].triggerHeight = targetLevel2Set; shutterSettings[s].triggerAction = downAction; - adapter.log.info('#28 Will close ID: ' + shutterSettings[s].shutterName + ' value: ' + targetLevel2Set + '%' + ' after the window has been closed '); + adapter.log.info('#28 Will close ID: ' + shutterSettings[s].shutterName + ' value: ' + targetLevel2Set + '%' + ' after the window has been closed'); adapter.log.debug('save new trigger height: ' + shutterSettings[s].triggerHeight + '%'); adapter.log.debug('save new trigger action: ' + shutterSettings[s].triggerAction); } diff --git a/lib/shutterGoldenHour.js b/lib/shutterGoldenHour.js index 87b6b6e9..dfbb8480 100644 --- a/lib/shutterGoldenHour.js +++ b/lib/shutterGoldenHour.js @@ -32,7 +32,6 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett if (goldenHourEnd) { const upTime = goldenHourEnd.split(':'); - //const statusAlarmFrost = await adapter.getForeignStateAsync(adapter.config.alarmFrost); //??? schedule.cancelJob('shutterUpGoldenHourEnd'); @@ -49,10 +48,12 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett const _autoUpState = await adapter.getStateAsync(`shutters.autoUp.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoUpState && _autoUpState === true || _autoUpState && _autoUpState.val === true) { + if (_autoUpState?.val === true) { let shutterHeight = 0; - if (shutterSettings[s].currentAction == 'sunProtect' || shutterSettings[s].currentAction == 'OpenInSunProtect') { + if (shutterSettings[s].currentAction == 'sunProtect' || + shutterSettings[s].currentAction == 'OpenInSunProtect') { + shutterHeight = parseFloat(shutterSettings[s].heightDownSun); shutterSettings[s].currentAction = 'sunProtect'; } else { @@ -61,21 +62,23 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett } if (pendingAlarm == false) { - let currentValue = ''; - const _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID) : null; - const mustValue = ('' + shutterSettings[s].triggerState); const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; - if (typeof _triggerState != undefined && _triggerState != null) { - currentValue = ('' + _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) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].lastAutoAction = 'GoldenHour_up'; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger @@ -86,7 +89,9 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('save current height: ' + shutterSettings[s].currentHeight + '%' + ' from ' + shutterSettings[s].shutterName); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger shutterSettings[s].triggerAction = shutterSettings[s].currentAction; // reset Trigger @@ -98,7 +103,9 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett } 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) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].lastAutoAction = 'GoldenHour_up'; @@ -107,18 +114,24 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('save current height: ' + shutterSettings[s].currentHeight + '%' + ' from ' + shutterSettings[s].shutterName); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; await setShutterInfo(adapter, shutterSettings, shutterSettings[s], nameDevice); adapter.log.debug('GoldenHour up ' + shutterSettings[s].shutterName + ' already up at: ' + shutterSettings[s].heightUp + '% - 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 (_shutterState?.val && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight) { + shutterSettings[s].triggerHeight = shutterHeight; shutterSettings[s].triggerAction = shutterSettings[s].currentAction; @@ -191,7 +204,10 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett shutterSettings[s].alarmTriggerAction = downAction; // overwrite target position and downAction if frost alarm is set. - if (statusAlarmFrost == true && shutterSettings[s].enableAlarmFrost == true && downAction != 'Xmas') { + if (statusAlarmFrost == true && + shutterSettings[s].enableAlarmFrost == true && + downAction != 'Xmas') { + targetLevel2Set = parseFloat(adapter.config.alarmFrostLevel); downAction = 'frost'; } @@ -199,21 +215,25 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett if (!inSummerNotDown) { const _autoDownState = await adapter.getStateAsync(`shutters.autoDown.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoDownState && _autoDownState === true || _autoDownState && _autoDownState.val === true) { + if (_autoDownState?.val === true) { if (pendingAlarm == false) { - let currentValue = ''; - const _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; + const mustValue = ('' + shutterSettings[s].triggerState); + const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; + + if (currentValue === mustValue || + currentValue === mustValueTilted || + (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].autoDrive != 'onlyUp' && + shutterSettings[s].autoDrive != 'off')) { - 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); - } - if (currentValue === mustValue || currentValue === mustValueTilted || (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyUp' && 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; shutterSettings[s].lastAutoAction = 'GoldenHour_down'; @@ -225,7 +245,9 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('save current height: ' + shutterSettings[s].currentHeight + '%' + ' from ' + shutterSettings[s].shutterName); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; @@ -236,7 +258,9 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett } 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; shutterSettings[s].lastAutoAction = 'GoldenHour_down'; @@ -246,7 +270,9 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('save current height: ' + shutterSettings[s].currentHeight + '%' + ' from ' + shutterSettings[s].shutterName); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; @@ -254,10 +280,15 @@ async function shutterGoldenHour(adapter, goldenHourEnd, goldenHour, shutterSett adapter.log.debug('GoldenHour down ' + shutterSettings[s].shutterName + ' already down at: ' + targetLevel2Set + '% - 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].triggerHeight = targetLevel2Set; shutterSettings[s].triggerAction = downAction; diff --git a/lib/shutterState.js b/lib/shutterState.js index 0329da2d..9e1f0a1e 100644 --- a/lib/shutterState.js +++ b/lib/shutterState.js @@ -13,7 +13,9 @@ async function shutterState(shutterID, adapter, shutterSettings, checkTimeDelay) return new Promise(async (resolve) => { let checkTime = 1000; - if (adapter.config.currentShutterState == true && checkTimeDelay == true) { + if (adapter.config.currentShutterState == true && + checkTimeDelay == true) { + checkTime = adapter.config.currentShutterStateTime ? (adapter.config.currentShutterStateTime * 1000) : 60000; } @@ -25,13 +27,14 @@ async function shutterState(shutterID, adapter, shutterSettings, checkTimeDelay) for (const s in shutterSettings) { if (shutterSettings[s].shutterName == result[i].shutterName) { const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); + const nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); - let nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); + if (_shutterState?.val && + shutterSettings[s].currentHeight != Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound) { - if (typeof _shutterState != undefined && _shutterState != null && shutterSettings[s].currentHeight != Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound) { shutterSettings[s].currentHeight = Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound; - await adapter.setStateAsync('shutters.autoLevel.' + nameDevice, { val: parseFloat(shutterSettings[s].currentHeight), ack: true }) + await adapter.setStateAsync(`shutters.autoLevel.${nameDevice}`, { val: parseFloat(shutterSettings[s].currentHeight), ack: true }) .catch((e) => adapter.log.warn(e)); adapter.log.debug(`save current height after State Check: ${shutterSettings[s].currentHeight}% from ${shutterSettings[s].shutterName}`); @@ -48,7 +51,6 @@ async function shutterState(shutterID, adapter, shutterSettings, checkTimeDelay) } clearTimeout(timerSleep); resolve(shutterSettings); - //return (shutterSettings); } else { resolve(shutterSettings); } diff --git a/lib/shutterSunriseSunset.js b/lib/shutterSunriseSunset.js index 6b8643b7..9c0c9824 100644 --- a/lib/shutterSunriseSunset.js +++ b/lib/shutterSunriseSunset.js @@ -48,7 +48,7 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti const _autoUpState = await adapter.getStateAsync(`shutters.autoUp.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoUpState && _autoUpState === true || _autoUpState && _autoUpState.val === true) { + if (_autoUpState?.val === true) { let shutterHeight = 0; if (shutterSettings[s].currentAction == 'sunProtect' || shutterSettings[s].currentAction == 'OpenInSunProtect') { @@ -64,21 +64,23 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti 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; - const mustValue = ('' + shutterSettings[s].triggerState); const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; - if (typeof _triggerState != undefined && _triggerState != null) { - currentValue = ('' + _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) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].lastAutoAction = 'Sunrise_up'; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger @@ -89,7 +91,9 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('save current height: ' + shutterSettings[s].currentHeight + '%' + ' from ' + shutterSettings[s].shutterName); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger shutterSettings[s].triggerAction = shutterSettings[s].currentAction; // reset Trigger @@ -101,7 +105,9 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti } 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) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; shutterSettings[s].lastAutoAction = 'Sunrise_up'; @@ -110,14 +116,18 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('save current height: ' + shutterSettings[s].currentHeight + '%' + ' from ' + shutterSettings[s].shutterName); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == shutterHeight) { + shutterSettings[s].currentHeight = shutterHeight; await setShutterInfo(adapter, shutterSettings, shutterSettings[s], nameDevice); adapter.log.debug('Sunrise up ' + shutterSettings[s].shutterName + ' already up at: ' + shutterSettings[s].heightUp + '% - 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 (_shutterState?.val && @@ -203,23 +213,25 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti const nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); const _autoDownState = await adapter.getStateAsync(`shutters.autoDown.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoDownState && _autoDownState === true || _autoDownState && _autoDownState.val === true) { + if (_autoDownState?.val === true) { if (pendingAlarm == false) { - let currentValue = ''; - const _triggerState = shutterSettings[s].triggerID != '' ? await adapter.getForeignStateAsync(shutterSettings[s].triggerID).catch((e) => adapter.log.warn(e)) : null; - const mustValue = ('' + shutterSettings[s].triggerState); const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; - if (typeof _triggerState != undefined && _triggerState != null) { - currentValue = ('' + _triggerState.val); - } + if (currentValue === mustValue || + currentValue === mustValueTilted || + (currentValue != mustValue && + currentValue != mustValueTilted && + shutterSettings[s].autoDrive != 'onlyUp' && + shutterSettings[s].autoDrive != 'off')) { - if (currentValue === mustValue || currentValue === mustValueTilted || (currentValue != mustValue && currentValue != mustValueTilted && shutterSettings[s].autoDrive != 'onlyUp' && 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].lastAutoAction = 'Sunset_down'; shutterSettings[s].currentAction = downAction; @@ -230,7 +242,9 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; @@ -241,7 +255,9 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti } 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].lastAutoAction = 'Sunset_down'; shutterSettings[s].currentAction = downAction; @@ -250,7 +266,9 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); } - else if (typeof _shutterState != undefined && _shutterState != null && Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + else if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound == targetLevel2Set) { + shutterSettings[s].currentHeight = targetLevel2Set; shutterSettings[s].currentAction = downAction; @@ -258,10 +276,15 @@ async function shutterSunriseSunset(adapter, sunriseStr, sunsetStr, shutterSetti adapter.log.debug('Sunset down ' + shutterSettings[s].shutterName + ' already down at: ' + targetLevel2Set + '% - 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 != targetLevel2Set) { + if (_shutterState?.val && + Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != targetLevel2Set) { + shutterSettings[s].triggerHeight = targetLevel2Set; shutterSettings[s].triggerAction = downAction; diff --git a/lib/shutterUpChildren.js b/lib/shutterUpChildren.js index 030961f2..e48a702d 100644 --- a/lib/shutterUpChildren.js +++ b/lib/shutterUpChildren.js @@ -2,7 +2,7 @@ // @ts-ignore const schedule = require('node-schedule'); -const checkPendingAlarm = require('./shutterAlarm.js').checkPendingAlarm; // shutterAlarm +const checkPendingAlarm = require('./shutterAlarm.js').checkPendingAlarm; // shutterAlarm const setShutterState = require('./setShutter.js').setShutterState; // set Shutter State const setShutterInfo = require('./setShutter.js').setShutterInfo; // set Shutter State @@ -40,9 +40,11 @@ async function driveshutterUpChildren(adapter, upTimeChildren, autoChildrenStr, const _autoUpState = await adapter.getStateAsync(`shutters.autoUp.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoUpState && _autoUpState === true || _autoUpState && _autoUpState.val === true) { + if (_autoUpState?.val === true) { let shutterHeight = 0; - if (shutterSettings[s].currentAction == 'sunProtect' || shutterSettings[s].currentAction == 'OpenInSunProtect') { + if (shutterSettings[s].currentAction == 'sunProtect' || + shutterSettings[s].currentAction == 'OpenInSunProtect') { + shutterHeight = parseFloat(shutterSettings[s].heightDownSun); shutterSettings[s].currentAction = 'sunProtect'; } else { @@ -55,21 +57,24 @@ async function driveshutterUpChildren(adapter, upTimeChildren, autoChildrenStr, 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; - const mustValue = ('' + shutterSettings[s].triggerState); const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; - if (typeof _triggerState != undefined && _triggerState != null) { - currentValue = ('' + _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_ChidrenTime'; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger @@ -80,7 +85,10 @@ async function driveshutterUpChildren(adapter, upTimeChildren, autoChildrenStr, adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterUpChildren #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 @@ -92,7 +100,10 @@ async function driveshutterUpChildren(adapter, upTimeChildren, autoChildrenStr, } 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_ChidrenTime'; @@ -101,17 +112,23 @@ async function driveshutterUpChildren(adapter, upTimeChildren, autoChildrenStr, adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterUpChildren #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('shutterUpChildren #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; @@ -140,7 +157,10 @@ async function driveshutterUpChildren(adapter, upTimeChildren, autoChildrenStr, adapter.log.debug(`Adapter Auto-Children is: ${autoChildrenStr}`); // start children-auto after drive children shutter - if ((autoChildrenStr === true || autoChildrenStr === 'true') && childrenType === 'children') { + if ((autoChildrenStr === true || + autoChildrenStr === 'true') && + childrenType === 'children') { + await sleep(timeoutChildrenAuto); childrenType = 'children-auto'; driveshutterUpChildren(adapter, upTimeChildren, autoChildrenStr, shutterSettings, childrenType, driveDelayUpChildren, timeoutChildrenAuto, noGoDelay); diff --git a/lib/shutterUpLiving.js b/lib/shutterUpLiving.js index 48c79da1..9600e01f 100644 --- a/lib/shutterUpLiving.js +++ b/lib/shutterUpLiving.js @@ -40,7 +40,7 @@ async function driveshutterUpLiving(adapter, upTimeLiving, autoLivingStr, shutte const _autoUpState = await adapter.getStateAsync(`shutters.autoUp.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoUpState && _autoUpState === true || _autoUpState && _autoUpState.val === true) { + if (_autoUpState?.val === true) { let shutterHeight = 0; if (shutterSettings[s].currentAction == 'sunProtect' || shutterSettings[s].currentAction == 'OpenInSunProtect') { @@ -56,21 +56,24 @@ async function driveshutterUpLiving(adapter, upTimeLiving, autoLivingStr, shutte 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; + const mustValue = ('' + shutterSettings[s].triggerState); + const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; - 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); - } + 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_LivingTime'; shutterSettings[s].triggerHeight = shutterSettings[s].currentHeight; // reset Trigger @@ -81,7 +84,10 @@ async function driveshutterUpLiving(adapter, upTimeLiving, autoLivingStr, shutte adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterUpLiving #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 @@ -93,7 +99,10 @@ async function driveshutterUpLiving(adapter, upTimeLiving, autoLivingStr, shutte } 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_LivingTime'; @@ -102,7 +111,10 @@ async function driveshutterUpLiving(adapter, upTimeLiving, autoLivingStr, shutte adapter.log.debug(`last automatic Action for ${shutterSettings[s].shutterName}: ${shutterSettings[s].lastAutoAction}`); adapter.log.debug('shutterUpLiving #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); @@ -146,7 +158,10 @@ async function driveshutterUpLiving(adapter, upTimeLiving, autoLivingStr, shutte adapter.log.debug(`Adapter Auto-Living is: ${autoLivingStr}`); // start living-auto after drive living shutter - if ((autoLivingStr === true || autoLivingStr === 'true') && livingType === 'living') { + if ((autoLivingStr === true || + autoLivingStr === 'true') && + livingType === 'living') { + await sleep(timeoutLivingAuto); livingType = 'living-auto'; driveshutterUpLiving(adapter, upTimeLiving, autoLivingStr, shutterSettings, livingType, driveDelayUpLiving, timeoutLivingAuto, noGoDelay); diff --git a/lib/shutterUpSleep.js b/lib/shutterUpSleep.js index 6c4e1887..735c3292 100644 --- a/lib/shutterUpSleep.js +++ b/lib/shutterUpSleep.js @@ -37,13 +37,14 @@ async function driveshutterUpSleep(adapter, upTimeSleep, autoSleepStr, shutterSe if (shutterSettings[s].shutterName == result[i].shutterName) { const nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); const pendingAlarm = await checkPendingAlarm(adapter, shutterSettings[s]); - const _autoUpState = await adapter.getStateAsync(`shutters.autoUp.${nameDevice}`).catch((e) => adapter.log.warn(e)); - if (_autoUpState && _autoUpState === true || _autoUpState && _autoUpState.val === true) { + if (_autoUpState?.val === true) { let shutterHeight = 0; - if (shutterSettings[s].currentAction == 'sunProtect' || shutterSettings[s].currentAction == 'OpenInSunProtect') { + if (shutterSettings[s].currentAction == 'sunProtect' || + shutterSettings[s].currentAction == 'OpenInSunProtect') { + shutterHeight = parseFloat(shutterSettings[s].heightDownSun); shutterSettings[s].currentAction = 'sunProtect'; } else { @@ -57,11 +58,9 @@ async function driveshutterUpSleep(adapter, upTimeSleep, autoSleepStr, shutterSe if (pendingAlarm == false) { 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); - - let currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; + const mustValue = ('' + shutterSettings[s].triggerState); + const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); + const currentValue = _triggerState?.val ? ('' + _triggerState.val) : ''; if (currentValue === mustValue || currentValue === mustValueTilted || @@ -160,7 +159,8 @@ 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') && + if ((autoSleepStr === true || + autoSleepStr === 'true') && sleepType === 'sleep') { await sleep(timeoutSleepAuto); diff --git a/lib/tools.js b/lib/tools.js index f8b40178..53f9b6d1 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -54,6 +54,7 @@ async function translateYandex(text, targetLang, apiKey) { } try { const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`; + // @ts-ignore const response = await axios({url, timeout: 15000}); if (response.data && response.data['text']) { return response.data['text'][0]; @@ -73,6 +74,7 @@ async function translateYandex(text, targetLang, apiKey) { async function translateGoogle(text, targetLang) { try { const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}&ie=UTF-8&oe=UTF-8`; + // @ts-ignore const response = await axios({url, timeout: 15000}); if (isArray(response.data)) { // we got a valid response diff --git a/lib/triggerChange.js b/lib/triggerChange.js index 7515c541..40c0d9db 100644 --- a/lib/triggerChange.js +++ b/lib/triggerChange.js @@ -49,11 +49,16 @@ async function triggerChange(resTriggerChange, adapter, shutterSettings) { adapter.log.debug(`${shutterSettings[s].shutterName} - shutter trigger change is: ${shutterSettings[s].triggerChange}`); if (_shutterState?.val && - Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != setTriggerHeight && shutterSettings[s].triggerChange != 'off' && + Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != setTriggerHeight && + shutterSettings[s].triggerChange != 'off' && ((parseFloat(shutterSettings[s].triggerDriveTildet) == setTriggerHeight && - Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != setTriggerHeight && shutterSettings[s].currentAction == 'triggered') || - (Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound < setTriggerHeight && convertShutter == false) || - (Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound > setTriggerHeight && convertShutter == true))) { + Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound != setTriggerHeight && + shutterSettings[s].currentAction == 'triggered') || + (Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound < setTriggerHeight && + convertShutter == false) || + (Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound > setTriggerHeight && + convertShutter == true))) { + await sleep(shutterSettings[s].trigDelyUp ? shutterSettings[s].trigDelyUp * 1000 : 10); shutterSettings[s].triggerHeight = shutterSettings[s].currentAction != 'triggered' ? Math.round(parseFloat(_shutterState.val) / adapter.config.shutterStateRound) * adapter.config.shutterStateRound : shutterSettings[s].triggerHeight; @@ -93,7 +98,9 @@ async function triggerChange(resTriggerChange, adapter, shutterSettings) { } } } - if (shutterSettings[s].triggerChange == 'onlyDown' || shutterSettings[s].triggerChange == 'upDown') { + if (shutterSettings[s].triggerChange == 'onlyDown' || + shutterSettings[s].triggerChange == 'upDown') { + const nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); const _autoDownState = await adapter.getStateAsync(`shutters.autoDown.${nameDevice}`).catch((e) => adapter.log.warn(e)); @@ -167,7 +174,9 @@ async function triggerChange(resTriggerChange, adapter, shutterSettings) { } } } - if (shutterSettings[s].triggerChange == 'off' && shutterSettings[s].driveAfterClose == true) { + if (shutterSettings[s].triggerChange == 'off' && + shutterSettings[s].driveAfterClose == true) { + const nameDevice = shutterSettings[s].shutterName.replace(/[.;, ]/g, '_'); const _autoDownState = await adapter.getStateAsync(`shutters.autoDown.${nameDevice}`).catch((e) => adapter.log.warn(e)); @@ -180,7 +189,9 @@ async function triggerChange(resTriggerChange, adapter, shutterSettings) { const mustValue = ('' + shutterSettings[s].triggerState); const mustValueTilted = shutterSettings[s].triggerStateTilted == 'none' ? ('' + shutterSettings[s].triggerState) : ('' + shutterSettings[s].triggerStateTilted); - if (currentValue === mustValue || currentValue === mustValueTilted) { + if (currentValue === mustValue || + currentValue === mustValueTilted) { + const _shutterState = await adapter.getForeignStateAsync(shutterSettings[s].name).catch((e) => adapter.log.warn(e)); if (_shutterState?.val) { adapter.log.debug(`${shutterSettings[s].shutterName} - shutter current state.val is: ${Math.round(_shutterState.val / adapter.config.shutterStateRound) * adapter.config.shutterStateRound}%`);