diff options
author | lonkaars <loek@pipeframe.xyz> | 2021-08-05 16:58:28 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2021-08-05 16:58:28 +0200 |
commit | 34144332104dbbb2c1243ccfac79a8f81516e53d (patch) | |
tree | 090344602ff2f0136c9deeb9b0bf76a6c749ca45 | |
parent | 333fc4c069e7101ea7b192e2d50fc5858c5adee4 (diff) |
working homebridge plugin
-rw-r--r-- | index.ts | 2 | ||||
-rw-r--r-- | lamp.ts | 18 | ||||
-rw-r--r-- | plugin.ts | 123 |
3 files changed, 120 insertions, 23 deletions
@@ -4,4 +4,4 @@ import BekenBridge from './plugin'; module.exports = (api: API) => { api.registerAccessory('BekenBridge', BekenBridge); -} +}; @@ -1,4 +1,6 @@ import { ChildProcess, spawn } from 'child_process'; +import { Logger } from 'homebridge'; +import { join } from 'path'; export type LampColor = [number, number, number, number]; @@ -6,8 +8,12 @@ export default class Lamp { #color: LampColor; private subpr: ChildProcess; - constructor(public addr: string) { - this.subpr = spawn('./venv/bin/python3', ['./main.py', addr]); + constructor(public addr: string, public log: Logger) { + this.subpr = spawn(join(__dirname, '/venv/bin/python3'), [join(__dirname, './main.py'), addr]); + + // debug + this.subpr.stderr.on('data', this.log.error); + this.subpr.stdout.on('data', this.log.log); } set color(newColor: LampColor) { @@ -24,3 +30,11 @@ export default class Lamp { } } +// ! DEBUG +// if (typeof require !== 'undefined' && require.main === module) { +// var lamp = new Lamp("FC:58:FA:A1:CF:F1"); +// +// setInterval(() => { +// lamp.color = [0, 0, 0, Math.floor(Math.random() * 255)]; +// }, 100); +// } @@ -1,19 +1,27 @@ -import { - API, - AccessoryPlugin, - Logger, - AccessoryConfig, - Service - -} from 'homebridge'; +import { AccessoryConfig, AccessoryPlugin, API, Logger, Service } from 'homebridge'; import Lamp, { LampColor } from './lamp'; +const Color = require('color'); export default class BekenBridge implements AccessoryPlugin { private lamp: Lamp; - private bulbService: Service; private infoService: Service; + private whiteBulbService: Service; + private RGBBulbService: Service; + + private whiteState: { + brt: number; + on: boolean; + }; + + private rgbState: { + hue: number; + sat: number; + brt: number; + on: boolean; + }; + public name: string; constructor( @@ -21,22 +29,97 @@ export default class BekenBridge implements AccessoryPlugin { public readonly config: AccessoryConfig, public readonly api: API, ) { - console.log("reached constructor"); //DEBUG - // this.name = config.name; - // this.lamp = new Lamp(config.address); + this.name = config.name; + this.lamp = new Lamp(config.address, log); + + this.whiteState = { + on: false, + brt: 0, + }; + + this.rgbState = { + on: false, + brt: 0, + sat: 0, + hue: 0, + }; + + this.infoService = new this.api.hap.Service.AccessoryInformation() + .setCharacteristic(this.api.hap.Characteristic.Manufacturer, 'Beken') + .setCharacteristic(this.api.hap.Characteristic.Model, 'Beken LED'); - // this.infoService = new this.api.hap.Service.AccessoryInformation() - // .setCharacteristic(this.api.hap.Characteristic.Manufacturer, "Beken") - // .setCharacteristic(this.api.hap.Characteristic.Model, "Beken LED"); + this.whiteBulbService = new this.api.hap.Service.Lightbulb('White', 'normal'); + this.RGBBulbService = new this.api.hap.Service.Lightbulb('RGB', 'rgb'); - // this.bulbService = new this.api.hap.Service.Lightbulb(this.name); + this.registerWhiteBulbServices(); + this.registerRGBBulbServices(); } - + + registerWhiteBulbServices() { + 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); + } + this.updateLamp(); + }); + this.whiteBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness) + .onGet(() => this.whiteState.brt) + .onSet((brt: number) => { + this.whiteState.brt = brt; + this.updateLamp(); + }); + } + + registerRGBBulbServices() { + 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); + } + this.updateLamp(); + }); + this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Brightness) + .onGet(() => this.rgbState.brt) + .onSet((brt: number) => { + this.rgbState.brt = brt; + this.updateLamp(); + }); + this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Hue) + .onGet(() => this.rgbState.hue) + .onSet((hue: number) => { + this.rgbState.hue = hue; + this.updateLamp(); + }); + this.RGBBulbService.getCharacteristic(this.api.hap.Characteristic.Saturation) + .onGet(() => this.rgbState.sat) + .onSet((sat: number) => { + this.rgbState.sat = sat; + this.updateLamp(); + }); + } + + updateLamp() { + if (this.rgbState.on) { + var rgb = Color({ h: this.rgbState.hue, s: this.rgbState.sat, v: this.rgbState.brt }); + this.lamp.color = [rgb.red(), rgb.green(), rgb.blue(), 0]; + } else if (this.whiteState.on) { + var value = Math.floor(this.whiteState.brt / 100 * 255); + this.lamp.color = [0, 0, 0, value]; + } else { + this.lamp.color = [0, 0, 0, 0]; + } + } + getServices() { - console.log("getting services"); //DEBUG return [ - // this.infoService, - // this.bulbService + this.infoService, + this.whiteBulbService, + this.RGBBulbService, ]; } } |