diff --git a/Sources/HAP/Server/Configuration.swift b/Sources/HAP/Server/Configuration.swift index 5d0cc8a4..72c2c0ed 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 0949ff2e..60f717d8 100644 --- a/Sources/HAP/Server/Device.swift +++ b/Sources/HAP/Server/Device.swift @@ -153,7 +153,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() { @@ -188,7 +191,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) || @@ -231,12 +234,25 @@ 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?.didAdd(accessories: 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 @@ -245,13 +261,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 @@ -292,6 +309,9 @@ public class Device { configuration.aidForAccessorySerialNumber.removeValue(forKey: serialNumber) } } + delegate?.didRemove(accessories: unwantedAccessories) + 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..121f6f7f 100644 --- a/Sources/HAP/Server/DeviceDelegate.swift +++ b/Sources/HAP/Server/DeviceDelegate.swift @@ -51,6 +51,18 @@ 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 one or more Accessories were added. + /// + func didAdd(accessories: [Accessory]) + + /// Tells the delegate that one or more Accessories were removed. + /// + func didRemove(accessories: [Accessory]) + /// Tells the delegate that the value of a characteristic has changed. /// /// - Parameters: @@ -59,6 +71,7 @@ public protocol DeviceDelegate: class { /// - service: the service to which the characteristic belongs /// - characteristic: the characteristic that was changed /// - newValue: the new value of the characteristic + /// func characteristic( _ characteristic: GenericCharacteristic, ofService: Service, @@ -83,6 +96,12 @@ public extension DeviceDelegate { func didChangePairingState(from: PairingState, to: PairingState) { } + func didChangeAccessoryList() { } + + func didAdd(accessories: [Accessory]) { } + + func didRemove(accessories: [Accessory]) { } + func characteristic( _ characteristic: GenericCharacteristic, ofService: Service,