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 6, 2024
1 parent caa09be commit cbe07d8
Show file tree
Hide file tree
Showing 2 changed files with 355 additions and 160 deletions.
56 changes: 38 additions & 18 deletions lib/elevationDown.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const sunProtect = require('./sunProtect.js'); // sunProtect
const checkPendingAlarm = require('./shutterAlarm.js').checkPendingAlarm; // shutterAlarm
const checkFrostAlarm = require('./shutterAlarm.js').checkFrostAlarm; // shutterAlarm - check frost alarm
const setShutterState = require('./setShutter.js').setShutterState; // set Shutter State
const setShutterInfo = require('./setShutter.js').setShutterInfo; // set Shutter State
const sunProtect = require('./sunProtect.js'); // sunProtect
const checkPendingAlarm = require('./shutterAlarm.js').checkPendingAlarm; // shutterAlarm
const checkFrostAlarm = require('./shutterAlarm.js').checkFrostAlarm; // shutterAlarm - check frost alarm
const setShutterState = require('./setShutter.js').setShutterState; // set Shutter State
const setShutterInfo = require('./setShutter.js').setShutterInfo; // set Shutter State
const CheckInSummerNotDown = require('./isSummerTime.js').CheckInSummerNotDown; // Check is summer
const GetXmasLevel = require('./isSummerTime.js').GetXmasLevel; // Check is XMas

Expand Down Expand Up @@ -70,26 +70,35 @@ async function elevationDown(adapter, elevation, azimuth, shutterSettings) {
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) {
const elevationEnd = (shutterSettings[s].elevation - 1);

if (elevation <= shutterSettings[s].elevation && elevation >= elevationEnd && shutterSettings[s].currentAction != 'down' && azimuth > 180) {
if (elevation <= shutterSettings[s].elevation &&
elevation >= elevationEnd &&
shutterSettings[s].currentAction != 'down' &&
azimuth > 180) {

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);

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 != '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 = downAction;
shutterSettings[s].lastAutoAction = 'Elevation_down';
Expand All @@ -101,7 +110,9 @@ async function elevationDown(adapter, elevation, azimuth, shutterSettings) {
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;

Expand All @@ -112,7 +123,9 @@ async function elevationDown(adapter, elevation, azimuth, 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 = downAction;
shutterSettings[s].lastAutoAction = 'Elevation_down';
Expand All @@ -122,7 +135,9 @@ async function elevationDown(adapter, elevation, azimuth, shutterSettings) {
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;

Expand All @@ -131,10 +146,15 @@ async function elevationDown(adapter, elevation, azimuth, shutterSettings) {
await setShutterInfo(adapter, shutterSettings, shutterSettings[s], nameDevice);
}

} 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
Loading

0 comments on commit cbe07d8

Please sign in to comment.