@drpollio/homebridge-reolink-camera-siren-and-light-nvr
Version:
Control alarm of Reolink cameras.
59 lines • 3.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReolinkSirenAndLightHomebridgePlatform = void 0;
const settings_1 = require("./settings");
const platformAccessory_1 = require("./platformAccessory");
class ReolinkSirenAndLightHomebridgePlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.accessories = [];
this.uuids = [];
this.log.debug('Finished initializing platform:', this.config.name);
// Validate that the config is properly formatted
if (!this.config || !this.config.cameras || !Array.isArray(this.config.cameras)) {
this.log.error('Invalid configuration: "cameras" is missing or not an array.');
return;
}
this.api.on('didFinishLaunching', () => {
this.log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
discoverDevices() {
for (const device of this.config.cameras) {
// Create a more unique identifier using both IP and name
const uniqueIdentifier = `${device.ip}-${device.name}`;
const uuid = this.api.hap.uuid.generate(uniqueIdentifier);
this.uuids.push(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.ReolinkSirenAndLightAccessory(this, existingAccessory);
}
else {
this.log.info('Adding new accessory:', device.name);
// Ensure displayName is set to a non-empty string
const displayName = device.name || 'Unnamed Accessory';
const accessory = new this.api.platformAccessory(displayName, uuid);
accessory.context.device = device;
new platformAccessory_1.ReolinkSirenAndLightAccessory(this, accessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
}
// Remove orphaned accessories not found in current config
for (const deviceOrphan of this.accessories.filter(x => !this.uuids.includes(x.UUID))) {
this.log.info('Removing orphan accessory:', deviceOrphan.displayName);
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [deviceOrphan]);
}
}
}
exports.ReolinkSirenAndLightHomebridgePlatform = ReolinkSirenAndLightHomebridgePlatform;
//# sourceMappingURL=platform.js.map