Skip to content

Commit bd23e11

Browse files
committed
Add onGetValue callback to GenericCharacteristic for lazy queries
Separate functions to obtain the value of a characteristic for a HAP server and to just get a jason formatted value.
1 parent 517ab96 commit bd23e11

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Sources/HAP/Base/Characteristic.swift

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import Foundation
22

3+
#if os(Linux)
4+
import Dispatch
5+
#endif
6+
37
public struct AnyCharacteristic {
48
let wrapped: Characteristic
59

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

3944
if permissions.contains(.read) {
4045
// TODO: fixit
41-
serialized["value"] = getValue() ?? 0 //NSNull()
46+
serialized["value"] = jsonValue() ?? 0 //NSNull()
4247
}
4348

4449
if let description = description { serialized["description"] = description }
@@ -88,10 +93,20 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
8893
}
8994
}
9095

91-
func getValue() -> JSONValueType? {
96+
func jsonValue() -> JSONValueType? {
9297
return value?.jsonValueType
9398
}
9499

100+
// Get Value for HAP controller
101+
func getValue(fromConnection connection: Server.Connection?) -> JSONValueType? {
102+
let currentValue = _value
103+
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
104+
self?.onDidGetValue?(currentValue)
105+
}
106+
return jsonValue()
107+
}
108+
109+
// Set Value by HAP controller
95110
func setValue(_ newValue: Any?, fromConnection connection: Server.Connection?) throws {
96111
switch newValue {
97112
case let some?:
@@ -105,6 +120,11 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
105120
service?.characteristic(self, didChangeValue: _value)
106121
}
107122

123+
// Subscribe a listener to value requests from (remote) HAP controllers.
124+
// Called asynchronously on a global queue with the current value.
125+
// Only a single listener is permitted.
126+
public var onDidGetValue: ((T?) -> Void)?
127+
108128
public let permissions: [CharacteristicPermission]
109129

110130
public var description: String?

Sources/HAP/Endpoints/characteristics().swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func characteristics(device: Device) -> Application {
4444
}
4545

4646
var value: Protocol.Value?
47-
switch characteristic.getValue() {
47+
switch characteristic.getValue(fromConnection: connection) {
4848
case let _value as Double: value = .double(_value)
4949
case let _value as Float: value = .double(Double(_value))
5050
case let _value as Int: value = .int(_value)

Sources/HAP/Utils/Event.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct Event {
6262
guard let aid = c.service?.accessory?.aid else {
6363
throw Error.characteristicWithoutAccessory
6464
}
65-
payload.append(["aid": aid, "iid": c.iid, "value": c.getValue() ?? NSNull()])
65+
payload.append(["aid": aid, "iid": c.iid, "value": c.jsonValue() ?? NSNull()])
6666
}
6767
let serialized = ["characteristics": payload]
6868
guard let body = try? JSONSerialization.data(withJSONObject: serialized, options: []) else {

0 commit comments

Comments
 (0)