import { clamp , convertHSLtoRGB , convertRGBtoHSL } from '../magichome-interface/utils' ; import { HomebridgeMagichomeDynamicPlatformAccessory } from '../platformAccessory' ; export class RGBWStrip extends HomebridgeMagichomeDynamicPlatformAccessory { public eightByteProtocol = 2 ; async updateDeviceState ( ) { const hsl = this . lightState . HSL ; let [ red , green , blue ] = convertHSLtoRGB ( hsl ) ; const brightness = this . lightState . brightness ; const mask = 0xFF ; let r = Math . round ( ( ( clamp ( red , 0 , 255 ) / 100 ) * brightness ) ) ; let g = Math . round ( ( ( clamp ( green , 0 , 255 ) / 100 ) * brightness ) ) ; let b = Math . round ( ( ( clamp ( blue , 0 , 255 ) / 100 ) * brightness ) ) ; let ww = 0 ; if ( ( hsl . hue == 31 && hsl . saturation == 33 ) || ( hsl . hue == 208 && hsl . saturation == 17 ) || ( hsl . hue == 0 && hsl . saturation == 0 ) || ( hsl . saturation < this . colorOffThresholdSimultaniousDevices ) ) { r = 0 ; g = 0 ; b = 0 ; ww = Math . round ( ( 255 / 100 ) * brightness ) ; } else if ( hsl . saturation < this . colorWhiteThresholdSimultaniousDevices && this . simultaniousDevicesColorWhite ) { [ red , green , blue ] = convertHSLtoRGB ( { hue : hsl . hue , saturation : 100 , luminance : hsl . luminance } ) ; r = red ; g = green ; b = blue ; ww = Math . round ( ( 255 / 100 ) * brightness ) ; } else { ww = 0 ; } if ( this . eightByteProtocol == 0 ) { await this . send ( [ 0x31 , r , g , b , ww , mask , 0x0F ] ) ; } else if ( this . eightByteProtocol == 1 ) { await this . send ( [ 0x31 , r , g , b , ww , 0x00 , mask , 0x0F ] ) ; } else if ( this . eightByteProtocol == 2 ) { this . eightByteProtocol = ( await this . send ( [ 0x31 , r , g , b , ww , 0x00 , mask , 0x0F ] ) ) == undefined ? 0 : 1 ; await this . send ( [ 0x31 , r , g , b , ww , mask , 0x0F ] ) ; } } async updateHomekitState ( ) { this . service . updateCharacteristic ( this . platform . Characteristic . On , this . lightState . isOn ) ; this . service . updateCharacteristic ( this . platform . Characteristic . Hue , this . lightState . HSL . hue ) ; this . service . updateCharacteristic ( this . platform . Characteristic . Saturation , this . lightState . HSL . saturation ) ; if ( this . lightState . HSL . luminance > 0 && this . lightState . isOn ) { this . service . updateCharacteristic ( this . platform . Characteristic . Brightness , this . lightState . HSL . luminance * 2 ) ; } else if ( this . lightState . isOn ) { this . service . updateCharacteristic ( this . platform . Characteristic . Brightness , clamp ( ( this . lightState . whiteValues . warmWhite / 2.55 ) , 0 , 100 ) ) ; } } }