Refactored to operate from sunset to sunrise

This commit is contained in:
watsonb8 2020-04-15 18:36:29 -04:00
parent e4cc9da122
commit a5e5c835be
2 changed files with 18 additions and 68 deletions

View File

@ -163,7 +163,7 @@ export class FluxAccessory implements IAccessory {
try { try {
await this._hue.lights.setLightState(light.id, state); await this._hue.lights.setLightState(light.id, state);
} catch (err) { } catch (err) {
if (this.isHueError(err) && err.message === "Error: parameter, xy, is not modifiable. Device is set to off.") { if (this.isHueError(err) && err.message === "parameter, xy, is not modifiable. Device is set to off.") {
//Eat this //Eat this
} else { } else {
this._log(`Error while setting lights: ${err}`); this._log(`Error while setting lights: ${err}`);
@ -199,85 +199,41 @@ export class FluxAccessory implements IAccessory {
const now = new Date(Date.now()); const now = new Date(Date.now());
const sunset = this._times.sunset; const sunset = this._times.sunset;
const sunrise = new Date(this._times.sunrise.setTime(this._times.sunrise.getTime() + 1 * 86400000));
let startTime: Date; // const sunset = new Date('Wed, 15 Apr 2020 21:00:00 GMT');
if (this._config.startTimeHour) { // const sunrise = new Date('Wed, 15 Apr 2020 23:00:00 GMT');
startTime = new Date(Date.now());
startTime.setHours(this._config.startTimeHour, this._config.startTimeMinute);
} else {
startTime = this._times.sunrise;
}
let stopTime: Date;
if (this._config.stopTimeHour) {
stopTime = new Date(Date.now());
stopTime.setHours(this._config.stopTimeHour, this._config.stopTimeMinute);
} else {
stopTime = this._times.dusk;
}
const startColorTemp = this._config.startColorTemp ? this._config.startColorTemp : 4000; const startColorTemp = this._config.startColorTemp ? this._config.startColorTemp : 4000;
const stopColorTemp = this._config.stopColorTemp ? this._config.stopColorTemp : 1900; const stopColorTemp = this._config.stopColorTemp ? this._config.stopColorTemp : 1900;
const sunsetColorTemp = this._config.sunsetColorTemp ? this._config.sunsetColorTemp : 3000;
let percentageComplete = 0; let percentageComplete = 0;
let newTemp = 0; let newTemp = 0;
//Adjust for next day times const midTime = (sunrise.getTime() - sunset.getTime()) / 2;
if (stopTime <= startTime) { const mid = new Date(new Date().setTime(sunset.getTime() + midTime));
//Stop time does not happen in the same day as start time
if (startTime < now) {
//stop time is tomorrow
stopTime.setTime(stopTime.getTime() + 1 * 86400000);
}
} else if (now < startTime) {
//Stop time was yesterday since the new start time is not reached
stopTime.setTime(stopTime.getTime() - 1 * 86400000);
}
if ((startTime < now) && (now < sunset)) {
//Before sunset; calculate temp based on TOD
const tempRange = Math.abs(startColorTemp - stopColorTemp); const tempRange = Math.abs(startColorTemp - stopColorTemp);
const dayLength = (sunset.getTime() - startTime.getTime()) / 1000;
const secondsFromStart = (now.getTime() - startTime.getTime()) / 1000; if ((sunset < now) && (now < mid)) {
percentageComplete = secondsFromStart / dayLength; //Before sunset; calculate temp based on TOD
const totalDecreaseTime = (mid.getTime() - sunset.getTime()) / 1000;
const secondsFromStart = (now.getTime() - sunset.getTime()) / 1000;
percentageComplete = secondsFromStart / totalDecreaseTime;
const tempOffset = tempRange * percentageComplete; const tempOffset = tempRange * percentageComplete;
if (startColorTemp > sunsetColorTemp) {
newTemp = startColorTemp - tempOffset; newTemp = startColorTemp - tempOffset;
} else { } else if ((mid < now) && (now < sunrise)) {
newTemp = startColorTemp + tempOffset;
}
} else {
//After sunset; calculate temp based on TOD //After sunset; calculate temp based on TOD
if (now < stopTime) { const totalIncreaseTime = (sunrise.getTime() - mid.getTime()) / 1000;
let sunsetTime; const secondsUntilSunrise = (sunrise.getTime() - now.getTime()) / 1000;
if ((stopTime < startTime) && stopTime.getDay() == sunset.getDay()) { percentageComplete = secondsUntilSunrise / totalIncreaseTime;
sunsetTime = new Date(sunset.setTime(stopTime.getTime() + 1 * 86400000));
} else {
sunsetTime = sunset;
}
const nightLength = (stopTime.getTime() - sunsetTime.getTime()) / 1000;
const secondsFromSunset = (now.getTime() - sunsetTime.getTime()) / 100;
percentageComplete = secondsFromSunset / nightLength;
} else {
percentageComplete = 1;
}
const tempRange = Math.abs(sunsetColorTemp - stopColorTemp);
const tempOffset = tempRange * percentageComplete; const tempOffset = tempRange * percentageComplete;
if (startColorTemp > sunsetColorTemp) {
newTemp = startColorTemp - tempOffset; newTemp = startColorTemp - tempOffset;
} else {
newTemp = startColorTemp + tempOffset;
}
} }
//Set lights //Set lights
const rgb = this.colorTempToRgb(newTemp); const rgb = this.colorTempToRgb(newTemp);
if (rgb) { if (rgb && newTemp !== 0) {
const lightState = new LightState(); const lightState = new LightState();
lightState lightState
.transitionInMillis(this._config.transition ? this._config.transition : 5000) .transitionInMillis(this._config.transition ? this._config.transition : 5000)

View File

@ -7,14 +7,8 @@ export interface IConfig {
longitude: number; longitude: number;
lights: Array<string>; lights: Array<string>;
name: string; name: string;
startTimeHour?: number;
startTimeMinute?: number;
stopTimeHour?: number;
stopTimeMinute?: number;
startColorTemp?: number; startColorTemp?: number;
stopColorTemp?: number; stopColorTemp?: number;
sunsetColorTemp?: number;
transition?: number; transition?: number;
delay?: number; delay?: number;
watchdog?: number;
} }