import { clamp } from "alcalzone-shared/math" ; import { Accessory } from "./accessory" ; import { deserializers , serializers } from "./conversions" ; import { IPSODevice } from "./ipsoDevice" ; import { deserializeWith , doNotSerialize , ipsoKey , IPSOOptions , serializeWith } from "./ipsoObject" ; export type BlindOperation = Partial < Pick < Blind , "position" | "trigger" > > ; export class Blind extends IPSODevice { constructor ( options ? , accessory ? ) { super ( options ) ; if ( accessory != null && ! accessory . isProxy ) { accessory = accessory . createProxy ( ) ; } this . _accessory = accessory ; if ( accessory != null && accessory . deviceInfo != null && accessory . deviceInfo . modelNumber != null && accessory . deviceInfo . modelNumber . length > 0 ) { this . _modelName = accessory . deviceInfo . modelNumber ; } } @ doNotSerialize private _modelName : string | undefined ; @ doNotSerialize private _accessory : Accessory | undefined ; @ ipsoKey ( "5536" ) @ serializeWith ( serializers . position ) @ deserializeWith ( deserializers . position ) public position : number = 0.0 ; @ ipsoKey ( "5523" ) public trigger : number | undefined ; /**
     * Returns true if the current blind is dimmable
     */ public get isDimmable ( ) : boolean { return true ; } /**
     * Returns true if the current blind is switchable
     */ public get isSwitchable ( ) : boolean { return false ; } public clone ( ) { const ret = super . clone ( this . _accessory ) ; ret . _modelName = this . _modelName ; return ret ; } /**
     * Creates a proxy which redirects the properties to the correct internal one, does nothing now
     */ public createProxy ( ) { return this ; } throws private ensureLink ( ) { if ( this . client == null ) { throw new Error ( "Cannot use the simplified API on devices which aren't linked to a client instance." ) ; } if ( ! ( this . _accessory instanceof Accessory ) ) { throw new Error ( "Cannot use the simplified API on plugs which aren't linked to an Accessory instance." ) ; } } /** Open these blinds */ public open ( ) { return this . operateBlind ( { position : 100 } ) ; } /** Close these blinds */ public close ( ) { return this . operateBlind ( { position : 0 } ) ; } /** Stops moving blinds */ public stop ( ) { return this . operateBlind ( { trigger : 0.0 } , true ) ; } private operateBlind ( operation , force ? ) { this . ensureLink ( ) ; return this . client ! . operateBlind ( this . _accessory ! , operation , force ) ; } returns public setPosition ( value ) { value = clamp ( value , 0 , 100 ) ; return this . operateBlind ( { position : value } ) ; } /** Turns this object into JSON while leaving out the potential circular reference */ public toJSON ( ) { return { position : this . position , isSwitchable : this . isSwitchable , isDimmable : this . isDimmable , } ; } }