UNPKG

node-red-contrib-victron-ble

Version:

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

40 lines (39 loc) 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const scanner_1 = require("../scanner"); let scannerInstance = null; module.exports = function (RED) { function getScanner() { if (!scannerInstance) { scannerInstance = new scanner_1.Scanner(); scannerInstance.start(); } return scannerInstance; } RED.httpAdmin.get('/victron-ble/discover', function (req, res) { const scanner = getScanner(); res.json(scanner.getDiscoveredDevices()); }); function VictronBleNode(config) { RED.nodes.createNode(this, config); const node = this; const address = (config.address || '').toLowerCase(); const key = this.credentials.key; const includeRaw = config.includeRaw || false; const scanner = getScanner(); if (address && key) { scanner.setSettings(address, key, includeRaw); } function onPacket(data) { if (data.address.toLowerCase() === address.toLocaleLowerCase()) { node.send(data); } } scanner.on('parsed', onPacket); node.on('close', function () { // unregister listener when node stops scanner.removeListener('parsed', onPacket); }); } RED.nodes.registerType('victron-ble', VictronBleNode, { credentials: { key: { type: "password" } } }); };