From 34bca9c7057f1fd2894eedfc1387d9cf39aeb9b9 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Mon, 13 Apr 2020 20:54:35 -0400 Subject: [PATCH] Removed debug logs and scheduler --- src/fluxAccessory.ts | 17 +++----- src/scheduler.ts | 93 -------------------------------------------- 2 files changed, 5 insertions(+), 105 deletions(-) delete mode 100644 src/scheduler.ts diff --git a/src/fluxAccessory.ts b/src/fluxAccessory.ts index 81575cf..84d7c4e 100644 --- a/src/fluxAccessory.ts +++ b/src/fluxAccessory.ts @@ -3,10 +3,8 @@ 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"; -import { Scheduler, Delegate } from "./scheduler"; import { IConfig } from "./models/iConfig"; //@ts-ignore -import { getSunset, getSunRise } from 'sunrise-sunset'; import { GetTimesResult, getTimes } from "suncalc"; import HueError = require("node-hue-api/lib/HueError"); @@ -116,7 +114,6 @@ export class FluxAccessory implements IAccessory { //@ts-ignore const light: Light = await this._hue.lights.getLightByName(value) this._lights.push(light); - this._log(`Got light ${light.name}`); } } @@ -156,22 +153,18 @@ export class FluxAccessory implements IAccessory { return x; } - private isHueError = (object: any): object is HueError => { - return '_hueError' in object; - } - private setLights = async (state: LightState) => { - // const promises: Array | PromiseLike> = []; - for (const light of this._lights) { + const promises: Array | PromiseLike> = []; + this._lights.map(async (light: Light) => { try { - this._log(`Setting light ${light.name}`); await this._hue.lights.setLightState(light.id, state); } catch (err) { this._log(`Error while setting lights: ${err}`); } - } + }); - // await Promise.all(promises); + + await Promise.all(promises); } /** diff --git a/src/scheduler.ts b/src/scheduler.ts deleted file mode 100644 index 1001b6c..0000000 --- a/src/scheduler.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { EventEmitter } from 'events'; -import { Sleep } from './sleep'; - -export interface ITask { - delegate: Delegate; - title: string; -} - -export type Delegate = () => Promise; - -export class Scheduler extends EventEmitter { - private _tasks: Array; - 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 => { - 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); - } - } -} \ No newline at end of file