From eb5d85718091f0129dc1f52cd8bb9518eaa374ed Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Mon, 22 Jan 2024 21:30:26 +0100 Subject: [PATCH] Better duration times based on how long do the HTTP requests take --- env.js | 3 ++- light-loop.js | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/env.js b/env.js index 340cf00..4c7d83d 100644 --- a/env.js +++ b/env.js @@ -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 }; diff --git a/light-loop.js b/light-loop.js index a39db3d..5025741 100644 --- a/light-loop.js +++ b/light-loop.js @@ -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);