SDB : capteur muet et garde batterie (workflow sous controle du depot) ; alertes de piles des capteurs

This commit is contained in:
Tallagrand Thierry
2026-10-10 00:30:06 +02:00
parent 1bbb54ae6e
commit 16658304c7
12 changed files with 1970 additions and 1 deletions

66
tests/sdb/run.mjs Normal file
View File

@@ -0,0 +1,66 @@
#!/usr/bin/env node
// Tests du workflow SDB Climate & Surplus (10/10/2026) : vrai code de COMPUTE et
// BUILD_ACTIONS en vm, horloge figee au 10/10/2026 00:15 (Paris).
import fs from 'fs';
import vm from 'vm';
import path from 'path';
import { fileURLToPath } from 'url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
const COMPUTE = fs.readFileSync(path.join(ROOT, 'src/sdb/COMPUTE.js'), 'utf8');
const BUILD = fs.readFileSync(path.join(ROOT, 'src/sdb/BUILD_ACTIONS.js'), 'utf8');
const FIXE = Date.parse('2026-10-10T00:15:00+02:00');
class D extends Date { constructor(...a) { super(...(a.length ? a : [FIXE])); } static now() { return FIXE; } }
function run(code, json) {
const ctx = { $input: { first: () => ({ json }) }, console, Date: D, Intl, Math, Number, Object, String, JSON, Array, Boolean };
vm.createContext(ctx);
return vm.runInContext(`(function(){\n${code}\n})()`, ctx);
}
// Releve reel du 10/10 00:15 : capteur muet depuis le 04/10, deshumidificateur et
// prise SDB allumes et memorises, SOC 26 %, EDF ouvert, nuit.
const base = {
soc_percent: '26', battery_current_a: '-28', pv_power_w: '0', pv_power_10m_avg_w: '0',
house_ac_power_w: '1400', grid_power_w: '0', grid_state: 'off', victron_acin1_available: '0',
sdb_master_power: 'on', sdb_master_energy_total: '4.1', deshumide_switch: 'on', deshumide_energy_total: '3.0',
radiator_switch: 'off', radiator_energy_total: '0',
sdb_temperature_c: 'unavailable', sdb_humidity_pct: 'unavailable', sdb_humidity_age_min: '7900',
auto_enabled: 'on', notifications_enabled: 'on', manual_adopt_enabled: 'on',
master_power_latch: 'on', deshumide_latch: 'on', radiator_latch: 'off',
temp_on_min_c: '20', temp_off_max_c: '24', humidity_on_pct: '66', humidity_off_pct: '41',
deshumide_min_on_minutes: '15', deshumide_min_off_minutes: '10',
radiator_auto_enabled: 'on', radiator_soc_on: '85', radiator_soc_off: '80',
master_last_on_at: '2026-10-09 22:39:35', deshumide_last_on_at: '2026-10-09 20:00:35',
deshumide_last_off_at: '2026-05-17 02:50:35', master_last_off_at: '2026-05-17 05:45:35',
balance_required_soc_prudent: '60', balance_status: 'ok',
};
const dec = (o) => run(COMPUTE, { data: JSON.stringify({ ...base, ...o }) })[0].json.decision;
let f = 0;
const ok = (l, c, d = '') => { if (c) console.log(`✅ ${l}`); else { f++; console.log(`❌ ${l} ${d}`); } };
let d = dec({});
ok('muet-releve-1010-coupe', d.deshumide_action === 'turn_off' && d.master_action === 'turn_off' && d.deshumide_latch_action === 'turn_off', JSON.stringify(d));
d = dec({ deshumide_last_on_at: '2026-10-10 00:10:00' });
ok('muet-temps-mini-respecte', d.deshumide_action === 'none', JSON.stringify(d));
d = dec({ deshumide_switch: 'off', deshumide_latch: 'off', master_power_latch: 'off', sdb_master_power: 'off' });
ok('muet-ne-demarre-pas', d.deshumide_action === 'none' && d.master_action === 'none', JSON.stringify(d));
const vivant = { sdb_temperature_c: '26.3', sdb_humidity_pct: '70', sdb_humidity_age_min: '2', deshumide_switch: 'off', deshumide_latch: 'off', master_power_latch: 'off', sdb_master_power: 'off' };
d = dec(vivant);
ok('batterie-reservee-pas-de-demarrage', d.deshumide_action === 'none', JSON.stringify(d));
d = dec({ ...vivant, grid_state: 'on' });
ok('edf-ferme-demarre', d.deshumide_action === 'turn_on' && d.master_action === 'turn_on', JSON.stringify(d));
d = dec({ ...vivant, soc_percent: '80' });
ok('batterie-au-dessus-du-bilan-demarre', d.deshumide_action === 'turn_on', JSON.stringify(d));
d = dec({ ...vivant, pv_power_10m_avg_w: '2500', house_ac_power_w: '1200' });
ok('surplus-solaire-demarre', d.deshumide_action === 'turn_on', JSON.stringify(d));
d = dec({ ...vivant, balance_status: 'degrade' });
ok('bilan-illisible-comportement-anterieur', d.deshumide_action === 'turn_on', JSON.stringify(d));
d = dec({ sdb_temperature_c: '26.3', sdb_humidity_pct: '56', sdb_humidity_age_min: '2' });
ok('batterie-reservee-coupe-apres-temps-mini', d.deshumide_action === 'turn_off', JSON.stringify(d));
d = dec({ sdb_temperature_c: '26.3', sdb_humidity_pct: '56', sdb_humidity_age_min: '2', soc_percent: '80' });
ok('hysteresis-humidite-inchangee', d.deshumide_action === 'none', JSON.stringify(d));
d = dec({ sdb_temperature_c: '26.3', sdb_humidity_pct: '56', sdb_humidity_age_min: '400', soc_percent: '80' });
ok('valeur-figee-3h-muet', d.deshumide_action === 'turn_off', JSON.stringify(d));
const out = run(COMPUTE, { data: JSON.stringify(base) })[0].json;
const acts = run(BUILD, out).map((x) => x.json.action);
ok('build-coupe-enfant-puis-master', acts.indexOf('deshumide') >= 0 && acts.indexOf('deshumide') < acts.indexOf('master_power'), acts.join(','));
console.log(f ? `\n${f} ECHEC(S)` : '\nSDB OK');
process.exit(f ? 1 : 0);