Skip to content

Commit

Permalink
Merge pull request #167 from ccutrer/stability
Browse files Browse the repository at this point in the history
Stability
  • Loading branch information
yfre authored Oct 12, 2022
2 parents b71a40b + daa07ee commit 324e80b
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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());
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected void setValue(Boolean value) throws Exception {

/** {@inheritDoc} */
@Override
protected Boolean getDefault() {
public Boolean getDefault() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ protected final CompletableFuture<Double> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ protected CompletableFuture<Integer> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public void setValue(String value) throws Exception {
/** {@inheritDoc} */
@Override
protected CompletableFuture<String> 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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@ public ColorTemperatureCharacteristic(
"color temperature",
minValue,
maxValue,
"K",
"mired",
Optional.of(getter),
Optional.of(setter),
Optional.of(subscriber),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<Integer, Characteristic> 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"));
Expand Down

0 comments on commit 324e80b

Please sign in to comment.