diff --git a/CHANGES.md b/CHANGES.md index 400e99ae2..debc2dfbf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +# HAP-Java 2.0.2 + * Various minor stability issues for after an accessory has been removed. + # HAP-Java 2.0.1 ## Fixes * Log accessory names instead of futures. [#150](https://github.com/hap-java/HAP-Java/issues/150) diff --git a/src/main/java/io/github/hapjava/characteristics/impl/base/BaseCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/base/BaseCharacteristic.java index a90f1fadc..77b0bc452 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/base/BaseCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/base/BaseCharacteristic.java @@ -125,15 +125,26 @@ public final void setValue(JsonValue jsonValue) { try { setValue(convert(jsonValue)); } catch (Exception e) { - logger.warn("Error while setting JSON value", e); + logger.warn( + "Error while setting JSON value {} for characteristic {}", + jsonValue, + getClass().getName(), + e); } } /** {@inheritDoc} */ @Override public void supplyValue(JsonObjectBuilder builder) { + CompletableFuture futureValue = getValue(); + + if (futureValue == null) { + setJsonValue(builder, getDefault()); + return; + } + try { - setJsonValue(builder, getValue().get()); + setJsonValue(builder, futureValue.get()); } catch (InterruptedException | ExecutionException e) { logger.warn("Error retrieving value", e); setJsonValue(builder, getDefault()); @@ -143,13 +154,13 @@ public void supplyValue(JsonObjectBuilder builder) { /** {@inheritDoc} */ @Override public void subscribe(HomekitCharacteristicChangeCallback callback) { - subscriber.get().accept(callback); + subscriber.ifPresent(s -> s.accept(callback)); } /** {@inheritDoc} */ @Override public void unsubscribe() { - unsubscriber.get().run(); + unsubscriber.ifPresent(u -> u.run()); } /** @@ -181,7 +192,7 @@ public void unsubscribe() { * * @return a sensible default value. */ - protected abstract T getDefault(); + public abstract T getDefault(); /** * Writes the value key to the serialized characteristic diff --git a/src/main/java/io/github/hapjava/characteristics/impl/base/BooleanCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/base/BooleanCharacteristic.java index 83d7f765a..3e07936a8 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/base/BooleanCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/base/BooleanCharacteristic.java @@ -71,7 +71,7 @@ protected void setValue(Boolean value) throws Exception { /** {@inheritDoc} */ @Override - protected Boolean getDefault() { + public Boolean getDefault() { return false; } } diff --git a/src/main/java/io/github/hapjava/characteristics/impl/base/EnumCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/base/EnumCharacteristic.java index f983efe16..df3287de5 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/base/EnumCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/base/EnumCharacteristic.java @@ -111,7 +111,7 @@ protected void setValue(Integer value) throws Exception { /** {@inheritDoc} */ @Override - protected Integer getDefault() { + public Integer getDefault() { // as default return first item from valid values if (validValues != null && validValues.length > 0) { return validValues[0].getCode(); diff --git a/src/main/java/io/github/hapjava/characteristics/impl/base/FloatCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/base/FloatCharacteristic.java index e15da4fde..a8a98dc27 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/base/FloatCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/base/FloatCharacteristic.java @@ -130,12 +130,12 @@ protected final CompletableFuture getValue() { @Override protected void setValue(Double value) throws Exception { - setter.get().accept(value); + if (setter.isPresent()) setter.get().accept(value); } /** {@inheritDoc} */ @Override - protected Double getDefault() { + public Double getDefault() { return minValue; } } diff --git a/src/main/java/io/github/hapjava/characteristics/impl/base/IntegerCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/base/IntegerCharacteristic.java index fcd804ccd..22a6dce83 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/base/IntegerCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/base/IntegerCharacteristic.java @@ -77,12 +77,12 @@ protected CompletableFuture getValue() { @Override protected void setValue(Integer value) throws Exception { - setter.get().accept(value); + if (setter.isPresent()) setter.get().accept(value); } /** {@inheritDoc} */ @Override - protected Integer getDefault() { + public Integer getDefault() { return minValue; } diff --git a/src/main/java/io/github/hapjava/characteristics/impl/base/StaticStringCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/base/StaticStringCharacteristic.java index d5de2f9ce..576df7e0e 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/base/StaticStringCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/base/StaticStringCharacteristic.java @@ -60,12 +60,12 @@ public void setValue(String value) throws Exception { /** {@inheritDoc} */ @Override protected CompletableFuture getValue() { - return getter.map(stringGetter -> stringGetter.get()).get(); + return getter.map(stringGetter -> stringGetter.get()).orElse(null); } /** {@inheritDoc} */ @Override - protected String getDefault() { + public String getDefault() { return "Unknown"; } } diff --git a/src/main/java/io/github/hapjava/characteristics/impl/base/StringCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/base/StringCharacteristic.java index 4373b06c9..65cf45a71 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/base/StringCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/base/StringCharacteristic.java @@ -63,18 +63,18 @@ public String convert(JsonValue jsonValue) { /** {@inheritDoc} */ @Override public void setValue(String value) throws Exception { - setter.get().accept(value); + if (setter.isPresent()) setter.get().accept(value); } /** {@inheritDoc} */ @Override protected CompletableFuture getValue() { - return getter.map(stringGetter -> stringGetter.get()).get(); + return getter.map(stringGetter -> stringGetter.get()).orElse(null); } /** {@inheritDoc} */ @Override - protected String getDefault() { + public String getDefault() { return "Unknown"; } } diff --git a/src/main/java/io/github/hapjava/characteristics/impl/lightbulb/ColorTemperatureCharacteristic.java b/src/main/java/io/github/hapjava/characteristics/impl/lightbulb/ColorTemperatureCharacteristic.java index 12886c7a1..987ab5eee 100644 --- a/src/main/java/io/github/hapjava/characteristics/impl/lightbulb/ColorTemperatureCharacteristic.java +++ b/src/main/java/io/github/hapjava/characteristics/impl/lightbulb/ColorTemperatureCharacteristic.java @@ -9,7 +9,7 @@ import java.util.function.Consumer; import java.util.function.Supplier; -/** This characteristic describes color temperature in Kelvin */ +/** This characteristic describes color temperature in mireds */ public class ColorTemperatureCharacteristic extends IntegerCharacteristic implements EventableCharacteristic { public static final int DEFAULT_MIN_VALUE = 50; @@ -27,7 +27,7 @@ public ColorTemperatureCharacteristic( "color temperature", minValue, maxValue, - "K", + "mired", Optional.of(getter), Optional.of(setter), Optional.of(subscriber), diff --git a/src/main/java/io/github/hapjava/server/impl/json/CharacteristicsController.java b/src/main/java/io/github/hapjava/server/impl/json/CharacteristicsController.java index 4d20a0c6b..d061a11fc 100644 --- a/src/main/java/io/github/hapjava/server/impl/json/CharacteristicsController.java +++ b/src/main/java/io/github/hapjava/server/impl/json/CharacteristicsController.java @@ -51,7 +51,7 @@ public HttpResponse get(HttpRequest request) throws Exception { characteristics.add(characteristic.add("aid", aid).add("iid", iid).build()); } else { logger.warn( - "Accessory " + aid + " does not have characteristic " + iid + "Request: " + uri); + "Accessory " + aid + " does not have characteristic " + iid + ". Request: " + uri); } } else { logger.warn( @@ -77,7 +77,16 @@ public HttpResponse put(HttpRequest request, HomekitClientConnection connection) JsonObject jsonCharacteristic = (JsonObject) value; int aid = jsonCharacteristic.getInt("aid"); int iid = jsonCharacteristic.getInt("iid"); - Characteristic characteristic = registry.getCharacteristics(aid).get(iid); + Map accessory = registry.getCharacteristics(aid); + if (accessory.isEmpty()) { + logger.warn("Accessory {} has no characteristics or does not exist.", aid); + return new HapJsonNoContentResponse(); + } + Characteristic characteristic = accessory.get(iid); + if (characteristic == null) { + logger.warn("Accessory {} does not have characteristic {}.", aid, iid); + return new HapJsonNoContentResponse(); + } if (jsonCharacteristic.containsKey("value")) { characteristic.setValue(jsonCharacteristic.get("value"));