Working with all lights
This commit is contained in:
parent
eacd56afa5
commit
a43ecd2700
@ -26,6 +26,7 @@ export class FluxAccessory implements IAccessory {
|
||||
private _homebridge: any;
|
||||
private _log: any = {};
|
||||
private _config: IConfig;
|
||||
private _isActive: boolean;
|
||||
|
||||
//Service fields
|
||||
private _switchService: HAPNodeJS.Service;
|
||||
@ -35,8 +36,6 @@ export class FluxAccessory implements IAccessory {
|
||||
|
||||
private _lights: Array<Light> = [];
|
||||
|
||||
private _scheduler: Scheduler;
|
||||
|
||||
private _times: GetTimesResult;
|
||||
|
||||
constructor(props: IFluxProps) {
|
||||
@ -47,10 +46,7 @@ export class FluxAccessory implements IAccessory {
|
||||
Service = props.api.hap.Service;
|
||||
Characteristic = props.api.hap.Characteristic;
|
||||
this._homebridge = props.homebridge;
|
||||
this._scheduler = new Scheduler(
|
||||
this._config.delay ? this._config.delay : 60000,
|
||||
this._config.watchdog ? this._config.watchdog : 120000, this._log);
|
||||
this._scheduler.addTask({ delegate: this.updateDelegate, title: "Update" })
|
||||
this._isActive = false;
|
||||
|
||||
this._hue = props.hue;
|
||||
this.name = this._config.name;
|
||||
@ -89,9 +85,9 @@ export class FluxAccessory implements IAccessory {
|
||||
private onSetEnabled = async (activeState: boolean, callback: (error?: Error | null | undefined) => void) => {
|
||||
if (activeState) {
|
||||
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
|
||||
this._scheduler.start();
|
||||
this.update();
|
||||
} else {
|
||||
this._scheduler.stop();
|
||||
this._isActive = false;
|
||||
}
|
||||
return callback();
|
||||
}
|
||||
@ -101,7 +97,7 @@ export class FluxAccessory implements IAccessory {
|
||||
* @param callback The callback function to call when complete
|
||||
*/
|
||||
private onGetEnabled = (callback: (error: Error | null, value: boolean) => void) => {
|
||||
return callback(null, this._scheduler.IsStarted);
|
||||
return callback(null, this._isActive);
|
||||
}
|
||||
|
||||
|
||||
@ -115,14 +111,13 @@ export class FluxAccessory implements IAccessory {
|
||||
/**
|
||||
* Popuplates internal lights array using the configuration values
|
||||
*/
|
||||
private getLights = async () => {
|
||||
//Get lights
|
||||
const lightPromises: Array<Promise<void>> = this._config.lights.map(async (value: string) => {
|
||||
private getLights = async (): Promise<void> => {
|
||||
for (const value of this._config.lights) {
|
||||
//@ts-ignore
|
||||
const light: Light = await this._hue.lights.getLightByName(value)
|
||||
this._lights.push(light);
|
||||
});
|
||||
await Promise.all(lightPromises);
|
||||
this._log(`Got light ${light.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
private colorTempToRgb = (kelvin: number): { red: number, green: number, blue: number } => {
|
||||
@ -166,19 +161,17 @@ export class FluxAccessory implements IAccessory {
|
||||
}
|
||||
|
||||
private setLights = async (state: LightState) => {
|
||||
const promises = this._lights.map(async (value: Light) => {
|
||||
// const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
|
||||
for (const light of this._lights) {
|
||||
try {
|
||||
await this._hue.lights.setLightState(value.id, state);
|
||||
this._log(`Setting light ${light.name}`);
|
||||
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.") {
|
||||
//Eat this
|
||||
} else {
|
||||
this._log(`Error while setting lights: ${err}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
// await Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -196,7 +189,9 @@ export class FluxAccessory implements IAccessory {
|
||||
});
|
||||
}
|
||||
|
||||
private updateDelegate: Delegate = async (): Promise<void> => {
|
||||
private update = async (): Promise<void> => {
|
||||
this._isActive = true;
|
||||
while (this._isActive) {
|
||||
if (this._lights.length === 0) {
|
||||
await this.getLights();
|
||||
}
|
||||
@ -285,10 +280,12 @@ export class FluxAccessory implements IAccessory {
|
||||
const lightState = new LightState();
|
||||
lightState
|
||||
.transitionInMillis(this._config.transition ? this._config.transition : 5000)
|
||||
.rgb(rgb.red, rgb.green, rgb.blue);
|
||||
.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)}`)
|
||||
}
|
||||
return;
|
||||
|
||||
await Sleep(this._config.delay ? this._config.delay : 60000);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user