Skip to content

add carbon dioxide sensor #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.6.0</version>
<version>2.9</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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<CarbonDioxideDetectedState> getCarbonDioxideDetectedState();

@Override
default Collection<Service> 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<Double> 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();
}
Original file line number Diff line number Diff line change
@@ -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<Integer, CarbonDioxideDetectedState> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Double> getDoubleValue() {
return sensor.getCarbonDioxideLevel();
}

@Override
public String toString() {
return "CarbonDioxideLevelCharacteristic{"
+ "sensor level ="
+ sensor.getCarbonDioxideLevel()
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}