forked from hap-java/HAP-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCharacteristic.java
More file actions
86 lines (78 loc) · 2.55 KB
/
StringCharacteristic.java
File metadata and controls
86 lines (78 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package io.github.hapjava.characteristics.impl.base;
import io.github.hapjava.characteristics.ExceptionalConsumer;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.json.JsonObjectBuilder;
import javax.json.JsonString;
import javax.json.JsonValue;
/**
* A characteristic that provides an string value.
*
* @author Eugen Freiter
*/
public class StringCharacteristic extends BaseCharacteristic<String> {
private final Optional<Supplier<CompletableFuture<String>>> getter;
private final Optional<ExceptionalConsumer<String>> setter;
/**
* Default constructor
*
* @param type a string containing a UUID that indicates the type of characteristic. Apple defines
* a set of these, however implementors can create their own as well.
* @param description a description of the characteristic to be passed to the consuming device.
* @param getter getter to retrieve the value
* @param setter setter for value
* @param subscriber subscriber to subscribe to changes
* @param unsubscriber unsubscriber to unsubscribe from chnages
*/
public StringCharacteristic(
String type,
String description,
Optional<Supplier<CompletableFuture<String>>> getter,
Optional<ExceptionalConsumer<String>> setter,
Optional<Consumer<HomekitCharacteristicChangeCallback>> subscriber,
Optional<Runnable> unsubscriber) {
super(
type,
"string",
description,
getter.isPresent(),
setter.isPresent(),
subscriber,
unsubscriber);
this.getter = getter;
this.setter = setter;
}
/** {@inheritDoc} */
@Override
protected CompletableFuture<JsonObjectBuilder> makeBuilder(int iid) {
return super.makeBuilder(iid);
}
/** {@inheritDoc} */
@Override
public String convert(JsonValue jsonValue) {
return ((JsonString) jsonValue).getString();
}
/** {@inheritDoc} */
@Override
public void setValue(String value) throws Exception {
setValue(value, null);
}
/** {@inheritDoc} */
@Override
public void setValue(String value, String username) throws Exception {
if (setter.isPresent()) setter.get().accept(value, username);
}
/** {@inheritDoc} */
@Override
public CompletableFuture<String> getValue() {
return getter.map(stringGetter -> stringGetter.get()).orElse(null);
}
/** {@inheritDoc} */
@Override
public String getDefault() {
return "Unknown";
}
}