Better duration times based on how long do the HTTP requests take

master
Kuba Orlik 10 months ago
parent 785622b46c
commit eb5d857180

@ -1,5 +1,6 @@
const TOKEN = process.env.HA_TOKEN;
const HA_URL = process.env.HA_URL || "http://127.0.0.1:8123";
const PORT = parseInt(process.env.HA_BRIDGE_PORT || "41234");
const TRANSITION_DURATION_DIVIDER = parseInt(process.env.HA_DURATION_DIVIDER || "2");
module.exports = { TOKEN, HA_URL, PORT };
module.exports = { TOKEN, HA_URL, PORT, TRANSITION_DURATION_DIVIDER };

@ -1,10 +1,16 @@
const { HA_URL, TOKEN } = require("./env.js");
const { HA_URL, TOKEN, TRANSITION_DURATION_DIVIDER } = require("./env.js");
const { sleep } = require("./util.js");
const latest_color = require("./latest_color.js");
const { lights } = require("./config.js");
async function send_color(light_data, color, max_brightness, debug) {
async function send_color(
light_data,
color,
max_brightness,
duration = 0.18,
debug = false
) {
const brightness = (color[0] + color[1] + color[2]) / 3 / 255;
if (debug) {
console.log({
@ -43,7 +49,7 @@ async function send_color(light_data, color, max_brightness, debug) {
module.exports = async function light_loop(light_index, max_brightness, debug) {
light_index = parseInt(light_index);
let last_time = 0;
let duration = 0.18; // 180ms - the default for start, will be adjusted later based on how long did the http request take
let last_color = [0, 0, 0];
while (true) {
last_time = Date.now();
@ -58,12 +64,22 @@ module.exports = async function light_loop(light_index, max_brightness, debug) {
}
}
if (is_changed) {
if (debug) {
console.time(`light ${light_index} http request took`);
}
const before = Date.now();
await send_color(
lights[light_index],
current_color,
max_brightness,
duration,
debug
);
const after = Date.now();
duration = (after - before) / 1000 / TRANSITION_DURATION_DIVIDER;
if (debug) {
console.timeEnd(`light ${light_index} http request took`);
}
last_color = current_color;
} else {
await sleep(10);

Loading…
Cancel
Save