diff --git a/src/core/reader.js b/src/core/reader.js index 990e8a2..69c2b7c 100644 --- a/src/core/reader.js +++ b/src/core/reader.js @@ -78,9 +78,14 @@ class MPPTReader extends EventEmitter { // Istanzia gli MPPT configurati (indicizzati per nome logico) this.mppts = new Map(); for (const config of mppts) { + const normalizedAddress = Number(config.address); + if (!config.id || !Number.isFinite(normalizedAddress)) { + this.log(`[reader] configurazione MPPT ignorata perche' incompleta: ${JSON.stringify(config)}`); + continue; + } const mppt = new MPPT({ name: config.id, - address: config.address, + address: normalizedAddress, log: this.log, }); this.mppts.set(config.id, mppt); diff --git a/src/index.js b/src/index.js index ab6be2a..157616b 100644 --- a/src/index.js +++ b/src/index.js @@ -228,14 +228,27 @@ module.exports = function (app) { } function normalizeOptions(options = {}) { + const configuredMppts = Array.isArray(options.mppts) && options.mppts.length > 0 + ? options.mppts + : defaultPluginOptions.mppts; + const normalizedMppts = configuredMppts.map((configuredMppt, index) => { + const defaultMppt = defaultPluginOptions.mppts[index] || defaultPluginOptions.mppts[0]; + const normalizedAddress = Number(configuredMppt.address); + + return { + // Identificativo SignalK del controller; se manca si usa il default per indice. + id: configuredMppt.id || defaultMppt.id, + // Indirizzo UART/DST del controller; evita polling con address=undefined. + address: Number.isFinite(normalizedAddress) ? normalizedAddress : defaultMppt.address, + }; + }); + return { device: options.device || defaultPluginOptions.device, baudRate: Number(options.baudRate || defaultPluginOptions.baudRate), publishIntervalMs: Number(options.publishIntervalMs || defaultPluginOptions.publishIntervalMs), pollIntervalMs: Number(options.pollIntervalMs || defaultPluginOptions.pollIntervalMs), - mppts: Array.isArray(options.mppts) && options.mppts.length > 0 - ? options.mppts - : defaultPluginOptions.mppts, + mppts: normalizedMppts, }; }