Skip to content

Commit

Permalink
Add didChangeAccessoryList to DeviceDelegate.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrooker committed Feb 23, 2019
1 parent 0655715 commit acc172a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions Sources/HAP/Server/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 27 additions & 9 deletions Sources/HAP/Server/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/HAP/Server/DeviceDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -83,6 +87,8 @@ public extension DeviceDelegate {

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

func didChangeAccessoryList() { }

func characteristic<T>(
_ characteristic: GenericCharacteristic<T>,
ofService: Service,
Expand Down

0 comments on commit acc172a

Please sign in to comment.