Switched from CAN connection to UART for correctly fetch the datas.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "meb-solars",
|
"name": "meb-solars",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Plugin SignalK per controller MPPT Poweren Boost via convertitore USB-CAN (slcan)",
|
"description": "Plugin SignalK per controller MPPT Poweren Boost via convertitore USB-UART",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
@@ -17,11 +17,10 @@
|
|||||||
"mppt",
|
"mppt",
|
||||||
"poweren",
|
"poweren",
|
||||||
"solar",
|
"solar",
|
||||||
"can"
|
"uart"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"serialport": "^12.0.0",
|
"serialport": "^12.0.0"
|
||||||
"@serialport/parser-readline": "^12.0.0"
|
|
||||||
},
|
},
|
||||||
"author": "MEB Team",
|
"author": "MEB Team",
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
Costanti del protocollo Poweren MPPT Boost.
|
|
||||||
Riferimento: Manuale Doc. 40.0000.206, Hw v1.2 Rev 01.
|
|
||||||
|
|
||||||
Il microcontrollore degli MPPT (PIC24/dsPIC33 a virgola fissa) trasmette interi a 16 bit.
|
Il microcontrollore degli MPPT (PIC24/dsPIC33 a virgola fissa) trasmette interi a 16 bit. Per
|
||||||
Per ottenere il valore fisico bisogna dividere il raw per il fattore di conversione.
|
inviare dati decimali piu precisi, il dato viene moltiplicato per un valore definito.
|
||||||
|
conversionFactors mappa i valori di conversione usati, in modo tale che il codice riceve i dati e li divide il fattore di conversione.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Fattori di conversione (valore_fisico = raw / factor)
|
|
||||||
const conversionsFactors = {
|
const conversionsFactors = {
|
||||||
voltageInput: 10, // tensione ingresso [V] es: 296 -> 29.6 V
|
voltageInput: 10, // tensione ingresso [V] es: 296 -> 29.6 V
|
||||||
currentInput: 100, // corrente ingresso [A] es: 1100 -> 11.00 A
|
currentInput: 100, // corrente ingresso [A] es: 1100 -> 11.00 A
|
||||||
@@ -50,25 +48,87 @@ const errorsFlags = {
|
|||||||
10: 'memory_flash_read_error',
|
10: 'memory_flash_read_error',
|
||||||
};
|
};
|
||||||
|
|
||||||
// Indirizzi dei registri richiesti via polling attivo
|
/*
|
||||||
|
Indirizzi dei registri Poweren da leggere via UART (comando READ singolo registro).
|
||||||
|
|
||||||
|
ATTENZIONE: i valori marcati con "(da verificare)" sono dedotti dalla struttura del
|
||||||
|
protocollo e NON sono confermati dal manuale fornito. Per ottenere la mappa completa e
|
||||||
|
certa, interrogare l'MPPT con i comandi GET_REG_NAMES (102) / GET_REG_VALUES (101) tramite
|
||||||
|
DataCOM, oppure consultare la tabella registri del manuale (Doc. 40.0000.206) e correggere
|
||||||
|
qui i numeri. I valori confermati dalla documentazione sono indicati come "(confermato)".
|
||||||
|
*/
|
||||||
const registerAddresses = {
|
const registerAddresses = {
|
||||||
powerInput: 20,
|
status1: 4, // St1 - flag di stato (confermato: "registro 4")
|
||||||
powerOutput: 21,
|
warning: 6, // Warn - flag di warning/errore (confermato: "registro 6")
|
||||||
temperature1: 35,
|
chargeCapacity: 7, // Chg_Cap - Ah caricati (da verificare)
|
||||||
temperature2: 36,
|
voltageInput: 16, // Vi - tensione ingresso (da verificare)
|
||||||
|
currentInput: 17, // Ii - corrente ingresso (da verificare)
|
||||||
|
voltageOutput: 18, // Vo - tensione uscita (da verificare)
|
||||||
|
currentOutput: 19, // Io - corrente uscita (da verificare)
|
||||||
|
powerInput: 20, // Pi - potenza ingresso (confermato)
|
||||||
|
powerOutput: 21, // Po - potenza uscita (confermato)
|
||||||
|
temperature1: 35, // T1 - temperatura fase 1 (confermato)
|
||||||
|
temperature2: 36, // T2 - temperatura fase 2 (confermato)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mapping dei bitrate CAN al comando slcan corrispondente (Lawicel CAN232/CANUSB)
|
/*
|
||||||
const slcanBitrateCommands = {
|
Elenco ordinato dei registri da interrogare ad ogni ciclo di polling per ciascun MPPT.
|
||||||
10000: 'S0',
|
Si leggono solo i registri che alimentano i path SignalK; Pi/Po non sono inclusi perche'
|
||||||
20000: 'S1',
|
la potenza viene calcolata come Vi*Ii e Vo*Io (come nella versione CAN).
|
||||||
50000: 'S2',
|
*/
|
||||||
100000: 'S3',
|
const pollRegisters = [
|
||||||
125000: 'S4',
|
registerAddresses.status1,
|
||||||
250000: 'S5',
|
registerAddresses.warning,
|
||||||
500000: 'S6',
|
registerAddresses.voltageInput,
|
||||||
800000: 'S7',
|
registerAddresses.currentInput,
|
||||||
1000000: 'S8',
|
registerAddresses.voltageOutput,
|
||||||
|
registerAddresses.currentOutput,
|
||||||
|
registerAddresses.chargeCapacity,
|
||||||
|
registerAddresses.temperature1,
|
||||||
|
registerAddresses.temperature2,
|
||||||
|
];
|
||||||
|
|
||||||
|
/*
|
||||||
|
Costanti del frame del protocollo seriale Poweren (UART).
|
||||||
|
Struttura pacchetto:
|
||||||
|
[SOF][DST][SRC][0x00][DLEN][DATA...][CHK_HI][CHK_LO][EOF]
|
||||||
|
- SOF = 0x42 ('B')
|
||||||
|
- SRC della GUI/host = 0xFF
|
||||||
|
- DST = indirizzo MPPT (0x00 = qualsiasi, es. 50 = MPPT di default)
|
||||||
|
- CHK = sum(DATA) & 0xFFFF in big-endian
|
||||||
|
- EOF = 0x0D
|
||||||
|
*/
|
||||||
|
const serialProtocol = {
|
||||||
|
startOfFrame: 0x42, // SOF
|
||||||
|
sourceHost: 0xFF, // SRC host/GUI
|
||||||
|
endOfFrame: 0x0D, // EOF
|
||||||
|
headerLength: 5, // SOF + DST + SRC + 0x00 + DLEN
|
||||||
|
tailLength: 3, // CHK_HI + CHK_LO + EOF
|
||||||
|
// Comandi (scritti nel registro 2)
|
||||||
|
commands: {
|
||||||
|
reset: 99,
|
||||||
|
defaultValues: 100,
|
||||||
|
getRegValues: 101,
|
||||||
|
getRegNames: 102,
|
||||||
|
getRegUnits: 103,
|
||||||
|
getRegTypes: 104,
|
||||||
|
getFirmwareVersion: 120,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parametri di default della porta seriale (Waveshare USB-UART, 8N1)
|
||||||
|
const serialDefaults = {
|
||||||
|
baudRate: 115200,
|
||||||
|
dataBits: 8,
|
||||||
|
parity: 'none',
|
||||||
|
stopBits: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Timeout e retry per le transazioni sincrone (richiesta -> risposta)
|
||||||
|
const transactionTiming = {
|
||||||
|
timeoutMs: 500,
|
||||||
|
retries: 5,
|
||||||
|
retryDelayMs: 100,
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@@ -76,5 +136,8 @@ module.exports = {
|
|||||||
status1Flags,
|
status1Flags,
|
||||||
errorsFlags,
|
errorsFlags,
|
||||||
registerAddresses,
|
registerAddresses,
|
||||||
slcanBitrateCommands,
|
pollRegisters,
|
||||||
|
serialProtocol,
|
||||||
|
serialDefaults,
|
||||||
|
transactionTiming,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,36 +3,35 @@
|
|||||||
Gli errori vengono propagati al SignalK app tramite app.error / app.setPluginError.
|
Gli errori vengono propagati al SignalK app tramite app.error / app.setPluginError.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Errore specifico per il driver slcan (connessione USB-CAN)
|
// Errore specifico per il driver seriale UART (apertura/scrittura porta)
|
||||||
class SlcanDriverError extends Error {
|
class SerialDriverError extends Error {
|
||||||
constructor(message, cause) {
|
constructor(message, cause) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = 'SlcanDriverError';
|
this.name = 'SerialDriverError';
|
||||||
if (cause) this.cause = cause;
|
if (cause) this.cause = cause;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errore di parsing dei frame CAN ricevuti
|
// Errore di parsing/validazione di un frame seriale Poweren ricevuto
|
||||||
class CanFrameParseError extends Error {
|
class SerialFrameParseError extends Error {
|
||||||
constructor(message, rawLine) {
|
constructor(message) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = 'CanFrameParseError';
|
this.name = 'SerialFrameParseError';
|
||||||
this.rawLine = rawLine;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errore di timeout su una richiesta di lettura registro
|
// Errore di timeout su una transazione di lettura registro
|
||||||
class RegisterReadTimeoutError extends Error {
|
class RegisterReadTimeoutError extends Error {
|
||||||
constructor(rxId, regAddr) {
|
constructor(address, regAddr) {
|
||||||
super(`Timeout su lettura registro ${regAddr} dal nodo CAN 0x${rxId.toString(16)}`);
|
super(`Timeout su lettura registro ${regAddr} dall'MPPT con indirizzo ${address}`);
|
||||||
this.name = 'RegisterReadTimeoutError';
|
this.name = 'RegisterReadTimeoutError';
|
||||||
this.rxId = rxId;
|
this.address = address;
|
||||||
this.regAddr = regAddr;
|
this.regAddr = regAddr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
SlcanDriverError,
|
SerialDriverError,
|
||||||
CanFrameParseError,
|
SerialFrameParseError,
|
||||||
RegisterReadTimeoutError,
|
RegisterReadTimeoutError,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
Modello di un singolo controller MPPT Poweren Boost.
|
|
||||||
|
|
||||||
Ogni istanza mantiene lo stato corrente del dispositivo (tensioni, correnti,
|
Ogni istanza mantiene lo stato corrente del dispositivo (tensioni, correnti,
|
||||||
temperature, flag operativi, warnings) aggiornato dai frame CAN broadcast
|
temperature, flag operativi, warnings) aggiornato dai frame CAN broadcast
|
||||||
(MSG1 status, MSG2 power) e dalle risposte alle richieste di lettura registro.
|
(MSG1 status, MSG2 power) e dalle risposte alle richieste di lettura registro.
|
||||||
@@ -15,7 +13,25 @@ const {
|
|||||||
registerAddresses,
|
registerAddresses,
|
||||||
} = require('./constants');
|
} = require('./constants');
|
||||||
|
|
||||||
// Arrotondamenti convenzionali per esporre dati leggibili nei getter
|
/*
|
||||||
|
Mappa indirizzo registro -> campo di stato e fattore di conversione.
|
||||||
|
Usata da updateRegister per convertire il valore raw a 16 bit nel valore fisico.
|
||||||
|
I registri di flag (status1, warning) sono gestiti a parte perche' vanno decodificati a bit.
|
||||||
|
signed=true indica un intero a 16 bit con segno (complemento a 2), usato per le temperature.
|
||||||
|
*/
|
||||||
|
const numericRegisterMap = {
|
||||||
|
[registerAddresses.voltageInput]: { field: 'voltageInput', factor: conversionsFactors.voltageInput, signed: false },
|
||||||
|
[registerAddresses.currentInput]: { field: 'currentInput', factor: conversionsFactors.currentInput, signed: false },
|
||||||
|
[registerAddresses.voltageOutput]: { field: 'voltageOutput', factor: conversionsFactors.voltageOutput, signed: false },
|
||||||
|
[registerAddresses.currentOutput]: { field: 'currentOutput', factor: conversionsFactors.currentOutput, signed: false },
|
||||||
|
[registerAddresses.powerInput]: { field: 'powerInput', factor: conversionsFactors.powerInput, signed: false },
|
||||||
|
[registerAddresses.powerOutput]: { field: 'powerOutput', factor: conversionsFactors.powerOutput, signed: false },
|
||||||
|
[registerAddresses.chargeCapacity]: { field: 'chargeCapacity', factor: conversionsFactors.chargeCapacity, signed: false },
|
||||||
|
[registerAddresses.temperature1]: { field: 'temperature1', factor: conversionsFactors.temperaturePhase1, signed: true },
|
||||||
|
[registerAddresses.temperature2]: { field: 'temperature2', factor: conversionsFactors.temperaturePhase2, signed: true },
|
||||||
|
};
|
||||||
|
|
||||||
|
// Arrotondamenti
|
||||||
const roundedTo1 = (v) => Number(v.toFixed(1));
|
const roundedTo1 = (v) => Number(v.toFixed(1));
|
||||||
const roundedTo2 = (v) => Number(v.toFixed(2));
|
const roundedTo2 = (v) => Number(v.toFixed(2));
|
||||||
const roundedTo3 = (v) => Number(v.toFixed(3));
|
const roundedTo3 = (v) => Number(v.toFixed(3));
|
||||||
@@ -32,27 +48,14 @@ const decodeFlags = (value, map) => {
|
|||||||
return active;
|
return active;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Parsing del payload broadcast: 8 byte -> 4 uint16 big-endian
|
|
||||||
function parsePayload(data) {
|
|
||||||
if (!data || data.length < 8) return null;
|
|
||||||
return [
|
|
||||||
(data[0] << 8) | data[1],
|
|
||||||
(data[2] << 8) | data[3],
|
|
||||||
(data[4] << 8) | data[5],
|
|
||||||
(data[6] << 8) | data[7],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
class MPPT {
|
class MPPT {
|
||||||
constructor({
|
constructor({
|
||||||
name,
|
name,
|
||||||
rxID,
|
address,
|
||||||
txIdBase,
|
|
||||||
log = () => {},
|
log = () => {},
|
||||||
} = {}) {
|
} = {}) {
|
||||||
this.name = name; // identificativo logico (es. 'port', 'starboard')
|
this.name = name; // identificativo logico (es. 'port', 'starboard')
|
||||||
this.rxID = rxID; // indirizzo CAN del dispositivo (registro Addr)
|
this.address = address; // indirizzo UART/DST del dispositivo (registro Addr)
|
||||||
this.txIdBase = txIdBase; // ID base di trasmissione (broadcast su +1 e +2)
|
|
||||||
this.log = log;
|
this.log = log;
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
@@ -95,51 +98,34 @@ class MPPT {
|
|||||||
return 'bulk';
|
return 'bulk';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aggiorna lo stato da un frame broadcast (MSG1=status, MSG2=power)
|
/*
|
||||||
updateFromBroadcast(type, data) {
|
Aggiorna lo stato a partire dalla lettura UART di un singolo registro.
|
||||||
const regs = parsePayload(data);
|
regAddr = indirizzo del registro letto
|
||||||
if (!regs) return;
|
rawValue = valore raw a 16 bit ricevuto nella risposta (big-endian, gia' decodificato)
|
||||||
|
*/
|
||||||
if (type === 'status') {
|
updateRegister(regAddr, rawValue) {
|
||||||
const [st1, , warn, chgCap] = regs;
|
if (regAddr === registerAddresses.status1) {
|
||||||
this.state.status1Raw = st1;
|
// Registro di stato St1: decodifica i bit in flag operativi
|
||||||
this.state.warningRaw = warn;
|
this.state.status1Raw = rawValue;
|
||||||
this.state.chargeCapacity = chgCap / conversionsFactors.chargeCapacity;
|
this.state.flags = decodeFlags(rawValue, status1Flags);
|
||||||
this.state.flags = decodeFlags(st1, status1Flags);
|
} else if (regAddr === registerAddresses.warning) {
|
||||||
this.state.warnings = decodeFlags(warn, errorsFlags);
|
// Registro Warning: decodifica i bit in codici di errore/warning
|
||||||
|
this.state.warningRaw = rawValue;
|
||||||
|
this.state.warnings = decodeFlags(rawValue, errorsFlags);
|
||||||
if (this.state.warnings.length) {
|
if (this.state.warnings.length) {
|
||||||
this.log(`[${this.name}] WARN: ${this.state.warnings.join(',')}`);
|
this.log(`[${this.name}] WARN: ${this.state.warnings.join(',')}`);
|
||||||
}
|
}
|
||||||
} else if (type === 'power') {
|
} else {
|
||||||
const [vi, ii, vo, io] = regs;
|
// Registri numerici (tensioni, correnti, temperature, capacita')
|
||||||
this.state.voltageInput = vi / conversionsFactors.voltageInput;
|
const target = numericRegisterMap[regAddr];
|
||||||
this.state.currentInput = ii / conversionsFactors.currentInput;
|
if (!target) return;
|
||||||
this.state.voltageOutput = vo / conversionsFactors.voltageOutput;
|
let value = rawValue;
|
||||||
this.state.currentOutput = io / conversionsFactors.currentOutput;
|
if (target.signed && value >= 0x8000) value -= 0x10000; // intero con segno
|
||||||
|
this.state[target.field] = value / target.factor;
|
||||||
}
|
}
|
||||||
this.state.lastUpdate = Date.now();
|
this.state.lastUpdate = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aggiorna lo stato a partire da una risposta a richiesta di registro
|
|
||||||
// Payload reply: [reg_addr, value_high, value_low]
|
|
||||||
updateFromRegisterReply(data) {
|
|
||||||
if (!data || data.length < 3) return;
|
|
||||||
const regAddr = data[0];
|
|
||||||
const rawValue = (data[1] << 8) | data[2];
|
|
||||||
|
|
||||||
const mapping = {
|
|
||||||
[registerAddresses.powerInput]: { field: 'powerInput', factor: conversionsFactors.powerInput },
|
|
||||||
[registerAddresses.powerOutput]: { field: 'powerOutput', factor: conversionsFactors.powerOutput },
|
|
||||||
[registerAddresses.temperature1]: { field: 'temperature1', factor: conversionsFactors.temperaturePhase1 },
|
|
||||||
[registerAddresses.temperature2]: { field: 'temperature2', factor: conversionsFactors.temperaturePhase2 },
|
|
||||||
};
|
|
||||||
|
|
||||||
const target = mapping[regAddr];
|
|
||||||
if (!target) return;
|
|
||||||
this.state[target.field] = rawValue / target.factor;
|
|
||||||
this.state.lastUpdate = Date.now();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Costruisce gli update SignalK (path + value) a partire dallo stato corrente
|
// Costruisce gli update SignalK (path + value) a partire dallo stato corrente
|
||||||
// Tutti i valori sono in unita' SI (V, A, W, K, ratio, Ah)
|
// Tutti i valori sono in unita' SI (V, A, W, K, ratio, Ah)
|
||||||
buildSignalKUpdates() {
|
buildSignalKUpdates() {
|
||||||
@@ -200,6 +186,5 @@ class MPPT {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
MPPT,
|
MPPT,
|
||||||
parsePayload,
|
|
||||||
decodeFlags,
|
decodeFlags,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
Driver per convertitore USB-CAN in modalita' slcan (Lawicel CAN232 / CANUSB).
|
Driver UART per i controller MPPT Poweren Boost collegati tramite convertitore
|
||||||
|
USB-UART (Waveshare USB TO RS232/485/TTL).
|
||||||
|
|
||||||
Apre la porta seriale a 115200 bps, esegue la sequenza di init
|
A differenza della versione CAN (broadcast asincrono), il protocollo UART Poweren e'
|
||||||
(C\r chiudi, S<n>\r bitrate, O\r apri) e parsea le righe in arrivo
|
SINCRONO: per ogni dato si invia un pacchetto di lettura registro e si attende subito la
|
||||||
terminate da CR. Mantiene una collezione di istanze MPPT e instrada
|
risposta. Non esistono notifiche spontanee. Il driver esegue quindi un loop di polling che,
|
||||||
ad esse i frame ricevuti in base al CAN ID, esponendo gli update
|
ad ogni ciclo, interroga in sequenza i registri necessari di ciascun MPPT configurato.
|
||||||
in formato SignalK delta.
|
|
||||||
|
Formato pacchetto Poweren:
|
||||||
|
[SOF=0x42][DST][SRC=0xFF][0x00][DLEN][DATA...][CHK_HI][CHK_LO][EOF=0x0D]
|
||||||
|
- Richiesta lettura registro: DATA = [reg] (DLEN=1)
|
||||||
|
- Risposta: DATA = [val_hi, val_lo] (valore a 16 bit big-endian)
|
||||||
|
- CHK = sum(DATA) & 0xFFFF in big-endian
|
||||||
|
|
||||||
Eventi emessi:
|
Eventi emessi:
|
||||||
'open' -> bus CAN aperto
|
'open' -> porta seriale aperta
|
||||||
'frame' -> { id, data: Buffer } per ogni frame valido
|
|
||||||
'updates' -> array di {path, value} pronti per la pubblicazione SignalK
|
'updates' -> array di {path, value} pronti per la pubblicazione SignalK
|
||||||
'error' -> Error
|
'error' -> Error
|
||||||
'close' -> chiusura porta
|
'close' -> chiusura porta
|
||||||
@@ -17,11 +22,19 @@
|
|||||||
|
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const { SerialPort } = require('serialport');
|
const { SerialPort } = require('serialport');
|
||||||
const { ReadlineParser } = require('@serialport/parser-readline');
|
|
||||||
|
|
||||||
const { MPPT } = require('./mpptscore');
|
const { MPPT } = require('./mpptscore');
|
||||||
const { slcanBitrateCommands, registerAddresses } = require('./constants');
|
const {
|
||||||
const { SlcanDriverError, CanFrameParseError } = require('./errors');
|
pollRegisters,
|
||||||
|
serialProtocol,
|
||||||
|
serialDefaults,
|
||||||
|
transactionTiming,
|
||||||
|
} = require('./constants');
|
||||||
|
const {
|
||||||
|
SerialDriverError,
|
||||||
|
SerialFrameParseError,
|
||||||
|
RegisterReadTimeoutError,
|
||||||
|
} = require('./errors');
|
||||||
|
|
||||||
// Riconnessione automatica con backoff esponenziale (clamp 30s)
|
// Riconnessione automatica con backoff esponenziale (clamp 30s)
|
||||||
const RECONNECT_BASE_DELAY_MS = 1000;
|
const RECONNECT_BASE_DELAY_MS = 1000;
|
||||||
@@ -30,136 +43,116 @@ const RECONNECT_MAX_DELAY_MS = 30000;
|
|||||||
class MPPTReader extends EventEmitter {
|
class MPPTReader extends EventEmitter {
|
||||||
constructor({
|
constructor({
|
||||||
device,
|
device,
|
||||||
canBitrate = 250000,
|
baudRate = serialDefaults.baudRate,
|
||||||
mppts = [],
|
mppts = [],
|
||||||
|
pollIntervalMs = 1000,
|
||||||
|
timeoutMs = transactionTiming.timeoutMs,
|
||||||
|
retries = transactionTiming.retries,
|
||||||
log = () => {},
|
log = () => {},
|
||||||
} = {}) {
|
} = {}) {
|
||||||
super();
|
super();
|
||||||
this.device = device;
|
this.device = device;
|
||||||
this.canBitrate = canBitrate;
|
this.baudRate = baudRate;
|
||||||
|
this.pollIntervalMs = pollIntervalMs;
|
||||||
|
this.timeoutMs = timeoutMs;
|
||||||
|
this.retries = retries;
|
||||||
this.log = log;
|
this.log = log;
|
||||||
|
|
||||||
this.port = null;
|
this.port = null;
|
||||||
this.parser = null;
|
|
||||||
this.isOpen = false;
|
this.isOpen = false;
|
||||||
this.shouldReconnect = true;
|
this.shouldReconnect = true;
|
||||||
this.reconnectAttempts = 0;
|
this.reconnectAttempts = 0;
|
||||||
this.reconnectTimer = null;
|
this.reconnectTimer = null;
|
||||||
|
this.pollTimer = null;
|
||||||
|
this.isPolling = false;
|
||||||
|
|
||||||
// Istanzia gli MPPT e costruisce la routing table CAN_ID -> {mppt, type}
|
// Buffer di ricezione e transazione pendente (protocollo sincrono: 1 alla volta)
|
||||||
this.mppts = new Map(); // name -> MPPT
|
this.rxBuffer = Buffer.alloc(0);
|
||||||
this.routing = new Map(); // canId -> { mppt, type: 'status'|'power'|'reply' }
|
this.pendingTransaction = null;
|
||||||
|
|
||||||
|
// Istanzia gli MPPT configurati (indicizzati per nome logico)
|
||||||
|
this.mppts = new Map();
|
||||||
for (const config of mppts) {
|
for (const config of mppts) {
|
||||||
const mppt = new MPPT({
|
const mppt = new MPPT({
|
||||||
name: config.id,
|
name: config.id,
|
||||||
rxID: config.rxId,
|
address: config.address,
|
||||||
txIdBase: config.txIdBase,
|
|
||||||
log: this.log,
|
log: this.log,
|
||||||
});
|
});
|
||||||
this.mppts.set(config.id, mppt);
|
this.mppts.set(config.id, mppt);
|
||||||
// Broadcast MSG1 (status) e MSG2 (power)
|
|
||||||
this.routing.set(config.txIdBase + 1, { mppt, type: 'status' });
|
|
||||||
this.routing.set(config.txIdBase + 2, { mppt, type: 'power' });
|
|
||||||
// Risposta a richiesta di lettura registro (txIdBase)
|
|
||||||
this.routing.set(config.txIdBase, { mppt, type: 'reply' });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apre la porta seriale ed esegue la sequenza di init slcan
|
// Apre la porta seriale e avvia il loop di polling
|
||||||
async open() {
|
async open() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
this.port = new SerialPort({
|
this.port = new SerialPort({
|
||||||
path: this.device,
|
path: this.device,
|
||||||
baudRate: 115200,
|
baudRate: this.baudRate,
|
||||||
|
dataBits: serialDefaults.dataBits,
|
||||||
|
parity: serialDefaults.parity,
|
||||||
|
stopBits: serialDefaults.stopBits,
|
||||||
autoOpen: false,
|
autoOpen: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.parser = this.port.pipe(new ReadlineParser({ delimiter: '\r' }));
|
// Protocollo binario: si leggono i byte grezzi (l'EOF 0x0D puo' comparire nei dati)
|
||||||
this.parser.on('data', (line) => this._onLine(line));
|
this.port.on('data', (chunk) => this._onData(chunk));
|
||||||
|
|
||||||
this.port.on('error', (err) => {
|
this.port.on('error', (err) => {
|
||||||
this.log(`[reader] errore porta seriale: ${err.message}`);
|
this.log(`[reader] errore porta seriale: ${err.message}`);
|
||||||
this.emit('error', new SlcanDriverError(err.message, err));
|
this.emit('error', new SerialDriverError(err.message, err));
|
||||||
});
|
});
|
||||||
|
|
||||||
this.port.on('close', () => {
|
this.port.on('close', () => {
|
||||||
this.isOpen = false;
|
this.isOpen = false;
|
||||||
|
this._stopPolling();
|
||||||
this.emit('close');
|
this.emit('close');
|
||||||
if (this.shouldReconnect) this._scheduleReconnect();
|
if (this.shouldReconnect) this._scheduleReconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.port.open(async (err) => {
|
this.port.open(async (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(new SlcanDriverError(`Impossibile aprire ${this.device}: ${err.message}`, err));
|
reject(new SerialDriverError(`Impossibile aprire ${this.device}: ${err.message}`, err));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await this._initSlcan();
|
// Disattiva DTR/RTS per non tenere l'MPPT in reset (alcuni adapter lo resettano)
|
||||||
|
await this._setControlLines(false, false);
|
||||||
this.isOpen = true;
|
this.isOpen = true;
|
||||||
this.reconnectAttempts = 0;
|
this.reconnectAttempts = 0;
|
||||||
this.emit('open');
|
this.emit('open');
|
||||||
|
this._startPolling();
|
||||||
resolve();
|
resolve();
|
||||||
} catch (initErr) {
|
} catch (initErr) {
|
||||||
reject(initErr);
|
reject(initErr);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(new SlcanDriverError(err.message, err));
|
reject(new SerialDriverError(err.message, err));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sequenza di inizializzazione slcan: C (close), S<n> (bitrate), O (open)
|
// Chiude la porta e ferma il polling
|
||||||
async _initSlcan() {
|
|
||||||
const bitrateCommand = slcanBitrateCommands[this.canBitrate];
|
|
||||||
if (!bitrateCommand) {
|
|
||||||
throw new SlcanDriverError(`Bitrate CAN non supportato: ${this.canBitrate}`);
|
|
||||||
}
|
|
||||||
await this._writeRaw('C\r');
|
|
||||||
await this._delay(50);
|
|
||||||
await this._writeRaw(`${bitrateCommand}\r`);
|
|
||||||
await this._delay(50);
|
|
||||||
await this._writeRaw('O\r');
|
|
||||||
await this._delay(50);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chiude il canale e la porta
|
|
||||||
async close() {
|
async close() {
|
||||||
this.shouldReconnect = false;
|
this.shouldReconnect = false;
|
||||||
if (this.reconnectTimer) {
|
if (this.reconnectTimer) {
|
||||||
clearTimeout(this.reconnectTimer);
|
clearTimeout(this.reconnectTimer);
|
||||||
this.reconnectTimer = null;
|
this.reconnectTimer = null;
|
||||||
}
|
}
|
||||||
|
this._stopPolling();
|
||||||
|
if (this.pendingTransaction) {
|
||||||
|
clearTimeout(this.pendingTransaction.timer);
|
||||||
|
this.pendingTransaction.reject(new SerialDriverError('Porta in chiusura'));
|
||||||
|
this.pendingTransaction = null;
|
||||||
|
}
|
||||||
if (this.port && this.port.isOpen) {
|
if (this.port && this.port.isOpen) {
|
||||||
try { await this._writeRaw('C\r'); } catch (_) { /* best effort */ }
|
|
||||||
await new Promise((resolve) => this.port.close(() => resolve()));
|
await new Promise((resolve) => this.port.close(() => resolve()));
|
||||||
}
|
}
|
||||||
this.isOpen = false;
|
this.isOpen = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invia una richiesta di lettura registro: t<rxId 3 hex><len=1><regAddr 2 hex>\r
|
// Costruisce gli update aggregati (somma dei lati attivi)
|
||||||
async sendReadRequest(rxId, regAddr) {
|
|
||||||
if (!this.isOpen) return;
|
|
||||||
const idHex = rxId.toString(16).toUpperCase().padStart(3, '0');
|
|
||||||
const addrHex = regAddr.toString(16).toUpperCase().padStart(2, '0');
|
|
||||||
const command = `t${idHex}1${addrHex}\r`;
|
|
||||||
await this._writeRaw(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lista dei registri da interrogare in polling per ciascun MPPT
|
|
||||||
getPollTargets() {
|
|
||||||
const targets = [];
|
|
||||||
for (const mppt of this.mppts.values()) {
|
|
||||||
for (const regAddr of Object.values(registerAddresses)) {
|
|
||||||
targets.push({ rxId: mppt.rxID, regAddr });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return targets;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Costruisce gli update aggregati (somma dei due lati Port + Starboard)
|
|
||||||
buildAggregateUpdates() {
|
buildAggregateUpdates() {
|
||||||
let panelPowerSum = 0;
|
let panelPowerSum = 0;
|
||||||
let powerSum = 0;
|
let powerSum = 0;
|
||||||
@@ -184,17 +177,201 @@ class MPPTReader extends EventEmitter {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- gestione interna ----
|
// ---- loop di polling ----
|
||||||
|
|
||||||
_writeRaw(data) {
|
_startPolling() {
|
||||||
|
if (this.pollTimer) return;
|
||||||
|
// setTimeout ricorsivo: garantisce che un ciclo finisca prima del successivo
|
||||||
|
const tick = async () => {
|
||||||
|
if (!this.isOpen) return;
|
||||||
|
try {
|
||||||
|
await this._pollCycle();
|
||||||
|
} catch (err) {
|
||||||
|
this.emit('error', err);
|
||||||
|
}
|
||||||
|
if (this.isOpen) {
|
||||||
|
this.pollTimer = setTimeout(tick, this.pollIntervalMs);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.pollTimer = setTimeout(tick, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
_stopPolling() {
|
||||||
|
if (this.pollTimer) {
|
||||||
|
clearTimeout(this.pollTimer);
|
||||||
|
this.pollTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Un ciclo di polling: per ogni MPPT legge tutti i registri e pubblica gli update
|
||||||
|
async _pollCycle() {
|
||||||
|
if (this.isPolling) return;
|
||||||
|
this.isPolling = true;
|
||||||
|
try {
|
||||||
|
for (const mppt of this.mppts.values()) {
|
||||||
|
let updated = false;
|
||||||
|
for (const regAddr of pollRegisters) {
|
||||||
|
try {
|
||||||
|
const rawValue = await this._readRegister(mppt.address, regAddr);
|
||||||
|
mppt.updateRegister(regAddr, rawValue);
|
||||||
|
updated = true;
|
||||||
|
} catch (err) {
|
||||||
|
// Timeout/errore sul singolo registro: log e si prosegue col prossimo
|
||||||
|
this.log(`[reader] lettura reg ${regAddr} MPPT ${mppt.address} fallita: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (updated) {
|
||||||
|
const updates = mppt.buildSignalKUpdates();
|
||||||
|
if (updates.length) this.emit('updates', updates);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.isPolling = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- transazione sincrona di lettura registro ----
|
||||||
|
|
||||||
|
/*
|
||||||
|
Legge un singolo registro dall'MPPT indicato (DST=address).
|
||||||
|
Ritorna il valore raw a 16 bit. Esegue retry in caso di timeout.
|
||||||
|
*/
|
||||||
|
async _readRegister(address, regAddr) {
|
||||||
|
let lastError = null;
|
||||||
|
for (let attempt = 0; attempt <= this.retries; attempt++) {
|
||||||
|
try {
|
||||||
|
const responseData = await this._transact(address, this._buildReadPacket(address, regAddr));
|
||||||
|
if (!responseData || responseData.length < 2) {
|
||||||
|
throw new SerialFrameParseError('risposta troppo corta');
|
||||||
|
}
|
||||||
|
return (responseData[0] << 8) | responseData[1];
|
||||||
|
} catch (err) {
|
||||||
|
lastError = err;
|
||||||
|
if (attempt < this.retries) await this._delay(transactionTiming.retryDelayMs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw lastError || new RegisterReadTimeoutError(address, regAddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invia un pacchetto e attende il prossimo frame valido (con timeout)
|
||||||
|
_transact(address, packet) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.port.write(data, (err) => {
|
if (!this.isOpen) {
|
||||||
if (err) reject(new SlcanDriverError(err.message, err));
|
reject(new SerialDriverError('porta non aperta'));
|
||||||
else resolve();
|
return;
|
||||||
|
}
|
||||||
|
// Pulisce eventuali residui dal buffer prima di una nuova transazione
|
||||||
|
this.rxBuffer = Buffer.alloc(0);
|
||||||
|
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
if (this.pendingTransaction && this.pendingTransaction.timer === timer) {
|
||||||
|
this.pendingTransaction = null;
|
||||||
|
reject(new RegisterReadTimeoutError(address, packet[5]));
|
||||||
|
}
|
||||||
|
}, this.timeoutMs);
|
||||||
|
|
||||||
|
this.pendingTransaction = { resolve, reject, timer };
|
||||||
|
|
||||||
|
this.port.write(packet, (err) => {
|
||||||
|
if (err) {
|
||||||
|
clearTimeout(timer);
|
||||||
|
this.pendingTransaction = null;
|
||||||
|
reject(new SerialDriverError(err.message, err));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Costruisce un pacchetto di lettura registro: DATA=[reg], CHK=reg
|
||||||
|
_buildReadPacket(address, regAddr) {
|
||||||
|
const checksum = regAddr & 0xFFFF;
|
||||||
|
return Buffer.from([
|
||||||
|
serialProtocol.startOfFrame,
|
||||||
|
address & 0xFF,
|
||||||
|
serialProtocol.sourceHost,
|
||||||
|
0x00,
|
||||||
|
0x01, // DLEN = 1
|
||||||
|
regAddr & 0xFF, // DATA
|
||||||
|
(checksum >> 8) & 0xFF,
|
||||||
|
checksum & 0xFF,
|
||||||
|
serialProtocol.endOfFrame,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- ricezione e parsing ----
|
||||||
|
|
||||||
|
_onData(chunk) {
|
||||||
|
this.rxBuffer = Buffer.concat([this.rxBuffer, chunk]);
|
||||||
|
let frame;
|
||||||
|
while ((frame = this._extractFrame()) !== null) {
|
||||||
|
if (this.pendingTransaction) {
|
||||||
|
const { resolve, timer } = this.pendingTransaction;
|
||||||
|
clearTimeout(timer);
|
||||||
|
this.pendingTransaction = null;
|
||||||
|
resolve(frame.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Estrae il primo frame completo e valido dal buffer di ricezione, consumandone i byte.
|
||||||
|
Ritorna { dst, src, data } oppure null se non c'e' ancora un frame completo.
|
||||||
|
Scarta i byte spuri finche' non trova il SOF.
|
||||||
|
*/
|
||||||
|
_extractFrame() {
|
||||||
|
const { startOfFrame, endOfFrame, headerLength, tailLength } = serialProtocol;
|
||||||
|
|
||||||
|
// Allinea il buffer al primo SOF disponibile
|
||||||
|
let sofIndex = this.rxBuffer.indexOf(startOfFrame);
|
||||||
|
if (sofIndex < 0) {
|
||||||
|
this.rxBuffer = Buffer.alloc(0);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (sofIndex > 0) {
|
||||||
|
this.rxBuffer = this.rxBuffer.slice(sofIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.rxBuffer.length < headerLength) return null;
|
||||||
|
|
||||||
|
const dlen = (this.rxBuffer[3] << 8) | this.rxBuffer[4];
|
||||||
|
const totalLength = headerLength + dlen + tailLength;
|
||||||
|
if (this.rxBuffer.length < totalLength) return null;
|
||||||
|
|
||||||
|
const dst = this.rxBuffer[1];
|
||||||
|
const src = this.rxBuffer[2];
|
||||||
|
const data = this.rxBuffer.slice(headerLength, headerLength + dlen);
|
||||||
|
const checksum = (this.rxBuffer[headerLength + dlen] << 8) | this.rxBuffer[headerLength + dlen + 1];
|
||||||
|
const eof = this.rxBuffer[headerLength + dlen + 2];
|
||||||
|
|
||||||
|
// Consuma il frame dal buffer (anche se invalido, per non bloccarsi)
|
||||||
|
this.rxBuffer = this.rxBuffer.slice(totalLength);
|
||||||
|
|
||||||
|
if (eof !== endOfFrame) {
|
||||||
|
this.log('[reader] frame scartato: EOF mancante');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
let sum = 0;
|
||||||
|
for (const b of data) sum += b;
|
||||||
|
if ((sum & 0xFFFF) !== checksum) {
|
||||||
|
this.log('[reader] frame scartato: checksum errato');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { dst, src, data };
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- helper vari ----
|
||||||
|
|
||||||
|
_setControlLines(dtr, rts) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
if (!this.port || typeof this.port.set !== 'function') {
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.port.set({ dtr, rts }, () => resolve());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
_delay(ms) {
|
_delay(ms) {
|
||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
@@ -210,72 +387,6 @@ class MPPTReader extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}, delay);
|
}, delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parsing di una riga slcan in arrivo
|
|
||||||
_onLine(rawLine) {
|
|
||||||
const line = rawLine.toString().trim();
|
|
||||||
if (!line) return;
|
|
||||||
// Risposte di ack/err: 'z', 'Z', BEL (0x07)
|
|
||||||
if (line === 'z' || line === 'Z') return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const frame = this._parseFrame(line);
|
|
||||||
if (!frame) return;
|
|
||||||
|
|
||||||
this.emit('frame', frame);
|
|
||||||
|
|
||||||
const route = this.routing.get(frame.id);
|
|
||||||
if (!route) return;
|
|
||||||
|
|
||||||
if (route.type === 'status' || route.type === 'power') {
|
|
||||||
route.mppt.updateFromBroadcast(route.type, frame.data);
|
|
||||||
} else if (route.type === 'reply') {
|
|
||||||
route.mppt.updateFromRegisterReply(frame.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Emette gli update SignalK aggiornati per questo MPPT
|
|
||||||
const updates = route.mppt.buildSignalKUpdates();
|
|
||||||
if (updates.length) this.emit('updates', updates);
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof CanFrameParseError) {
|
|
||||||
this.log(`[reader] frame ignorato: ${err.message}`);
|
|
||||||
} else {
|
|
||||||
this.emit('error', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parser di un singolo frame slcan:
|
|
||||||
// tIIILDD... standard (ID 3 hex)
|
|
||||||
// TIIIIIIIILDD... extended (ID 8 hex)
|
|
||||||
_parseFrame(line) {
|
|
||||||
const type = line[0];
|
|
||||||
if (type !== 't' && type !== 'T') return null;
|
|
||||||
|
|
||||||
const isExtended = type === 'T';
|
|
||||||
const idLen = isExtended ? 8 : 3;
|
|
||||||
if (line.length < 1 + idLen + 1) throw new CanFrameParseError('frame troppo corto', line);
|
|
||||||
|
|
||||||
const idHex = line.substr(1, idLen);
|
|
||||||
const id = parseInt(idHex, 16);
|
|
||||||
if (Number.isNaN(id)) throw new CanFrameParseError('CAN ID non valido', line);
|
|
||||||
|
|
||||||
const dlc = parseInt(line.substr(1 + idLen, 1), 10);
|
|
||||||
if (Number.isNaN(dlc) || dlc < 0 || dlc > 8) throw new CanFrameParseError('DLC non valido', line);
|
|
||||||
|
|
||||||
const dataStart = 1 + idLen + 1;
|
|
||||||
const dataHex = line.substr(dataStart, dlc * 2);
|
|
||||||
if (dataHex.length < dlc * 2) throw new CanFrameParseError('payload incompleto', line);
|
|
||||||
|
|
||||||
const data = Buffer.alloc(dlc);
|
|
||||||
for (let i = 0; i < dlc; i++) {
|
|
||||||
const byte = parseInt(dataHex.substr(i * 2, 2), 16);
|
|
||||||
if (Number.isNaN(byte)) throw new CanFrameParseError('byte payload non valido', line);
|
|
||||||
data[i] = byte;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { id, data, extended: isExtended };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { MPPTReader };
|
module.exports = { MPPTReader };
|
||||||
|
|||||||
72
src/index.js
72
src/index.js
@@ -1,45 +1,32 @@
|
|||||||
/*
|
|
||||||
Plugin SignalK: MEB Solars - Poweren MPPT Boost (CAN).
|
|
||||||
|
|
||||||
Legge i dati dai controller MPPT Poweren Boost collegati via convertitore
|
|
||||||
USB-CAN (slcan) e li pubblica nei path SignalK sotto il namespace
|
|
||||||
meb.solar.<id>.* (port / starboard / total).
|
|
||||||
|
|
||||||
Pattern di pubblicazione: gli update arrivano dal driver in modo asincrono
|
|
||||||
ad ogni frame, vengono bufferizzati e inviati a SignalK in blocco con
|
|
||||||
cadenza configurabile (publishIntervalMs) per non saturare i WebSocket.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const { MPPTReader } = require('./core/reader');
|
const { MPPTReader } = require('./core/reader');
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function (app) {
|
||||||
const plugin = {
|
const plugin = {
|
||||||
id: 'meb-solars',
|
id: 'meb-solars',
|
||||||
name: 'MEB Solar Panels (Poweren MPPT)',
|
name: 'MEB Solar Panels',
|
||||||
description: 'Plugin SignalK per controller MPPT Poweren Boost via convertitore USB-CAN',
|
description: 'Plugin SignalK per controller MPPT Poweren Boost via convertitore USB-UART',
|
||||||
};
|
};
|
||||||
|
|
||||||
let reader = null;
|
let reader = null;
|
||||||
let pollInterval = null;
|
|
||||||
let publishInterval = null;
|
let publishInterval = null;
|
||||||
let pendingUpdatesByPath = new Map(); // path -> value (deduplicazione)
|
let pendingUpdatesByPath = new Map(); // path -> value (deduplicazione)
|
||||||
|
|
||||||
// Schema configurazione mostrato nella UI di SignalK
|
// Schema configurazione mostrato nella UI di SignalK
|
||||||
plugin.schema = {
|
plugin.schema = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
required: ['device', 'canBitrate'],
|
required: ['device', 'baudRate'],
|
||||||
properties: {
|
properties: {
|
||||||
device: {
|
device: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
title: 'Path del dispositivo seriale USB-CAN',
|
title: 'Path del dispositivo seriale USB-UART',
|
||||||
default: '/dev/ttyUSB1',
|
default: '/dev/ttyUSB0',
|
||||||
description: 'Es. /dev/ttyUSB1 o symlink udev /dev/mppt-can',
|
description: 'Es. /dev/ttyUSB0 (Linux) o /dev/cu.usbserial-XXXX (macOS)',
|
||||||
},
|
},
|
||||||
canBitrate: {
|
baudRate: {
|
||||||
type: 'number',
|
type: 'number',
|
||||||
title: 'Bitrate del bus CAN',
|
title: 'Baud rate seriale',
|
||||||
default: 250000,
|
default: 115200,
|
||||||
enum: [125000, 250000, 500000],
|
enum: [9600, 19200, 38400, 57600, 115200],
|
||||||
},
|
},
|
||||||
publishIntervalMs: {
|
publishIntervalMs: {
|
||||||
type: 'number',
|
type: 'number',
|
||||||
@@ -47,12 +34,12 @@ module.exports = function (app) {
|
|||||||
default: 1000,
|
default: 1000,
|
||||||
minimum: 200,
|
minimum: 200,
|
||||||
},
|
},
|
||||||
pollExtraIntervalMs: {
|
pollIntervalMs: {
|
||||||
type: 'number',
|
type: 'number',
|
||||||
title: 'Intervallo polling registri extra (ms)',
|
title: 'Intervallo di polling UART (ms)',
|
||||||
default: 10000,
|
default: 1000,
|
||||||
minimum: 2000,
|
minimum: 200,
|
||||||
description: 'Polling di temperature e potenze pre-calcolate (registri 20,21,35,36)',
|
description: 'Frequenza con cui interrogare i registri di ciascun MPPT',
|
||||||
},
|
},
|
||||||
mppts: {
|
mppts: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
@@ -61,13 +48,11 @@ module.exports = function (app) {
|
|||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: 'string', title: 'ID logico (usato nei path SignalK)', default: 'port' },
|
id: { type: 'string', title: 'ID logico (usato nei path SignalK)', default: 'port' },
|
||||||
rxId: { type: 'number', title: 'CAN Rx ID (registro Addr)', default: 50 },
|
address: { type: 'number', title: 'Indirizzo UART/DST (registro Addr)', default: 50 },
|
||||||
txIdBase: { type: 'number', title: 'CAN Tx ID base (registro CAN_TxId)', default: 51 },
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default: [
|
default: [
|
||||||
{ id: 'port', rxId: 50, txIdBase: 51 },
|
{ id: 'port', address: 50 },
|
||||||
{ id: 'starboard', rxId: 60, txIdBase: 61 },
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -78,8 +63,9 @@ module.exports = function (app) {
|
|||||||
|
|
||||||
reader = new MPPTReader({
|
reader = new MPPTReader({
|
||||||
device: options.device,
|
device: options.device,
|
||||||
canBitrate: options.canBitrate,
|
baudRate: options.baudRate,
|
||||||
mppts: options.mppts,
|
mppts: options.mppts,
|
||||||
|
pollIntervalMs: options.pollIntervalMs,
|
||||||
log: app.debug ? app.debug.bind(app) : console.log,
|
log: app.debug ? app.debug.bind(app) : console.log,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -91,31 +77,24 @@ module.exports = function (app) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
reader.on('open', () => {
|
reader.on('open', () => {
|
||||||
app.setPluginStatus(`Connesso al bus CAN (${options.device} @ ${options.canBitrate} bps)`);
|
app.setPluginStatus(`Connesso alla seriale (${options.device} @ ${options.baudRate} bps)`);
|
||||||
});
|
});
|
||||||
|
|
||||||
reader.on('error', (err) => {
|
reader.on('error', (err) => {
|
||||||
app.error(`Errore driver CAN: ${err.message}`);
|
app.error(`Errore driver UART: ${err.message}`);
|
||||||
app.setPluginError(err.message);
|
app.setPluginError(err.message);
|
||||||
});
|
});
|
||||||
|
|
||||||
reader.on('close', () => {
|
reader.on('close', () => {
|
||||||
app.setPluginStatus('Bus CAN disconnesso, tentativo di riconnessione in corso');
|
app.setPluginStatus('Seriale disconnessa, tentativo di riconnessione in corso');
|
||||||
});
|
});
|
||||||
|
|
||||||
reader.open()
|
reader.open()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
publishMetadata();
|
publishMetadata();
|
||||||
|
|
||||||
// Polling ciclico dei registri extra (temperature, potenze calcolate)
|
// Pubblicazione periodica degli update bufferizzati + aggregati.
|
||||||
pollInterval = setInterval(() => {
|
// Il polling dei registri UART e' gestito internamente dal driver.
|
||||||
const targets = reader.getPollTargets();
|
|
||||||
targets.forEach(({ rxId, regAddr }) => {
|
|
||||||
reader.sendReadRequest(rxId, regAddr).catch(() => { /* best effort */ });
|
|
||||||
});
|
|
||||||
}, options.pollExtraIntervalMs);
|
|
||||||
|
|
||||||
// Pubblicazione periodica degli update bufferizzati + aggregati
|
|
||||||
publishInterval = setInterval(() => {
|
publishInterval = setInterval(() => {
|
||||||
const aggregateUpdates = reader.buildAggregateUpdates();
|
const aggregateUpdates = reader.buildAggregateUpdates();
|
||||||
for (const update of aggregateUpdates) {
|
for (const update of aggregateUpdates) {
|
||||||
@@ -128,14 +107,13 @@ module.exports = function (app) {
|
|||||||
}, options.publishIntervalMs);
|
}, options.publishIntervalMs);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
app.error(`Impossibile aprire il driver CAN: ${err.message}`);
|
app.error(`Impossibile aprire il driver UART: ${err.message}`);
|
||||||
app.setPluginError(err.message);
|
app.setPluginError(err.message);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
plugin.stop = function () {
|
plugin.stop = function () {
|
||||||
app.debug('Arresto plugin meb-solars');
|
app.debug('Arresto plugin meb-solars');
|
||||||
if (pollInterval) { clearInterval(pollInterval); pollInterval = null; }
|
|
||||||
if (publishInterval) { clearInterval(publishInterval); publishInterval = null; }
|
if (publishInterval) { clearInterval(publishInterval); publishInterval = null; }
|
||||||
if (reader) {
|
if (reader) {
|
||||||
reader.close().catch((err) => app.error(`Errore in chiusura: ${err.message}`));
|
reader.close().catch((err) => app.error(`Errore in chiusura: ${err.message}`));
|
||||||
|
|||||||
Reference in New Issue
Block a user