mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 18:24:36 +00:00
Updated handling for use upnp iface and start color upgrades
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>com.bwssystems.HABridge</groupId>
|
||||
<artifactId>ha-bridge</artifactId>
|
||||
<version>5.3.0</version>
|
||||
<version>5.3.0a</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>HA Bridge</name>
|
||||
@@ -111,7 +111,7 @@
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
@@ -196,7 +196,7 @@
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M3</version>
|
||||
<configuration>
|
||||
<skipTests>true</skipTests>
|
||||
<skipTests>false</skipTests>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
||||
@@ -21,6 +21,11 @@ public class ColorDecode {
|
||||
private static final String COLOR_BX = "${color.bx}";
|
||||
private static final String COLOR_RGBX = "${color.rgbx}";
|
||||
private static final String COLOR_HSL = "${color.hsl}";
|
||||
private static final String COLOR_H = "${color.h}";
|
||||
private static final String COLOR_S = "${color.s}";
|
||||
private static final String COLOR_L = "${color.l}";
|
||||
private static final String COLOR_XY = "${color.xy}";
|
||||
private static final String COLOR_BRI = "${colorbri}";
|
||||
private static final Pattern COLOR_MILIGHT = Pattern.compile("\\$\\{color.milight\\:([01234])\\}");
|
||||
|
||||
public static List<Integer> convertHSLtoRGB(HueSatBri hsl) {
|
||||
@@ -49,32 +54,29 @@ public class ColorDecode {
|
||||
s = (float) (hsl.getSat() / 254.0);
|
||||
}
|
||||
|
||||
if (s == 0)
|
||||
{
|
||||
if (s == 0) {
|
||||
r = decimalBrightness * (float) 255;
|
||||
g = decimalBrightness * (float) 255;
|
||||
b = decimalBrightness * (float) 255;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (decimalBrightness < 0.5)
|
||||
{
|
||||
} else {
|
||||
if (decimalBrightness < 0.5) {
|
||||
var_2 = decimalBrightness * (1 + s);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
var_2 = (decimalBrightness + s) - (s * decimalBrightness);
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
var_1 = 2 * decimalBrightness - var_2;
|
||||
float onethird = (float) 0.33333;
|
||||
float h2Plus = (h2 + onethird);
|
||||
float h2Minus = (h2 - onethird);
|
||||
log.debug("calculate HSL vars - var1: " + var_1 + ", var_2: " + var_2 + ", h2: " + h2 + ", h2 + 1/3: " + h2Plus + ", h2 - 1/3: " + h2Minus);
|
||||
log.debug("calculate HSL vars - var1: " + var_1 + ", var_2: " + var_2 + ", h2: " + h2 + ", h2 + 1/3: "
|
||||
+ h2Plus + ", h2 - 1/3: " + h2Minus);
|
||||
r = 255 * hue_2_rgb(var_1, var_2, h2Plus);
|
||||
g = 255 * hue_2_rgb(var_1, var_2, h2);
|
||||
b = 255 * hue_2_rgb(var_1, var_2, h2Minus);
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
rgb = new ArrayList<Integer>();
|
||||
rgb.add((int) Math.round(r));
|
||||
@@ -88,30 +90,30 @@ public class ColorDecode {
|
||||
|
||||
public static float hue_2_rgb(float v1, float v2, float vh) {
|
||||
log.debug("hue_2_rgb vh: " + vh);
|
||||
if (vh < 0.0)
|
||||
{
|
||||
if (vh < 0.0) {
|
||||
vh = vh + (float) 1;
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
if (vh > 1.0)
|
||||
{
|
||||
if (vh > 1.0) {
|
||||
vh = vh - (float) 1;
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
if (((float)6.0 * vh) < 1.0)
|
||||
{
|
||||
if (((float) 6.0 * vh) < 1.0) {
|
||||
return (v1 + (v2 - v1) * (float) 6.0 * vh);
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
if (((float)2.0 * vh) < 1.0)
|
||||
{
|
||||
if (((float) 2.0 * vh) < 1.0) {
|
||||
return (v2);
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
if ((3.0 * vh) < 2.0)
|
||||
{
|
||||
if ((3.0 * vh) < 2.0) {
|
||||
return (v1 + (v2 - v1) * (((float) 2.0 / (float) 3.0 - vh) * (float) 6.0));
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
return (v1);
|
||||
}
|
||||
@@ -297,13 +299,69 @@ public class ColorDecode {
|
||||
notDone = true;
|
||||
}
|
||||
|
||||
if (request.contains(COLOR_XY)) {
|
||||
if (colorMode == ColorData.ColorMode.XY) {
|
||||
List<Double> xyData = (List<Double>) colorData.getData();
|
||||
request = request.replace(COLOR_XY, String.format("%f,%f", xyData.get(0), xyData.get(1)));
|
||||
} else {
|
||||
List<Double> xyData = (List<Double>) colorData.getData();
|
||||
request = request.replace(COLOR_XY, String.format("%f,%f", xyData.get(0), xyData.get(1)));
|
||||
}
|
||||
notDone = true;
|
||||
}
|
||||
|
||||
if (request.contains(COLOR_H)) {
|
||||
if (colorMode == ColorData.ColorMode.HS) {
|
||||
HueSatBri hslData = (HueSatBri) colorData.getData();
|
||||
request = request.replace(COLOR_H, String.format("%d", hslData.getHue()));
|
||||
} else {
|
||||
float[] hsb = new float[3];
|
||||
Color.RGBtoHSB(rgb.get(0), rgb.get(1), rgb.get(2), hsb);
|
||||
float hue = hsb[0] * (float) 360.0;
|
||||
request = request.replace(COLOR_H, String.format("%f", hue));
|
||||
}
|
||||
notDone = true;
|
||||
}
|
||||
|
||||
if (request.contains(COLOR_S)) {
|
||||
if (colorMode == ColorData.ColorMode.HS) {
|
||||
HueSatBri hslData = (HueSatBri) colorData.getData();
|
||||
request = request.replace(COLOR_S, String.format("%d", hslData.getSat()));
|
||||
} else {
|
||||
float[] hsb = new float[3];
|
||||
Color.RGBtoHSB(rgb.get(0), rgb.get(1), rgb.get(2), hsb);
|
||||
float sat = hsb[1] * (float) 100.0;
|
||||
request = request.replace(COLOR_S, String.format("%f", sat));
|
||||
}
|
||||
notDone = true;
|
||||
}
|
||||
|
||||
if (request.contains(COLOR_L)) {
|
||||
if (colorMode == ColorData.ColorMode.HS) {
|
||||
HueSatBri hslData = (HueSatBri) colorData.getData();
|
||||
request = request.replace(COLOR_L, String.format("%d", hslData.getBri()));
|
||||
} else {
|
||||
float[] hsb = new float[3];
|
||||
Color.RGBtoHSB(rgb.get(0), rgb.get(1), rgb.get(2), hsb);
|
||||
float bright = hsb[2] * (float) 100.0;
|
||||
request = request.replace(COLOR_L, String.format("%f", bright));
|
||||
}
|
||||
notDone = true;
|
||||
}
|
||||
|
||||
if (request.contains(COLOR_HSL)) {
|
||||
if (colorMode == ColorData.ColorMode.HS) {
|
||||
HueSatBri hslData = (HueSatBri) colorData.getData();
|
||||
request = request.replace(COLOR_HSL,
|
||||
String.format("%d,%d,%d", hslData.getHue(), hslData.getSat(), hslData.getBri()));
|
||||
} else {
|
||||
float[] hsb = new float[3];
|
||||
Color.RGBtoHSB(rgb.get(0), rgb.get(1), rgb.get(2), hsb);
|
||||
float hue = hsb[0] * (float) 360.0;
|
||||
float sat = hsb[1] * (float) 100.0;
|
||||
float bright = hsb[2] * (float) 100.0;
|
||||
request = request.replace(COLOR_HSL, String.format("%f,%f,%f", hue, sat, bright));
|
||||
}
|
||||
notDone = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,63 +38,33 @@ public class UpnpListener {
|
||||
private String httpType;
|
||||
private HuePublicConfig aHueConfig;
|
||||
private Integer theUpnpSendDelay;
|
||||
private String responseTemplate1 = "HTTP/1.1 200 OK\r\n" +
|
||||
"HOST: %s:%s\r\n" +
|
||||
"CACHE-CONTROL: max-age=100\r\n" +
|
||||
"EXT:\r\n" +
|
||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
||||
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
|
||||
"hue-bridgeid: %s\r\n" +
|
||||
"ST: upnp:rootdevice\r\n" +
|
||||
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n";
|
||||
private String responseTemplate2 = "HTTP/1.1 200 OK\r\n" +
|
||||
"HOST: %s:%s\r\n" +
|
||||
"CACHE-CONTROL: max-age=100\r\n" +
|
||||
"EXT:\r\n" +
|
||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
||||
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
|
||||
"hue-bridgeid: %s\r\n" +
|
||||
"ST: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n" +
|
||||
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
private String responseTemplate3 = "HTTP/1.1 200 OK\r\n" +
|
||||
"HOST: %s:%s\r\n" +
|
||||
"CACHE-CONTROL: max-age=100\r\n" +
|
||||
"EXT:\r\n" +
|
||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
||||
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
|
||||
"hue-bridgeid: %s\r\n" +
|
||||
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
|
||||
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
private String responseTemplate1 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||
+ "EXT:\r\n" + "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||
+ HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: upnp:rootdevice\r\n" + "USN: uuid:"
|
||||
+ HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n";
|
||||
private String responseTemplate2 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||
+ "EXT:\r\n" + "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||
+ HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: uuid:" + HueConstants.UUID_PREFIX
|
||||
+ "%s\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
private String responseTemplate3 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||
+ "EXT:\r\n" + "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||
+ HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: urn:schemas-upnp-org:device:basic:1\r\n"
|
||||
+ "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
|
||||
private String notifyTemplate1 = "NOTIFY * HTTP/1.1\r\n" +
|
||||
"HOST: %s:%s\r\n" +
|
||||
"CACHE-CONTROL: max-age=100\r\n" +
|
||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
||||
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
|
||||
"NTS: ssdp:alive\r\n" +
|
||||
"hue-bridgeid: %s\r\n" +
|
||||
"NT: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n" +
|
||||
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
private String notifyTemplate1 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||
+ "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||
+ HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n" + "NT: uuid:"
|
||||
+ HueConstants.UUID_PREFIX + "%s\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
|
||||
private String notifyTemplate2 = "NOTIFY * HTTP/1.1\r\n" +
|
||||
"HOST: %s:%s\r\n" +
|
||||
"CACHE-CONTROL: max-age=100\r\n" +
|
||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
||||
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
|
||||
"NTS: ssdp:alive\r\n" +
|
||||
"hue-bridgeid: %s\r\n" +
|
||||
"NT: upnp:rootdevice\r\n" +
|
||||
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n";
|
||||
private String notifyTemplate2 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||
+ "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||
+ HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n"
|
||||
+ "NT: upnp:rootdevice\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n";
|
||||
|
||||
private String notifyTemplate3 = "NOTIFY * HTTP/1.1\r\n" +
|
||||
"HOST: %s:%s\r\n" +
|
||||
"CACHE-CONTROL: max-age=100\r\n" +
|
||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
||||
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
|
||||
"NTS: ssdp:alive\r\n" +
|
||||
"hue-bridgeid: %s\r\n" +
|
||||
"NT: urn:schemas-upnp-org:device:basic:1\r\n" +
|
||||
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
private String notifyTemplate3 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||
+ "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||
+ HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n"
|
||||
+ "NT: urn:schemas-upnp-org:device:basic:1\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||
|
||||
public UpnpListener(BridgeSettings theSettings, BridgeControlDescriptor theControl,
|
||||
UDPDatagramSender aUdpDatagramSender) throws IOException {
|
||||
@@ -204,7 +174,8 @@ public class UpnpListener {
|
||||
final HashMap<String, String> values = new HashMap<String, String>();
|
||||
values.put("modelid", HueConstants.MODEL_ID);
|
||||
values.put("bridgeid", bridgeId);
|
||||
ServiceInfo serviceInfo = ServiceInfo.create("_hue._tcp.local.", "Philips Hue - " + bridgeId.substring(bridgeId.length() - 6), httpServerPort, 0, 0, values);
|
||||
ServiceInfo serviceInfo = ServiceInfo.create("_hue._tcp.local.",
|
||||
"Philips Hue - " + bridgeId.substring(bridgeId.length() - 6), httpServerPort, 0, 0, values);
|
||||
jmdns.registerService(serviceInfo);
|
||||
|
||||
} catch (IOException e) {
|
||||
@@ -235,25 +206,22 @@ public class UpnpListener {
|
||||
if (isSSDPDiscovery(packet)) {
|
||||
try {
|
||||
sendUpnpResponse(packet);
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
log.warn("UpnpListener encountered an error sending upnp response packet. IP: "
|
||||
+ packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
|
||||
log.debug("UpnpListener send upnp exception: ", e);
|
||||
}
|
||||
}
|
||||
/*
|
||||
current = Instant.now();
|
||||
if (ChronoUnit.MILLIS.between(previous, current) > Configuration.UPNP_NOTIFY_TIMEOUT) {
|
||||
try {
|
||||
sendUpnpNotify(socketAddress.getAddress());
|
||||
} catch (IOException e) {
|
||||
log.warn("UpnpListener encountered an error sending upnp notify packets. IP: "
|
||||
+ packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
|
||||
log.debug("UpnpListener send upnp notify exception: ", e);
|
||||
}
|
||||
previous = Instant.now();
|
||||
|
||||
}
|
||||
* current = Instant.now(); if (ChronoUnit.MILLIS.between(previous, current) >
|
||||
* Configuration.UPNP_NOTIFY_TIMEOUT) { try {
|
||||
* sendUpnpNotify(socketAddress.getAddress()); } catch (IOException e) { log.
|
||||
* warn("UpnpListener encountered an error sending upnp notify packets. IP: " +
|
||||
* packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
|
||||
* log.debug("UpnpListener send upnp notify exception: ", e); } previous =
|
||||
* Instant.now();
|
||||
*
|
||||
* }
|
||||
*/
|
||||
} catch (SocketTimeoutException e) {
|
||||
try {
|
||||
@@ -300,9 +268,8 @@ public class UpnpListener {
|
||||
String packetString = new String(packet.getData(), 0, packet.getLength());
|
||||
if (packetString != null && packetString.startsWith("M-SEARCH * HTTP/1.1")
|
||||
&& packetString.contains("\"ssdp:discover\"")) {
|
||||
if ((packetString.contains("ST: urn:schemas-upnp-org:device:basic:1") ||
|
||||
packetString.contains("ST: upnp:rootdevice") ||
|
||||
packetString.contains("ST: ssdp:all"))) {
|
||||
if ((packetString.contains("ST: urn:schemas-upnp-org:device:basic:1")
|
||||
|| packetString.contains("ST: upnp:rootdevice") || packetString.contains("ST: ssdp:all"))) {
|
||||
if (traceupnp) {
|
||||
log.info("Traceupnp: SSDP M-SEARCH packet from " + packet.getAddress().getHostAddress() + ":"
|
||||
+ packet.getPort());
|
||||
@@ -328,7 +295,7 @@ public class UpnpListener {
|
||||
}
|
||||
|
||||
protected void sendUpnpResponse(DatagramPacket aPacket) throws IOException {
|
||||
SocketAddress requesterAddress = aPacket.getSocketAddress();
|
||||
// SocketAddress requesterAddress = aPacket.getSocketAddress();
|
||||
InetAddress requester = aPacket.getAddress();
|
||||
int sourcePort = aPacket.getPort();
|
||||
String discoveryResponse = null;
|
||||
@@ -337,7 +304,7 @@ public class UpnpListener {
|
||||
httpLocationAddress = upnpConfigIP;
|
||||
} else {
|
||||
// refactored suggestion by https://github.com/pvint
|
||||
httpLocationAddress = AddressUtil.getOutboundAddress(requesterAddress).getHostAddress();
|
||||
httpLocationAddress = AddressUtil.getOutboundAddress(requester.getHostAddress(), sourcePort).getHostAddress();
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -347,12 +314,13 @@ public class UpnpListener {
|
||||
}
|
||||
|
||||
discoveryResponse = String.format(responseTemplate1, Configuration.UPNP_MULTICAST_ADDRESS,
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId, bridgeSNUUID);
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId,
|
||||
bridgeSNUUID);
|
||||
if (traceupnp) {
|
||||
log.info("Traceupnp: send upnp discovery template 1 with response address: " + httpLocationAddress + ":"
|
||||
+ httpServerPort + " to address: " + requester + ":" + sourcePort);
|
||||
+ httpServerPort + " to address: " + requester.getHostAddress() + ":" + sourcePort);
|
||||
}
|
||||
log.debug("sendUpnpResponse to address: " + requester + ":" + sourcePort
|
||||
log.debug("sendUpnpResponse to address: " + requester.getHostAddress() + ":" + sourcePort
|
||||
+ " with discovery responseTemplate1 is <<<" + discoveryResponse + ">>>");
|
||||
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
||||
|
||||
@@ -362,13 +330,13 @@ public class UpnpListener {
|
||||
// noop
|
||||
}
|
||||
discoveryResponse = String.format(responseTemplate2, Configuration.UPNP_MULTICAST_ADDRESS,
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId, bridgeSNUUID,
|
||||
bridgeSNUUID);
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId,
|
||||
bridgeSNUUID, bridgeSNUUID);
|
||||
if (traceupnp) {
|
||||
log.info("Traceupnp: send upnp discovery template 2 with response address: " + httpLocationAddress + ":"
|
||||
+ httpServerPort + " to address: " + requester + ":" + sourcePort);
|
||||
+ httpServerPort + " to address: " + requester.getHostAddress() + ":" + sourcePort);
|
||||
}
|
||||
log.debug("sendUpnpResponse to address: " + requester + ":" + sourcePort
|
||||
log.debug("sendUpnpResponse to address: " + requester.getHostAddress() + ":" + sourcePort
|
||||
+ " discovery responseTemplate2 is <<<" + discoveryResponse + ">>>");
|
||||
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
||||
|
||||
@@ -378,12 +346,13 @@ public class UpnpListener {
|
||||
// noop
|
||||
}
|
||||
discoveryResponse = String.format(responseTemplate3, Configuration.UPNP_MULTICAST_ADDRESS,
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId, bridgeSNUUID);
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId,
|
||||
bridgeSNUUID);
|
||||
if (traceupnp) {
|
||||
log.info("Traceupnp: send upnp discovery template 3 with response address: " + httpLocationAddress + ":"
|
||||
+ httpServerPort + " to address: " + requester + ":" + sourcePort);
|
||||
+ httpServerPort + " to address: " + requester.getHostAddress() + ":" + sourcePort);
|
||||
}
|
||||
log.debug("sendUpnpResponse to address: " + requester + ":" + sourcePort
|
||||
log.debug("sendUpnpResponse to address: " + requester.getHostAddress() + ":" + sourcePort
|
||||
+ " discovery responseTemplate3 is <<<" + discoveryResponse + ">>>");
|
||||
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
||||
}
|
||||
@@ -409,7 +378,8 @@ public class UpnpListener {
|
||||
}
|
||||
|
||||
notifyData = String.format(notifyTemplate1, Configuration.UPNP_MULTICAST_ADDRESS,
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, upnpConfigIP, httpServerPort, bridgeId, bridgeSNUUID, bridgeSNUUID);
|
||||
Configuration.UPNP_DISCOVERY_PORT, httpType, upnpConfigIP, httpServerPort, bridgeId, bridgeSNUUID,
|
||||
bridgeSNUUID);
|
||||
if (traceupnp) {
|
||||
log.info("Traceupnp: sendUpnpNotify notifyTemplate1");
|
||||
}
|
||||
|
||||
@@ -8,14 +8,9 @@ import com.bwssystems.HABridge.BridgeSettingsDescriptor;
|
||||
import com.bwssystems.HABridge.api.hue.HueConstants;
|
||||
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
||||
import com.bwssystems.HABridge.util.AddressUtil;
|
||||
import com.bwssystems.HABridge.util.ParseRoute;
|
||||
|
||||
import static spark.Spark.get;
|
||||
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -16,23 +16,19 @@ public class AddressUtil {
|
||||
public static InetAddress getOutboundAddress(String remoteAddress, int remotePort) {
|
||||
InetAddress localAddress = null;
|
||||
try {
|
||||
DatagramSocket sock = new DatagramSocket();
|
||||
// connect is needed to bind the socket and retrieve the local address
|
||||
// later (it would return 0.0.0.0 otherwise)
|
||||
sock.connect(new InetSocketAddress(remoteAddress, remotePort));
|
||||
localAddress = sock.getLocalAddress();
|
||||
sock.disconnect();
|
||||
sock.close();
|
||||
sock = null;
|
||||
getOutboundAddress(new InetSocketAddress(remoteAddress, remotePort));
|
||||
} catch (Exception e) {
|
||||
ParseRoute theRoute = ParseRoute.getInstance();
|
||||
try {
|
||||
localAddress = InetAddress.getByName(theRoute.getLocalIPAddress());
|
||||
} catch (Exception e1) {
|
||||
}
|
||||
log.warn("Error <" + e.getMessage() + "> on determining interface to reply for <" + remoteAddress
|
||||
+ ">. Using default route IP Address of " + localAddress.getHostAddress());
|
||||
} catch (Exception e1) {
|
||||
log.error("Cannot find address for parsed local ip address: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if(localAddress != null)
|
||||
log.debug("getOutbountAddress returning IP Address of " + localAddress.getHostAddress());
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user