Skip to content

Commit

Permalink
#13 When a Bridge is discovered, confirm its config from itself
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroOne3010 committed Feb 8, 2020
1 parent cc1aea3 commit 03ce77b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
29 changes: 25 additions & 4 deletions src/main/java/io/github/zeroone3010/yahueapi/HueBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,51 @@
public class HueBridge {
private final String name;
private final String ip;
private final String mac;

public HueBridge(final String ip) {
this.name = ip;
this.ip = ip;
this(ip, ip, null);
}

public HueBridge(final String name, final String ip) {
this.name = name;
public HueBridge(final String ip, final String name, final String mac) {
this.ip = ip;
this.name = name;
this.mac = mac;
}

/**
* Returns the human-readable name of the Bridge.
*
* @return The name of the Bridge.
*/
public String getName() {
return name;
}

/**
* Returns the IP address of the Bridge.
*
* @return The IP address of the Bridge.
*/
public String getIp() {
return ip;
}

/**
* Returns the MAC address of the Bridge.
*
* @return The MAC address of the Bridge.
*/
public String getMac() {
return mac;
}

@Override
public String toString() {
return "HueBridge{" +
"name='" + name + '\'' +
", ip='" + ip + '\'' +
", mac='" + mac + '\'' +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.github.zeroone3010.yahueapi.discovery;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.zeroone3010.yahueapi.HueBridge;
import io.github.zeroone3010.yahueapi.domain.BridgeConfig;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -11,11 +16,14 @@
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Stream;

import static java.util.Arrays.asList;

public final class HueBridgeDiscoveryService {
private static final Logger logger = Logger.getLogger("HueBridgeDiscoveryService");

public enum DiscoveryMethod {
/**
* With the N-UPnP method the Philips Hue portal is polled over the internet
Expand Down Expand Up @@ -73,14 +81,26 @@ Function<Consumer<HueBridge>, HueBridgeDiscoverer> getDiscovererCreator() {
public Future<List<HueBridge>> discoverBridges(final Consumer<HueBridge> bridgeDiscoverer,
final DiscoveryMethod... discoveryMethods) {

final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

final Collection<HueBridge> bridges = new HashSet<>();
final Consumer<HueBridge> commonConsumer = (bridge) -> {
final Collection<String> ips = new HashSet<>();
final Consumer<HueBridge> commonConsumer = (discoveredBridge) -> {
final String ip = discoveredBridge.getIp();
boolean added;
synchronized (bridges) {
added = bridges.add(bridge);
synchronized (ips) {
added = ips.add(ip);
}
if (added) {
bridgeDiscoverer.accept(bridge);
final BridgeConfig config = fetchBridgeConfiguration(objectMapper, ip);
if (config != null) {
final HueBridge confirmedBridge = new HueBridge(config.getName(), ip, config.getMac());
synchronized (bridges) {
bridges.add(confirmedBridge);
}
bridgeDiscoverer.accept(confirmedBridge);
}
}
};
final List<DiscoveryMethod> methods = parseMethods(discoveryMethods);
Expand All @@ -93,6 +113,15 @@ public Future<List<HueBridge>> discoverBridges(final Consumer<HueBridge> bridgeD
return CompletableFuture.allOf(futures).thenApply(allDone -> new ArrayList<>(bridges));
}

private BridgeConfig fetchBridgeConfiguration(final ObjectMapper objectMapper, final String ip) {
try {
return objectMapper.readValue(new URL("http://" + ip + "/api/config"), BridgeConfig.class);
} catch (final IOException e) {
logger.severe("Unable to connect to a found Bridge at " + ip + ": " + e);
return null;
}
}

private List<DiscoveryMethod> parseMethods(final DiscoveryMethod[] discoveryMethods) {
final List<DiscoveryMethod> methods = new ArrayList<>();
if (discoveryMethods == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ void testIpOnly() {
}

@Test
void testNameAndIp() {
final HueBridge hueBridge = new HueBridge("West Wing", "100.100.200.300");
void testIpAndNameAndMac() {
final HueBridge hueBridge = new HueBridge("100.100.200.300", "West Wing", "11:22:33:44:55:66");
assertEquals("West Wing", hueBridge.getName());
assertEquals("100.100.200.300", hueBridge.getIp());
assertEquals("11:22:33:44:55:66", hueBridge.getMac());
}
}

0 comments on commit 03ce77b

Please sign in to comment.