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 PlugOperation = Partial < Pick < Plug , "onOff" | "dimmer" > > ; export class Plug 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 ( "5805" ) public cumulativeActivePower : number = 0.0 ; @ ipsoKey ( "5851" ) @ serializeWith ( serializers . brightness ) @ deserializeWith ( deserializers . brightness ) public dimmer : number = 0 ; @ ipsoKey ( "5850" ) public onOff : boolean = false ; @ ipsoKey ( "5852" ) public onTime : number = 0 ; @ ipsoKey ( "5820" ) public powerFactor : number = 0.0 ; /**
     * Returns true if the current plug is dimmable
     */ public get isDimmable ( ) : boolean { return false ; } /**
     * Returns true if the current plug is switchable
     */ public get isSwitchable ( ) : boolean { return true ; } 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." ) ; } } /** Turn this plug on */ public turnOn ( ) { return this . operatePlug ( { onOff : true } ) ; } /** Turn this plug off */ public turnOff ( ) { return this . operatePlug ( { onOff : false } ) ; } /** Toggles this plug on or off */ public toggle ( value = ! this . onOff ) { return this . operatePlug ( { onOff : value } ) ; } private operatePlug ( operation ) { this . ensureLink ( ) ; return this . client ! . operatePlug ( this . _accessory ! , operation ) ; } returns public setBrightness ( value ) { value = clamp ( value , 0 , 100 ) ; return this . operatePlug ( { dimmer : value } ) ; } /** Turns this object into JSON while leaving out the potential circular reference */ public toJSON ( ) { return { onOff : this . onOff , isDimmable : this . isDimmable , isSwitchable : this . isSwitchable , } ; } }