diff --git a/.travis.yml b/.travis.yml index a7bec579c..f14d2e2b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: java -jdk: oraclejdk8 +jdk: openjdk8 after_success: - openssl aes-256-cbc -pass pass:$ENCRYPTION_PASSWORD -in deploy/pubring.gpg.enc -out deploy/pubring.gpg -d diff --git a/pom.xml b/pom.xml index fdbbeae88..d5709647c 100644 --- a/pom.xml +++ b/pom.xml @@ -129,7 +129,7 @@ com.coveo fmt-maven-plugin - 2.6.0 + 2.9 diff --git a/src/main/java/io/github/hapjava/accessories/CarbonDioxideSensor.java b/src/main/java/io/github/hapjava/accessories/CarbonDioxideSensor.java new file mode 100644 index 000000000..27c332471 --- /dev/null +++ b/src/main/java/io/github/hapjava/accessories/CarbonDioxideSensor.java @@ -0,0 +1,60 @@ +package io.github.hapjava.accessories; + +import io.github.hapjava.HomekitAccessory; +import io.github.hapjava.HomekitCharacteristicChangeCallback; +import io.github.hapjava.Service; +import io.github.hapjava.accessories.properties.CarbonDioxideDetectedState; +import io.github.hapjava.impl.services.CarbonDioxideSensorService; +import java.util.Collection; +import java.util.Collections; +import java.util.concurrent.CompletableFuture; + +/** + * A carbon dioxide sensor reports whether carbon dioxide has been detected or not. + * + *

Carbon dioxide sensors that run on batteries will need to implement this interface and also + * implement {@link BatteryStatusAccessory}. + * + * @author Eugen Freiter + */ +public interface CarbonDioxideSensor extends HomekitAccessory { + + /** + * Retrieves the state of the sensor that indicates if carbon dioxide has been detected. + * + * @return a future that will contain the carbon dioxide sensor's state + */ + CompletableFuture getCarbonDioxideDetectedState(); + + @Override + default Collection getServices() { + return Collections.singleton(new CarbonDioxideSensorService(this)); + } + + /** + * Subscribes to changes in the carbon dioxide's state. + * + * @param callback the function to call when the state changes. + */ + void subscribeCarbonDioxideDetectedState(HomekitCharacteristicChangeCallback callback); + + /** + * Retrieves the carbon dioxide level + * + * @return a future that will contain the carbon dioxide level as a value between 0 and 100000 + */ + CompletableFuture getCarbonDioxideLevel(); + + /** Unsubscribes from changes in the carbon dioxide's state. */ + void unsubscribeCarbonDioxideDetectedState(); + + /** + * Subscribes to changes in the carbon dioxide level. + * + * @param callback the function to call when the state changes. + */ + void subscribeCarbonDioxideLevel(HomekitCharacteristicChangeCallback callback); + + /** Unsubscribes from changes in the carbon dioxide level. */ + void unsubscribeCarbonDioxideLevel(); +} diff --git a/src/main/java/io/github/hapjava/accessories/properties/CarbonDioxideDetectedState.java b/src/main/java/io/github/hapjava/accessories/properties/CarbonDioxideDetectedState.java new file mode 100644 index 000000000..66dabf5b6 --- /dev/null +++ b/src/main/java/io/github/hapjava/accessories/properties/CarbonDioxideDetectedState.java @@ -0,0 +1,32 @@ +package io.github.hapjava.accessories.properties; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; + +public enum CarbonDioxideDetectedState { + NORMAL(0), + ABNORMAL(1); + + private static final Map reverse; + + static { + reverse = + Arrays.stream(CarbonDioxideDetectedState.values()) + .collect(Collectors.toMap(CarbonDioxideDetectedState::getCode, t -> t)); + } + + public static CarbonDioxideDetectedState fromCode(Integer code) { + return reverse.get(code); + } + + private final int code; + + CarbonDioxideDetectedState(int code) { + this.code = code; + } + + public int getCode() { + return code; + } +} diff --git a/src/main/java/io/github/hapjava/impl/characteristics/carbondioxide/CarbonDioxideDetectedCharacteristic.java b/src/main/java/io/github/hapjava/impl/characteristics/carbondioxide/CarbonDioxideDetectedCharacteristic.java new file mode 100644 index 000000000..5ce03856a --- /dev/null +++ b/src/main/java/io/github/hapjava/impl/characteristics/carbondioxide/CarbonDioxideDetectedCharacteristic.java @@ -0,0 +1,41 @@ +package io.github.hapjava.impl.characteristics.carbondioxide; + +import io.github.hapjava.HomekitCharacteristicChangeCallback; +import io.github.hapjava.accessories.CarbonDioxideSensor; +import io.github.hapjava.accessories.properties.CarbonDioxideDetectedState; +import io.github.hapjava.characteristics.EnumCharacteristic; +import io.github.hapjava.characteristics.EventableCharacteristic; +import java.util.concurrent.CompletableFuture; + +public class CarbonDioxideDetectedCharacteristic extends EnumCharacteristic + implements EventableCharacteristic { + + private final CarbonDioxideSensor carbonDioxideSensor; + + public CarbonDioxideDetectedCharacteristic(CarbonDioxideSensor carbonDioxideSensor) { + super("00000092-0000-1000-8000-0026BB765291", false, true, "Carbon Dioxide Detected", 1); + this.carbonDioxideSensor = carbonDioxideSensor; + } + + @Override + protected CompletableFuture getValue() { + return carbonDioxideSensor + .getCarbonDioxideDetectedState() + .thenApply(CarbonDioxideDetectedState::getCode); + } + + @Override + protected void setValue(Integer value) throws Exception { + // Read Only + } + + @Override + public void subscribe(HomekitCharacteristicChangeCallback callback) { + carbonDioxideSensor.subscribeCarbonDioxideDetectedState(callback); + } + + @Override + public void unsubscribe() { + carbonDioxideSensor.unsubscribeCarbonDioxideDetectedState(); + } +} diff --git a/src/main/java/io/github/hapjava/impl/characteristics/carbondioxide/CarbonDioxideLevelCharacteristic.java b/src/main/java/io/github/hapjava/impl/characteristics/carbondioxide/CarbonDioxideLevelCharacteristic.java new file mode 100644 index 000000000..f6f6ecc9e --- /dev/null +++ b/src/main/java/io/github/hapjava/impl/characteristics/carbondioxide/CarbonDioxideLevelCharacteristic.java @@ -0,0 +1,54 @@ +package io.github.hapjava.impl.characteristics.carbondioxide; + +import io.github.hapjava.HomekitCharacteristicChangeCallback; +import io.github.hapjava.accessories.CarbonDioxideSensor; +import io.github.hapjava.characteristics.EventableCharacteristic; +import io.github.hapjava.characteristics.FloatCharacteristic; +import java.util.concurrent.CompletableFuture; + +public class CarbonDioxideLevelCharacteristic extends FloatCharacteristic + implements EventableCharacteristic { + + private final CarbonDioxideSensor sensor; + + public CarbonDioxideLevelCharacteristic(CarbonDioxideSensor sensor) { + super( + "00000093-0000-1000-8000-0026BB765291", + false, + true, + "Carbon Dioxide level", + 0, + 100000, + 0.1, + "%"); + this.sensor = sensor; + } + + @Override + public void subscribe(HomekitCharacteristicChangeCallback callback) { + sensor.subscribeCarbonDioxideLevel(callback); + } + + @Override + public void unsubscribe() { + sensor.unsubscribeCarbonDioxideLevel(); + } + + @Override + protected void setValue(Double value) throws Exception { + // Read Only + } + + @Override + protected CompletableFuture getDoubleValue() { + return sensor.getCarbonDioxideLevel(); + } + + @Override + public String toString() { + return "CarbonDioxideLevelCharacteristic{" + + "sensor level =" + + sensor.getCarbonDioxideLevel() + + '}'; + } +} diff --git a/src/main/java/io/github/hapjava/impl/services/CarbonDioxideSensorService.java b/src/main/java/io/github/hapjava/impl/services/CarbonDioxideSensorService.java new file mode 100644 index 000000000..f00f04996 --- /dev/null +++ b/src/main/java/io/github/hapjava/impl/services/CarbonDioxideSensorService.java @@ -0,0 +1,18 @@ +package io.github.hapjava.impl.services; + +import io.github.hapjava.accessories.CarbonDioxideSensor; +import io.github.hapjava.impl.characteristics.carbondioxide.CarbonDioxideDetectedCharacteristic; +import io.github.hapjava.impl.characteristics.carbondioxide.CarbonDioxideLevelCharacteristic; + +public class CarbonDioxideSensorService extends AbstractServiceImpl { + + public CarbonDioxideSensorService(CarbonDioxideSensor carbonDioxideSensor) { + this(carbonDioxideSensor, carbonDioxideSensor.getLabel()); + } + + public CarbonDioxideSensorService(CarbonDioxideSensor carbonDioxideSensor, String serviceName) { + super("00000097-0000-1000-8000-0026BB765291", carbonDioxideSensor, serviceName); + addCharacteristic(new CarbonDioxideDetectedCharacteristic(carbonDioxideSensor)); + addCharacteristic(new CarbonDioxideLevelCharacteristic(carbonDioxideSensor)); + } +}