|
5 | 5 |
|
6 | 6 | import javax.json.Json; |
7 | 7 | import javax.json.JsonArray; |
| 8 | +import javax.json.JsonArrayBuilder; |
8 | 9 | import javax.json.JsonObject; |
9 | 10 | import javax.json.JsonObjectBuilder; |
10 | 11 | import javax.json.JsonValue; |
@@ -32,30 +33,28 @@ public CharacteristicsController(HomekitRegistry registry, SubscriptionManager s |
32 | 33 | this.registry = registry; |
33 | 34 | this.subscriptions = subscriptions; |
34 | 35 | } |
35 | | - |
| 36 | + |
36 | 37 | public HttpResponse get(HttpRequest request) throws Exception { |
37 | 38 | String uri = request.getUri(); |
38 | | - String query = uri.substring("/characteristics?q=".length()+1); |
39 | | - String[] parts = query.split("\\."); |
40 | | - if (parts.length != 2) { |
41 | | - logger.error("Unexpected characteristics request: "+uri); |
42 | | - return new NotFoundResponse(); |
| 39 | + // Characteristics are requested with /characteristics?id=1.1,2.1,3.1 |
| 40 | + String query = uri.substring("/characteristics?id=".length()); |
| 41 | + String[] ids = query.split(","); |
| 42 | + JsonArrayBuilder characteristics = Json.createArrayBuilder(); |
| 43 | + for (String id : ids) { |
| 44 | + String[] parts = id.split("\\."); |
| 45 | + if (parts.length != 2) { |
| 46 | + logger.error("Unexpected characteristics request: " + uri); |
| 47 | + return new NotFoundResponse(); |
| 48 | + } |
| 49 | + int aid = Integer.parseInt(parts[0]); |
| 50 | + int iid = Integer.parseInt(parts[1]); |
| 51 | + JsonObjectBuilder characteristic = Json.createObjectBuilder(); |
| 52 | + registry.getCharacteristics(aid).get(iid).supplyValue(characteristic); |
| 53 | + |
| 54 | + characteristics.add(characteristic.add("aid", aid).add("iid", iid).build()); |
43 | 55 | } |
44 | | - int aid = Integer.parseInt(parts[0]); |
45 | | - int iid = Integer.parseInt(parts[1]); |
46 | | - JsonObjectBuilder characteristic = Json.createObjectBuilder(); |
47 | | - registry.getCharacteristics(aid).get(iid).supplyValue(characteristic); |
48 | | - |
49 | | - JsonObject result = Json.createObjectBuilder() |
50 | | - .add("characteristics", Json.createArrayBuilder() |
51 | | - .add(characteristic |
52 | | - .add("aid", aid) |
53 | | - .add("iid", iid) |
54 | | - .build() |
55 | | - ).build() |
56 | | - ).build(); |
57 | 56 | try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
58 | | - Json.createWriter(baos).write(result); |
| 57 | + Json.createWriter(baos).write(Json.createObjectBuilder().add("characteristics", characteristics.build()).build()); |
59 | 58 | return new HapJsonResponse(baos.toByteArray()); |
60 | 59 | } |
61 | 60 | } |
|
0 commit comments