Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
a31ec7c282 | |||
5e697021ed | |||
f935896cf2 | |||
829e45b329 | |||
728d3a64a2 | |||
362a5e9d7c | |||
b5fe4a64b9 | |||
c80b01d3e2 | |||
87e6ff99f6 |
74
.drone.yml
74
.drone.yml
@ -2,40 +2,70 @@ kind: pipeline
|
|||||||
type: docker
|
type: docker
|
||||||
name: default
|
name: default
|
||||||
|
|
||||||
clone:
|
|
||||||
disable: true
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: clone
|
|
||||||
image: alpine/git
|
|
||||||
commands:
|
|
||||||
- git clone https://gitea.watsonlabs.net/watsonb8/homebridge-flux.git .
|
|
||||||
- git checkout $DRONE_COMMIT
|
|
||||||
|
|
||||||
- name: build
|
- name: build
|
||||||
image: node
|
image: node
|
||||||
commands:
|
commands:
|
||||||
- npm install
|
- npm install
|
||||||
- npm run build
|
- npm run build
|
||||||
|
|
||||||
- name: publish
|
- name: version
|
||||||
|
image: node
|
||||||
|
commands:
|
||||||
|
- export version=`node -p "require('./package.json').version"`
|
||||||
|
- export commit=`echo $DRONE_COMMIT | cut -c1-5`
|
||||||
|
- npm version prerelease --preid=$commit --git-tag-version=false --allow-same-version=true
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
exclude:
|
||||||
|
- tag
|
||||||
|
- pull_request
|
||||||
|
branch:
|
||||||
|
include:
|
||||||
|
- master
|
||||||
|
|
||||||
|
- name: publish pre
|
||||||
image: plugins/npm
|
image: plugins/npm
|
||||||
settings:
|
settings:
|
||||||
username: admin
|
username:
|
||||||
|
from_secret: npm_username
|
||||||
password:
|
password:
|
||||||
from_secret: npm_password
|
from_secret: npm_password
|
||||||
email: brandon@watsonlabs.net
|
email: b.watson@watsonlabs.net
|
||||||
registry: "http://linuxhost.me:4873/"
|
registry: "http://10.44.1.6:4873/"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
exclude:
|
||||||
|
- tag
|
||||||
|
- pull_request
|
||||||
|
branch:
|
||||||
|
include:
|
||||||
|
- master
|
||||||
|
|
||||||
|
- name: publish tagged version
|
||||||
|
image: plugins/npm
|
||||||
|
settings:
|
||||||
|
username:
|
||||||
|
from_secret: npm_username
|
||||||
|
password:
|
||||||
|
from_secret: npm_password
|
||||||
|
email: b.watson@watsonlabs.net
|
||||||
|
registry: "http://10.44.1.6:4873/"
|
||||||
when:
|
when:
|
||||||
event:
|
event:
|
||||||
- tag
|
- tag
|
||||||
|
exclude:
|
||||||
|
- pull_request
|
||||||
|
|
||||||
notify:
|
- name: Notify
|
||||||
image: drillster/drone-email
|
image: drillster/drone-email
|
||||||
host: smtp.watsonlabs.net
|
settings:
|
||||||
username: srvGitea
|
host: 10.44.1.13
|
||||||
password:
|
username: srvGitea
|
||||||
from_secret: smtp_password
|
password:
|
||||||
from: drone@watsonlabs.net
|
from_secret: smtp_password
|
||||||
when:
|
from: drone@watsonlabs.net
|
||||||
status: [failure]
|
skip_verify: true
|
||||||
|
when:
|
||||||
|
status:
|
||||||
|
- failure
|
||||||
|
1325
package-lock.json
generated
1325
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@ -1,8 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "homebridge-flux",
|
"name": "@watsonb8/homebridge-flux",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "bin/index.js",
|
"main": "bin/index.js",
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "http://10.44.1.6:4873/"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
@ -26,12 +29,13 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/node-cron": "^2.0.3",
|
"@types/node-cron": "^2.0.3",
|
||||||
"@types/suncalc": "^1.8.0",
|
"@types/suncalc": "^1.8.0",
|
||||||
"homebridge": "^0.4.53",
|
"homebridge": "^1.3.8",
|
||||||
"node-cron": "^2.0.3",
|
"node-cron": "^2.0.3",
|
||||||
"node-hue-api": "^4.0.5",
|
"node-hue-api": "^4.0.5",
|
||||||
"suncalc": "^1.8.0"
|
"suncalc": "^1.8.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^13.11.1"
|
"@types/node": "^13.11.1",
|
||||||
|
"typescript": "^4.5.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import { IConfig } from "./models/iConfig";
|
|||||||
import { GetTimesResult, getTimes } from "suncalc";
|
import { GetTimesResult, getTimes } from "suncalc";
|
||||||
import HueError = require("node-hue-api/lib/HueError");
|
import HueError = require("node-hue-api/lib/HueError");
|
||||||
import cron from "node-cron";
|
import cron from "node-cron";
|
||||||
|
import { callbackify } from "util";
|
||||||
|
|
||||||
let Service: HAPNodeJS.Service;
|
let Service: HAPNodeJS.Service;
|
||||||
let Characteristic: HAPNodeJS.Characteristic;
|
let Characteristic: HAPNodeJS.Characteristic;
|
||||||
@ -16,255 +17,335 @@ const MINUTES_IN_MILLISECOND = 60000;
|
|||||||
const SECONDS_IN_HOUR = 3600;
|
const SECONDS_IN_HOUR = 3600;
|
||||||
|
|
||||||
export interface IFluxProps {
|
export interface IFluxProps {
|
||||||
api: any;
|
api: any;
|
||||||
log: any;
|
log: any;
|
||||||
homebridge: any;
|
homebridge: any;
|
||||||
hue: Api,
|
hue: Api;
|
||||||
config: IConfig;
|
config: IConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FluxAccessory implements IAccessory {
|
export class FluxAccessory implements IAccessory {
|
||||||
private _api: any;
|
private _api: any;
|
||||||
private _homebridge: any;
|
private _homebridge: any;
|
||||||
private _log: any = {};
|
private _log: any = {};
|
||||||
private _config: IConfig;
|
private _config: IConfig;
|
||||||
private _isActive: boolean;
|
private _isActive: boolean;
|
||||||
|
|
||||||
//Service fields
|
//Service fields
|
||||||
private _switchService: HAPNodeJS.Service;
|
private _switchService: HAPNodeJS.Service;
|
||||||
private _infoService: HAPNodeJS.Service;
|
private _infoService: HAPNodeJS.Service;
|
||||||
|
|
||||||
private _hue: Api;
|
private _hue: Api;
|
||||||
|
|
||||||
private _lights: Array<Light> = [];
|
private _lights: Array<Light> = [];
|
||||||
|
|
||||||
private _times: GetTimesResult;
|
private _times: GetTimesResult;
|
||||||
|
|
||||||
constructor(props: IFluxProps) {
|
constructor(props: IFluxProps) {
|
||||||
//Assign class variables
|
//Assign class variables
|
||||||
this._log = props.log;
|
this._log = props.log;
|
||||||
this._api = props.api;
|
this._api = props.api;
|
||||||
this._config = props.config;
|
this._config = props.config;
|
||||||
Service = props.api.hap.Service;
|
Service = props.api.hap.Service;
|
||||||
Characteristic = props.api.hap.Characteristic;
|
Characteristic = props.api.hap.Characteristic;
|
||||||
this._homebridge = props.homebridge;
|
this._homebridge = props.homebridge;
|
||||||
this._isActive = false;
|
this._isActive = false;
|
||||||
|
|
||||||
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
|
this._times = getTimes(
|
||||||
|
new Date(),
|
||||||
|
this._config.latitude,
|
||||||
|
this._config.longitude
|
||||||
|
);
|
||||||
|
|
||||||
//Schedule job to refresh times
|
//Schedule job to refresh times
|
||||||
cron.schedule("0 12 * * *", () => {
|
cron
|
||||||
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
|
.schedule(
|
||||||
this._log("Updated sunset times");
|
"0 12 * * *",
|
||||||
}, {
|
() => {
|
||||||
scheduled: true
|
this._times = getTimes(
|
||||||
}).start();
|
new Date(),
|
||||||
|
this._config.latitude,
|
||||||
|
this._config.longitude
|
||||||
|
);
|
||||||
|
this._log("Updated sunset times");
|
||||||
|
},
|
||||||
|
{
|
||||||
|
scheduled: true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.start();
|
||||||
|
|
||||||
this._hue = props.hue;
|
this._hue = props.hue;
|
||||||
this.name = this._config.name;
|
this.name = this._config.name;
|
||||||
|
|
||||||
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
|
||||||
this._infoService = new Service.AccessoryInformation();
|
this._infoService = new Service.AccessoryInformation();
|
||||||
this._infoService.setCharacteristic(Characteristic.Manufacturer, "Brandon Watson")
|
this._infoService.setCharacteristic(
|
||||||
this._infoService.setCharacteristic(Characteristic.Model, "F.lux")
|
Characteristic.Manufacturer,
|
||||||
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789");
|
"Brandon Watson"
|
||||||
|
);
|
||||||
|
this._infoService.setCharacteristic(Characteristic.Model, "F.lux");
|
||||||
|
this._infoService.setCharacteristic(
|
||||||
|
Characteristic.SerialNumber,
|
||||||
|
"123-456-789"
|
||||||
|
);
|
||||||
|
|
||||||
this._switchService = new Service.Switch(
|
this._switchService = new Service.Switch(this.name, "fluxService");
|
||||||
this.name,
|
|
||||||
'fluxService'
|
|
||||||
)
|
|
||||||
|
|
||||||
this._switchService.getCharacteristic(Characteristic.On)
|
this._switchService
|
||||||
//@ts-ignore
|
.getCharacteristic(Characteristic.On)
|
||||||
.on("set", this.onSetEnabled)
|
//@ts-ignore
|
||||||
.on("get", this.onGetEnabled);
|
.on("set", this.onSetEnabled)
|
||||||
|
.on("get", this.onGetEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
public name: string = "Flux";
|
||||||
|
|
||||||
|
public platformAccessory: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for switch set event
|
||||||
|
* @param callback The callback function to call when complete
|
||||||
|
*/
|
||||||
|
private onSetEnabled = async (
|
||||||
|
activeState: boolean,
|
||||||
|
callback: (error?: Error | null | undefined) => void
|
||||||
|
) => {
|
||||||
|
if (activeState) {
|
||||||
|
this._times = getTimes(
|
||||||
|
new Date(),
|
||||||
|
this._config.latitude,
|
||||||
|
this._config.longitude
|
||||||
|
);
|
||||||
|
this.update();
|
||||||
|
} else {
|
||||||
|
this._isActive = false;
|
||||||
}
|
}
|
||||||
|
return callback();
|
||||||
|
};
|
||||||
|
|
||||||
public name: string = "Flux";
|
/**
|
||||||
|
* Handler for switch get event
|
||||||
|
* @param callback The callback function to call when complete
|
||||||
|
*/
|
||||||
|
private onGetEnabled = (): boolean => {
|
||||||
|
return this._isActive;
|
||||||
|
};
|
||||||
|
|
||||||
public platformAccessory: any;
|
/**
|
||||||
|
* Called by homebridge to gather services.
|
||||||
|
*/
|
||||||
|
public getServices = (): Array<HAPNodeJS.Service> => {
|
||||||
|
return [this._infoService, this._switchService!];
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for switch set event
|
* Popuplates internal lights array using the configuration values
|
||||||
* @param callback The callback function to call when complete
|
*/
|
||||||
*/
|
private getLights = async (): Promise<void> => {
|
||||||
private onSetEnabled = async (activeState: boolean, callback: (error?: Error | null | undefined) => void) => {
|
for (const value of this._config.lights) {
|
||||||
if (activeState) {
|
//@ts-ignore
|
||||||
this._times = getTimes(new Date(), this._config.latitude, this._config.longitude);
|
const light: Light = await this._hue.lights.getLightByName(value);
|
||||||
this.update();
|
this._lights.push(light);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private colorTempToRgb = (
|
||||||
|
kelvin: number
|
||||||
|
): { red: number; green: number; blue: number } => {
|
||||||
|
var temp = kelvin / 100;
|
||||||
|
var red, green, blue;
|
||||||
|
if (temp <= 66) {
|
||||||
|
red = 255;
|
||||||
|
green = temp;
|
||||||
|
green = 99.4708025861 * Math.log(green) - 161.1195681661;
|
||||||
|
|
||||||
|
if (temp <= 19) {
|
||||||
|
blue = 0;
|
||||||
|
} else {
|
||||||
|
blue = temp - 10;
|
||||||
|
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
red = temp - 60;
|
||||||
|
red = 329.698727446 * Math.pow(red, -0.1332047592);
|
||||||
|
|
||||||
|
green = temp - 60;
|
||||||
|
green = 288.1221695283 * Math.pow(green, -0.0755148492);
|
||||||
|
|
||||||
|
blue = 255;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
red: this.clamp(red, 0, 255),
|
||||||
|
green: this.clamp(green, 0, 255),
|
||||||
|
blue: this.clamp(blue, 0, 255),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
private clamp(x: number, min: number, max: number) {
|
||||||
|
if (x < min) {
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
if (x > max) {
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
private isHueError = (object: any): object is HueError => {
|
||||||
|
return "_hueError" in object;
|
||||||
|
};
|
||||||
|
|
||||||
|
private setLights = async (state: LightState) => {
|
||||||
|
const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
|
||||||
|
this._lights.map(async (light: Light) => {
|
||||||
|
try {
|
||||||
|
await this._hue.lights.setLightState(light.id, state);
|
||||||
|
} catch (err) {
|
||||||
|
if (
|
||||||
|
this.isHueError(err) &&
|
||||||
|
err.message ===
|
||||||
|
"parameter, xy, is not modifiable. Device is set to off."
|
||||||
|
) {
|
||||||
|
//Eat this
|
||||||
} else {
|
} else {
|
||||||
this._isActive = false;
|
this._log(`Error while setting lights: ${err}`);
|
||||||
}
|
}
|
||||||
return callback();
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(promises);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to generate a UUID
|
||||||
|
*/
|
||||||
|
private generateUUID(): string {
|
||||||
|
// Public Domain/MIT
|
||||||
|
var d = new Date().getTime();
|
||||||
|
if (
|
||||||
|
typeof performance !== "undefined" &&
|
||||||
|
typeof performance.now === "function"
|
||||||
|
) {
|
||||||
|
d += performance.now(); //use high-precision timer if available
|
||||||
}
|
}
|
||||||
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
|
||||||
|
/[xy]/g,
|
||||||
|
function (c) {
|
||||||
|
var r = (d + Math.random() * 16) % 16 | 0;
|
||||||
|
d = Math.floor(d / 16);
|
||||||
|
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for switch get event
|
* Gets adjusted color temperature.
|
||||||
* @param callback The callback function to call when complete
|
*/
|
||||||
*/
|
private getTempOffset = (
|
||||||
private onGetEnabled = (callback: (error: Error | null, value: boolean) => void) => {
|
startTemp: number,
|
||||||
return callback(null, this._isActive);
|
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> => {
|
||||||
|
this._isActive = true;
|
||||||
|
while (this._isActive) {
|
||||||
|
if (this._lights.length === 0) {
|
||||||
|
await this.getLights();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
const now = this.getNow();
|
||||||
* Called by homebridge to gather services.
|
//Pad start time by an hour before sunset
|
||||||
*/
|
const start = new Date(
|
||||||
public getServices = (): Array<HAPNodeJS.Service> => {
|
this._times.sunset.getTime() - 60 * MINUTES_IN_MILLISECOND
|
||||||
return [this._infoService, this._switchService!];
|
);
|
||||||
|
const sunsetStart = this._times.sunsetStart;
|
||||||
|
const sunsetEnd = new Date(
|
||||||
|
this._times.sunset.getTime() + this._config.sunsetDuration
|
||||||
|
);
|
||||||
|
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;
|
||||||
|
const sunsetColorTemp = this._config.sunsetColorTemp
|
||||||
|
? this._config.sunsetColorTemp
|
||||||
|
: 2800;
|
||||||
|
const floorColorTemp = this._config.floorColorTemp
|
||||||
|
? this._config.floorColorTemp
|
||||||
|
: 1900;
|
||||||
|
|
||||||
|
let newTemp = 0;
|
||||||
|
|
||||||
|
if (start < now && now < sunsetStart) {
|
||||||
|
newTemp = this.getTempOffset(
|
||||||
|
startColorTemp,
|
||||||
|
sunsetColorTemp,
|
||||||
|
start,
|
||||||
|
sunsetStart
|
||||||
|
);
|
||||||
|
} else if (sunsetStart < now && now < sunsetEnd) {
|
||||||
|
newTemp = this._config.sunsetColorTemp;
|
||||||
|
} else if (sunsetEnd < now && now < nightStart) {
|
||||||
|
newTemp = this.getTempOffset(
|
||||||
|
sunsetColorTemp,
|
||||||
|
floorColorTemp,
|
||||||
|
sunsetEnd,
|
||||||
|
nightStart
|
||||||
|
);
|
||||||
|
} else if (nightStart < now && now < sunrise) {
|
||||||
|
newTemp = this._config.floorColorTemp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set lights
|
||||||
|
const rgb = this.colorTempToRgb(newTemp);
|
||||||
|
if (rgb && newTemp !== 0) {
|
||||||
|
const lightState = new LightState();
|
||||||
|
lightState
|
||||||
|
.transitionInMillis(
|
||||||
|
this._config.transition ? this._config.transition : 5000
|
||||||
|
)
|
||||||
|
.rgb(
|
||||||
|
rgb.red ? rgb.red : 0,
|
||||||
|
rgb.green ? rgb.green : 0,
|
||||||
|
rgb.blue ? rgb.blue : 0
|
||||||
|
);
|
||||||
|
await this.setLights(lightState);
|
||||||
|
this._log(`Adjusting light temp to ${newTemp}, ${JSON.stringify(rgb)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Sleep(this._config.delay ? this._config.delay : 60000);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
/**
|
}
|
||||||
* Popuplates internal lights array using the configuration values
|
|
||||||
*/
|
|
||||||
private getLights = async (): Promise<void> => {
|
|
||||||
for (const value of this._config.lights) {
|
|
||||||
//@ts-ignore
|
|
||||||
const light: Light = await this._hue.lights.getLightByName(value)
|
|
||||||
this._lights.push(light);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private colorTempToRgb = (kelvin: number): { red: number, green: number, blue: number } => {
|
|
||||||
var temp = kelvin / 100;
|
|
||||||
var red, green, blue;
|
|
||||||
if (temp <= 66) {
|
|
||||||
red = 255;
|
|
||||||
green = temp;
|
|
||||||
green = 99.4708025861 * Math.log(green) - 161.1195681661;
|
|
||||||
|
|
||||||
if (temp <= 19) {
|
|
||||||
blue = 0;
|
|
||||||
} else {
|
|
||||||
blue = temp - 10;
|
|
||||||
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
red = temp - 60;
|
|
||||||
red = 329.698727446 * Math.pow(red, -0.1332047592);
|
|
||||||
|
|
||||||
green = temp - 60;
|
|
||||||
green = 288.1221695283 * Math.pow(green, -0.0755148492);
|
|
||||||
|
|
||||||
blue = 255;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
red: this.clamp(red, 0, 255),
|
|
||||||
green: this.clamp(green, 0, 255),
|
|
||||||
blue: this.clamp(blue, 0, 255)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private clamp(x: number, min: number, max: number) {
|
|
||||||
if (x < min) { return min; }
|
|
||||||
if (x > max) { return max; }
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
private isHueError = (object: any): object is HueError => {
|
|
||||||
return '_hueError' in object;
|
|
||||||
}
|
|
||||||
|
|
||||||
private setLights = async (state: LightState) => {
|
|
||||||
const promises: Array<Promise<unknown> | PromiseLike<unknown>> = [];
|
|
||||||
this._lights.map(async (light: Light) => {
|
|
||||||
try {
|
|
||||||
await this._hue.lights.setLightState(light.id, state);
|
|
||||||
} catch (err) {
|
|
||||||
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}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
await Promise.all(promises);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function to generate a UUID
|
|
||||||
*/
|
|
||||||
private generateUUID(): string { // Public Domain/MIT
|
|
||||||
var d = new Date().getTime();
|
|
||||||
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
|
|
||||||
d += performance.now(); //use high-precision timer if available
|
|
||||||
}
|
|
||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
||||||
var r = (d + Math.random() * 16) % 16 | 0;
|
|
||||||
d = Math.floor(d / 16);
|
|
||||||
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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> => {
|
|
||||||
this._isActive = true;
|
|
||||||
while (this._isActive) {
|
|
||||||
if (this._lights.length === 0) {
|
|
||||||
await this.getLights();
|
|
||||||
}
|
|
||||||
|
|
||||||
const now = this.getNow();
|
|
||||||
//Pad start time by an hour before sunset
|
|
||||||
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() + 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;
|
|
||||||
const sunsetColorTemp = this._config.sunsetColorTemp ? this._config.sunsetColorTemp : 2800;
|
|
||||||
const floorColorTemp = this._config.floorColorTemp ? this._config.floorColorTemp : 1900;
|
|
||||||
|
|
||||||
let newTemp = 0;
|
|
||||||
|
|
||||||
if ((start < now) && (now < sunsetStart)) {
|
|
||||||
newTemp = this.getTempOffset(startColorTemp, sunsetColorTemp, start, sunsetStart);
|
|
||||||
} else if ((sunsetStart < now) && (now < sunsetEnd)) {
|
|
||||||
newTemp = this._config.sunsetColorTemp;
|
|
||||||
} else if ((sunsetEnd < now) && (now < nightStart)) {
|
|
||||||
newTemp = this.getTempOffset(sunsetColorTemp, floorColorTemp, sunsetEnd, nightStart);
|
|
||||||
} else if ((nightStart < now) && (now < sunrise)) {
|
|
||||||
newTemp = this._config.floorColorTemp;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Set lights
|
|
||||||
const rgb = this.colorTempToRgb(newTemp);
|
|
||||||
if (rgb && newTemp !== 0) {
|
|
||||||
const lightState = new LightState();
|
|
||||||
lightState
|
|
||||||
.transitionInMillis(this._config.transition ? this._config.transition : 5000)
|
|
||||||
.rgb(rgb.red ? rgb.red : 0, rgb.green ? rgb.green : 0, rgb.blue ? rgb.blue : 0);
|
|
||||||
await this.setLights(lightState)
|
|
||||||
this._log(`Adjusting light temp to ${newTemp}, ${JSON.stringify(rgb)}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
await Sleep(this._config.delay ? this._config.delay : 60000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -62,7 +62,7 @@ class FluxPlatform {
|
|||||||
this.log(`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`)
|
this.log(`UserName: ${createdUser.username}, ClientKey: ${createdUser.clientkey}`)
|
||||||
connected = true;
|
connected = true;
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
if (err.getHueErrorType() === 101) {
|
if (err.getHueErrorType() === 101) {
|
||||||
this.log('The Link button on the bridge was not pressed. Please press the Link button and try again.');
|
this.log('The Link button on the bridge was not pressed. Please press the Link button and try again.');
|
||||||
Sleep(5000);
|
Sleep(5000);
|
||||||
|
Reference in New Issue
Block a user