homebridge-flux/src/fluxAccessory.ts

100 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-04-09 01:02:08 +00:00
import { IAccessory } from "./models/iAccessory";
import Api = require("node-hue-api/lib/api/Api");
import Light = require("node-hue-api/lib/model/Light");
import LightState = require("node-hue-api/lib/model/lightstate/LightState");
import { Sleep } from "./sleep";
let Service: HAPNodeJS.Service;
let Characteristic: HAPNodeJS.Characteristic;
export interface IFluxProps {
api: any;
log: any;
homebridge: any;
hue: Api
}
export class FluxAccessory implements IAccessory {
private _api: any;
private _homebridge: any;
private _log: any = {};
//Service fields
private _switchService: HAPNodeJS.Service;
private _infoService: HAPNodeJS.Service;
private _isActive: boolean;
private _hue: Api;
private _lights: Array<Light> = [];
private _brightness: number = 0;
constructor(props: IFluxProps) {
//Assign class variables
this._log = props.log;
this._api = props.api;
Service = props.api.hap.Service;
Characteristic = props.api.hap.Characteristic;
this._homebridge = props.homebridge;
this._isActive = false;
this._hue = props.hue;
this.platformAccessory = new this._homebridge.platformAccessory(this.name, this.generateUUID(), this._homebridge.hap.Accessory.Categories.SWITCH);
//@ts-ignore
this._infoService = new Service.AccessoryInformation();
this._infoService.setCharacteristic(Characteristic.Manufacturer, "Brandon Watson")
this._infoService.setCharacteristic(Characteristic.Model, "F.lux")
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789");
this._switchService = new Service.Switch(
this.name,
'fluxService'
)
this._switchService.getCharacteristic(Characteristic.On)
//@ts-ignore
// .on("set", this.onPowerSet)
// .on("get", this.onPowerGet);
}
public name: string = "Flux";
public platformAccessory: any;
public getServices(): HAPNodeJS.Service[] {
throw new Error("Method not implemented.");
}
/**
* Helper function to convert a hex string to rgb
* @param hex hex string starting with "#"
*/
private hexToRgb = (hex: string): { red: number, green: number, blue: number } | null => {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
red: parseInt(result[1], 16),
green: parseInt(result[2], 16),
blue: parseInt(result[3], 16)
} : null;
}
/**
* Helper function to generate a UUID
*/
private generateUUID(): string { // Public Domain/MIT
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
}