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 }
@@ -88,10 +93,20 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
88
93
}
89
94
}
90
95
91
- func getValue ( ) -> JSONValueType ? {
96
+ func jsonValue ( ) -> JSONValueType ? {
92
97
return value? . jsonValueType
93
98
}
94
99
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
95
110
func setValue( _ newValue: Any ? , fromConnection connection: Server . Connection ? ) throws {
96
111
switch newValue {
97
112
case let some? :
@@ -105,6 +120,11 @@ public class GenericCharacteristic<T: CharacteristicValueType>: Characteristic,
105
120
service? . characteristic ( self , didChangeValue: _value)
106
121
}
107
122
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
+
108
128
public let permissions : [ CharacteristicPermission ]
109
129
110
130
public var description : String ?
0 commit comments