diff options
| author | Li Zhineng <[email protected]> | 2025-07-22 09:37:10 +0800 |
|---|---|---|
| committer | Li Zhineng <[email protected]> | 2025-07-22 09:37:10 +0800 |
| commit | b45b70be6d9f9acf880f24abebdf032d1e8ea8f5 (patch) | |
| tree | b260073fc4130c17e5bc13f965eb05a336841cf3 | |
| parent | 391eeb229882870a1c2311c820cc910890b35d9d (diff) | |
| download | homebridge-airmx-b45b70be6d9f9acf880f24abebdf032d1e8ea8f5.tar.gz homebridge-airmx-b45b70be6d9f9acf880f24abebdf032d1e8ea8f5.zip | |
extract methods
| -rw-r--r-- | src/index.ts | 34 |
1 files changed, 19 insertions, 15 deletions
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<AccessoryContext>( - 'AIRMX Pro', - uuid - ) + const accessory = new this.api.platformAccessory<AccessoryContext>('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<AccessoryContext>): void { + this.log.info('Restoring existing accessory from cache:', accessory.context.device.id) + new AirmxProAccessory(this, accessory) + } + + private registerAccessory(accessory: PlatformAccessory<AccessoryContext>): 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 { |
