Skip to content

Commit 97d066a

Browse files
authored
Merge pull request #4 from hap-java/master
merge to upstream
2 parents b099f56 + 8b77a6e commit 97d066a

File tree

10 files changed

+220
-17
lines changed

10 files changed

+220
-17
lines changed
Lines changed: 60 additions & 0 deletions
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+
}
Lines changed: 32 additions & 0 deletions
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+
}
Lines changed: 41 additions & 0 deletions
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+
}
Lines changed: 54 additions & 0 deletions
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+
}

src/main/java/io/github/hapjava/impl/connections/HttpSession.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public HttpResponse handleRequest(HttpRequest request) throws IOException {
6666
}
6767

6868
public HttpResponse handleAuthenticatedRequest(HttpRequest request) throws IOException {
69+
advertiser.setDiscoverable(
70+
false); // brigde is already bound and should not be discoverable anymore
6971
try {
7072
switch (request.getUri()) {
7173
case "/accessories":
@@ -101,7 +103,7 @@ private HttpResponse handlePairSetup(HttpRequest request) {
101103
if (pairingManager == null) {
102104
synchronized (HttpSession.class) {
103105
if (pairingManager == null) {
104-
pairingManager = new PairingManager(authInfo, registry, advertiser);
106+
pairingManager = new PairingManager(authInfo, registry);
105107
}
106108
}
107109
}

src/main/java/io/github/hapjava/impl/json/AccessoryController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ private CompletableFuture<JsonObject> toJson(Service service, int interfaceId) t
8181
.thenApply(
8282
v -> {
8383
JsonArrayBuilder jsonCharacteristics = Json.createArrayBuilder();
84-
characteristicFutures
85-
.stream()
84+
characteristicFutures.stream()
8685
.map(future -> future.join())
8786
.forEach(c -> jsonCharacteristics.add(c));
8887
builder.add("characteristics", jsonCharacteristics);

src/main/java/io/github/hapjava/impl/pairing/FinalPairHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.github.hapjava.HomekitAuthInfo;
44
import io.github.hapjava.impl.crypto.*;
55
import io.github.hapjava.impl.http.HttpResponse;
6-
import io.github.hapjava.impl.jmdns.JmdnsHomekitAdvertiser;
76
import io.github.hapjava.impl.pairing.PairSetupRequest.Stage3Request;
87
import io.github.hapjava.impl.pairing.TypeLengthValueUtils.DecodeResult;
98
import io.github.hapjava.impl.pairing.TypeLengthValueUtils.Encoder;
@@ -16,14 +15,12 @@ class FinalPairHandler {
1615

1716
private final byte[] k;
1817
private final HomekitAuthInfo authInfo;
19-
private final JmdnsHomekitAdvertiser advertiser;
2018

2119
private byte[] hkdf_enc_key;
2220

23-
public FinalPairHandler(byte[] k, HomekitAuthInfo authInfo, JmdnsHomekitAdvertiser advertiser) {
21+
public FinalPairHandler(byte[] k, HomekitAuthInfo authInfo) {
2422
this.k = k;
2523
this.authInfo = authInfo;
26-
this.advertiser = advertiser;
2724
}
2825

2926
public HttpResponse handle(PairSetupRequest req) throws Exception {
@@ -66,7 +63,6 @@ private HttpResponse createUser(byte[] username, byte[] ltpk, byte[] proof) thro
6663
throw new Exception("Invalid signature");
6764
}
6865
authInfo.createUser(authInfo.getMac() + new String(username, StandardCharsets.UTF_8), ltpk);
69-
advertiser.setDiscoverable(false);
7066
return createResponse();
7167
}
7268

src/main/java/io/github/hapjava/impl/pairing/PairingManager.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.github.hapjava.impl.HomekitRegistry;
55
import io.github.hapjava.impl.http.HttpRequest;
66
import io.github.hapjava.impl.http.HttpResponse;
7-
import io.github.hapjava.impl.jmdns.JmdnsHomekitAdvertiser;
87
import io.github.hapjava.impl.responses.NotFoundResponse;
98
import io.github.hapjava.impl.responses.UnauthorizedResponse;
109
import org.slf4j.Logger;
@@ -16,15 +15,12 @@ public class PairingManager {
1615

1716
private final HomekitAuthInfo authInfo;
1817
private final HomekitRegistry registry;
19-
private final JmdnsHomekitAdvertiser advertiser;
2018

2119
private SrpHandler srpHandler;
2220

23-
public PairingManager(
24-
HomekitAuthInfo authInfo, HomekitRegistry registry, JmdnsHomekitAdvertiser advertiser) {
21+
public PairingManager(HomekitAuthInfo authInfo, HomekitRegistry registry) {
2522
this.authInfo = authInfo;
2623
this.registry = registry;
27-
this.advertiser = advertiser;
2824
}
2925

3026
public HttpResponse handle(HttpRequest httpRequest) throws Exception {
@@ -54,7 +50,7 @@ public HttpResponse handle(HttpRequest httpRequest) throws Exception {
5450
logger.warn("Received unexpected stage 3 request for " + registry.getLabel());
5551
return new UnauthorizedResponse();
5652
} else {
57-
FinalPairHandler handler = new FinalPairHandler(srpHandler.getK(), authInfo, advertiser);
53+
FinalPairHandler handler = new FinalPairHandler(srpHandler.getK(), authInfo);
5854
try {
5955
return handler.handle(req);
6056
} catch (Exception e) {
Lines changed: 18 additions & 0 deletions
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+
}

src/test/java/com/github/hapjava/HomekitRootTest.java renamed to src/test/java/io/github/hapjava/HomekitRootTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
import static org.mockito.Matchers.any;
44
import static org.mockito.Matchers.eq;
5-
import static org.mockito.Mockito.*;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.never;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.when;
69

710
import io.github.hapjava.impl.HomekitWebHandler;
811
import io.github.hapjava.impl.http.HomekitClientConnectionFactory;
912
import io.github.hapjava.impl.jmdns.JmdnsHomekitAdvertiser;
1013
import java.util.concurrent.CompletableFuture;
11-
import org.junit.*;
14+
import org.junit.Assert;
15+
import org.junit.Before;
16+
import org.junit.Test;
1217

1318
public class HomekitRootTest {
1419

@@ -64,7 +69,7 @@ public void testWebHandlerStops() throws Exception {
6469

6570
@Test
6671
public void testAdvertiserStarts() throws Exception {
67-
String mac = "00:00:00:00:00:00";
72+
final String mac = "00:00:00:00:00:00";
6873
when(authInfo.getMac()).thenReturn(mac);
6974
root.start();
7075
verify(advertiser).advertise(eq(LABEL), eq(mac), eq(PORT), eq(1));

0 commit comments

Comments
 (0)