Skip to content

Commit 324e80b

Browse files
authored
Merge pull request #167 from ccutrer/stability
Stability
2 parents b71a40b + daa07ee commit 324e80b

10 files changed

+43
-20
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# HAP-Java 2.0.2
2+
* Various minor stability issues for after an accessory has been removed.
3+
14
# HAP-Java 2.0.1
25
## Fixes
36
* Log accessory names instead of futures. [#150](https://github.com/hap-java/HAP-Java/issues/150)

src/main/java/io/github/hapjava/characteristics/impl/base/BaseCharacteristic.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,26 @@ public final void setValue(JsonValue jsonValue) {
125125
try {
126126
setValue(convert(jsonValue));
127127
} catch (Exception e) {
128-
logger.warn("Error while setting JSON value", e);
128+
logger.warn(
129+
"Error while setting JSON value {} for characteristic {}",
130+
jsonValue,
131+
getClass().getName(),
132+
e);
129133
}
130134
}
131135

132136
/** {@inheritDoc} */
133137
@Override
134138
public void supplyValue(JsonObjectBuilder builder) {
139+
CompletableFuture<T> futureValue = getValue();
140+
141+
if (futureValue == null) {
142+
setJsonValue(builder, getDefault());
143+
return;
144+
}
145+
135146
try {
136-
setJsonValue(builder, getValue().get());
147+
setJsonValue(builder, futureValue.get());
137148
} catch (InterruptedException | ExecutionException e) {
138149
logger.warn("Error retrieving value", e);
139150
setJsonValue(builder, getDefault());
@@ -143,13 +154,13 @@ public void supplyValue(JsonObjectBuilder builder) {
143154
/** {@inheritDoc} */
144155
@Override
145156
public void subscribe(HomekitCharacteristicChangeCallback callback) {
146-
subscriber.get().accept(callback);
157+
subscriber.ifPresent(s -> s.accept(callback));
147158
}
148159

149160
/** {@inheritDoc} */
150161
@Override
151162
public void unsubscribe() {
152-
unsubscriber.get().run();
163+
unsubscriber.ifPresent(u -> u.run());
153164
}
154165

155166
/**
@@ -181,7 +192,7 @@ public void unsubscribe() {
181192
*
182193
* @return a sensible default value.
183194
*/
184-
protected abstract T getDefault();
195+
public abstract T getDefault();
185196

186197
/**
187198
* Writes the value key to the serialized characteristic

src/main/java/io/github/hapjava/characteristics/impl/base/BooleanCharacteristic.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected void setValue(Boolean value) throws Exception {
7171

7272
/** {@inheritDoc} */
7373
@Override
74-
protected Boolean getDefault() {
74+
public Boolean getDefault() {
7575
return false;
7676
}
7777
}

src/main/java/io/github/hapjava/characteristics/impl/base/EnumCharacteristic.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected void setValue(Integer value) throws Exception {
111111

112112
/** {@inheritDoc} */
113113
@Override
114-
protected Integer getDefault() {
114+
public Integer getDefault() {
115115
// as default return first item from valid values
116116
if (validValues != null && validValues.length > 0) {
117117
return validValues[0].getCode();

src/main/java/io/github/hapjava/characteristics/impl/base/FloatCharacteristic.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ protected final CompletableFuture<Double> getValue() {
130130

131131
@Override
132132
protected void setValue(Double value) throws Exception {
133-
setter.get().accept(value);
133+
if (setter.isPresent()) setter.get().accept(value);
134134
}
135135

136136
/** {@inheritDoc} */
137137
@Override
138-
protected Double getDefault() {
138+
public Double getDefault() {
139139
return minValue;
140140
}
141141
}

src/main/java/io/github/hapjava/characteristics/impl/base/IntegerCharacteristic.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ protected CompletableFuture<Integer> getValue() {
7777

7878
@Override
7979
protected void setValue(Integer value) throws Exception {
80-
setter.get().accept(value);
80+
if (setter.isPresent()) setter.get().accept(value);
8181
}
8282

8383
/** {@inheritDoc} */
8484
@Override
85-
protected Integer getDefault() {
85+
public Integer getDefault() {
8686
return minValue;
8787
}
8888

src/main/java/io/github/hapjava/characteristics/impl/base/StaticStringCharacteristic.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public void setValue(String value) throws Exception {
6060
/** {@inheritDoc} */
6161
@Override
6262
protected CompletableFuture<String> getValue() {
63-
return getter.map(stringGetter -> stringGetter.get()).get();
63+
return getter.map(stringGetter -> stringGetter.get()).orElse(null);
6464
}
6565

6666
/** {@inheritDoc} */
6767
@Override
68-
protected String getDefault() {
68+
public String getDefault() {
6969
return "Unknown";
7070
}
7171
}

src/main/java/io/github/hapjava/characteristics/impl/base/StringCharacteristic.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ public String convert(JsonValue jsonValue) {
6363
/** {@inheritDoc} */
6464
@Override
6565
public void setValue(String value) throws Exception {
66-
setter.get().accept(value);
66+
if (setter.isPresent()) setter.get().accept(value);
6767
}
6868

6969
/** {@inheritDoc} */
7070
@Override
7171
protected CompletableFuture<String> getValue() {
72-
return getter.map(stringGetter -> stringGetter.get()).get();
72+
return getter.map(stringGetter -> stringGetter.get()).orElse(null);
7373
}
7474

7575
/** {@inheritDoc} */
7676
@Override
77-
protected String getDefault() {
77+
public String getDefault() {
7878
return "Unknown";
7979
}
8080
}

src/main/java/io/github/hapjava/characteristics/impl/lightbulb/ColorTemperatureCharacteristic.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.function.Consumer;
1010
import java.util.function.Supplier;
1111

12-
/** This characteristic describes color temperature in Kelvin */
12+
/** This characteristic describes color temperature in mireds */
1313
public class ColorTemperatureCharacteristic extends IntegerCharacteristic
1414
implements EventableCharacteristic {
1515
public static final int DEFAULT_MIN_VALUE = 50;
@@ -27,7 +27,7 @@ public ColorTemperatureCharacteristic(
2727
"color temperature",
2828
minValue,
2929
maxValue,
30-
"K",
30+
"mired",
3131
Optional.of(getter),
3232
Optional.of(setter),
3333
Optional.of(subscriber),

src/main/java/io/github/hapjava/server/impl/json/CharacteristicsController.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public HttpResponse get(HttpRequest request) throws Exception {
5151
characteristics.add(characteristic.add("aid", aid).add("iid", iid).build());
5252
} else {
5353
logger.warn(
54-
"Accessory " + aid + " does not have characteristic " + iid + "Request: " + uri);
54+
"Accessory " + aid + " does not have characteristic " + iid + ". Request: " + uri);
5555
}
5656
} else {
5757
logger.warn(
@@ -77,7 +77,16 @@ public HttpResponse put(HttpRequest request, HomekitClientConnection connection)
7777
JsonObject jsonCharacteristic = (JsonObject) value;
7878
int aid = jsonCharacteristic.getInt("aid");
7979
int iid = jsonCharacteristic.getInt("iid");
80-
Characteristic characteristic = registry.getCharacteristics(aid).get(iid);
80+
Map<Integer, Characteristic> accessory = registry.getCharacteristics(aid);
81+
if (accessory.isEmpty()) {
82+
logger.warn("Accessory {} has no characteristics or does not exist.", aid);
83+
return new HapJsonNoContentResponse();
84+
}
85+
Characteristic characteristic = accessory.get(iid);
86+
if (characteristic == null) {
87+
logger.warn("Accessory {} does not have characteristic {}.", aid, iid);
88+
return new HapJsonNoContentResponse();
89+
}
8190

8291
if (jsonCharacteristic.containsKey("value")) {
8392
characteristic.setValue(jsonCharacteristic.get("value"));

0 commit comments

Comments
 (0)