node-red-contrib-victron-ble
Version:
node-red node to parse Instant Readout advertisement data from Victron BLE devices
49 lines (48 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadProductMappings = loadProductMappings;
exports.getProductName = getProductName;
exports.getProductMappings = getProductMappings;
const fs_1 = require("fs");
const path_1 = require("path");
let productMappings = null;
function loadProductMappings() {
if (productMappings) {
return productMappings;
}
try {
const mappingPath = (0, path_1.join)(__dirname, 'products.txt');
const content = (0, fs_1.readFileSync)(mappingPath, 'utf8');
productMappings = content
.split('\n')
.filter(line => line.trim() && !line.startsWith('#'))
.map(line => {
const trimmedLine = line.trim();
// Find the first space to separate ID from name
const firstSpaceIndex = trimmedLine.indexOf(' ');
if (firstSpaceIndex > 0) {
const idStr = trimmedLine.substring(0, firstSpaceIndex);
const name = trimmedLine.substring(firstSpaceIndex + 1).trim();
const id = parseInt(idStr, 10);
if (!isNaN(id) && name) {
return { id, name };
}
}
return null;
})
.filter((mapping) => mapping !== null);
return productMappings;
}
catch (error) {
console.warn('Could not load Victron Product ID mapping:', error);
return [];
}
}
function getProductName(modelId) {
const mappings = loadProductMappings();
const mapping = mappings.find(m => m.id === modelId);
return mapping ? mapping.name : null;
}
function getProductMappings() {
return loadProductMappings();
}