Repository navigation
Expand file tree
/
Copy pathIntegerCharacteristic.java
More file actions
146 lines (135 loc) · 4.63 KB
/
Copy pathIntegerCharacteristic.java
File metadata and controls
146 lines (135 loc) · 4.63 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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.JsonNumber;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
/**
* A characteristic that provides an Integer data type.
*
* @author Andy Lintner
*/
public abstract class IntegerCharacteristic extends BaseCharacteristic<Integer> {
private final int minValue;
private final int maxValue;
private final String unit;
private final Optional<Supplier<CompletableFuture<Integer>>> getter;
private final Optional<ExceptionalConsumer<Integer>> 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 minValue the minimum supported value.
* @param maxValue the maximum supported value
* @param unit a description of the unit this characteristic supports.
* @param getter getter for value
* @param setter setter for value
* @param subscriber subscribers to changes
* @param unsubscriber unsubscribers to changes
*/
public IntegerCharacteristic(
String type,
String description,
int minValue,
int maxValue,
String unit,
Optional<Supplier<CompletableFuture<Integer>>> getter,
Optional<ExceptionalConsumer<Integer>> setter,
Optional<Consumer<HomekitCharacteristicChangeCallback>> subscriber,
Optional<Runnable> unsubscriber) {
this(
type,
"int",
description,
minValue,
maxValue,
unit,
getter,
setter,
subscriber,
unsubscriber);
}
/**
* Constructor for characteristics whose HAP format differs from the default "int", e.g. "uint8",
* "uint16" or "uint32". Values are still handled as Java {@link Integer}, so the supported range
* must fit into a signed 32-bit integer.
*
* @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 format the HAP format to advertise, e.g. "int" or "uint32".
* @param description a description of the characteristic to be passed to the consuming device.
* @param minValue the minimum supported value.
* @param maxValue the maximum supported value
* @param unit a description of the unit this characteristic supports.
* @param getter getter for value
* @param setter setter for value
* @param subscriber subscribers to changes
* @param unsubscriber unsubscribers to changes
*/
protected IntegerCharacteristic(
String type,
String format,
String description,
int minValue,
int maxValue,
String unit,
Optional<Supplier<CompletableFuture<Integer>>> getter,
Optional<ExceptionalConsumer<Integer>> setter,
Optional<Consumer<HomekitCharacteristicChangeCallback>> subscriber,
Optional<Runnable> unsubscriber) {
super(
type,
format,
description,
getter.isPresent(),
setter.isPresent(),
subscriber,
unsubscriber);
this.minValue = minValue;
this.maxValue = maxValue;
this.unit = unit;
this.getter = getter;
this.setter = setter;
}
/** {@inheritDoc} */
@Override
protected CompletableFuture<JsonObjectBuilder> makeBuilder(int iid) {
return super.makeBuilder(iid)
.thenApply(
builder -> {
builder.add("minValue", minValue).add("maxValue", maxValue).add("minStep", 1);
if (this.unit != null) {
builder.add("unit", unit);
}
return builder;
});
}
@Override
public CompletableFuture<Integer> getValue() {
return getter.map(integerGetter -> integerGetter.get()).orElse(null);
}
@Override
public void setValue(Integer value) throws Exception {
setValue(value, null);
}
@Override
public void setValue(Integer value, String username) throws Exception {
if (setter.isPresent()) setter.get().accept(value, username);
}
/** {@inheritDoc} */
@Override
public Integer getDefault() {
return minValue;
}
/** {@inheritDoc} */
@Override
protected Integer convert(JsonValue jsonValue) {
return ((JsonNumber) jsonValue).intValue();
}
}