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 {
await this._hue.lights.setLightState(light.id, state);
} 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
} else {
this._log(`Error while setting lights: ${err}`);
@ -199,85 +199,41 @@ export class FluxAccessory implements IAccessory {
const now = new Date(Date.now());
const sunset = this._times.sunset;
let startTime: Date;
if (this._config.startTimeHour) {
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 sunrise = new Date(this._times.sunrise.setTime(this._times.sunrise.getTime() + 1 * 86400000));
// const sunset = new Date('Wed, 15 Apr 2020 21:00:00 GMT');
// const sunrise = new Date('Wed, 15 Apr 2020 23:00:00 GMT');
const startColorTemp = this._config.startColorTemp ? this._config.startColorTemp : 4000;
const stopColorTemp = this._config.stopColorTemp ? this._config.stopColorTemp : 1900;
const sunsetColorTemp = this._config.sunsetColorTemp ? this._config.sunsetColorTemp : 3000;
let percentageComplete = 0;
let newTemp = 0;
//Adjust for next day times
if (stopTime <= startTime) {
//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);
}
const midTime = (sunrise.getTime() - sunset.getTime()) / 2;
const mid = new Date(new Date().setTime(sunset.getTime() + midTime));
const tempRange = Math.abs(startColorTemp - stopColorTemp);
if ((startTime < now) && (now < sunset)) {
if ((sunset < now) && (now < mid)) {
//Before sunset; calculate temp based on TOD
const tempRange = Math.abs(startColorTemp - stopColorTemp);
const dayLength = (sunset.getTime() - startTime.getTime()) / 1000;
const secondsFromStart = (now.getTime() - startTime.getTime()) / 1000;
percentageComplete = secondsFromStart / dayLength;
const totalDecreaseTime = (mid.getTime() - sunset.getTime()) / 1000;
const secondsFromStart = (now.getTime() - sunset.getTime()) / 1000;
percentageComplete = secondsFromStart / totalDecreaseTime;
const tempOffset = tempRange * percentageComplete;
if (startColorTemp > sunsetColorTemp) {
newTemp = startColorTemp - tempOffset;
} else {
newTemp = startColorTemp + tempOffset;
}
} else {
newTemp = startColorTemp - tempOffset;
} else if ((mid < now) && (now < sunrise)) {
//After sunset; calculate temp based on TOD
if (now < stopTime) {
let sunsetTime;
if ((stopTime < startTime) && stopTime.getDay() == sunset.getDay()) {
sunsetTime = new Date(sunset.setTime(stopTime.getTime() + 1 * 86400000));
} else {
sunsetTime = sunset;
}
const totalIncreaseTime = (sunrise.getTime() - mid.getTime()) / 1000;
const secondsUntilSunrise = (sunrise.getTime() - now.getTime()) / 1000;
percentageComplete = secondsUntilSunrise / totalIncreaseTime;
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;
if (startColorTemp > sunsetColorTemp) {
newTemp = startColorTemp - tempOffset;
} else {
newTemp = startColorTemp + tempOffset;
}
newTemp = startColorTemp - tempOffset;
}
//Set lights
const rgb = this.colorTempToRgb(newTemp);
if (rgb) {
if (rgb && newTemp !== 0) {
const lightState = new LightState();
lightState
.transitionInMillis(this._config.transition ? this._config.transition : 5000)

View File

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