From b45b70be6d9f9acf880f24abebdf032d1e8ea8f5 Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Tue, 22 Jul 2025 09:37:10 +0800 Subject: extract methods --- src/index.ts | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'src/index.ts') diff --git a/src/index.ts b/src/index.ts index 5d5871c..01c1c3d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -97,6 +97,7 @@ class AirmxPlatform implements DynamicPlatformPlugin { this.api.on('didFinishLaunching', () => { this.registerDevices() + this.cleanUpObsolete() }) } @@ -111,33 +112,36 @@ class AirmxPlatform implements DynamicPlatformPlugin { const existingAccessory = this.accessories.get(uuid) if (existingAccessory) { - this.log.info('Restoring existing accessory from cache:', device.id) - new AirmxProAccessory(this, existingAccessory) + this.restoreAccessory(existingAccessory) } else { - this.log.info('Adding new accessory:', device.id) - const accessory = new this.api.platformAccessory( - 'AIRMX Pro', - uuid - ) + const accessory = new this.api.platformAccessory('AIRMX Pro', uuid) accessory.context.device = device - this.accessories.set(uuid, accessory) - new AirmxProAccessory(this, accessory) - this.api.registerPlatformAccessories(pluginIdentifier, platformName, [ - accessory - ]) + this.registerAccessory(accessory) } this.discoveredUuids.push(uuid) } + } + private cleanUpObsolete() { for (const [uuid, accessory] of this.accessories) { if (!this.discoveredUuids.includes(uuid)) { - this.api.unregisterPlatformAccessories(pluginIdentifier, platformName, [ - accessory - ]) + this.api.unregisterPlatformAccessories(pluginIdentifier, platformName, [accessory]) } } } + + private restoreAccessory(accessory: PlatformAccessory): void { + this.log.info('Restoring existing accessory from cache:', accessory.context.device.id) + new AirmxProAccessory(this, accessory) + } + + private registerAccessory(accessory: PlatformAccessory): void { + this.log.info('Adding new accessory:', accessory.context.device.id) + this.accessories.set(accessory.UUID, accessory) + new AirmxProAccessory(this, accessory) + this.api.registerPlatformAccessories(pluginIdentifier, platformName, [accessory]) + } } export class AirmxProAccessory { -- cgit v1.2.3