Skip to content

Commit 19a762d

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 b1d13ac commit 19a762d

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 }
@@ -89,10 +94,20 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
8994
}
9095
}
9196

92-
func getValue() -> JSONValueType? {
97+
func jsonValue() -> JSONValueType? {
9398
return value?.jsonValueType
9499
}
95100

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

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

111131
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)