Skip to content

Commit

Permalink
(simatec) Fix sleep up when open the Window
Browse files Browse the repository at this point in the history
  • Loading branch information
simatec committed Jul 31, 2024
1 parent afff2cf commit 27c8d0a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 53 additions & 22 deletions lib/shutterUpSleep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -79,59 +84,85 @@ 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);
}
}
}
}
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);
Expand Down

0 comments on commit 27c8d0a

Please sign in to comment.