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.
93 lines (92 loc) • 4 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LEDLightAccessory = void 0;
const noble_1 = __importDefault(require("@abandonware/noble"));
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
class LEDLightAccessory {
platform;
accessory;
peripheral;
service;
characteristic;
connected = false;
peripheralInstance; // Use a separate variable to hold the peripheral reference
constructor(platform, accessory, peripheral) {
this.platform = platform;
this.accessory = accessory;
this.peripheral = peripheral;
// Set accessory information
this.accessory
.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Your Manufacturer')
.setCharacteristic(this.platform.Characteristic.Model, 'LED Light Model')
.setCharacteristic(this.platform.Characteristic.SerialNumber, peripheral.uuid);
// Get the LightBulb service, or create a new one if it doesn't exist
this.service =
this.accessory.getService(this.platform.Service.Lightbulb) ||
this.accessory.addService(this.platform.Service.Lightbulb);
// Set the service name (this is shown in the Home app)
this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.name);
// Register handlers for the On/Off Characteristic
this.service
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
// Start scanning for Bluetooth devices
noble_1.default.on('stateChange', async (state) => {
if (state === 'poweredOn') {
await noble_1.default.startScanningAsync([], false);
}
});
noble_1.default.on('discover', async (discoveredPeripheral) => {
if (discoveredPeripheral.uuid === this.accessory.context.device.uuid) {
await noble_1.default.stopScanningAsync();
this.peripheralInstance = discoveredPeripheral; // Store peripheral in a separate variable
discoveredPeripheral.disconnectAsync();
}
});
}
async connectToDevice() {
if (!this.peripheralInstance) {
await noble_1.default.startScanningAsync();
return;
}
await this.peripheralInstance.connectAsync();
this.connected = true;
// Discover services and characteristics
const { characteristics } = await this.peripheralInstance.discoverSomeServicesAndCharacteristicsAsync(['LED_SERVICE_UUID'], // Replace with actual service UUID
['LED_CHARACTERISTIC_UUID']);
this.characteristic = characteristics[0];
this.platform.log.debug('Connected to LED device');
}
async setOn(value) {
if (!this.connected) {
await this.connectToDevice();
}
if (!this.characteristic) {
return;
}
const data = Buffer.from(value ? '01' : '00', 'hex'); // Example on/off command
this.characteristic.write(data, true, (error) => {
if (error) {
this.platform.log.error('Error writing characteristic:', error);
}
else {
this.platform.log.debug(`Set Characteristic On -> ${value}`);
}
});
}
async getOn() {
const isOn = this.connected ? true : false; // Adjust logic as needed
this.platform.log.debug('Get Characteristic On ->', isOn);
return isOn;
}
}
exports.LEDLightAccessory = LEDLightAccessory;