From 548da932add63f5d4ecc320b4c70b9ac0becd576 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 20 Oct 2017 11:04:05 -0500 Subject: [PATCH] Updated hue hub version to be 999999999 to help alleviate version issues. Updated upnp multicast to use one socket. Updated http Handler code to check for null on conn. --- .../com/bwssystems/HABridge/HABridge.java | 12 ++++- .../HABridge/api/hue/HueConstants.java | 2 +- .../HABridge/plugins/http/HTTPHandler.java | 3 +- .../HABridge/upnp/UpnpListener.java | 44 +++++++++++-------- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/bwssystems/HABridge/HABridge.java b/src/main/java/com/bwssystems/HABridge/HABridge.java index b636a84..25742d5 100644 --- a/src/main/java/com/bwssystems/HABridge/HABridge.java +++ b/src/main/java/com/bwssystems/HABridge/HABridge.java @@ -2,6 +2,8 @@ package com.bwssystems.HABridge; import static spark.Spark.*; +import java.io.IOException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,8 +84,14 @@ public class HABridge { awaitInitialization(); // start the upnp ssdp discovery listener - theUpnpListener = new UpnpListener(bridgeSettings.getBridgeSettingsDescriptor(), bridgeSettings.getBridgeControl(), udpSender); - if(theUpnpListener.startListening()) + theUpnpListener = null; + try { + theUpnpListener = new UpnpListener(bridgeSettings.getBridgeSettingsDescriptor(), bridgeSettings.getBridgeControl(), udpSender); + } catch (IOException e) { + log.error("Could not initialize UpnpListener, exiting....", e); + theUpnpListener = null; + } + if(theUpnpListener != null && theUpnpListener.startListening()) log.info("HA Bridge (v" + theVersion.getVersion() + ") reinitialization requessted...."); else bridgeSettings.getBridgeControl().setStop(true); diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/HueConstants.java b/src/main/java/com/bwssystems/HABridge/api/hue/HueConstants.java index da2da74..72f136d 100644 --- a/src/main/java/com/bwssystems/HABridge/api/hue/HueConstants.java +++ b/src/main/java/com/bwssystems/HABridge/api/hue/HueConstants.java @@ -1,7 +1,7 @@ package com.bwssystems.HABridge.api.hue; public class HueConstants { - public final static String HUB_VERSION = "1705121051"; + public final static String HUB_VERSION = "9999999999"; public final static String API_VERSION = "1.19.0"; public final static String MODEL_ID = "BSB002"; public final static String UUID_PREFIX = "2f402f80-da50-11e1-9b23-"; diff --git a/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHandler.java b/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHandler.java index 67e9330..9179914 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHandler.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/http/HTTPHandler.java @@ -208,7 +208,8 @@ public class HTTPHandler { public void closeHandler() { try { // httpClient.close(); - conn.close(); + if(conn != null) + conn.close(); connMgr.closeExpiredConnections(); connMgr.shutdown(); } catch (IOException e) { diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java index 8f74a80..571cef4 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java @@ -20,7 +20,7 @@ import org.apache.http.conn.util.*; public class UpnpListener { private Logger log = LoggerFactory.getLogger(UpnpListener.class); - private UDPDatagramSender theUDPDatagramSender; + private MulticastSocket upnpMulticastSocket; private int httpServerPort; private String responseAddress; private boolean strict; @@ -68,9 +68,9 @@ public class UpnpListener { "NT: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n"; - public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl, UDPDatagramSender aUdpDatagramSender) { + public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl, UDPDatagramSender aUdpDatagramSender) throws IOException { super(); - theUDPDatagramSender = aUdpDatagramSender; + upnpMulticastSocket = null; httpServerPort = Integer.valueOf(theSettings.getServerPort()); responseAddress = theSettings.getUpnpConfigAddress(); strict = theSettings.isUpnpStrict(); @@ -80,21 +80,19 @@ public class UpnpListener { aHueConfig = HuePublicConfig.createConfig("temp", responseAddress, HueConstants.HUB_VERSION); bridgeId = aHueConfig.getBridgeid(); bridgeSNUUID = aHueConfig.getSNUUIDFromMac(); - } - - @SuppressWarnings("resource") - public boolean startListening(){ - log.info("UPNP Discovery Listener starting...."); - MulticastSocket upnpMulticastSocket = null; - Enumeration ifs = null; - try { upnpMulticastSocket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT); } catch(IOException e){ log.error("Upnp Discovery Port is in use, or restricted by admin (try running with sudo or admin privs): " + Configuration.UPNP_DISCOVERY_PORT + " with message: " + e.getMessage()); - return false; + throw(e); } + } + + public boolean startListening(){ + log.info("UPNP Discovery Listener starting...."); + Enumeration ifs = null; + InetSocketAddress socketAddress = new InetSocketAddress(Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT); try { ifs = NetworkInterface.getNetworkInterfaces(); @@ -163,12 +161,12 @@ public class UpnpListener { current = Instant.now(); if(ChronoUnit.MILLIS.between(previous, current) > Configuration.UPNP_NOTIFY_TIMEOUT) { - sendUpnpNotify(socketAddress.getAddress(), upnpMulticastSocket); + sendUpnpNotify(socketAddress.getAddress()); previous = Instant.now(); } } catch (SocketTimeoutException e) { - sendUpnpNotify(socketAddress.getAddress(), upnpMulticastSocket); + sendUpnpNotify(socketAddress.getAddress()); } catch (IOException e) { log.error("UpnpListener encountered an error reading socket. Shutting down", e); error = true; @@ -242,7 +240,7 @@ public class UpnpListener { } else log.debug("sendUpnpResponse discovery responseTemplate1 is <<<" + discoveryResponse + ">>>"); - theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); + sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); discoveryResponse = String.format(responseTemplate2, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID, bridgeSNUUID); if(traceupnp) { @@ -250,7 +248,7 @@ public class UpnpListener { } else log.debug("sendUpnpResponse discovery responseTemplate2 is <<<" + discoveryResponse + ">>>"); - theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); + sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); discoveryResponse = String.format(responseTemplate3, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID); if(traceupnp) { @@ -258,10 +256,18 @@ public class UpnpListener { } else log.debug("sendUpnpResponse discovery responseTemplate3 is <<<" + discoveryResponse + ">>>"); - theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); + sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); + } + + private void sendUDPResponse(byte[] udpMessage, InetAddress requester, int sourcePort) throws IOException { + log.debug("Sending response string: <<<" + new String(udpMessage) + ">>>"); + if(upnpMulticastSocket == null) + throw new IOException("Socket not initialized"); + DatagramPacket response = new DatagramPacket(udpMessage, udpMessage.length, requester, sourcePort); + upnpMulticastSocket.send(response); } - protected void sendUpnpNotify(InetAddress aSocketAddress, MulticastSocket theUpnpMulticastSocket) { + protected void sendUpnpNotify(InetAddress aSocketAddress) { String notifyData = null; log.debug("Sending notify packet for upnp."); notifyData = String.format(notifyTemplate, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID, bridgeSNUUID); @@ -270,7 +276,7 @@ public class UpnpListener { } DatagramPacket notifyPacket = new DatagramPacket(notifyData.getBytes(), notifyData.length(), aSocketAddress, Configuration.UPNP_DISCOVERY_PORT); try { - theUpnpMulticastSocket.send(notifyPacket); + upnpMulticastSocket.send(notifyPacket); } catch (IOException e1) { log.warn("UpnpListener encountered an error sending upnp notify packet. IP: " + notifyPacket.getAddress().getHostAddress() + " with message: " + e1.getMessage()); log.debug("UpnpListener send upnp notify exception: ", e1);