Skip to content

Commit 34b6799

Browse files
authored
Merge pull request #17 from gdombiak/service_name
We can now specify service name
2 parents 1160ddd + 367fe2a commit 34b6799

26 files changed

+227
-72
lines changed

src/main/java/com/beowulfe/hap/HomekitRoot.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.beowulfe.hap;
22

3-
import java.io.IOException;
4-
import java.net.InetAddress;
5-
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
8-
93
import com.beowulfe.hap.impl.HomekitRegistry;
104
import com.beowulfe.hap.impl.HomekitWebHandler;
115
import com.beowulfe.hap.impl.accessories.Bridge;
126
import com.beowulfe.hap.impl.connections.HomekitClientConnectionFactoryImpl;
137
import com.beowulfe.hap.impl.connections.SubscriptionManager;
148
import com.beowulfe.hap.impl.jmdns.JmdnsHomekitAdvertiser;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.io.IOException;
13+
import java.net.InetAddress;
1514

1615
/**
1716
* Provides advertising and handling for Homekit accessories. This class handles the advertising of Homekit accessories and
@@ -69,7 +68,7 @@ public void addAccessory(HomekitAccessory accessory) {
6968
*/
7069
void addAccessorySkipRangeCheck(HomekitAccessory accessory) {
7170
this.registry.add(accessory);
72-
logger.info("Added accessory "+accessory.getLabel());
71+
logger.info("Added accessory " + accessory.getLabel());
7372
if (started) {
7473
registry.reset();
7574
webHandler.resetConnections();
@@ -84,6 +83,7 @@ void addAccessorySkipRangeCheck(HomekitAccessory accessory) {
8483
*/
8584
public void removeAccessory(HomekitAccessory accessory) {
8685
this.registry.remove(accessory);
86+
logger.info("Removed accessory " + accessory.getLabel());
8787
if (started) {
8888
registry.reset();
8989
webHandler.resetConnections();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.beowulfe.hap.accessories;
2+
3+
import com.beowulfe.hap.HomekitAccessory;
4+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
5+
import com.beowulfe.hap.Service;
6+
import com.beowulfe.hap.impl.services.LightSensorService;
7+
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.concurrent.CompletableFuture;
11+
12+
/**
13+
* A light sensor that reports current ambient light level.
14+
*
15+
* @author Gaston Dombiak
16+
*/
17+
public interface LightSensor extends HomekitAccessory {
18+
19+
/**
20+
* Retrieves the current ambient light level.
21+
*
22+
* @return a future that will contain the luminance level expressed in LUX.
23+
*/
24+
CompletableFuture<Double> getCurrentAmbientLightLevel();
25+
26+
@Override
27+
default Collection<Service> getServices() {
28+
return Collections.singleton(new LightSensorService(this));
29+
}
30+
31+
/**
32+
* Subscribes to changes in the current ambient light level.
33+
*
34+
* @param callback the function to call when the state changes.
35+
*/
36+
void subscribeCurrentAmbientLightLevel(HomekitCharacteristicChangeCallback callback);
37+
38+
/**
39+
* Unsubscribes from changes in the current ambient light level.
40+
*/
41+
void unsubscribeCurrentAmbientLightLevel();
42+
}

src/main/java/com/beowulfe/hap/impl/HomekitRegistry.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package com.beowulfe.hap.impl;
22

3-
import java.util.ArrayList;
4-
import java.util.Collection;
5-
import java.util.Collections;
6-
import java.util.HashMap;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.concurrent.ConcurrentHashMap;
10-
11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
13-
143
import com.beowulfe.hap.HomekitAccessory;
154
import com.beowulfe.hap.Service;
165
import com.beowulfe.hap.characteristics.Characteristic;
176
import com.beowulfe.hap.impl.services.AccessoryInformationService;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import java.util.*;
11+
import java.util.concurrent.ConcurrentHashMap;
1812

1913
public class HomekitRegistry {
2014

@@ -72,7 +66,11 @@ public List<Service> getServices(Integer aid) {
7266
}
7367

7468
public Map<Integer, Characteristic> getCharacteristics(Integer aid) {
75-
return Collections.unmodifiableMap(characteristics.get(accessories.get(aid)));
69+
Map<Integer, Characteristic> characteristics = this.characteristics.get(accessories.get(aid));
70+
if (characteristics == null) {
71+
return Collections.emptyMap();
72+
}
73+
return Collections.unmodifiableMap(characteristics);
7674
}
7775

7876
public void add(HomekitAccessory accessory) {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.beowulfe.hap.impl.characteristics.common;
22

3-
import com.beowulfe.hap.HomekitAccessory;
43
import com.beowulfe.hap.characteristics.StaticStringCharacteristic;
54

65
public class Name extends StaticStringCharacteristic {
76

8-
public Name(HomekitAccessory accessory) {
7+
public Name(String label) {
98
super("00000023-0000-1000-8000-0026BB765291",
109
"Name of the accessory",
11-
accessory.getLabel());
10+
label);
1211
}
1312
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.beowulfe.hap.impl.characteristics.light;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.LightSensor;
5+
import com.beowulfe.hap.characteristics.EventableCharacteristic;
6+
import com.beowulfe.hap.characteristics.FloatCharacteristic;
7+
8+
import java.util.concurrent.CompletableFuture;
9+
10+
public class AmbientLightLevelCharacteristic extends FloatCharacteristic implements EventableCharacteristic {
11+
12+
private final LightSensor lightSensor;
13+
14+
public AmbientLightLevelCharacteristic(LightSensor lightSensor) {
15+
super("0000006B-0000-1000-8000-0026BB765291", false, true, "Current ambient light level", 0.0001, 100000,
16+
0.0001, "lux");
17+
this.lightSensor = lightSensor;
18+
}
19+
20+
@Override
21+
protected void setValue(Double value) throws Exception {
22+
//Read Only
23+
}
24+
25+
@Override
26+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
27+
lightSensor.subscribeCurrentAmbientLightLevel(callback);
28+
}
29+
30+
@Override
31+
public void unsubscribe() {
32+
lightSensor.unsubscribeCurrentAmbientLightLevel();
33+
}
34+
35+
@Override
36+
protected CompletableFuture<Double> getDoubleValue() {
37+
return lightSensor.getCurrentAmbientLightLevel();
38+
}
39+
}

src/main/java/com/beowulfe/hap/impl/http/impl/AccessoryHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
8787
@Override
8888
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
8989
throws Exception {
90-
boolean errorLevel = true;
91-
if (cause instanceof IOException) {
92-
// Decide level of logging based on exception
93-
errorLevel = !"Connection timed out".equals(cause.getMessage());
94-
}
90+
boolean errorLevel = !(cause instanceof IOException);
9591
if (errorLevel) {
9692
LOGGER.error("Exception caught in web handler", cause);
9793
} else {

src/main/java/com/beowulfe/hap/impl/http/impl/BinaryHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in,
5252
@Override
5353
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
5454
throws Exception {
55-
boolean errorLevel = true;
56-
if (cause instanceof IOException) {
57-
// Decide level of logging based on exception
58-
errorLevel = !"Connection timed out".equals(cause.getMessage());
59-
}
55+
boolean errorLevel = !(cause instanceof IOException);
6056
if (errorLevel) {
6157
logger.error("Exception in binary handler", cause);
6258
} else {

src/main/java/com/beowulfe/hap/impl/json/CharacteristicsController.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
package com.beowulfe.hap.impl.json;
22

3-
import java.io.ByteArrayInputStream;
4-
import java.io.ByteArrayOutputStream;
5-
6-
import javax.json.Json;
7-
import javax.json.JsonArray;
8-
import javax.json.JsonArrayBuilder;
9-
import javax.json.JsonObject;
10-
import javax.json.JsonObjectBuilder;
11-
import javax.json.JsonValue;
12-
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
163
import com.beowulfe.hap.characteristics.Characteristic;
174
import com.beowulfe.hap.characteristics.EventableCharacteristic;
185
import com.beowulfe.hap.impl.HomekitRegistry;
@@ -21,6 +8,13 @@
218
import com.beowulfe.hap.impl.http.HttpRequest;
229
import com.beowulfe.hap.impl.http.HttpResponse;
2310
import com.beowulfe.hap.impl.responses.NotFoundResponse;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
import javax.json.*;
15+
import java.io.ByteArrayInputStream;
16+
import java.io.ByteArrayOutputStream;
17+
import java.util.Map;
2418

2519
public class CharacteristicsController {
2620

@@ -49,9 +43,19 @@ public HttpResponse get(HttpRequest request) throws Exception {
4943
int aid = Integer.parseInt(parts[0]);
5044
int iid = Integer.parseInt(parts[1]);
5145
JsonObjectBuilder characteristic = Json.createObjectBuilder();
52-
registry.getCharacteristics(aid).get(iid).supplyValue(characteristic);
46+
Map<Integer, Characteristic> characteristicMap = registry.getCharacteristics(aid);
47+
if (!characteristicMap.isEmpty()) {
48+
Characteristic targetCharacteristic = characteristicMap.get(iid);
49+
if (targetCharacteristic != null) {
50+
targetCharacteristic.supplyValue(characteristic);
5351

54-
characteristics.add(characteristic.add("aid", aid).add("iid", iid).build());
52+
characteristics.add(characteristic.add("aid", aid).add("iid", iid).build());
53+
} else {
54+
logger.warn("Accessory " + aid + " does not have characteristic " + iid + "Request: " + uri);
55+
}
56+
} else {
57+
logger.warn("Accessory " + aid + " has no characteristics or does not exist. Request: " + uri);
58+
}
5559
}
5660
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
5761
Json.createWriter(baos).write(Json.createObjectBuilder().add("characteristics", characteristics.build()).build());

src/main/java/com/beowulfe/hap/impl/services/AbstractServiceImpl.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ abstract class AbstractServiceImpl implements Service {
1717
private final List<Characteristic> characteristics = new LinkedList<>();
1818

1919
/**
20-
* This constructor has been deprecated and replaced with {@link #AbstractServiceImpl(String, HomekitAccessory)}.
21-
* Usages of this constructor will need to manually configure {@link Name} characteristic and
22-
* {@link BatteryLevelCharacteristic} if needed.
20+
* This constructor has been deprecated and replaced with
21+
* {@link #AbstractServiceImpl(String, HomekitAccessory, String)}. Usages of
22+
* this constructor will need to manually configure {@link Name} characteristic
23+
* and {@link BatteryLevelCharacteristic} if needed.
2324
*
2425
* @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator.
25-
*/
26+
*/
2627
@Deprecated
2728
public AbstractServiceImpl(String type) {
28-
this(type, null);
29+
this(type, null, null);
2930
}
3031

3132
/**
@@ -39,13 +40,14 @@ public AbstractServiceImpl(String type) {
3940
*
4041
* @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator.
4142
* @param accessory HomeKit accessory exposed as a service.
43+
* @param serviceName name of the service. This information is usually the name of the accessory.
4244
*/
43-
public AbstractServiceImpl(String type, HomekitAccessory accessory) {
45+
public AbstractServiceImpl(String type, HomekitAccessory accessory, String serviceName) {
4446
this.type = type;
4547

4648
if (accessory != null) {
4749
// Add name characteristic
48-
addCharacteristic(new Name(accessory));
50+
addCharacteristic(new Name(serviceName));
4951

5052
// If battery operated accessory then add BatteryLevelCharacteristic
5153
if (accessory instanceof BatteryAccessory) {

src/main/java/com/beowulfe/hap/impl/services/AccessoryInformationService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
public class AccessoryInformationService extends AbstractServiceImpl {
1010

1111
public AccessoryInformationService(HomekitAccessory accessory) throws Exception {
12-
super("0000003E-0000-1000-8000-0026BB765291", accessory);
12+
this(accessory, accessory.getLabel());
13+
}
14+
15+
public AccessoryInformationService(HomekitAccessory accessory, String serviceName) throws Exception {
16+
super("0000003E-0000-1000-8000-0026BB765291", accessory, serviceName);
1317
addCharacteristic(new Manufacturer(accessory));
1418
addCharacteristic(new Model(accessory));
1519
addCharacteristic(new SerialNumber(accessory));

0 commit comments

Comments
 (0)