UNPKG

node-red-contrib-victron-ble

Version:

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

81 lines (80 loc) 2.42 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const scanner_1 = require("./scanner"); // Singleton scanner instance let scanner = null; function getScanner() { if (!scanner) { scanner = new scanner_1.Scanner(); scanner.start(); } return scanner; } async function discoverDevices() { const scanner = getScanner(); console.log('Discovering devices. Press Ctrl+C to stop.'); process.on('SIGINT', () => { scanner.stop(); process.exit(0); }); let lastDevices = []; // Keep alive setInterval(() => { const devices = scanner.getDiscoveredDevices(); if (JSON.stringify(devices) != JSON.stringify(lastDevices)) { console.log('\nDiscovered devices:'); devices.forEach(dev => { console.log(`${dev.address}: ${dev.name} (RSSI: ${dev.rssi})`); }); lastDevices = devices; } }, 1000); } async function readDeviceData(address, key) { const scanner = getScanner(); scanner.setKey(address, key); console.log(`Reading data for ${address}. Press Ctrl+C to stop.`); scanner.on('parsed', (data) => { if (data.address.toLowerCase() === address.toLowerCase()) { console.log(JSON.stringify(data, null, 2)); } }); process.on('SIGINT', () => { scanner.stop(); process.exit(0); }); setInterval(() => { }, 1000); } const program = new commander_1.Command(); program .name('victron-ble') .description('Node.js library to parse Instant Readout advertisement data from Victron devices') .version('1.0.0'); program .option('-v, --verbose', 'Increase logging output') .hook('preAction', (thisCommand) => { const options = thisCommand.opts(); if (options.verbose) { console.debug = console.log; } else { console.debug = () => { }; } }); program .command('discover') .description('Discover Victron devices with Instant Readout') .action(async () => { await discoverDevices(); }); program .command('read') .description('Read data from a specified device') .argument('<address>', 'Device address') .argument('<key>', 'Decryption key') .action(async (address, key) => { await readDeviceData(address, key); }); program.parse();