Skip to content

Commit 4e980a8

Browse files
authoredFeb 10, 2022
Merge pull request #159 from yfre/add_category_support
add support for HomeKit categories
2 parents 54908ff + 1f72143 commit 4e980a8

File tree

9 files changed

+99
-21
lines changed

9 files changed

+99
-21
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.github.hapjava.server;
2+
3+
public final class HomekitAccessoryCategories {
4+
public static final int OTHER = 1;
5+
public static final int BRIDGES = 2;
6+
public static final int FANS = 3;
7+
public static final int GARAGE_DOOR_OPENERS = 4;
8+
public static final int LIGHTING = 5;
9+
public static final int LOCKS = 6;
10+
public static final int OUTLETS = 7;
11+
public static final int SWITCHES = 8;
12+
public static final int THERMOSTATS = 9;
13+
public static final int SENSORS = 10;
14+
public static final int SECURITY_SYSTEMS = 11;
15+
public static final int DOORS = 12;
16+
public static final int WINDOWS = 13;
17+
public static final int WINDOW_COVERINGS = 14;
18+
public static final int PROGRAMMABLE_SWITCHES = 15;
19+
public static final int RANGE_EXTENDER = 16;
20+
public static final int IP_CAMERAS = 17;
21+
public static final int VIDEO_DOORBELLS = 18;
22+
public static final int AIR_PURIFIERS = 19;
23+
public static final int HEATERS = 20;
24+
public static final int AIR_CONDITIONERS = 21;
25+
public static final int HUMIDIFIERS = 22;
26+
public static final int DEHUMIDIFIERS = 23;
27+
public static final int APPLE_TV = 24;
28+
public static final int HOMEPOD = 25;
29+
public static final int SPEAKER = 26;
30+
public static final int AIRPORT = 27;
31+
public static final int SPRINKLERS = 28;
32+
public static final int FAUCETS = 29;
33+
public static final int SHOWER_SYSTEMS = 30;
34+
public static final int TELEVISION = 31;
35+
public static final int REMOTES = 32;
36+
public static final int ROUTER = 33;
37+
public static final int AUDIO_RECEIVER = 34;
38+
public static final int TV_SET_TOP_BOX = 35;
39+
public static final int TV_STREAMING_STICK = 36;
40+
}

‎src/main/java/io/github/hapjava/server/impl/HomekitRoot.java

+35-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.hapjava.accessories.Bridge;
44
import io.github.hapjava.accessories.HomekitAccessory;
5+
import io.github.hapjava.server.HomekitAccessoryCategories;
56
import io.github.hapjava.server.HomekitAuthInfo;
67
import io.github.hapjava.server.HomekitWebHandler;
78
import io.github.hapjava.server.impl.connections.HomekitClientConnectionFactoryImpl;
@@ -25,24 +26,36 @@
2526
public class HomekitRoot {
2627

2728
private static final Logger logger = LoggerFactory.getLogger(HomekitRoot.class);
28-
29+
private static final int DEFAULT_ACCESSORY_CATEGORY = HomekitAccessoryCategories.OTHER;
2930
private final JmdnsHomekitAdvertiser advertiser;
3031
private final HomekitWebHandler webHandler;
3132
private final HomekitAuthInfo authInfo;
3233
private final String label;
34+
private final int category;
3335
private final HomekitRegistry registry;
3436
private final SubscriptionManager subscriptions = new SubscriptionManager();
3537
private boolean started = false;
3638
private int configurationIndex = 1;
3739

3840
HomekitRoot(
39-
String label, HomekitWebHandler webHandler, InetAddress localhost, HomekitAuthInfo authInfo)
41+
String label, HomekitWebHandler webHandler, InetAddress host, HomekitAuthInfo authInfo)
42+
throws IOException {
43+
this(label, DEFAULT_ACCESSORY_CATEGORY, webHandler, authInfo, new JmdnsHomekitAdvertiser(host));
44+
}
45+
46+
HomekitRoot(
47+
String label,
48+
int category,
49+
HomekitWebHandler webHandler,
50+
InetAddress host,
51+
HomekitAuthInfo authInfo)
4052
throws IOException {
41-
this(label, webHandler, authInfo, new JmdnsHomekitAdvertiser(localhost));
53+
this(label, category, webHandler, authInfo, new JmdnsHomekitAdvertiser(host));
4254
}
4355

