UNPKG

homebridge-netman

Version:

A Homebridge plugin to monitor devices connected to your network, track online/offline status, display Wi-Fi stats, and provide optional router controls.

88 lines (87 loc) 3.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BluetoothLEDPlatform = void 0; const noble_1 = __importDefault(require("@abandonware/noble")); const settings_1 = require("./settings"); const platformAccessory_1 = require("./platformAccessory"); /** * BluetoothLEDPlatform * This class is the main constructor for your plugin, this is where you should * parse the user config and discover/register accessories with Homebridge. */ class BluetoothLEDPlatform { log; config; api; Service; Characteristic; accessories = []; connected = false; constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = api.hap.Service; this.Characteristic = api.hap.Characteristic; this.log.debug('Finished initializing platform:', this.config.name); // Start scanning for Bluetooth devices when Homebridge finishes launching this.api.on('didFinishLaunching', () => { log.debug('Executed didFinishLaunching callback'); this.discoverDevices(); }); } /** * This function is invoked when homebridge restores cached accessories from disk at startup. * It should be used to set up event handlers for characteristics and update respective values. */ configureAccessory(accessory) { this.log.info('Loading accessory from cache:', accessory.displayName); this.accessories.push(accessory); } /** * Discover and register devices based on Bluetooth name from the config. */ discoverDevices() { noble_1.default.on('stateChange', async (state) => { if (state === 'poweredOn') { await noble_1.default.startScanningAsync([], false); } }); noble_1.default.on('discover', async (peripheral) => { if (this.connected) { return; } this.log.debug(`name: ${peripheral.advertisement.localName} uuid: ${peripheral.uuid}`); // Match the Bluetooth name against the config if (peripheral.advertisement.localName === this.config.bluetoothName) { await noble_1.default.stopScanningAsync(); this.log.success(`Connection OK! ${peripheral.advertisement.localName} ${peripheral.uuid}`); const uuid = this.api.hap.uuid.generate(peripheral.uuid); const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid); if (existingAccessory) { this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); new platformAccessory_1.LEDLightAccessory(this, existingAccessory, peripheral); } else { this.log.info('Setting up new accessory:', peripheral.advertisement.localName); const accessory = new this.api.platformAccessory(peripheral.advertisement.localName || 'Bluetooth LED Light', uuid); accessory.context.device = { uuid: peripheral.uuid, name: peripheral.advertisement.localName, }; new platformAccessory_1.LEDLightAccessory(this, accessory, peripheral); this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [ accessory, ]); this.log.debug('Registration OK!'); } this.connected = true; peripheral.disconnectAsync(); } }); } } exports.BluetoothLEDPlatform = BluetoothLEDPlatform;