forked from hap-java/HAP-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatCharacteristic.java
More file actions
158 lines (144 loc) · 4.68 KB
/
FloatCharacteristic.java
File metadata and controls
158 lines (144 loc) · 4.68 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
147
148
149
150
151
152
153
154
155
156
157
158
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A characteristic that provides a Float value type.
*
* @author Andy Lintner
*/
public abstract class FloatCharacteristic extends BaseCharacteristic<Double> {
private static final Logger LOGGER = LoggerFactory.getLogger(FloatCharacteristic.class);
private final double minValue;
private final double maxValue;
private final double minStep;
private final String unit;
private final Optional<Supplier<CompletableFuture<Double>>> getter;
private final Optional<ExceptionalConsumer<Double>> 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 minStep the smallest supported step. Values will be rounded to a multiple of this.
* @param unit a description of the unit this characteristic supports.
* @param getter getter to retrieve the value
* @param setter setter to set value
* @param subscriber subscriber to subscribe to changes
* @param unsubscriber unsubscriber to unsubscribe from chnages
*/
public FloatCharacteristic(
String type,
String description,
double minValue,
double maxValue,
double minStep,
String unit,
Optional<Supplier<CompletableFuture<Double>>> getter,
Optional<ExceptionalConsumer<Double>> setter,
Optional<Consumer<HomekitCharacteristicChangeCallback>> subscriber,
Optional<Runnable> unsubscriber) {
super(
type,
"float",
description,
getter.isPresent(),
setter.isPresent(),
subscriber,
unsubscriber);
this.minValue = minValue;
this.maxValue = maxValue;
this.unit = unit;
this.getter = getter;
this.setter = setter;
this.minStep = minStep;
}
/** {@inheritDoc} */
@Override
protected CompletableFuture<JsonObjectBuilder> makeBuilder(int iid) {
return super.makeBuilder(iid)
.thenApply(
builder ->
builder
.add("minValue", minValue)
.add("maxValue", maxValue)
.add("minStep", minStep)
.add("unit", unit));
}
/** {@inheritDoc} */
@Override
protected Double convert(JsonValue jsonValue) {
return ((JsonNumber) jsonValue).doubleValue();
}
/**
* {@inheritDoc}. Calls the getDoubleValue method and applies rounding to the minStep supplied in
* the constructor.
*/
@Override
public final CompletableFuture<Double> getValue() {
if (!getter.isPresent()) {
return null;
}
double rounder = 1 / this.minStep;
return getter
.get()
.get()
.thenApply(d -> d == null ? null : Math.round(d * rounder) / rounder)
.thenApply(
d -> {
if (d != null) {
if (d < minValue) {
LOGGER.warn(
"Detected value out of range "
+ d
+ ". Returning min value instead. Characteristic "
+ this);
return minValue;
}
if (d > maxValue) {
LOGGER.warn(
"Detected value out of range "
+ d
+ ". Returning max value instead. Characteristic "
+ this);
return maxValue;
}
return d;
}
return null;
});
}
@Override
public void setValue(Double value) throws Exception {
setValue(value, null);
}
@Override
public void setValue(Double value, String username) throws Exception {
if (setter.isPresent()) setter.get().accept(value, username);
}
/** {@inheritDoc} */
@Override
public Double getDefault() {
return minValue;
}
public double getMinValue() {
return minValue;
}
public double getMaxValue() {
return maxValue;
}
public double getMinStep() {
return minStep;
}
}