merge update_pool() into update_temp()

This commit is contained in:
Gilles Grandou 2022-04-21 11:31:12 +02:00
parent 87c43ff253
commit 57549371e2
1 changed files with 58 additions and 56 deletions

114
pool.js
View File

@ -11,11 +11,11 @@ let status = {
update_temp_max_last: null,
disable_temp: null,
lock_update: false,
tick_mqtt: 0,
tick_temp: 0,
tick_pool: 0,
tick_pump: 0,
tick_timer: 0,
tick_day: 0,
};
@ -76,32 +76,6 @@ function time_to_timespec(t) {
return ts;
}
// update temperature from Sensor
// - trigger pump update if needed
function update_temp(temp) {
status.tick_temp++;
print("[POOL] update_temp", status.tick_temp, temp);
if (status.disable_temp !== null) {
print("[POOL] updated disabled");
return;
}
status.temp = temp;
status.temp_today = Math.max(status.temp_today, temp);
status.temp_max = Math.max(status.temp_today, status.temp_yesterday);
print("[POOL] update_temp - max:", status.temp_max, "today:", status.temp_today, "yesterday:", status.temp_yesterday);
if (status.temp_max !== status.update_temp_max_last) {
update_pool();
}
else {
print("[POOL] no temp change, skip update_pool");
}
}
// new day, update status
function update_new_day() {
@ -184,42 +158,68 @@ function update_pump(temp, max, time) {
do_call(calls);
}
// update pool:
// update temperature from Sensor
// - update max temp
// - retrieve current time
// - switch to next day
// - do a pump update if the last one didn't not happen too close
// - switch to new day
// - do a pump update if the last one didn't happen too close and if max temp has changed
function update_pool() {
status.tick_pool++;
print("update_pool", status.tick_pool);
function update_temp(temp) {
status.tick_temp++;
Shelly.call (
"Sys.GetStatus",
{},
function (result) {
let time = result.time; // "HH:MM"
print("[POOL] time", time);
print("[POOL] update_temp", status.tick_temp, temp);
// compute current time in float format (12h45 -> 12.75)
let t = JSON.parse(time.slice(0,2)) + JSON.parse(time.slice(3,5)) / 60;
if (status.disable_temp !== null) {
print("[POOL] updated disabled");
return;
}
if (t < status.update_time)
update_new_day();
if (status.lock_update) {
print("[POOL] update_temp locked");
return;
}
status.lock_update = true;
status.update_time = t;
status.temp = temp;
status.temp_today = Math.max(status.temp_today, temp);
status.temp_max = Math.max(status.temp_today, status.temp_yesterday);
if (status.temp_max !== null) {
if ((t - status.update_time_last) > 0.15) { // 9 minutes
update_pump(status.temp, status.temp_max, t);
status.update_time_last = t;
status.update_temp_max_last = status.temp_max;
}
else {
print("[POOL] to much update_pool, skipped");
}
print("[POOL] update_temp - max:", status.temp_max, "today:", status.temp_today, "yesterday:", status.temp_yesterday);
if (status.temp_max !== status.update_temp_max_last) {
Shelly.call (
"Sys.GetStatus",
{},
function (result) {
let time = result.time; // "HH:MM"
print("[POOL] time", time);
// compute current time in float format (12h45 -> 12.75)
let t = JSON.parse(time.slice(0,2)) + JSON.parse(time.slice(3,5)) / 60;
if (t < status.update_time)
update_new_day();
status.update_time = t;
if (status.temp_max !== null) {
if ((t - status.update_time_last) > 0.15) { // 9 minutes
update_pump(status.temp, status.temp_max, t);
status.update_time_last = t;
status.update_temp_max_last = status.temp_max;
}
else {
print("[POOL] to much update_pump, skipped");
}
}
}
}
);
);
}
else {
print("[POOL] no temp change, skip update_pump");
}
status.lock_update = false;
}
// receives update from Pool Sensor
@ -228,6 +228,8 @@ function update_pool() {
MQTT.subscribe(
"zigbee2mqtt/Piscine",
function (topic, msg) {
status.tick_mqtt++;
print("[POOL] mqtt", topic);
let obj = JSON.parse(msg);
if (obj.temperature === undefined) {
return;