Removed debug logs and scheduler
This commit is contained in:
parent
a43ecd2700
commit
34bca9c705
@ -3,10 +3,8 @@ import Api = require("node-hue-api/lib/api/Api");
|
|||||||
import Light = require("node-hue-api/lib/model/Light");
|
import Light = require("node-hue-api/lib/model/Light");
|
||||||
import LightState = require("node-hue-api/lib/model/lightstate/LightState");
|
import LightState = require("node-hue-api/lib/model/lightstate/LightState");
|
||||||
import { Sleep } from "./sleep";
|
import { Sleep } from "./sleep";
|
||||||
import { Scheduler, Delegate } from "./scheduler";
|
|
||||||
import { IConfig } from "./models/iConfig";
|
import { IConfig } from "./models/iConfig";
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
import { getSunset, getSunRise } from 'sunrise-sunset';
|
|
||||||
import { GetTimesResult, getTimes } from "suncalc";
|
import { GetTimesResult, getTimes } from "suncalc";
|
||||||
import HueError = require("node-hue-api/lib/HueError");
|
import HueError = require("node-hue-api/lib/HueError");
|
||||||
|
|
||||||
@ -116,7 +114,6 @@ export class FluxAccessory implements IAccessory {
|
|||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
const light: Light = await this._hue.lights.getLightByName(value)
|
const light: Light = await this._hue.lights.getLightByName(value)
|
||||||
this._lights.push(light);
|
this._lights.push(light);
|
||||||
this._log(`Got light ${light.name}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,22 +153,18 @@ export class FluxAccessory implements IAccessory {
|
|||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
private isHueError = (object: any): object is HueError => {
|
|
||||||
return '_hueError' in object;
|
|
||||||
}
|
|
||||||
|
|
||||||
private setLights = async (state: LightState) => {
|
private setLights = async (state: LightState) => {
|
||||||
// const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
|
const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
|
||||||
for (const light of this._lights) {
|
this._lights.map(async (light: Light) => {
|
||||||
try {
|
try {
|
||||||
this._log(`Setting light ${light.name}`);
|
|
||||||
await this._hue.lights.setLightState(light.id, state);
|
await this._hue.lights.setLightState(light.id, state);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._log(`Error while setting lights: ${err}`);
|
this._log(`Error while setting lights: ${err}`);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// await Promise.all(promises);
|
|
||||||
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,93 +0,0 @@
|
|||||||
import { EventEmitter } from 'events';
|
|
||||||
import { Sleep } from './sleep';
|
|
||||||
|
|
||||||
export interface ITask {
|
|
||||||
delegate: Delegate;
|
|
||||||
title: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Delegate = () => Promise<void>;
|
|
||||||
|
|
||||||
export class Scheduler extends EventEmitter {
|
|
||||||
private _tasks: Array<ITask>;
|
|
||||||
private _isStarted: boolean;
|
|
||||||
private _log: any;
|
|
||||||
private _delay: number;
|
|
||||||
private _watchdog: number;
|
|
||||||
|
|
||||||
constructor(delay: number, watchdog: number, log: any) {
|
|
||||||
super();
|
|
||||||
this._tasks = [];
|
|
||||||
this._isStarted = false;
|
|
||||||
this._log = log;
|
|
||||||
this._delay = delay;
|
|
||||||
this._watchdog = watchdog;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get IsStarted(): boolean {
|
|
||||||
return this._isStarted;
|
|
||||||
}
|
|
||||||
|
|
||||||
public addTask = (task: ITask): Scheduler => {
|
|
||||||
if (this._isStarted) {
|
|
||||||
throw new Error("Stop the scheduler before adding a task");
|
|
||||||
}
|
|
||||||
|
|
||||||
this._tasks.push(task);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public deleteTask = (task: ITask): Scheduler => {
|
|
||||||
if (this._isStarted) {
|
|
||||||
throw new Error("Stop the scheduler before removing a task");
|
|
||||||
}
|
|
||||||
|
|
||||||
const idx = this._tasks.indexOf(task);
|
|
||||||
if (idx > -1) {
|
|
||||||
this._tasks.splice(idx, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public start = (): void => {
|
|
||||||
if (this._tasks.length > 0 && !this._isStarted) {
|
|
||||||
this.begin();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public stop = (): void => {
|
|
||||||
if (this._isStarted) {
|
|
||||||
this._isStarted = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private begin = async (): Promise<void> => {
|
|
||||||
let current = "";
|
|
||||||
|
|
||||||
const timeoutId = setTimeout(() => {
|
|
||||||
this.emit('error', new Error(`Task '${current}' failed to execute within the watchdog timer`));
|
|
||||||
},
|
|
||||||
this._watchdog);
|
|
||||||
|
|
||||||
timeoutId.unref();
|
|
||||||
|
|
||||||
this._isStarted = true;
|
|
||||||
while (this._isStarted) {
|
|
||||||
for (const task of this._tasks) {
|
|
||||||
try {
|
|
||||||
timeoutId.refresh();
|
|
||||||
current = task.title;
|
|
||||||
|
|
||||||
await task.delegate();
|
|
||||||
} catch (err) {
|
|
||||||
this.emit('error', err);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await Sleep(this._delay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user