UNPKG

node-red-contrib-victron-ble

Version:

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

30 lines (29 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBleAdapter = getBleAdapter; const noble_adapter_1 = require("./noble-adapter"); const bluetoothctl_adapter_1 = require("./bluetoothctl-adapter"); const dbus_adapter_1 = require("./dbus-adapter"); const ADAPTERS = [ { mode: 'bluez', name: 'BlueZ DBus', create: () => new dbus_adapter_1.DbusBleAdapter() }, { mode: 'bluetoothctl', name: 'bluetoothctl', create: () => new bluetoothctl_adapter_1.BluetoothctlBleAdapter() }, { mode: 'noble', name: 'noble', create: () => new noble_adapter_1.NobleBleAdapter() }, ]; async function getBleAdapter(mode = 'auto') { const adapters = mode === 'auto' ? ADAPTERS : ADAPTERS.filter((adapter) => adapter.mode === mode); const errors = []; for (const { name, create } of adapters) { const adapter = create(); try { await adapter.startScan(); console.debug(`Using ${name} BLE adapter.`); return adapter; } catch (error) { errors.push(`${name}: ${error instanceof Error ? error.message : String(error)}`); await adapter.stopScan().catch(() => { }); console.debug(`${name} BLE adapter unavailable: ${error instanceof Error ? error.message : String(error)}`); } } throw new Error(`No BLE adapter available. Tried ${errors.join('; ')}`); }