A bit more specific about transitions
This commit is contained in:
parent
afc83f5685
commit
26a5fb48c7
@ -11,6 +11,7 @@ import HueError = require("node-hue-api/lib/HueError");
|
|||||||
let Service: HAPNodeJS.Service;
|
let Service: HAPNodeJS.Service;
|
||||||
let Characteristic: HAPNodeJS.Characteristic;
|
let Characteristic: HAPNodeJS.Characteristic;
|
||||||
const SECONDS_IN_DAY = 86400000;
|
const SECONDS_IN_DAY = 86400000;
|
||||||
|
const MINUTES_IN_MILLISECOND = 60000;
|
||||||
const SECONDS_IN_HOUR = 3600;
|
const SECONDS_IN_HOUR = 3600;
|
||||||
|
|
||||||
export interface IFluxProps {
|
export interface IFluxProps {
|
||||||
@ -53,8 +54,6 @@ export class FluxAccessory implements IAccessory {
|
|||||||
|
|
||||||
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
|
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
|
||||||
|
|
||||||
this.getLights();
|
|
||||||
|
|
||||||
this.platformAccessory = new this._homebridge.platformAccessory(this.name, this.generateUUID(), this._homebridge.hap.Accessory.Categories.SWITCH);
|
this.platformAccessory = new this._homebridge.platformAccessory(this.name, this.generateUUID(), this._homebridge.hap.Accessory.Categories.SWITCH);
|
||||||
|
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
@ -192,6 +191,28 @@ export class FluxAccessory implements IAccessory {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets adjusted color temperature.
|
||||||
|
*/
|
||||||
|
private getTempOffset = (startTemp: number, endTemp: number, startTime: Date, endTime: Date) => {
|
||||||
|
const now = this.getNow().getTime()
|
||||||
|
const percentComplete = ((now - startTime.getTime()) / (endTime.getTime() - startTime.getTime()));
|
||||||
|
const tempRange = Math.abs(startTemp - endTemp);
|
||||||
|
const tempOffset = tempRange * percentComplete
|
||||||
|
return startTemp - tempOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current time. Use test time if present.
|
||||||
|
*/
|
||||||
|
private getNow() {
|
||||||
|
if (this._config.testNowDateString) {
|
||||||
|
return new Date(this._config.testNowDateString);
|
||||||
|
} else {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private update = async (): Promise<void> => {
|
private update = async (): Promise<void> => {
|
||||||
this._isActive = true;
|
this._isActive = true;
|
||||||
while (this._isActive) {
|
while (this._isActive) {
|
||||||
@ -199,39 +220,28 @@ export class FluxAccessory implements IAccessory {
|
|||||||
await this.getLights();
|
await this.getLights();
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = new Date(Date.now());
|
const now = this.getNow();
|
||||||
//Pad start time by an hour before sunset
|
//Pad start time by an hour before sunset
|
||||||
const start = new Date(this._times.sunset.getTime() - 3 * 1000 * SECONDS_IN_HOUR);
|
const start = new Date(this._times.sunset.getTime() - (30 * MINUTES_IN_MILLISECOND));
|
||||||
const end = new Date(this._times.sunrise.getTime() + 1 * SECONDS_IN_DAY);
|
const sunsetStart = this._times.sunsetStart;
|
||||||
// const sunset = new Date('Wed, 15 Apr 2020 21:00:00 GMT');
|
const sunsetEnd = new Date(this._times.sunset.getTime() + this._config.sunsetDuration);
|
||||||
// const sunrise = new Date('Wed, 15 Apr 2020 23:00:00 GMT');
|
const nightStart = new Date(sunsetEnd.getTime() + 30 * MINUTES_IN_MILLISECOND);
|
||||||
|
const sunrise = new Date(this._times.sunrise.getTime() + 1 * SECONDS_IN_DAY);
|
||||||
|
|
||||||
const startColorTemp = this._config.startColorTemp ? this._config.startColorTemp : 4000;
|
const startColorTemp = this._config.ceilingColorTemp ? this._config.ceilingColorTemp : 4000;
|
||||||
const stopColorTemp = this._config.stopColorTemp ? this._config.stopColorTemp : 1900;
|
const sunsetColorTemp = this._config.sunsetColorTemp ? this._config.sunsetColorTemp : 2800;
|
||||||
|
const floorColorTemp = this._config.floorColorTemp ? this._config.floorColorTemp : 1900;
|
||||||
|
|
||||||
let percentageComplete = 0;
|
|
||||||
let newTemp = 0;
|
let newTemp = 0;
|
||||||
|
|
||||||
const midTime = (end.getTime() - start.getTime()) / 2;
|
if ((start < now) && (now < sunsetStart)) {
|
||||||
const mid = new Date(new Date().setTime(start.getTime() + midTime));
|
newTemp = this.getTempOffset(startColorTemp, sunsetColorTemp, start, sunsetStart);
|
||||||
const tempRange = Math.abs(startColorTemp - stopColorTemp);
|
} else if ((sunsetStart < now) && (now < sunsetEnd)) {
|
||||||
|
newTemp = this._config.sunsetColorTemp;
|
||||||
if ((start < now) && (now < mid)) {
|
} else if ((sunsetEnd < now) && (now < nightStart)) {
|
||||||
//Before sunset; calculate temp based on TOD
|
newTemp = this.getTempOffset(sunsetColorTemp, floorColorTemp, sunsetEnd, nightStart);
|
||||||
const totalDecreaseTime = (mid.getTime() - start.getTime()) / 1000;
|
} else if ((nightStart < now) && (now < sunrise)) {
|
||||||
const secondsFromStart = (now.getTime() - start.getTime()) / 1000;
|
newTemp = this._config.floorColorTemp;
|
||||||
percentageComplete = secondsFromStart / totalDecreaseTime;
|
|
||||||
const tempOffset = tempRange * percentageComplete;
|
|
||||||
|
|
||||||
newTemp = startColorTemp - tempOffset;
|
|
||||||
} else if ((mid < now) && (now < end)) {
|
|
||||||
//After sunset; calculate temp based on TOD
|
|
||||||
const totalIncreaseTime = (end.getTime() - mid.getTime()) / 1000;
|
|
||||||
const secondsUntilSunrise = (end.getTime() - now.getTime()) / 1000;
|
|
||||||
percentageComplete = secondsUntilSunrise / totalIncreaseTime;
|
|
||||||
|
|
||||||
const tempOffset = tempRange * percentageComplete;
|
|
||||||
newTemp = startColorTemp - tempOffset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set lights
|
//Set lights
|
||||||
|
@ -3,12 +3,59 @@ export interface IConfig {
|
|||||||
ipAddress: string;
|
ipAddress: string;
|
||||||
userName?: string;
|
userName?: string;
|
||||||
clientKey?: string;
|
clientKey?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Latitude
|
||||||
|
*/
|
||||||
latitude: number;
|
latitude: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Longitute
|
||||||
|
*/
|
||||||
longitude: number;
|
longitude: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of lights to affect
|
||||||
|
*/
|
||||||
lights: Array<string>;
|
lights: Array<string>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the enable switch in homekit
|
||||||
|
*/
|
||||||
name: string;
|
name: string;
|
||||||
startColorTemp?: number;
|
|
||||||
stopColorTemp?: number;
|
/**
|
||||||
|
* The color temperature at the start of sunset transition
|
||||||
|
*/
|
||||||
|
ceilingColorTemp: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The color temp during the night
|
||||||
|
*/
|
||||||
|
floorColorTemp: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The color temp at sunet
|
||||||
|
*/
|
||||||
|
sunsetColorTemp: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The time in milliseconds the lights should remain at sunset temperature.
|
||||||
|
*/
|
||||||
|
sunsetDuration: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of milliseconds the lights take to transition
|
||||||
|
*/
|
||||||
transition?: number;
|
transition?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of milliseconds to wait btw updates
|
||||||
|
*/
|
||||||
delay?: number;
|
delay?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current formatted date and time to use with testing
|
||||||
|
*/
|
||||||
|
testNowDateString?: string;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user