Skip to content

Commit acc172a

Browse files
committed
Add didChangeAccessoryList to DeviceDelegate.
1 parent 0655715 commit acc172a

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

Sources/HAP/Server/Configuration.swift

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extension Device {
4242
// Accessories must increment the config number after a firmware update.
4343
// This must have a range of 1-4294967295 and wrap to 1 when it overflows.
4444
// This value must persist across reboots, power cycles, etc.
45+
// The first time a Device is created, this number is incremented to 1
4546
internal var number: UInt32 = 0
4647

4748
// HAP Specification 2.6.1: Instance IDs

Sources/HAP/Server/Device.swift

+27-9
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ public class Device {
152152
// The first accessory must be aid 1
153153
accessories[0].aid = 1
154154

155-
addAccessories(accessories)
155+
addToAccessoryList(accessories)
156+
157+
// Write configuration data to persist updated aid's and notify listeners
158+
updatedConfiguration()
156159
}
157160

158161
private func persistConfig() {
@@ -187,7 +190,7 @@ public class Device {
187190
/// It is an error to try and add accessories with duplicate serial numbers.
188191
/// It is an error to try and add accessories to a non-bridge device.
189192
/// It is an error to try and increase the number of accessories above 99.
190-
public func addAccessories(_ newAccessories: [Accessory]) {
193+
private func addToAccessoryList(_ newAccessories: [Accessory]) {
191194
let totalNumberOfAccessories = accessories.count + newAccessories.count
192195
precondition(
193196
(isBridge && totalNumberOfAccessories <= 100) ||
@@ -230,12 +233,24 @@ public class Device {
230233
configuration.aidForAccessorySerialNumber[serialNumber] = accessory.aid
231234
}
232235
}
236+
}
237+
238+
/// Add an array of accessories to this bridge device, and notify changes
239+
///
240+
/// It is an error to try and add accessories with duplicate serial numbers.
241+
/// It is an error to try and add accessories to a non-bridge device.
242+
/// It is an error to try and increase the number of accessories above 99.
243+
public func addAccessories(_ newAccessories: [Accessory]) {
244+
245+
addToAccessoryList(newAccessories)
246+
247+
delegate?.didChangeAccessoryList()
233248

234249
// Write configuration data to persist updated aid's and notify listeners
235250
updatedConfiguration()
236251
}
237252

238-
/// When a configuration changes
253+
/// If a configuration has changed
239254
/// - update the configuration number
240255
/// - write the configuration to storage
241256
/// - notify interested parties of the change
@@ -244,13 +259,14 @@ public class Device {
244259
if newStableHash != configuration.stableHash {
245260
configuration.number = configuration.number &+ 1
246261
configuration.stableHash = newStableHash
247-
}
248-
if configuration.number < 1 {
249-
configuration.number = 1
250-
}
251262

252-
persistConfig()
253-
notifyConfigurationChange()
263+
if configuration.number < 1 {
264+
configuration.number = 1
265+
}
266+
267+
persistConfig()
268+
notifyConfigurationChange()
269+
}
254270
}
255271

256272
/// Generate uniqueness hash for device configuration, used to determine
@@ -288,6 +304,8 @@ public class Device {
288304
let serialNumber = accessory.serialNumber
289305
configuration.aidForAccessorySerialNumber.removeValue(forKey: serialNumber)
290306
}
307+
delegate?.didChangeAccessoryList()
308+
291309
// write configuration data to persist updated aid's
292310
updatedConfiguration()
293311
}

Sources/HAP/Server/DeviceDelegate.swift

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public protocol DeviceDelegate: class {
5151
///
5252
func didChangePairingState(from: PairingState, to: PairingState)
5353

54+
/// Tells the delegate that one or more Accessories were added or removed.
55+
///
56+
func didChangeAccessoryList()
57+
5458
/// Tells the delegate that the value of a characteristic has changed.
5559
///
5660
/// - Parameters:
@@ -83,6 +87,8 @@ public extension DeviceDelegate {
8387

8488
func didChangePairingState(from: PairingState, to: PairingState) { }
8589

90+
func didChangeAccessoryList() { }
91+
8692
func characteristic<T>(
8793
_ characteristic: GenericCharacteristic<T>,
8894
ofService: Service,

0 commit comments

Comments
 (0)