diff --git a/package-lock.json b/package-lock.json index db2693d..5651d3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,11 +10,24 @@ "integrity": "sha512-eWQGP3qtxwL8FGneRrC5DwrJLGN4/dH1clNTuLfN81HCrxVtxRjygDTUoZJ5ASlDEeo0ppYFQjQIlXhtXpOn6g==", "dev": true }, + "@types/node-cron": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@types/node-cron/-/node-cron-2.0.3.tgz", + "integrity": "sha512-gwBBGeY2XeYBLE0R01K9Sm2hvNcPGmoloL6aqthA3QmBB1GYXTHIJ42AGZL7bdXBRiwbRV8b6NB5iKpl20R3gw==", + "requires": { + "@types/tz-offset": "*" + } + }, "@types/suncalc": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/@types/suncalc/-/suncalc-1.8.0.tgz", "integrity": "sha512-1Bx7KgoCLP8LuKaY9whWiX0Y8JMEB9gmZHNJigainwFuv3gEkZvTx0AGNvnA5nSu1daQcJDKScm9tNpW/ZjpjA==" }, + "@types/tz-offset": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/@types/tz-offset/-/tz-offset-0.0.0.tgz", + "integrity": "sha512-XLD/llTSB6EBe3thkN+/I0L+yCTB6sjrcVovQdx2Cnl6N6bTzHmwe/J8mWnsXFgxLrj/emzdv8IR4evKYG2qxQ==" + }, "ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", @@ -339,6 +352,15 @@ "resolved": "https://registry.npmjs.org/multicast-dns-service-types/-/multicast-dns-service-types-1.1.0.tgz", "integrity": "sha1-iZ8R2WhuXgXLkbNdXw5jt3PPyQE=" }, + "node-cron": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-2.0.3.tgz", + "integrity": "sha512-eJI+QitXlwcgiZwNNSRbqsjeZMp5shyajMR81RZCqeW0ZDEj4zU9tpd4nTh/1JsBiKbF8d08FCewiipDmVIYjg==", + "requires": { + "opencollective-postinstall": "^2.0.0", + "tz-offset": "0.0.1" + } + }, "node-hue-api": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/node-hue-api/-/node-hue-api-4.0.5.tgz", @@ -391,6 +413,11 @@ "object-keys": "^1.0.11" } }, + "opencollective-postinstall": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.2.tgz", + "integrity": "sha512-pVOEP16TrAO2/fjej1IdOyupJY8KDUM1CvsaScRbw6oddvpQoOfGk4ywha0HKKVAD6RkW4x6Q+tNBwhf3Bgpuw==" + }, "q": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/q/-/q-1.1.2.tgz", @@ -485,6 +512,11 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" + }, + "tz-offset": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/tz-offset/-/tz-offset-0.0.1.tgz", + "integrity": "sha512-kMBmblijHJXyOpKzgDhKx9INYU4u4E1RPMB0HqmKSgWG8vEcf3exEfLh4FFfzd3xdQOw9EuIy/cP0akY6rHopQ==" } } } diff --git a/package.json b/package.json index f81b01e..88634bf 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,10 @@ "author": "Brandon Watson", "license": "ISC", "dependencies": { + "@types/node-cron": "^2.0.3", "@types/suncalc": "^1.8.0", "homebridge": "^0.4.53", + "node-cron": "^2.0.3", "node-hue-api": "^4.0.5", "suncalc": "^1.8.0" }, diff --git a/src/fluxAccessory.ts b/src/fluxAccessory.ts index fb988e8..53f6cc9 100644 --- a/src/fluxAccessory.ts +++ b/src/fluxAccessory.ts @@ -7,6 +7,7 @@ import { IConfig } from "./models/iConfig"; //@ts-ignore import { GetTimesResult, getTimes } from "suncalc"; import HueError = require("node-hue-api/lib/HueError"); +import cron from "node-cron"; let Service: HAPNodeJS.Service; let Characteristic: HAPNodeJS.Characteristic; @@ -49,11 +50,19 @@ export class FluxAccessory implements IAccessory { this._homebridge = props.homebridge; this._isActive = false; + this._times = getTimes(new Date(), this._config.latitude, this._config.longitude); + + //Schedule job to refresh times + cron.schedule("0 12 * * *", () => { + this._times = getTimes(new Date(), this._config.latitude, this._config.longitude); + this._log("Updated sunset times"); + }, { + scheduled: true + }).start(); + this._hue = props.hue; this.name = this._config.name; - this._times = getTimes(new Date(), this._config.latitude, this._config.longitude); - this.platformAccessory = new this._homebridge.platformAccessory(this.name, this.generateUUID(), this._homebridge.hap.Accessory.Categories.SWITCH); //@ts-ignore @@ -222,10 +231,10 @@ export class FluxAccessory implements IAccessory { const now = this.getNow(); //Pad start time by an hour before sunset - const start = new Date(this._times.sunset.getTime() - (30 * MINUTES_IN_MILLISECOND)); + const start = new Date(this._times.sunset.getTime() - (60 * MINUTES_IN_MILLISECOND)); const sunsetStart = this._times.sunsetStart; const sunsetEnd = new Date(this._times.sunset.getTime() + this._config.sunsetDuration); - const nightStart = new Date(sunsetEnd.getTime() + 30 * MINUTES_IN_MILLISECOND); + const nightStart = new Date(sunsetEnd.getTime() + 60 * MINUTES_IN_MILLISECOND); const sunrise = new Date(this._times.sunrise.getTime() + 1 * SECONDS_IN_DAY); const startColorTemp = this._config.ceilingColorTemp ? this._config.ceilingColorTemp : 4000;