From 6eceb7884335d3bd601ef2cd7a86db0db407f2df Mon Sep 17 00:00:00 2001 From: Serge Wagener Date: Wed, 9 Feb 2022 20:02:31 +0100 Subject: [PATCH] Added WindowCovering --- src/Accessories/WindowCovering.ts | 63 ++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/src/Accessories/WindowCovering.ts b/src/Accessories/WindowCovering.ts index 9a28d8a..c525787 100644 --- a/src/Accessories/WindowCovering.ts +++ b/src/Accessories/WindowCovering.ts @@ -4,6 +4,8 @@ import { Service, Nullable, } from 'homebridge'; +import { setMaxListeners } from 'process'; +import { setFlagsFromString } from 'v8'; import { SmartHomeNGPlatform } from '../platform'; @@ -12,11 +14,8 @@ export class WindowCovering implements AccessoryPlugin { private readonly informationService: Service; public name: string; - private currentPosition = 0; - private currentPositionMin = 0; - private currentPositionMax = 100; - private currentPositionInverted = false; - private targetPosition = 0; + private currentPosition = 0; private currentPositionMin = 0; private currentPositionMax = 100; private currentPositionInverted = false; + private targetPosition = 0; private targetPositionMin = 0; private targetPositionMax = 100; private targetPositionInverted = false; private positionState = this.platform.Characteristic.PositionState.STOPPED; constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) { @@ -40,11 +39,15 @@ export class WindowCovering implements AccessoryPlugin { .setCharacteristic(this.platform.Characteristic.Model, accessory.model) .setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.currentposition); - this.platform.shng.addMonitor(accessory.currentposition, this.shngCallback.bind(this)); + this.platform.shng.addMonitor(accessory.currentposition, this.shngCurrentPositionCallback.bind(this)); + this.platform.shng.addMonitor(accessory.targetposition, this.shngTargetPositionCallback.bind(this)); this.currentPositionMax = accessory.currentpositionmax ? accessory.currentpositionmax : this.currentPositionMax; this.currentPositionMin = accessory.currentpositionmin ? accessory.currentpositionmin : this.currentPositionMin; this.currentPositionInverted = accessory.currentpositioninverted ? accessory.currentpositioninverted : this.currentPositionInverted; + this.targetPositionMax = accessory.targetpositionmax ? accessory.targetpositionmax : this.targetPositionMax; + this.targetPositionMin = accessory.targetpositionmin ? accessory.targetpositionmin : this.targetPositionMin; + this.targetPositionInverted = accessory.targetpositioninverted ? accessory.targetpositioninverted : this.targetPositionInverted; this.platform.log.info("WindowCovering '%s' created!", accessory.name); } @@ -67,7 +70,6 @@ export class WindowCovering implements AccessoryPlugin { } getTargetPosition(): Nullable { - this.targetPosition = this.currentPosition; this.platform.log.info('getTargetPosition:', this.accessory.name, 'is currently', this.targetPosition); return this.targetPosition; } @@ -75,11 +77,17 @@ export class WindowCovering implements AccessoryPlugin { setTargetPosition(value: CharacteristicValue) { this.targetPosition = value as number; this.platform.log.info('SetOn:', this.accessory.name, 'was set to', this.targetPosition); - //this.platform.shng.setItem(this.accessory.targetPosition, this.targetPosition); + const transposedTarget = this.convertRange( + value as number, + 0, 100, + this.targetPositionMin, this.targetPositionMax, + this.targetPositionInverted, + ); + this.platform.shng.setItem(this.accessory.targetposition, transposedTarget); } - shngCallback(value: unknown): void { - this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); + shngCurrentPositionCallback(value: unknown): void { + this.platform.log.error('shngCurrentPositionCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); if (typeof value === 'number') { this.currentPosition = this.convertRange( value as number, @@ -91,6 +99,34 @@ export class WindowCovering implements AccessoryPlugin { } else { this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); } + this.updateDirection(); + } + + + shngTargetPositionCallback(value: unknown): void { + this.platform.log.error('shngTargetPositionCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); + if (typeof value === 'number') { + this.targetPosition = this.convertRange( + value as number, + this.targetPositionMin, this.targetPositionMax, + 0, 100, + this.targetPositionInverted, + ); + this.deviceService.updateCharacteristic(this.platform.Characteristic.TargetPosition, this.targetPosition); + } else { + this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); + } + this.updateDirection(); + } + + updateDirection() { + if (this.targetPosition < this.currentPosition) { + this.positionState = this.platform.Characteristic.PositionState.DECREASING; + } else if (this.targetPosition > this.currentPosition) { + this.positionState = this.platform.Characteristic.PositionState.INCREASING; + } else { + this.positionState = this.platform.Characteristic.PositionState.STOPPED; + } } convertRange(value: number, oldmin: number, oldmax: number, newmin: number, newmax: number, inverted: boolean): number { @@ -98,6 +134,13 @@ export class WindowCovering implements AccessoryPlugin { if (inverted) { result = newmax - result; } + this.platform.log.warn( + 'Transposing', value, + 'from range', oldmin, '-', oldmax, + 'to', newmin, '-', newmax, + 'with inverted', inverted, + '=', result, + ); return result; } }