From acc172a56f3e5a643f47ee869e59f685707609fd Mon Sep 17 00:00:00 2001 From: gbrooker Date: Sun, 11 Nov 2018 10:50:37 +0800 Subject: [PATCH] Add didChangeAccessoryList to DeviceDelegate. --- Sources/HAP/Server/Configuration.swift | 1 + Sources/HAP/Server/Device.swift | 36 ++++++++++++++++++------- Sources/HAP/Server/DeviceDelegate.swift | 6 +++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Sources/HAP/Server/Configuration.swift b/Sources/HAP/Server/Configuration.swift index 68791c31..00f921cc 100644 --- a/Sources/HAP/Server/Configuration.swift +++ b/Sources/HAP/Server/Configuration.swift @@ -42,6 +42,7 @@ extension Device { // Accessories must increment the config number after a firmware update. // This must have a range of 1-4294967295 and wrap to 1 when it overflows. // This value must persist across reboots, power cycles, etc. + // The first time a Device is created, this number is incremented to 1 internal var number: UInt32 = 0 // HAP Specification 2.6.1: Instance IDs diff --git a/Sources/HAP/Server/Device.swift b/Sources/HAP/Server/Device.swift index 593d93f1..26df5160 100644 --- a/Sources/HAP/Server/Device.swift +++ b/Sources/HAP/Server/Device.swift @@ -152,7 +152,10 @@ public class Device { // The first accessory must be aid 1 accessories[0].aid = 1 - addAccessories(accessories) + addToAccessoryList(accessories) + + // Write configuration data to persist updated aid's and notify listeners + updatedConfiguration() } private func persistConfig() { @@ -187,7 +190,7 @@ public class Device { /// It is an error to try and add accessories with duplicate serial numbers. /// It is an error to try and add accessories to a non-bridge device. /// It is an error to try and increase the number of accessories above 99. - public func addAccessories(_ newAccessories: [Accessory]) { + private func addToAccessoryList(_ newAccessories: [Accessory]) { let totalNumberOfAccessories = accessories.count + newAccessories.count precondition( (isBridge && totalNumberOfAccessories <= 100) || @@ -230,12 +233,24 @@ public class Device { configuration.aidForAccessorySerialNumber[serialNumber] = accessory.aid } } + } + + /// Add an array of accessories to this bridge device, and notify changes + /// + /// It is an error to try and add accessories with duplicate serial numbers. + /// It is an error to try and add accessories to a non-bridge device. + /// It is an error to try and increase the number of accessories above 99. + public func addAccessories(_ newAccessories: [Accessory]) { + + addToAccessoryList(newAccessories) + + delegate?.didChangeAccessoryList() // Write configuration data to persist updated aid's and notify listeners updatedConfiguration() } - /// When a configuration changes + /// If a configuration has changed /// - update the configuration number /// - write the configuration to storage /// - notify interested parties of the change @@ -244,13 +259,14 @@ public class Device { if newStableHash != configuration.stableHash { configuration.number = configuration.number &+ 1 configuration.stableHash = newStableHash - } - if configuration.number < 1 { - configuration.number = 1 - } - persistConfig() - notifyConfigurationChange() + if configuration.number < 1 { + configuration.number = 1 + } + + persistConfig() + notifyConfigurationChange() + } } /// Generate uniqueness hash for device configuration, used to determine @@ -288,6 +304,8 @@ public class Device { let serialNumber = accessory.serialNumber configuration.aidForAccessorySerialNumber.removeValue(forKey: serialNumber) } + delegate?.didChangeAccessoryList() + // write configuration data to persist updated aid's updatedConfiguration() } diff --git a/Sources/HAP/Server/DeviceDelegate.swift b/Sources/HAP/Server/DeviceDelegate.swift index a6d570dd..5e46d09c 100644 --- a/Sources/HAP/Server/DeviceDelegate.swift +++ b/Sources/HAP/Server/DeviceDelegate.swift @@ -51,6 +51,10 @@ public protocol DeviceDelegate: class { /// func didChangePairingState(from: PairingState, to: PairingState) + /// Tells the delegate that one or more Accessories were added or removed. + /// + func didChangeAccessoryList() + /// Tells the delegate that the value of a characteristic has changed. /// /// - Parameters: @@ -83,6 +87,8 @@ public extension DeviceDelegate { func didChangePairingState(from: PairingState, to: PairingState) { } + func didChangeAccessoryList() { } + func characteristic( _ characteristic: GenericCharacteristic, ofService: Service,