4456
HomekitRoot(
4557
String label,
58+
int category,
4659
HomekitWebHandler webHandler,
4760
HomekitAuthInfo authInfo,
4861
JmdnsHomekitAdvertiser advertiser)
@@ -51,14 +64,25 @@ public class HomekitRoot {
5164
this.webHandler = webHandler;
5265
this.authInfo = authInfo;
5366
this.label = label;
67+
this.category = category;
5468
this.registry = new HomekitRegistry(label);
5569
}
5670

57-
HomekitRoot(String label, HomekitWebHandler webHandler, JmDNS jmdns, HomekitAuthInfo authInfo)
71+
HomekitRoot(
72+
String label,
73+
int category,
74+
HomekitWebHandler webHandler,
75+
JmDNS jmdns,
76+
HomekitAuthInfo authInfo)
5877
throws IOException {
59-
this(label, webHandler, authInfo, new JmdnsHomekitAdvertiser(jmdns));
78+
this(label, category, webHandler, authInfo, new JmdnsHomekitAdvertiser(jmdns));
6079
}
6180

81+
HomekitRoot(String label, HomekitWebHandler webHandler, JmDNS jmdns, HomekitAuthInfo authInfo)
82+
throws IOException {
83+
this(
84+
label, DEFAULT_ACCESSORY_CATEGORY, webHandler, authInfo, new JmdnsHomekitAdvertiser(jmdns));
85+
}
6286
/**
6387
* Add an accessory to be handled and advertised by this root. Any existing HomeKit connections
6488
* will be terminated to allow the clients to reconnect and see the updated accessory list. When
@@ -127,7 +151,12 @@ public void start() {
127151
try {
128152
refreshAuthInfo();
129153
advertiser.advertise(
130-
label, authInfo.getMac(), port, configurationIndex, authInfo.getSetupId());
154+
label,
155+
category,
156+
authInfo.getMac(),
157+
port,
158+
configurationIndex,
159+
authInfo.getSetupId());
131160
} catch (Exception e) {
132161
throw new RuntimeException(e);
133162
}

‎src/main/java/io/github/hapjava/server/impl/HomekitServer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public HomekitStandaloneAccessoryServer createStandaloneAccessory(
139139
public HomekitRoot createBridge(
140140
HomekitAuthInfo authInfo,
141141
String label,
142+
int category,
142143
String manufacturer,
143144
String model,
144145
String serialNumber,
@@ -147,9 +148,9 @@ public HomekitRoot createBridge(
147148
throws IOException {
148149
HomekitRoot root;
149150
if (jmdns != null) {
150-
root = new HomekitRoot(label, http, jmdns, authInfo);
151+
root = new HomekitRoot(label, category, http, jmdns, authInfo);
151152
} else {
152-
root = new HomekitRoot(label, http, localAddress, authInfo);
153+
root = new HomekitRoot(label, category, http, localAddress, authInfo);
153154
}
154155
root.addAccessory(
155156
new HomekitBridge(

‎src/main/java/io/github/hapjava/server/impl/http/impl/AccessoryHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ private void sendResponse(
7272
HttpVersion.HTTP_1_1,
7373
status,
7474
Unpooled.copiedBuffer(responseBody.getBytes(StandardCharsets.UTF_8)));
75-
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
76-
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
77-
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
75+
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
76+
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
77+
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
7878
ctx.write(response);
7979
ctx.flush();
8080
}

‎src/main/java/io/github/hapjava/server/impl/http/impl/DefaultHttpRequestImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public DefaultHttpRequestImpl(io.netty.handler.codec.http.HttpRequest request) {
1313

1414
@Override
1515
public String getUri() {
16-
return request.getUri();
16+
return request.uri();
1717
}
1818

1919
@Override
@@ -23,7 +23,7 @@ public byte[] getBody() {
2323

2424
@Override
2525
public HttpMethod getMethod() {
26-
io.netty.handler.codec.http.HttpMethod method = request.getMethod();
26+
io.netty.handler.codec.http.HttpMethod method = request.method();
2727
if (method.equals(io.netty.handler.codec.http.HttpMethod.GET)) {
2828
return HttpMethod.GET;
2929
} else if (method.equals(io.netty.handler.codec.http.HttpMethod.POST)) {

‎src/main/java/io/github/hapjava/server/impl/http/impl/NettyResponseUtil.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import io.netty.buffer.Unpooled;
55
import io.netty.handler.codec.http.DefaultFullHttpResponse;
66
import io.netty.handler.codec.http.FullHttpResponse;
7-
import io.netty.handler.codec.http.HttpHeaders;
7+
import io.netty.handler.codec.http.HttpHeaderNames;
8+
import io.netty.handler.codec.http.HttpHeaderValues;
89
import io.netty.handler.codec.http.HttpResponseStatus;
910
import io.netty.handler.codec.http.HttpVersion;
1011
import java.util.Map.Entry;
@@ -25,8 +26,8 @@ public static FullHttpResponse createResponse(HttpResponse homekitResponse) {
2526
for (Entry<String, String> header : homekitResponse.getHeaders().entrySet()) {
2627
response.headers().add(header.getKey(), header.getValue());
2728
}
28-
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
29-
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
29+
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
30+
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
3031
return response;
3132
}
3233
}

‎src/main/java/io/github/hapjava/server/impl/jmdns/JmdnsHomekitAdvertiser.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class JmdnsHomekitAdvertiser {
2727
private int port;
2828
private int configurationIndex;
2929
private ServiceInfo serviceInfo;
30+
private int category;
3031

3132
public JmdnsHomekitAdvertiser(JmDNS jmdns) {
3233
this.jmdns = jmdns;
@@ -37,14 +38,16 @@ public JmdnsHomekitAdvertiser(InetAddress localAddress) throws UnknownHostExcept
3738
}
3839

3940
public synchronized void advertise(
40-
String label, String mac, int port, int configurationIndex, String setupId) throws Exception {
41+
String label, int category, String mac, int port, int configurationIndex, String setupId)
42+
throws Exception {
4143
if (isAdvertising) {
4244
throw new IllegalStateException("HomeKit advertiser is already running");
4345
}
4446
this.label = label;
4547
this.mac = mac;
4648
this.port = port;
4749
this.setupId = setupId;
50+
this.category = category;
4851
this.configurationIndex = configurationIndex;
4952

5053
logger.trace("Advertising accessory " + label);
@@ -114,7 +117,9 @@ private ServiceInfo buildServiceInfo() {
114117
props.put("c#", Integer.toString(configurationIndex));
115118
props.put("s#", "1");
116119
props.put("ff", "0");
117-
props.put("ci", "1");
120+
props.put("ci", Integer.toString(category));
121+
props.put("pv", "1.1");
122+
118123
return ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props);
119124
}
120125
}

‎src/test/java/io/github/hapjava/server/impl/HomekitRootTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.mockito.Mockito.when;
99

1010
import io.github.hapjava.accessories.HomekitAccessory;
11+
import io.github.hapjava.server.HomekitAccessoryCategories;
1112
import io.github.hapjava.server.HomekitAuthInfo;
1213
import io.github.hapjava.server.HomekitWebHandler;
1314
import io.github.hapjava.server.impl.http.HomekitClientConnectionFactory;
@@ -38,7 +39,8 @@ public void setup() throws Exception {
3839
when(webHandler.start(any())).thenReturn(CompletableFuture.completedFuture(PORT));
3940
advertiser = mock(JmdnsHomekitAdvertiser.class);
4041
authInfo = mock(HomekitAuthInfo.class);
41-
root = new HomekitRoot(LABEL, webHandler, authInfo, advertiser);
42+
root =
43+
new HomekitRoot(LABEL, HomekitAccessoryCategories.OTHER, webHandler, authInfo, advertiser);
4244
}
4345

4446
@Test
@@ -78,7 +80,7 @@ public void testAdvertiserStarts() throws Exception {
7880
when(authInfo.getSetupId()).thenReturn(SETUPID);
7981

8082
root.start();
81-
verify(advertiser).advertise(eq(LABEL), eq(mac), eq(PORT), eq(1), eq(SETUPID));
83+
verify(advertiser).advertise(eq(LABEL), eq(1), eq(mac), eq(PORT), eq(1), eq(SETUPID));
8284
}
8385

8486
@Test

‎src/test/java/io/github/hapjava/server/impl/jmdns/JmdnsHomekitAdvertiserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ private ServiceInfo getArgumentFromUnregister() {
6161
}
6262

6363
private void advertise() throws Exception {
64-
subject.advertise("test", "00:00:00:00:00:00", 1234, 1, "1");
64+
subject.advertise("test", 1, "00:00:00:00:00:00", 1234, 1, "1");
6565
}
6666
}

0 commit comments

Comments
 (0)
Please sign in to comment.