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 1d2d55c5b..38d1d8e6d 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 @@ -183,14 +183,14 @@ public void unsubscribe() { * @param value the new value to set. * @throws Exception if the value cannot be set. */ - protected abstract void setValue(T value) throws Exception; + public abstract void setValue(T value) throws Exception; /** * Retrieves the current value of the characteristic. * * @return a future that will complete with the current value. */ - protected abstract CompletableFuture getValue(); + public abstract CompletableFuture getValue(); /** * Supplies a default value for the characteristic to send to connected clients when the real 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 3e07936a8..4b484eb85 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 @@ -60,12 +60,12 @@ protected Boolean convert(JsonValue jsonValue) { } @Override - protected CompletableFuture getValue() { + public CompletableFuture getValue() { return getter.isPresent() ? getter.map(booleanGetter -> booleanGetter.get()).get() : null; } @Override - protected void setValue(Boolean value) throws Exception { + public void setValue(Boolean value) throws Exception { if (setter.isPresent()) setter.get().accept(value); } 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 df3287de5..65e82747f 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 @@ -84,16 +84,35 @@ protected Integer convert(JsonValue jsonValue) { } } + /** + * @return the current value of this characteristic, or null if it has no value or can't be + * fetched + */ + public CompletableFuture getEnumValue() { + if (!getter.isPresent()) { + return null; + } + return getter.get().get(); + } + @Override - protected CompletableFuture getValue() { + public CompletableFuture getValue() { if (!getter.isPresent()) { return null; } return getter.get().get().thenApply(T::getCode); } + public void setValue(T value) throws Exception { + if (!setter.isPresent()) { + return; + } + + setter.get().accept(value); + } + @Override - protected void setValue(Integer value) throws Exception { + public void setValue(Integer value) throws Exception { if (!setter.isPresent()) { return; } @@ -102,7 +121,7 @@ protected void setValue(Integer value) throws Exception { if (validValues != null && value != null) { for (T valid : validValues) { if (valid.getCode() == value) { - setter.get().accept(valid); + setValue(valid); return; } } 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 a8a98dc27..f504322a0 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 @@ -94,7 +94,7 @@ protected Double convert(JsonValue jsonValue) { * the constructor. */ @Override - protected final CompletableFuture getValue() { + public final CompletableFuture getValue() { if (!getter.isPresent()) { return null; } @@ -129,7 +129,7 @@ protected final CompletableFuture getValue() { } @Override - protected void setValue(Double value) throws Exception { + public void setValue(Double value) throws Exception { if (setter.isPresent()) setter.get().accept(value); } 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 22a6dce83..6d1b7df6d 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 @@ -71,12 +71,12 @@ protected CompletableFuture makeBuilder(int iid) { } @Override - protected CompletableFuture getValue() { + public CompletableFuture getValue() { return getter.map(integerGetter -> integerGetter.get()).orElse(null); } @Override - protected void setValue(Integer value) throws Exception { + public void setValue(Integer value) throws Exception { if (setter.isPresent()) setter.get().accept(value); } 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 576df7e0e..fc5961d4d 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 @@ -59,7 +59,7 @@ public void setValue(String value) throws Exception { /** {@inheritDoc} */ @Override - protected CompletableFuture getValue() { + public CompletableFuture getValue() { return getter.map(stringGetter -> stringGetter.get()).orElse(null); } 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 65cf45a71..53196efb9 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 @@ -68,7 +68,7 @@ public void setValue(String value) throws Exception { /** {@inheritDoc} */ @Override - protected CompletableFuture getValue() { + public CompletableFuture getValue() { return getter.map(stringGetter -> stringGetter.get()).orElse(null); }