import type { SendOptions } from '../client' ; import Device , { isBulbSysinfo } from '../device' ; import type { CommonSysinfo , DeviceConstructorOptions } from '../device' ; import Cloud from '../shared/cloud' ; import Emeter , { RealtimeNormalized } from '../shared/emeter' ; import Lighting , { LightState } from './lighting' ; import Schedule from './schedule' ; import Time from '../shared/time' ; type BulbSysinfoLightState = { on_off : 0 | 1 ; } ; export type BulbSysinfo = CommonSysinfo & { mic_type : string ; mic_mac : string ; description : string ; light_state : BulbSysinfoLightState ; is_dimmable : 0 | 1 ; is_color : 0 | 1 ; is_variable_color_temp : 0 | 1 ; } ; export interface BulbConstructorOptions extends DeviceConstructorOptions { sysInfo : BulbSysinfo ; } export interface BulbEventEmitter { on ( event , listener ) : this ; deprecated on ( event , listener ) : this ; event property on ( event , listener ) : this ; event property on ( event , listener ) : this ; event property on ( event , listener ) : this ; event property on ( event , listener ) : this ; emit ( event , error ) : boolean ; emit ( event , value ) : boolean ; emit ( event , value ) : boolean ; emit ( event , value ) : boolean ; emit ( event , value ) : boolean ; emit ( event , value ) : boolean ; } fires fires fires fires fires export default class Bulb extends Device implements BulbEventEmitter { protected _sysInfo : BulbSysinfo ; internal lastState = { inUse : false , powerOn : false } ; readonly supportsEmeter = true ; static readonly apiModules = { system : 'smartlife.iot.common.system' , cloud : 'smartlife.iot.common.cloud' , schedule : 'smartlife.iot.common.schedule' , timesetting : 'smartlife.iot.common.timesetting' , emeter : 'smartlife.iot.common.emeter' , netif : 'netif' , lightingservice : 'smartlife.iot.smartbulb.lightingservice' , } ; borrows borrows borrows borrows borrows readonly cloud = new Cloud ( this , 'smartlife.iot.common.cloud' ) ; borrows borrows borrows borrows borrows readonly emeter = new Emeter ( this , 'smartlife.iot.common.emeter' ) ; borrows borrows borrows readonly lighting = new Lighting ( this , 'smartlife.iot.smartbulb.lightingservice' ) ; borrows borrows borrows borrows borrows borrows borrows borrows borrows borrows borrows readonly schedule = new Schedule ( this , 'smartlife.iot.common.schedule' ) ; borrows borrows readonly time = new Time ( this , 'smartlife.iot.common.timesetting' ) ; /**
     * Created by  Client - Do not instantiate directly.
     *
     * See [Device constructor] Device for common options.
     *  see Device param options constructor ( options ) { super ( { client : options . client , _sysInfo : options . sysInfo , host : options . host , port : options . port , logger : options . logger , defaultSendOptions : options . defaultSendOptions , } ) ; this . lastState = Object . assign ( this . lastState , { powerOn : null , inUse : null , } ) ; this . setSysInfo ( options . sysInfo ) ; this . _sysInfo = options . sysInfo ; } returns get sysInfo ( ) : BulbSysinfo { return this . _sysInfo ; } internal setSysInfo ( sysInfo ) { super . setSysInfo ( sysInfo ) ; this . lighting . lightState = sysInfo . light_state ; } protected setAliasProperty ( alias ) { this . sysInfo . alias = alias ; } /**
     * Cached value of `sysinfo.[description|dev_name]`.
     */ get description ( ) : string | undefined { return this . sysInfo . description ; } get deviceType ( ) : 'bulb' { return 'bulb' ; } returns get supportsBrightness ( ) : boolean { return this . sysInfo . is_dimmable === 1 ; } returns get supportsColor ( ) : boolean { return this . sysInfo . is_color === 1 ; } returns get supportsColorTemperature ( ) : boolean { return this . sysInfo . is_variable_color_temp === 1 ; } returns get colorTemperatureRange ( ) : { min : number ; max : number ; } | null { if ( ! this . supportsColorTemperature ) return null ; switch ( true ) { case /LB130/i . test ( this . sysInfo . model ) : return { min : 2500 , max : 9000 } ; default : return { min : 2700 , max : 6500 } ; } } returns deprecated Renamed, use  Bulb . colorTemperatureRange get getColorTemperatureRange ( ) : { min : number ; max : number ; } | null { return this . colorTemperatureRange ; } returns async getSysInfo ( sendOptions ? ) { const response = await super . getSysInfo ( sendOptions ) ; if ( ! isBulbSysinfo ( response ) ) { throw new Error ( `Unexpected Response: ${ response }` ) ; } return this . sysInfo ; } returns async getInfo ( sendOptions ? ) { const sendOptionsForGetInfo = sendOptions == null ? { } : sendOptions ; if ( ! ( 'transport' in sendOptionsForGetInfo ) ) sendOptionsForGetInfo . transport = 'tcp' ; const response = await this . send ( `{"${ this . apiModules . emeter }":{"get_realtime":{}},"${ this . apiModules . lightingservice }":{"get_light_state":{}},"${ this . apiModules . schedule }":{"get_next_action":{}},"system":{"get_sysinfo":{}},"${ this . apiModules . cloud }":{"get_info":{}}}` , sendOptionsForGetInfo ) ; const data = JSON . parse ( response ) ; this . setSysInfo ( data . system . get_sysinfo ) ; this . cloud . info = data [ this . apiModules . cloud ] . get_info ; this . emeter . setRealtime ( data [ this . apiModules . emeter ] . get_realtime ) ; this . schedule . nextAction = data [ this . apiModules . schedule ] . get_next_action ; this . lighting . lightState = data [ this . apiModules . lightingservice ] . get_light_state ; return { sysInfo : this . sysInfo , cloud : { info : this . cloud . info } , emeter : { realtime : this . emeter . realtime } , schedule : { nextAction : this . schedule . nextAction } , lighting : { lightState : this . lighting . lightState } , } ; } throws ResponseError async getPowerState ( sendOptions ? ) { const lightState = await this . lighting . getLightState ( sendOptions ) ; return lightState . on_off === 1 ; } param value throws ResponseError async setPowerState ( value , sendOptions ? ) { return this . lighting . setLightState ( { on_off : value ? 1 : 0 } , sendOptions ) ; } throws ResponseError async togglePowerState ( sendOptions ? ) { const powerState = await this . getPowerState ( sendOptions ) ; await this . setPowerState ( ! powerState , sendOptions ) ; return ! powerState ; } }