Kilometrage : Jeep lue par Uconnect (releve a chaque trajet, niveau au branchement), saisie a la seconde

This commit is contained in:
Tallagrand Thierry
2026-10-04 15:01:44 +02:00
parent 74415080d2
commit a1ff5633db
3 changed files with 111 additions and 3 deletions

View File

@@ -3,7 +3,7 @@
// node tests/kilometrage/run.mjs --vues -> vues SQL, dans une transaction
// ANNULEE (a lancer dans le
// conteneur d'historisation)
import { lireReleves, ajouterReleve, creerSchemaKm } from '../../tools/kilometrage.mjs';
import { lireReleves, ajouterReleve, creerSchemaKm, ingererJeep, JEEP } from '../../tools/kilometrage.mjs';
let ok = 0, ko = 0;
const eq = (nom, a, b) => {
@@ -31,6 +31,39 @@ eq('255 car max', b.length <= 255, true);
eq('dernier garde', JSON.parse(b).at(-1)[1], 112703);
eq('helper corrompu repart a vide', JSON.parse(ajouterReleve('pas du json', { vehicule: 'VW', km: 5, niveau: null, ts: new Date(T * 1000), source: 'ha' })).length, 1);
// --- ingererJeep : base simulee en memoire (04/10/2026)
{
const base = { releves: [], niveaux: [], branche: new Date('2026-10-04T11:28:00Z') };
const sql = async (q, v) => {
if (q.includes('INSERT INTO releve_km')) { base.releves.push({ ts: v[0], km: v[1], niveau: v[2] }); return { rowCount: 1 }; }
if (q.includes('INSERT INTO niveau_saisi')) { base.niveaux.push({ saisi_a: v[0], niveau: v[1] }); return { rowCount: 1 }; }
return { rowCount: 1 };
};
const lire = async (q, v) => {
if (q.includes('FROM releve_km')) { const r = [...base.releves].sort((a, b) => b.ts - a.ts); return { rows: r.length ? [{ km: r[0].km, ts: r[0].ts }] : [] }; }
if (q.includes('FROM evenement')) return { rows: [{ t: base.branche }] };
if (q.includes('FROM niveau_saisi')) return { rows: base.niveaux.filter(n => n.saisi_a >= v[0]) };
return { rows: [] };
};
const journ = async () => {};
const etat = (km, niv, maj, branchee = 'on', borne = 'on') => new Map([
[JEEP.odometre, { state: String(km) }], [JEEP.niveau, { state: String(niv) }], [JEEP.majA, { state: maj }],
[JEEP.branchee, { state: branchee }], ['binary_sensor.victron_evcs_vehicle_connected', { state: borne }],
['input_number.victron_evcs_capacity_jeep_kwh', { state: '8.7' }], ['input_number.victron_evcs_autonomie_jeep_km', { state: '50' }]]);
await ingererJeep(sql, lire, etat(112716, 46, '2026-10-04T11:28:39+00:00'), journ);
eq('jeep : premier releve + niveau au branchement', [base.releves.length, base.niveaux.length, base.niveaux[0]?.niveau], [1, 1, 46]);
await ingererJeep(sql, lire, etat(112716, 60, '2026-10-04T11:58:39+00:00'), journ);
eq('jeep : compteur immobile et 2e maj du branchement -> rien', [base.releves.length, base.niveaux.length], [1, 1]);
base.branche = new Date('2026-10-05T16:00:00Z');
await ingererJeep(sql, lire, etat(112760, 30, '2026-10-05T17:00:00+00:00'), journ);
eq('jeep : trajet -> releve ; maj 1 h apres le branchement -> pas de niveau', [base.releves.length, base.releves[1]?.km, base.niveaux.length], [2, 112760, 1]);
await ingererJeep(sql, lire, etat(112700, 30, '2026-10-05T18:00:00+00:00'), journ);
eq('jeep : compteur en baisse refuse', base.releves.length, 2);
base.branche = new Date('2026-10-06T16:00:00Z');
await ingererJeep(sql, lire, etat(112800, 25, '2026-10-06T16:05:00+00:00', 'on', 'off'), journ);
eq('jeep : borne debranchee -> releve oui, niveau au branchement non', [base.releves.length, base.niveaux.length], [3, 1]);
}
if (process.argv.includes('--vues')) {
const pg = (await import('pg')).default;
const c = new pg.Client(); await c.connect();