Refactoring e correzione degli indirizzi

This commit is contained in:
Giuseppe Raffa
2026-05-13 13:11:59 +02:00
parent 9a299586f4
commit cc006a8791

View File

@@ -1,24 +1,24 @@
const { SerialPort } = require('serialport');
const errors = require('./errors');
const address = 0x04;
const frameLength = 13;
const timeoutMS = 700;
const address = 0x40;
const frameLength = 13;
const timeoutMS = 700;
const retryDelayMS = 200;
//Arrotondamenti
const r1 = v => Math.round(v * 10) / 10; // 0.1 — tensione totale, corrente, SOC%
const r3 = v => Math.round(v * 1000) / 1000; // 0.001 — tensioni cella, capacità Ah
const r2 = v => Math.round(v * 100) / 100; // 0.01 — temperature in kelvin (SignalK)
const r1 = v => Math.round(v * 10) / 10; // 0.1 — tensione totale, corrente, SOC%
const r3 = v => Math.round(v * 1000) / 1000; // 0.001 — tensioni cella, capacità Ah
const r2 = v => Math.round(v * 100) / 100; // 0.01 — temperature in kelvin (SignalK)
class BMS {
constructor({ device, retries = 5, log = () => {} } = {}) {
this.device = device;
constructor({ device, retries = 5, log = () => { } } = {}) {
this.device = device;
this.retries = retries;
this.log = log;
this.port = null;
this.status = null; // ultimo getStatus (n. celle, sensori temp)
this._queue = Promise.resolve(); // serializza le richieste sulla seriale
this.log = log;
this.port = null;
this.status = null; // ultimo getStatus (n. celle, sensori temp)
this._queue = Promise.resolve(); // serializza le richieste sulla seriale
}
open() {
@@ -75,7 +75,7 @@ class BMS {
throw lastErr || new Error(`cmd ${cmd} failed after ${this.retries} retries`);
};
const next = this._queue.then(run, run);
this._queue = next.catch(() => {}); // non rompere la coda al primo errore
this._queue = next.catch(() => { }); // non rompere la coda al primo errore
return next;
}
@@ -133,8 +133,8 @@ class BMS {
if (!d) return null;
return {
total_voltage: r1(d.readInt16BE(0) / 10),
current: r1((d.readInt16BE(4) - 30000) / 10), // < 0 charging, > 0 discharging
soc_percent: r1(d.readInt16BE(6) / 10)
current: r1((d.readInt16BE(4) - 30000) / 10), // < 0 charging, > 0 discharging
soc_percent: r1(d.readInt16BE(6) / 10)
};
}
@@ -144,9 +144,9 @@ class BMS {
if (!d) return null;
return {
highest_voltage: r3(d.readInt16BE(0) / 1000),
highest_cell: d.readInt8(2),
lowest_voltage: r3(d.readInt16BE(3) / 1000),
lowest_cell: d.readInt8(5)
highest_cell: d.readInt8(2),
lowest_voltage: r3(d.readInt16BE(3) / 1000),
lowest_cell: d.readInt8(5)
};
}
@@ -156,9 +156,9 @@ class BMS {
if (!d) return null;
return {
highest_temperature: d.readInt8(0) - 40,
highest_sensor: d.readInt8(1),
lowest_temperature: d.readInt8(2) - 40,
lowest_sensor: d.readInt8(3)
highest_sensor: d.readInt8(1),
lowest_temperature: d.readInt8(2) - 40,
lowest_sensor: d.readInt8(3)
};
}
@@ -169,9 +169,9 @@ class BMS {
const m = d.readInt8(0);
return {
mode: m === 0 ? 'stationary' : m === 1 ? 'charging' : 'discharging',
charging_mosfet: !!d[1],
charging_mosfet: !!d[1],
discharging_mosfet: !!d[2],
capacity_ah: r3(d.readInt32BE(4) / 1000)
capacity_ah: r3(d.readInt32BE(4) / 1000)
};
}
@@ -179,17 +179,17 @@ class BMS {
async getStatus() {
const d = await this._sendBatch('94');
if (!d) return null;
const stateByte = d.readInt8(4);
const stateNames = ['DI1','DI2','DI3','DI4','DO1','DO2','DO3','DO4'];
const stateByte = d.readInt8(4);
const stateNames = ['DI1', 'DI2', 'DI3', 'DI4', 'DO1', 'DO2', 'DO3', 'DO4'];
const states = {};
for (let i = 0; i < 8; i++) states[stateNames[i]] = !!((stateByte >> i) & 1);
const data = {
cells: d.readInt8(0),
cells: d.readInt8(0),
temperature_sensors: d.readInt8(1),
charger_running: !!d[2],
load_running: !!d[3],
charger_running: !!d[2],
load_running: !!d[3],
states,
cycles: d.readInt16BE(5)
cycles: d.readInt16BE(5)
};
this.status = data;
return data;