Fixed bug where flux doesn't run after the first night

This commit is contained in:
watsonb8
2020-04-25 08:46:37 -04:00
parent 26a5fb48c7
commit 602ad31d2d
3 changed files with 47 additions and 4 deletions

View File

@ -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;