Compare commits

...

3 Commits

1 changed files with 48 additions and 18 deletions

66
pool.js
View File

@ -28,27 +28,37 @@ let status = {
tick_day: 0,
};
// compute duration of filtration for a given max temperature
// temperature->duration chart
let filt_time = [
[ 5, 1.0 ], // at 5°C, 1 hour of filtering
[ 10, 2.0 ],
[ 12, 4.0 ],
[ 16, 6.0 ],
[ 24, 8.0 ],
[ 27, 12.0 ],
[ 30, 24.0 ]
];
// compute filtration time for a given max temperature
// duration is returned in float format (1.25 -> 1h 15mn)
function compute_duration_filt(t) {
if (t < 5)
return 1;
if (t < 10)
return (t/5); // 1 -> 2
if (t < 12)
return (t-8); // 2 -> 4
if (t < 16)
return (t/2-2); // 4 -> 6
if (t < 24)
return (t/4+2); // 6 -> 8
if (t < 27)
return (t*4/3-24) // 8 -> 12
if (t < 30)
return (t*4 - 96); // 12 -> 24
return 24;
function compute_filtration_time(t) {
let len = filt_time.length;
if (t < filt_time[0][0])
return filt_time[0][1];
for (let i = 0; i < len-1; i++) {
if (t >= filt_time[i][0] && t < filt_time[i+1][0]) {
// linear interpolation between two points
return filt_time[i][1] + (filt_time[i+1][1] - filt_time[i][1]) / (filt_time[i+1][0] - filt_time[i][0]) * (t - filt_time[i][0]);
}
}
return filt_time[len-1][1];
}
// compute the pump schedule for a given duration
// returns an array of start/stop times in float
// [ start1, stop1, start2, stop2, ... ]
@ -92,6 +102,14 @@ function update_new_day() {
print("[POOL] update_new_day", status.tick_day);
status.temp_yesterday = status.temp_today;
status.temp_today = null;
Shelly.call(
"KVS.Set",
{ key: "pool.temp_yesterday", value: status.temp_yesterday },
function (result) {
print("[POOL] KVS set: ", JSON.stringify(result));
}
);
}
// call a chain of API calls
@ -127,7 +145,7 @@ function update_pump(temp, max, time) {
status.tick_pump++;
print("[POOL] update_pump", status.tick_pump, "- temp:", temp, "max:", max, "time:", time);
let duration = compute_duration_filt(max);
let duration = compute_filtration_time(max);
let schedule = compute_schedule_filt(duration);
print("[POOL] update_pump - duration:", duration);
@ -242,6 +260,18 @@ function update_temp(temp) {
status.lock_update = false;
}
// Set initial yesterday temp from KVS
Shelly.call (
"KVS.Get",
{ key: "pool.temp_yesterday" },
function (result) {
if (result) {
status.temp_yesterday = result.value;
print("[POOL] Restore from KVS: temp_yesterday:", status.temp_yesterday);
}
}
)
// receives update from Pool Sensor
// - trigger all temperature and pump updates