homebridge-flux/src/fluxAccessory.ts

251 lines
8.7 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-11 19:49:07 +00:00
import { IConfig } from "./models/iConfig";
//@ts-ignore
import { GetTimesResult, getTimes } from "suncalc";
import HueError = require("node-hue-api/lib/HueError");
2020-04-09 01:02:08 +00:00
let Service: HAPNodeJS.Service;
let Characteristic: HAPNodeJS.Characteristic;
2020-04-16 01:20:17 +00:00
const SECONDS_IN_DAY = 86400000;
const SECONDS_IN_HOUR = 3600;
2020-04-09 01:02:08 +00:00
export interface IFluxProps {
api: any;
log: any;
homebridge: any;
2020-04-11 19:49:07 +00:00
hue: Api,
config: IConfig;
2020-04-09 01:02:08 +00:00
}
export class FluxAccessory implements IAccessory {
private _api: any;
private _homebridge: any;
private _log: any = {};
2020-04-11 19:49:07 +00:00
private _config: IConfig;
2020-04-14 00:50:07 +00:00
private _isActive: boolean;
2020-04-09 01:02:08 +00:00
//Service fields
private _switchService: HAPNodeJS.Service;
private _infoService: HAPNodeJS.Service;
private _hue: Api;
private _lights: Array<Light> = [];
2020-04-11 19:49:07 +00:00
private _times: GetTimesResult;
2020-04-09 01:02:08 +00:00
constructor(props: IFluxProps) {
//Assign class variables
this._log = props.log;
this._api = props.api;
2020-04-11 19:49:07 +00:00
this._config = props.config;
2020-04-09 01:02:08 +00:00
Service = props.api.hap.Service;
Characteristic = props.api.hap.Characteristic;
this._homebridge = props.homebridge;
2020-04-14 00:50:07 +00:00
this._isActive = false;
2020-04-09 01:02:08 +00:00
this._hue = props.hue;
2020-04-11 19:49:07 +00:00
this.name = this._config.name;
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
this.getLights();
2020-04-09 01:02:08 +00:00
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) => {
2020-04-11 19:49:07 +00:00
if (activeState) {
2020-04-11 20:44:55 +00:00
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
2020-04-14 00:50:07 +00:00
this.update();
2020-04-11 19:49:07 +00:00
} else {
2020-04-14 00:50:07 +00:00
this._isActive = false;
2020-04-11 19:49:07 +00:00
}
2020-04-09 01:36:02 +00:00
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) => {
2020-04-14 00:50:07 +00:00
return callback(null, this._isActive);
2020-04-09 01:36:02 +00:00
}
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-11 19:49:07 +00:00
* Popuplates internal lights array using the configuration values
2020-04-09 01:02:08 +00:00
*/
2020-04-14 00:50:07 +00:00
private getLights = async (): Promise<void> => {
for (const value of this._config.lights) {
2020-04-11 19:49:07 +00:00
//@ts-ignore
const light: Light = await this._hue.lights.getLightByName(value)
this._lights.push(light);
2020-04-14 00:50:07 +00:00
}
2020-04-11 19:49:07 +00:00
}
private colorTempToRgb = (kelvin: number): { red: number, green: number, blue: number } => {
var temp = kelvin / 100;
var red, green, blue;
if (temp <= 66) {
red = 255;
green = temp;
green = 99.4708025861 * Math.log(green) - 161.1195681661;
if (temp <= 19) {
blue = 0;
} else {
blue = temp - 10;
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
}
} else {
red = temp - 60;
red = 329.698727446 * Math.pow(red, -0.1332047592);
green = temp - 60;
green = 288.1221695283 * Math.pow(green, -0.0755148492);
blue = 255;
}
return {
red: this.clamp(red, 0, 255),
green: this.clamp(green, 0, 255),
blue: this.clamp(blue, 0, 255)
}
}
private clamp(x: number, min: number, max: number) {
if (x < min) { return min; }
if (x > max) { return max; }
return x;
}
2020-04-14 01:16:56 +00:00
private isHueError = (object: any): object is HueError => {
return '_hueError' in object;
}
2020-04-11 19:49:07 +00:00
private setLights = async (state: LightState) => {
2020-04-14 00:54:35 +00:00
const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
this._lights.map(async (light: Light) => {
try {
2020-04-14 00:50:07 +00:00
await this._hue.lights.setLightState(light.id, state);
} catch (err) {
if (this.isHueError(err) && err.message === "parameter, xy, is not modifiable. Device is set to off.") {
2020-04-14 01:16:56 +00:00
//Eat this
} else {
this._log(`Error while setting lights: ${err}`);
}
2020-04-11 19:49:07 +00:00
}
2020-04-14 00:54:35 +00:00
});
2020-04-14 00:54:35 +00:00
await Promise.all(promises);
2020-04-09 01:02:08 +00:00
}
/**
* 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);
});
}
2020-04-14 00:50:07 +00:00
private update = async (): Promise<void> => {
this._isActive = true;
while (this._isActive) {
if (this._lights.length === 0) {
await this.getLights();
}
2020-04-11 19:49:07 +00:00
2020-04-14 00:50:07 +00:00
const now = new Date(Date.now());
2020-04-16 01:20:17 +00:00
//Pad start time by an hour before sunset
const start = new Date(this._times.sunset.getTime() - 3 * 1000 * SECONDS_IN_HOUR);
const end = new Date(this._times.sunrise.getTime() + 1 * SECONDS_IN_DAY);
// const sunset = new Date('Wed, 15 Apr 2020 21:00:00 GMT');
// const sunrise = new Date('Wed, 15 Apr 2020 23:00:00 GMT');
2020-04-11 19:49:07 +00:00
2020-04-14 00:50:07 +00:00
const startColorTemp = this._config.startColorTemp ? this._config.startColorTemp : 4000;
const stopColorTemp = this._config.stopColorTemp ? this._config.stopColorTemp : 1900;
2020-04-11 19:49:07 +00:00
2020-04-14 00:50:07 +00:00
let percentageComplete = 0;
let newTemp = 0;
2020-04-11 19:49:07 +00:00
2020-04-16 01:20:17 +00:00
const midTime = (end.getTime() - start.getTime()) / 2;
const mid = new Date(new Date().setTime(start.getTime() + midTime));
const tempRange = Math.abs(startColorTemp - stopColorTemp);
2020-04-11 19:49:07 +00:00
2020-04-16 01:20:17 +00:00
if ((start < now) && (now < mid)) {
2020-04-14 00:50:07 +00:00
//Before sunset; calculate temp based on TOD
2020-04-16 01:20:17 +00:00
const totalDecreaseTime = (mid.getTime() - start.getTime()) / 1000;
const secondsFromStart = (now.getTime() - start.getTime()) / 1000;
percentageComplete = secondsFromStart / totalDecreaseTime;
2020-04-14 00:50:07 +00:00
const tempOffset = tempRange * percentageComplete;
2020-04-11 19:49:07 +00:00
newTemp = startColorTemp - tempOffset;
2020-04-16 01:20:17 +00:00
} else if ((mid < now) && (now < end)) {
2020-04-14 00:50:07 +00:00
//After sunset; calculate temp based on TOD
2020-04-16 01:20:17 +00:00
const totalIncreaseTime = (end.getTime() - mid.getTime()) / 1000;
const secondsUntilSunrise = (end.getTime() - now.getTime()) / 1000;
percentageComplete = secondsUntilSunrise / totalIncreaseTime;
2020-04-11 19:49:07 +00:00
2020-04-14 00:50:07 +00:00
const tempOffset = tempRange * percentageComplete;
newTemp = startColorTemp - tempOffset;
2020-04-14 00:50:07 +00:00
}
2020-04-11 19:49:07 +00:00
2020-04-14 00:50:07 +00:00
//Set lights
const rgb = this.colorTempToRgb(newTemp);
if (rgb && newTemp !== 0) {
2020-04-14 00:50:07 +00:00
const lightState = new LightState();
lightState
.transitionInMillis(this._config.transition ? this._config.transition : 5000)
.rgb(rgb.red ? rgb.red : 0, rgb.green ? rgb.green : 0, rgb.blue ? rgb.blue : 0);
await this.setLights(lightState)
this._log(`Adjusting light temp to ${newTemp}, ${JSON.stringify(rgb)}`)
2020-04-11 19:49:07 +00:00
}
2020-04-14 00:50:07 +00:00
await Sleep(this._config.delay ? this._config.delay : 60000);
2020-04-11 19:49:07 +00:00
}
}
2020-04-09 01:02:08 +00:00
}