Skip to content

Commit 5c989b5

Browse files
committed
Add didChangeAccessoryList to DeviceDelegate.
1 parent 83fbb3f commit 5c989b5

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-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
@@ -153,7 +153,10 @@ public class Device {
153153
// The first accessory must be aid 1
154154
accessories[0].aid = 1
155155

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

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

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

239-
/// When a configuration changes
254+
/// If a configuration has changed
240255
/// - update the configuration number
241256
/// - write the configuration to storage
242257
/// - notify interested parties of the change
@@ -245,13 +260,14 @@ public class Device {
245260
if newStableHash != configuration.stableHash {
246261
configuration.number = configuration.number &+ 1
247262
configuration.stableHash = newStableHash
248-
}
249-
if configuration.number < 1 {
250-
configuration.number = 1
251-
}
252263

253-
persistConfig()
254-
notifyConfigurationChange()
264+
if configuration.number < 1 {
265+
configuration.number = 1
266+
}
267+
268+
persistConfig()
269+
notifyConfigurationChange()
270+
}
255271
}
256272

257273
/// Generate uniqueness hash for device configuration, used to determine
@@ -292,6 +308,8 @@ public class Device {
292308
configuration.aidForAccessorySerialNumber.removeValue(forKey: serialNumber)
293309
}
294310
}
311+
delegate?.didChangeAccessoryList()
312+
295313
// write configuration data to persist updated aid's
296314
updatedConfiguration()
297315
}

Sources/HAP/Server/DeviceDelegate.swift

+7
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:
@@ -59,6 +63,7 @@ public protocol DeviceDelegate: class {
5963
/// - service: the service to which the characteristic belongs
6064
/// - characteristic: the characteristic that was changed
6165
/// - newValue: the new value of the characteristic
66+
///
6267
func characteristic<T>(
6368
_ characteristic: GenericCharacteristic<T>,
6469
ofService: Service,
@@ -83,6 +88,8 @@ public extension DeviceDelegate {
8388

8489
func didChangePairingState(from: PairingState, to: PairingState) { }
8590

91+
func didChangeAccessoryList() { }
92+
8693
func characteristic<T>(
8794
_ characteristic: GenericCharacteristic<T>,
8895
ofService: Service,

0 commit comments

Comments
 (0)