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 54e39cb commit df40d08
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions lib/shutterDownComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ async function shutterDownComplete(adapter, delayDown, shutterSettings) {

for (const i in result) {
for (const s in shutterSettings) {
if (shutterSettings[s].shutterName == result[i].shutterName && shutterSettings[s].currentAction == 'middle') {
if (shutterSettings[s].shutterName == result[i].shutterName &&
shutterSettings[s].currentAction == 'middle') {

const inSummerNotDown = await CheckInSummerNotDown(adapter, shutterSettings[s]);
const XmasLevel = await GetXmasLevel(adapter, shutterSettings[s]);
let targetLevel2Set = 0;
Expand All @@ -54,7 +56,10 @@ async function shutterDownComplete(adapter, delayDown, shutterSettings) {
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 @@ -64,21 +69,25 @@ async function shutterDownComplete(adapter, delayDown, shutterSettings) {

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) {
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].currentAction = 'down';
shutterSettings[s].lastAutoAction = 'Down_Complete';
Expand All @@ -89,7 +98,9 @@ async function shutterDownComplete(adapter, delayDown, shutterSettings) {

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 = 'down';

Expand All @@ -100,7 +111,9 @@ async function shutterDownComplete(adapter, delayDown, shutterSettings) {
} 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 = 'down';
shutterSettings[s].lastAutoAction = 'Down_Complete';
Expand All @@ -109,18 +122,25 @@ async function shutterDownComplete(adapter, delayDown, shutterSettings) {

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 = 'down';

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

adapter.log.debug('Complete 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 = 'down';

Expand Down

0 comments on commit df40d08

Please sign in to comment.