Skip to content

Commit

Permalink
Add onGetValue callback to GenericCharacteristic for lazy queries
Browse files Browse the repository at this point in the history
Separate functions to obtain the value of a characteristic for a HAP server and to just get a jason formatted value.
  • Loading branch information
gbrooker committed Mar 26, 2018
1 parent 517ab96 commit bd23e11
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
26 changes: 23 additions & 3 deletions Sources/HAP/Base/Characteristic.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation

#if os(Linux)
import Dispatch
#endif

public struct AnyCharacteristic {
let wrapped: Characteristic

Expand All @@ -17,7 +21,8 @@ protocol Characteristic: class, JSONSerializable {
var iid: InstanceID { get set }
var type: CharacteristicType { get }
var permissions: [CharacteristicPermission] { get }
func getValue() -> JSONValueType?
func jsonValue() -> JSONValueType?
func getValue(fromConnection: Server.Connection?) -> JSONValueType?
func setValue(_: Any?, fromConnection: Server.Connection?) throws
var description: String? { get }
var format: CharacteristicFormat? { get }
Expand All @@ -38,7 +43,7 @@ extension Characteristic {

if permissions.contains(.read) {
// TODO: fixit
serialized["value"] = getValue() ?? 0 //NSNull()
serialized["value"] = jsonValue() ?? 0 //NSNull()
}

if let description = description { serialized["description"] = description }
Expand Down Expand Up @@ -88,10 +93,20 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
}
}

func getValue() -> JSONValueType? {
func jsonValue() -> JSONValueType? {
return value?.jsonValueType
}

// Get Value for HAP controller
func getValue(fromConnection connection: Server.Connection?) -> JSONValueType? {
let currentValue = _value
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.onDidGetValue?(currentValue)
}
return jsonValue()
}

// Set Value by HAP controller
func setValue(_ newValue: Any?, fromConnection connection: Server.Connection?) throws {
switch newValue {
case let some?:
Expand All @@ -105,6 +120,11 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
service?.characteristic(self, didChangeValue: _value)
}

// Subscribe a listener to value requests from (remote) HAP controllers.
// Called asynchronously on a global queue with the current value.
// Only a single listener is permitted.
public var onDidGetValue: ((T?) -> Void)?

public let permissions: [CharacteristicPermission]

public var description: String?
Expand Down
2 changes: 1 addition & 1 deletion Sources/HAP/Endpoints/characteristics().swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func characteristics(device: Device) -> Application {
}

var value: Protocol.Value?
switch characteristic.getValue() {
switch characteristic.getValue(fromConnection: connection) {
case let _value as Double: value = .double(_value)
case let _value as Float: value = .double(Double(_value))
case let _value as Int: value = .int(_value)
Expand Down
2 changes: 1 addition & 1 deletion Sources/HAP/Utils/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct Event {
guard let aid = c.service?.accessory?.aid else {
throw Error.characteristicWithoutAccessory
}
payload.append(["aid": aid, "iid": c.iid, "value": c.getValue() ?? NSNull()])
payload.append(["aid": aid, "iid": c.iid, "value": c.jsonValue() ?? NSNull()])
}
let serialized = ["characteristics": payload]
guard let body = try? JSONSerialization.data(withJSONObject: serialized, options: []) else {
Expand Down

0 comments on commit bd23e11

Please sign in to comment.