diff options
-rw-r--r-- | lamp.ts | 7 | ||||
-rw-r--r-- | plugin.ts | 30 |
2 files changed, 26 insertions, 11 deletions
@@ -7,6 +7,7 @@ export type LampColor = [number, number, number, number]; export default class Lamp { #color: LampColor; private subpr: ChildProcess; + private last: string; constructor(public addr: string, public log: Logger) { this.subpr = spawn(join(__dirname, '/venv/bin/python3'), [join(__dirname, './main.py'), addr]); @@ -18,7 +19,11 @@ export default class Lamp { set color(newColor: LampColor) { this.#color = newColor.map(c => Math.floor(c)) as LampColor; - this.subpr.stdin.write(this.colorToString() + '\n'); + var message = this.colorToString(); + if (this.last == message) return; // prevent duplicate messages + this.log.info(message); + this.subpr.stdin.write(message + '\n'); + this.last = message; } get color() { @@ -34,12 +34,12 @@ export default class BekenBridge implements AccessoryPlugin { this.whiteState = { on: false, - brt: 0, + brt: 100, }; this.rgbState = { on: false, - brt: 0, + brt: 100, sat: 0, hue: 0, }; @@ -56,48 +56,58 @@ export default class BekenBridge implements AccessoryPlugin { } registerWhiteBulbServices() { + var setOn = (on: boolean) => { + this.whiteState.on = on; + if (this.rgbState.on) { + this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false); + } + }; this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On) .onGet(() => this.whiteState.on) .onSet((on: boolean) => { - this.whiteState.on = on; - if (this.rgbState.on) { - this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false); - } + setOn(on); this.updateLamp(); }); this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness) .onGet(() => this.whiteState.brt) .onSet((brt: number) => { + setOn(true); this.whiteState.brt = brt; this.updateLamp(); }); } registerRGBBulbServices() { + var setOn = (on: boolean) => { + this.rgbState.on = on; + if (this.whiteState.on) { + this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false); + } + }; this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.On) .onGet(() => this.rgbState.on) .onSet((on: boolean) => { - this.rgbState.on = on; - if (this.whiteState.on) { - this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.On).setValue(false); - } + setOn(on); this.updateLamp(); }); this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness) .onGet(() => this.rgbState.brt) .onSet((brt: number) => { + setOn(true); this.rgbState.brt = brt; this.updateLamp(); }); this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Hue) .onGet(() => this.rgbState.hue) .onSet((hue: number) => { + setOn(true); this.rgbState.hue = hue; this.updateLamp(); }); this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Saturation) .onGet(() => this.rgbState.sat) .onSet((sat: number) => { + setOn(true); this.rgbState.sat = sat; this.updateLamp(); }); |