1
1
import Foundation
2
2
3
+ #if os(Linux)
4
+ import Dispatch
5
+ #endif
6
+
3
7
public struct AnyCharacteristic {
4
8
let wrapped : Characteristic
5
9
@@ -17,7 +21,8 @@ protocol Characteristic: class, JSONSerializable {
17
21
var iid : InstanceID { get set }
18
22
var type : CharacteristicType { get }
19
23
var permissions : [ CharacteristicPermission ] { get }
20
- func getValue( ) -> JSONValueType ?
24
+ func jsonValue( ) -> JSONValueType ?
25
+ func getValue( fromConnection: Server . Connection ? ) -> JSONValueType ?
21
26
func setValue( _: Any ? , fromConnection: Server . Connection ? ) throws
22
27
var description : String ? { get }
23
28
var format : CharacteristicFormat ? { get }
@@ -38,7 +43,7 @@ extension Characteristic {
38
43
39
44
if permissions. contains ( . read) {
40
45
// TODO: fixit
41
- serialized [ " value " ] = getValue ( ) ?? 0 //NSNull()
46
+ serialized [ " value " ] = jsonValue ( ) ?? 0 //NSNull()
42
47
}
43
48
44
49
if let description = description { serialized [ " description " ] = description }
@@ -89,10 +94,20 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
89
94
}
90
95
}
91
96
92
- func getValue ( ) -> JSONValueType ? {
97
+ func jsonValue ( ) -> JSONValueType ? {
93
98
return value? . jsonValueType
94
99
}
95
100
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
96
111
func setValue( _ newValue: Any ? , fromConnection connection: Server . Connection ? ) throws {
97
112
switch newValue {
98
113
case let some? :
@@ -106,6 +121,11 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
106
121
service? . characteristic ( self , didChangeValue: _value)
107
122
}
108
123
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
+
109
129
public let permissions : [ CharacteristicPermission ]
110
130
111
131
public var description : String ?
0 commit comments