Skip to content

Commit

Permalink
Fix for accessory configuration on restart. Add didChangeAccessoryLis…
Browse files Browse the repository at this point in the history
…t to DeviceDelegate.
  • Loading branch information
gbrooker committed Nov 11, 2018
1 parent 60d7875 commit 5a13362
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/HAP/Server/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +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.
internal var number: UInt32 = 0
internal var number: UInt32 = 1

// HAP Specification 2.6.1: Instance IDs
//
Expand Down
18 changes: 16 additions & 2 deletions Sources/HAP/Server/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public class Device {
// The first accessory must be aid 1
accessories[0].aid = 1

addAccessories(accessories)
addToAccessoryList(accessories)
}

private func persistConfig() {
Expand Down Expand Up @@ -185,7 +185,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 @@ -228,6 +228,18 @@ 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()
Expand Down Expand Up @@ -266,6 +278,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 5a13362

Please sign in to comment.