Skip to content

Commit

Permalink
(simatec) Code revision and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
simatec committed Aug 7, 2024
1 parent df40d08 commit aef7479
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 143 deletions.
49 changes: 33 additions & 16 deletions lib/shutterDownLiving.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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';
Expand All @@ -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;

Expand All @@ -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';
Expand All @@ -128,18 +139,24 @@ 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;

await setShutterInfo(adapter, shutterSettings, shutterSettings[s], nameDevice);

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;

Expand Down
49 changes: 33 additions & 16 deletions lib/shutterDownSleep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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';
Expand All @@ -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
Expand All @@ -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';
Expand All @@ -131,22 +141,29 @@ 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;

await setShutterInfo(adapter, shutterSettings, shutterSettings[s], nameDevice);

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);
}
Expand Down
Loading

0 comments on commit aef7479

Please sign in to comment.