Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* `services` package consists of services, which grouping characteristics. e.g. `WindowCoveringService` defines mandatory and optional characteristics for a window covering service as it is defined in HAP spec.
* `server` package consists classes to run HomeKit server and handle communication
* the process is following: client, e.g. openHAB bindings, extends accessory classes, e.g. `WindowCoveringAccessory` and implements all required methods. WindowCoveringAccessory is linked already to WindowCoveringService, that in turn is link to single characteristics.
* linked service support

# HAP-Java 1.1.5

Expand Down
32 changes: 21 additions & 11 deletions src/main/java/io/github/hapjava/server/impl/HomekitRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import io.github.hapjava.characteristics.Characteristic;
import io.github.hapjava.services.Service;
import io.github.hapjava.services.impl.AccessoryInformationService;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,7 +20,7 @@ public class HomekitRegistry {

private final String label;
private final Map<Integer, HomekitAccessory> accessories;
private final Map<HomekitAccessory, List<Service>> services = new HashMap<>();
private final Map<HomekitAccessory, Map<Integer, Service>> services = new HashMap<>();
private final Map<HomekitAccessory, Map<Integer, Characteristic>> characteristics =
new HashMap<>();
private boolean isAllowUnauthenticatedRequests = false;
Expand All @@ -35,21 +40,26 @@ public synchronized void reset() {
try {
newServices = new ArrayList<>(2);
newServices.add(new AccessoryInformationService(accessory));
newServices.addAll(accessory.getServices());
for (Service service : accessory.getServices()) {
newServices.add(service);
newServices.addAll(service.getLinkedServices());
}
} catch (Exception e) {
logger.warn("Could not instantiate services for accessory " + accessory.getName(), e);
services.put(accessory, Collections.emptyList());
services.put(accessory, Collections.emptyMap());
continue;
}
Map<Integer, Characteristic> newCharacteristics = new HashMap<>();
services.put(accessory, newServices);

Map<Integer, Characteristic> newCharacteristicsByInterfaceId = new HashMap<>();
Map<Integer, Service> newServicesByInterfaceId = new HashMap<>();
for (Service service : newServices) {
iid++;
newServicesByInterfaceId.put(++iid, service);
for (Characteristic characteristic : service.getCharacteristics()) {
newCharacteristics.put(++iid, characteristic);
newCharacteristicsByInterfaceId.put(++iid, characteristic);
}
}
characteristics.put(accessory, newCharacteristics);
services.put(accessory, newServicesByInterfaceId);
characteristics.put(accessory, newCharacteristicsByInterfaceId);
}
}

Expand All @@ -61,8 +71,8 @@ public Collection<HomekitAccessory> getAccessories() {
return accessories.values();
}

public List<Service> getServices(Integer aid) {
return Collections.unmodifiableList(services.get(accessories.get(aid)));
public Map<Integer, Service> getServices(Integer aid) {
return Collections.unmodifiableMap(services.get(accessories.get(aid)));
}

public Map<Integer, Characteristic> getCharacteristics(Integer aid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
Expand All @@ -31,12 +32,18 @@ public HttpResponse listing() throws Exception {

Map<Integer, List<CompletableFuture<JsonObject>>> accessoryServiceFutures = new HashMap<>();
for (HomekitAccessory accessory : registry.getAccessories()) {
int iid = 0;
List<CompletableFuture<JsonObject>> serviceFutures = new ArrayList<>();
for (Service service : registry.getServices(accessory.getId())) {
serviceFutures.add(toJson(service, iid));
iid += service.getCharacteristics().size() + 1;

Map<Integer, Service> servicesByInterfaceId = registry.getServices(accessory.getId());

Map<Object, Integer> iidLookup = new HashMap<>();
iidLookup.putAll(swapKeyAndValue(servicesByInterfaceId));
iidLookup.putAll(swapKeyAndValue(registry.getCharacteristics(accessory.getId())));

for (Service service : servicesByInterfaceId.values()) {
serviceFutures.add(toJson(service, iidLookup));
}

accessoryServiceFutures.put(accessory.getId(), serviceFutures);
}

Expand Down Expand Up @@ -64,16 +71,18 @@ public HttpResponse listing() throws Exception {
}
}

private CompletableFuture<JsonObject> toJson(Service service, int interfaceId) throws Exception {
private CompletableFuture<JsonObject> toJson(Service service, Map<Object, Integer> iidLookup)
throws Exception {
String shortType =
service.getType().replaceAll("^0*([0-9a-fA-F]+)-0000-1000-8000-0026BB765291$", "$1");
JsonObjectBuilder builder =
Json.createObjectBuilder().add("iid", ++interfaceId).add("type", shortType);
Json.createObjectBuilder().add("iid", iidLookup.get(service)).add("type", shortType);
List<Characteristic> characteristics = service.getCharacteristics();
Collection<CompletableFuture<JsonObject>> characteristicFutures =
new ArrayList<>(characteristics.size());
for (Characteristic characteristic : characteristics) {
characteristicFutures.add(characteristic.toJson(++interfaceId));
Integer iid = iidLookup.get(characteristic);
characteristicFutures.add(characteristic.toJson(iid));
}

return CompletableFuture.allOf(
Expand All @@ -85,7 +94,20 @@ private CompletableFuture<JsonObject> toJson(Service service, int interfaceId) t
.map(future -> future.join())
.forEach(c -> jsonCharacteristics.add(c));
builder.add("characteristics", jsonCharacteristics);

if (!service.getLinkedServices().isEmpty()) {
JsonArrayBuilder jsonLinkedServices = Json.createArrayBuilder();
service.getLinkedServices().stream()
.map(iidLookup::get)
.forEach(jsonLinkedServices::add);
builder.add("linked", jsonLinkedServices);
}

return builder.build();
});
}

private <K, V> Map<V, K> swapKeyAndValue(Map<K, V> map) {
return map.entrySet().stream().collect(Collectors.toMap(Entry::getValue, Entry::getKey));
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/github/hapjava/services/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public interface Service {
* ########-####-####-####-############.
*/
String getType();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add

Suggested change
void addLinkedService(Service s);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Characteristics are handled just the same as LinkedServices in the Service interface and AbstractServiceImpl. And so I didn't add addLinkedService to the Service interface, but only to the abstract class, which is just how it's done for Characteristics as well.

/**
* List of all the services to which the service links
*
* @return the list of linked services.
*/
List<Service> getLinkedServices();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract class AbstractServiceImpl implements Service {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final String type;
private final List<Characteristic> characteristics = new LinkedList<>();
private final List<Service> linkedServices = new LinkedList<>();

/** @param type unique UUID of the service according to HAP specification. */
public AbstractServiceImpl(String type) {
Expand All @@ -28,7 +29,16 @@ public String getType() {
return type;
}

@Override
public List<Service> getLinkedServices() {
return Collections.unmodifiableList(linkedServices);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make the linkedService list persistent by adding following

Suggested change
private final List<Service> linkedServices = new LinkedList<>();
@Override
public void addLinkedService(Service s) { linkedServices.add(s);}
@Override
public List<Service> getLinkedServices() { return Collections.unmodifiableList(linkedServices); }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

public void addCharacteristic(Characteristic characteristic) {
this.characteristics.add(characteristic);
}

public void addLinkedService(Service service) {
this.linkedServices.add(service);
}
}