UNPKG

node-red-contrib-victron-ble

Version:

node-red node to parse Instant Readout advertisement data from Victron BLE devices

50 lines (49 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProductMappings = exports.getProductName = exports.loadProductMappings = void 0; 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 []; } } exports.loadProductMappings = loadProductMappings; function getProductName(modelId) { const mappings = loadProductMappings(); const mapping = mappings.find(m => m.id === modelId); return mapping ? mapping.name : null; } exports.getProductName = getProductName; function getProductMappings() { return loadProductMappings(); } exports.getProductMappings = getProductMappings;