homebridge-flux/src/fluxAccessory.ts

123 lines
3.8 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";
2020-04-09 01:36:02 +00:00
import { Scheduler } from "./scheduler";
2020-04-09 01:02:08 +00:00
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;
2020-04-09 01:36:02 +00:00
private _isEnabled: boolean;
2020-04-09 01:02:08 +00:00
private _hue: Api;
private _lights: Array<Light> = [];
2020-04-09 01:36:02 +00:00
private _scheduler: Scheduler;
2020-04-09 01:02:08 +00:00
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;
2020-04-09 01:36:02 +00:00
this._scheduler = new Scheduler(60000, 60000, this._log);
2020-04-09 01:02:08 +00:00
2020-04-09 01:36:02 +00:00
this._isEnabled = false;
2020-04-09 01:02:08 +00:00
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)
2020-04-09 01:36:02 +00:00
//@ts-ignore
.on("set", this.onSetEnabled)
.on("get", this.onGetEnabled);
2020-04-09 01:02:08 +00:00
}
public name: string = "Flux";
public platformAccessory: any;
2020-04-09 01:36:02 +00:00
/**
* Handler for switch set event
* @param callback The callback function to call when complete
*/
private onSetEnabled = async (activeState: boolean, callback: (error?: Error | null | undefined) => void) => {
return callback();
}
/**
* Handler for switch get event
* @param callback The callback function to call when complete
*/
private onGetEnabled = (callback: (error: Error | null, value: boolean) => void) => {
return callback(null, this._isEnabled);
}
2020-04-09 01:02:08 +00:00
2020-04-09 01:36:02 +00:00
/**
* Called by homebridge to gather services.
*/
public getServices = (): Array<HAPNodeJS.Service> => {
return [this._infoService, this._switchService!];
2020-04-09 01:02:08 +00:00
}
2020-04-09 01:36:02 +00:00
2020-04-09 01:02:08 +00:00
/**
* 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);
});
}
}