Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.beowulfe.hap.characteristics;

import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.json.JsonNumber;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import java.util.concurrent.CompletableFuture;

/**
* A characteristic that provides a Float value type.
Expand All @@ -13,6 +15,8 @@
*/
public abstract class FloatCharacteristic extends BaseCharacteristic<Double> {

private final static Logger LOGGER = LoggerFactory.getLogger(FloatCharacteristic.class);

private final double minValue;
private final double maxValue;
private final double minStep;
Expand Down Expand Up @@ -45,14 +49,11 @@ public FloatCharacteristic(String type, boolean isWritable, boolean isReadable,
*/
@Override
protected CompletableFuture<JsonObjectBuilder> makeBuilder(int iid) {
return super.makeBuilder(iid).thenApply(builder -> {
return builder
.add("minValue", minValue)
.add("maxValue", maxValue)
.add("minStep", minStep)
.add("unit", unit);
});

return super.makeBuilder(iid).thenApply(builder -> builder
.add("minValue", minValue)
.add("maxValue", maxValue)
.add("minStep", minStep)
.add("unit", unit));
}

/**
Expand All @@ -69,7 +70,23 @@ protected Double convert(JsonValue jsonValue) {
@Override
protected final CompletableFuture<Double> getValue() {
double rounder = 1 / this.minStep;
return getDoubleValue().thenApply(d -> d == null ? null : Math.round(d * rounder) / rounder);
return getDoubleValue().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;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.beowulfe.hap.impl.characteristics.carbonmonoxide.Carbon;
package com.beowulfe.hap.impl.characteristics.carbonmonoxide;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.CarbonMonoxideSensor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.beowulfe.hap.impl.services;

import com.beowulfe.hap.accessories.CarbonMonoxideSensor;
import com.beowulfe.hap.impl.characteristics.carbonmonoxide.Carbon.CarbonMonoxideDetectedCharacteristic;
import com.beowulfe.hap.impl.characteristics.carbonmonoxide.CarbonMonoxideDetectedCharacteristic;

public class CarbonMonoxideSensorService extends AbstractServiceImpl {

Expand Down