Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for accessory configuration on restart #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 29 additions & 9 deletions Sources/HAP/Server/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/HAP/Server/DeviceDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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<T>(
_ characteristic: GenericCharacteristic<T>,
ofService: Service,
Expand All @@ -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<T>(
_ characteristic: GenericCharacteristic<T>,
ofService: Service,
Expand Down