Skip to content

Commit 8b77a6e

Browse files
yfreTim Harper
authored and
Tim Harper
committed
add carbon dioxide sensor (#82)
* increase java version * switch to openjdk8
1 parent 5248eeb commit 8b77a6e

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.hapjava.accessories;
2+
3+
import io.github.hapjava.HomekitAccessory;
4+
import io.github.hapjava.HomekitCharacteristicChangeCallback;
5+
import io.github.hapjava.Service;
6+
import io.github.hapjava.accessories.properties.CarbonDioxideDetectedState;
7+
import io.github.hapjava.impl.services.CarbonDioxideSensorService;
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.concurrent.CompletableFuture;
11+
12+
/**
13+
* A carbon dioxide sensor reports whether carbon dioxide has been detected or not.
14+
*
15+
* <p>Carbon dioxide sensors that run on batteries will need to implement this interface and also
16+
* implement {@link BatteryStatusAccessory}.
17+
*
18+
* @author Eugen Freiter
19+
*/
20+
public interface CarbonDioxideSensor extends HomekitAccessory {
21+
22+
/**
23+
* Retrieves the state of the sensor that indicates if carbon dioxide has been detected.
24+
*
25+
* @return a future that will contain the carbon dioxide sensor's state
26+
*/
27+
CompletableFuture<CarbonDioxideDetectedState> getCarbonDioxideDetectedState();
28+
29+
@Override
30+
default Collection<Service> getServices() {
31+
return Collections.singleton(new CarbonDioxideSensorService(this));
32+
}
33+
34+
/**
35+
* Subscribes to changes in the carbon dioxide's state.
36+
*
37+
* @param callback the function to call when the state changes.
38+
*/
39+
void subscribeCarbonDioxideDetectedState(HomekitCharacteristicChangeCallback callback);
40+
41+
/**
42+
* Retrieves the carbon dioxide level
43+
*
44+
* @return a future that will contain the carbon dioxide level as a value between 0 and 100000
45+
*/
46+
CompletableFuture<Double> getCarbonDioxideLevel();
47+
48+
/** Unsubscribes from changes in the carbon dioxide's state. */
49+
void unsubscribeCarbonDioxideDetectedState();
50+
51+
/**
52+
* Subscribes to changes in the carbon dioxide level.
53+
*
54+
* @param callback the function to call when the state changes.
55+
*/
56+
void subscribeCarbonDioxideLevel(HomekitCharacteristicChangeCallback callback);
57+
58+
/** Unsubscribes from changes in the carbon dioxide level. */
59+
void unsubscribeCarbonDioxideLevel();
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.github.hapjava.accessories.properties;
2+
3+
import java.util.Arrays;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
6+
7+
public enum CarbonDioxideDetectedState {
8+
NORMAL(0),
9+
ABNORMAL(1);
10+
11+
private static final Map<Integer, CarbonDioxideDetectedState> reverse;
12+
13+
static {
14+
reverse =
15+
Arrays.stream(CarbonDioxideDetectedState.values())
16+
.collect(Collectors.toMap(CarbonDioxideDetectedState::getCode, t -> t));
17+
}
18+
19+
public static CarbonDioxideDetectedState fromCode(Integer code) {
20+
return reverse.get(code);
21+
}
22+
23+
private final int code;
24+
25+
CarbonDioxideDetectedState(int code) {
26+
this.code = code;
27+
}
28+
29+
public int getCode() {
30+
return code;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.github.hapjava.impl.characteristics.carbondioxide;
2+
3+
import io.github.hapjava.HomekitCharacteristicChangeCallback;
4+
import io.github.hapjava.accessories.CarbonDioxideSensor;
5+
import io.github.hapjava.accessories.properties.CarbonDioxideDetectedState;
6+
import io.github.hapjava.characteristics.EnumCharacteristic;
7+
import io.github.hapjava.characteristics.EventableCharacteristic;
8+
import java.util.concurrent.CompletableFuture;
9+
10+
public class CarbonDioxideDetectedCharacteristic extends EnumCharacteristic
11+
implements EventableCharacteristic {
12+
13+
private final CarbonDioxideSensor carbonDioxideSensor;
14+
15+
public CarbonDioxideDetectedCharacteristic(CarbonDioxideSensor carbonDioxideSensor) {
16+
super("00000092-0000-1000-8000-0026BB765291", false, true, "Carbon Dioxide Detected", 1);
17+
this.carbonDioxideSensor = carbonDioxideSensor;
18+
}
19+
20+
@Override
21+
protected CompletableFuture<Integer> getValue() {
22+
return carbonDioxideSensor
23+
.getCarbonDioxideDetectedState()
24+
.thenApply(CarbonDioxideDetectedState::getCode);
25+
}
26+
27+
@Override
28+
protected void setValue(Integer value) throws Exception {
29+
// Read Only
30+
}
31+
32+
@Override
33+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
34+
carbonDioxideSensor.subscribeCarbonDioxideDetectedState(callback);
35+
}
36+
37+
@Override
38+
public void unsubscribe() {
39+
carbonDioxideSensor.unsubscribeCarbonDioxideDetectedState();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.github.hapjava.impl.characteristics.carbondioxide;
2+
3+
import io.github.hapjava.HomekitCharacteristicChangeCallback;
4+
import io.github.hapjava.accessories.CarbonDioxideSensor;
5+
import io.github.hapjava.characteristics.EventableCharacteristic;
6+
import io.github.hapjava.characteristics.FloatCharacteristic;
7+
import java.util.concurrent.CompletableFuture;
8+
9+
public class CarbonDioxideLevelCharacteristic extends FloatCharacteristic
10+
implements EventableCharacteristic {
11+
12+
private final CarbonDioxideSensor sensor;
13+
14+
public CarbonDioxideLevelCharacteristic(CarbonDioxideSensor sensor) {
15+
super(
16+
"00000093-0000-1000-8000-0026BB765291",
17+
false,
18+
true,
19+
"Carbon Dioxide level",
20+
0,
21+
100000,
22+
0.1,
23+
"%");
24+
this.sensor = sensor;
25+
}
26+
27+
@Override
28+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
29+
sensor.subscribeCarbonDioxideLevel(callback);
30+
}
31+
32+
@Override
33+
public void unsubscribe() {
34+
sensor.unsubscribeCarbonDioxideLevel();
35+
}
36+
37+
@Override
38+
protected void setValue(Double value) throws Exception {
39+
// Read Only
40+
}
41+
42+
@Override
43+
protected CompletableFuture<Double> getDoubleValue() {
44+
return sensor.getCarbonDioxideLevel();
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "CarbonDioxideLevelCharacteristic{"
50+
+ "sensor level ="
51+
+ sensor.getCarbonDioxideLevel()
52+
+ '}';
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.hapjava.impl.services;
2+
3+
import io.github.hapjava.accessories.CarbonDioxideSensor;
4+
import io.github.hapjava.impl.characteristics.carbondioxide.CarbonDioxideDetectedCharacteristic;
5+
import io.github.hapjava.impl.characteristics.carbondioxide.CarbonDioxideLevelCharacteristic;
6+
7+
public class CarbonDioxideSensorService extends AbstractServiceImpl {
8+
9+
public CarbonDioxideSensorService(CarbonDioxideSensor carbonDioxideSensor) {
10+
this(carbonDioxideSensor, carbonDioxideSensor.getLabel());
11+
}
12+
13+
public CarbonDioxideSensorService(CarbonDioxideSensor carbonDioxideSensor, String serviceName) {
14+
super("00000097-0000-1000-8000-0026BB765291", carbonDioxideSensor, serviceName);
15+
addCharacteristic(new CarbonDioxideDetectedCharacteristic(carbonDioxideSensor));
16+
addCharacteristic(new CarbonDioxideLevelCharacteristic(carbonDioxideSensor));
17+
}
18+
}

0 commit comments

Comments
 (0)