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>
|
<groupId>com.bwssystems.HABridge</groupId>
|
||||||
<artifactId>ha-bridge</artifactId>
|
<artifactId>ha-bridge</artifactId>
|
||||||
<version>5.3.0</version>
|
<version>5.3.0a</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
@@ -111,7 +111,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.paho</groupId>
|
<groupId>org.eclipse.paho</groupId>
|
||||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
@@ -196,7 +196,7 @@
|
|||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>3.0.0-M3</version>
|
<version>3.0.0-M3</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<skipTests>true</skipTests>
|
<skipTests>false</skipTests>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -21,6 +21,11 @@ public class ColorDecode {
|
|||||||
private static final String COLOR_BX = "${color.bx}";
|
private static final String COLOR_BX = "${color.bx}";
|
||||||
private static final String COLOR_RGBX = "${color.rgbx}";
|
private static final String COLOR_RGBX = "${color.rgbx}";
|
||||||
private static final String COLOR_HSL = "${color.hsl}";
|
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])\\}");
|
private static final Pattern COLOR_MILIGHT = Pattern.compile("\\$\\{color.milight\\:([01234])\\}");
|
||||||
|
|
||||||
public static List<Integer> convertHSLtoRGB(HueSatBri hsl) {
|
public static List<Integer> convertHSLtoRGB(HueSatBri hsl) {
|
||||||
@@ -35,46 +40,43 @@ public class ColorDecode {
|
|||||||
double g = 0.0;
|
double g = 0.0;
|
||||||
double b = 0.0;
|
double b = 0.0;
|
||||||
|
|
||||||
if(hsl.getBri() > 0)
|
if (hsl.getBri() > 0)
|
||||||
decimalBrightness = (float) (hsl.getBri() / 255.0);
|
decimalBrightness = (float) (hsl.getBri() / 255.0);
|
||||||
|
|
||||||
if(hsl.getHue() > 0) {
|
if (hsl.getHue() > 0) {
|
||||||
h = ((float)hsl.getHue() / (float)65535.0);
|
h = ((float) hsl.getHue() / (float) 65535.0);
|
||||||
h2 = h + (float)0.5;
|
h2 = h + (float) 0.5;
|
||||||
if(h2 > 1.0) {
|
if (h2 > 1.0) {
|
||||||
h2 = h2 - (float)1.0;
|
h2 = h2 - (float) 1.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(hsl.getSat() > 0) {
|
if (hsl.getSat() > 0) {
|
||||||
s = (float)(hsl.getSat() / 254.0);
|
s = (float) (hsl.getSat() / 254.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s == 0)
|
if (s == 0) {
|
||||||
{
|
r = decimalBrightness * (float) 255;
|
||||||
r = decimalBrightness * (float)255;
|
g = decimalBrightness * (float) 255;
|
||||||
g = decimalBrightness * (float)255;
|
b = decimalBrightness * (float) 255;
|
||||||
b = decimalBrightness * (float)255;
|
} else {
|
||||||
}
|
if (decimalBrightness < 0.5) {
|
||||||
else
|
var_2 = decimalBrightness * (1 + s);
|
||||||
{
|
} else {
|
||||||
if (decimalBrightness < 0.5)
|
var_2 = (decimalBrightness + s) - (s * decimalBrightness);
|
||||||
{
|
}
|
||||||
var_2 = decimalBrightness * (1 + s);
|
;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var_2 = (decimalBrightness + s) - (s * decimalBrightness);
|
|
||||||
};
|
|
||||||
|
|
||||||
var_1 = 2 * decimalBrightness - var_2;
|
var_1 = 2 * decimalBrightness - var_2;
|
||||||
float onethird = (float)0.33333;
|
float onethird = (float) 0.33333;
|
||||||
float h2Plus = (h2 + onethird);
|
float h2Plus = (h2 + onethird);
|
||||||
float h2Minus = (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: "
|
||||||
r = 255 * hue_2_rgb(var_1, var_2, h2Plus);
|
+ h2Plus + ", h2 - 1/3: " + h2Minus);
|
||||||
g = 255 * hue_2_rgb(var_1, var_2, h2);
|
r = 255 * hue_2_rgb(var_1, var_2, h2Plus);
|
||||||
b = 255 * hue_2_rgb(var_1, var_2, h2Minus);
|
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 = new ArrayList<Integer>();
|
||||||
rgb.add((int) Math.round(r));
|
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) {
|
public static float hue_2_rgb(float v1, float v2, float vh) {
|
||||||
log.debug("hue_2_rgb vh: " + vh);
|
log.debug("hue_2_rgb vh: " + vh);
|
||||||
if (vh < 0.0)
|
if (vh < 0.0) {
|
||||||
{
|
vh = vh + (float) 1;
|
||||||
vh = vh + (float)1;
|
}
|
||||||
};
|
;
|
||||||
|
|
||||||
if (vh > 1.0)
|
if (vh > 1.0) {
|
||||||
{
|
vh = vh - (float) 1;
|
||||||
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);
|
||||||
return (v1 + (v2 - v1) * (float)6.0 * vh);
|
}
|
||||||
};
|
;
|
||||||
|
|
||||||
if (((float)2.0 * vh) < 1.0)
|
if (((float) 2.0 * vh) < 1.0) {
|
||||||
{
|
return (v2);
|
||||||
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 + (v2 - v1) * (((float)2.0 / (float)3.0 - vh) * (float)6.0));
|
}
|
||||||
};
|
;
|
||||||
|
|
||||||
return (v1);
|
return (v1);
|
||||||
}
|
}
|
||||||
@@ -297,13 +299,69 @@ public class ColorDecode {
|
|||||||
notDone = true;
|
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 (request.contains(COLOR_HSL)) {
|
||||||
float[] hsb = new float[3];
|
if (colorMode == ColorData.ColorMode.HS) {
|
||||||
Color.RGBtoHSB(rgb.get(0), rgb.get(1), rgb.get(2), hsb);
|
HueSatBri hslData = (HueSatBri) colorData.getData();
|
||||||
float hue = hsb[0] * (float) 360.0;
|
request = request.replace(COLOR_HSL,
|
||||||
float sat = hsb[1] * (float) 100.0;
|
String.format("%d,%d,%d", hslData.getHue(), hslData.getSat(), hslData.getBri()));
|
||||||
float bright = hsb[2] * (float) 100.0;
|
} else {
|
||||||
request = request.replace(COLOR_HSL, String.format("%f,%f,%f", hue, sat, bright));
|
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;
|
notDone = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,63 +38,33 @@ public class UpnpListener {
|
|||||||
private String httpType;
|
private String httpType;
|
||||||
private HuePublicConfig aHueConfig;
|
private HuePublicConfig aHueConfig;
|
||||||
private Integer theUpnpSendDelay;
|
private Integer theUpnpSendDelay;
|
||||||
private String responseTemplate1 = "HTTP/1.1 200 OK\r\n" +
|
private String responseTemplate1 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||||
"HOST: %s:%s\r\n" +
|
+ "EXT:\r\n" + "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||||
"CACHE-CONTROL: max-age=100\r\n" +
|
+ HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: upnp:rootdevice\r\n" + "USN: uuid:"
|
||||||
"EXT:\r\n" +
|
+ HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n";
|
||||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
private String responseTemplate2 = "HTTP/1.1 200 OK\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||||
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
|
+ "EXT:\r\n" + "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||||
"hue-bridgeid: %s\r\n" +
|
+ HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: uuid:" + HueConstants.UUID_PREFIX
|
||||||
"ST: upnp:rootdevice\r\n" +
|
+ "%s\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
|
||||||
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\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"
|
||||||
private String responseTemplate2 = "HTTP/1.1 200 OK\r\n" +
|
+ "EXT:\r\n" + "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||||
"HOST: %s:%s\r\n" +
|
+ HueConstants.API_VERSION + "\r\n" + "hue-bridgeid: %s\r\n" + "ST: urn:schemas-upnp-org:device:basic:1\r\n"
|
||||||
"CACHE-CONTROL: max-age=100\r\n" +
|
+ "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\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" +
|
private String notifyTemplate1 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||||
"HOST: %s:%s\r\n" +
|
+ "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||||
"CACHE-CONTROL: max-age=100\r\n" +
|
+ HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n" + "NT: uuid:"
|
||||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
+ HueConstants.UUID_PREFIX + "%s\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\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" +
|
private String notifyTemplate2 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||||
"HOST: %s:%s\r\n" +
|
+ "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||||
"CACHE-CONTROL: max-age=100\r\n" +
|
+ HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n"
|
||||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
+ "NT: upnp:rootdevice\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\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" +
|
private String notifyTemplate3 = "NOTIFY * HTTP/1.1\r\n" + "HOST: %s:%s\r\n" + "CACHE-CONTROL: max-age=100\r\n"
|
||||||
"HOST: %s:%s\r\n" +
|
+ "LOCATION: %s://%s:%s/description.xml\r\n" + "SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/"
|
||||||
"CACHE-CONTROL: max-age=100\r\n" +
|
+ HueConstants.API_VERSION + "\r\n" + "NTS: ssdp:alive\r\n" + "hue-bridgeid: %s\r\n"
|
||||||
"LOCATION: %s://%s:%s/description.xml\r\n" +
|
+ "NT: urn:schemas-upnp-org:device:basic:1\r\n" + "USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\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,
|
public UpnpListener(BridgeSettings theSettings, BridgeControlDescriptor theControl,
|
||||||
UDPDatagramSender aUdpDatagramSender) throws IOException {
|
UDPDatagramSender aUdpDatagramSender) throws IOException {
|
||||||
@@ -113,7 +83,7 @@ public class UpnpListener {
|
|||||||
theSettings.getBridgeSettingsDescriptor().getHubmac());
|
theSettings.getBridgeSettingsDescriptor().getHubmac());
|
||||||
bridgeId = aHueConfig.getBridgeid();
|
bridgeId = aHueConfig.getBridgeid();
|
||||||
bridgeSNUUID = aHueConfig.getSNUUIDFromMac();
|
bridgeSNUUID = aHueConfig.getSNUUIDFromMac();
|
||||||
if(theSettings.getBridgeSecurity().isUseHttps()) {
|
if (theSettings.getBridgeSecurity().isUseHttps()) {
|
||||||
httpType = "https";
|
httpType = "https";
|
||||||
} else {
|
} else {
|
||||||
httpType = "http";
|
httpType = "http";
|
||||||
@@ -204,7 +174,8 @@ public class UpnpListener {
|
|||||||
final HashMap<String, String> values = new HashMap<String, String>();
|
final HashMap<String, String> values = new HashMap<String, String>();
|
||||||
values.put("modelid", HueConstants.MODEL_ID);
|
values.put("modelid", HueConstants.MODEL_ID);
|
||||||
values.put("bridgeid", bridgeId);
|
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);
|
jmdns.registerService(serviceInfo);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -225,8 +196,8 @@ public class UpnpListener {
|
|||||||
} catch (SocketException e1) {
|
} catch (SocketException e1) {
|
||||||
log.warn("Could not sent soTimeout on multi-cast socket");
|
log.warn("Could not sent soTimeout on multi-cast socket");
|
||||||
}
|
}
|
||||||
// Instant current, previous;
|
// Instant current, previous;
|
||||||
// previous = Instant.now();
|
// previous = Instant.now();
|
||||||
while (loopControl) { // trigger shutdown here
|
while (loopControl) { // trigger shutdown here
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||||
@@ -235,26 +206,23 @@ public class UpnpListener {
|
|||||||
if (isSSDPDiscovery(packet)) {
|
if (isSSDPDiscovery(packet)) {
|
||||||
try {
|
try {
|
||||||
sendUpnpResponse(packet);
|
sendUpnpResponse(packet);
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
log.warn("UpnpListener encountered an error sending upnp response packet. IP: "
|
log.warn("UpnpListener encountered an error sending upnp response packet. IP: "
|
||||||
+ packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
|
+ packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
|
||||||
log.debug("UpnpListener send upnp exception: ", e);
|
log.debug("UpnpListener send upnp exception: ", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
current = Instant.now();
|
* current = Instant.now(); if (ChronoUnit.MILLIS.between(previous, current) >
|
||||||
if (ChronoUnit.MILLIS.between(previous, current) > Configuration.UPNP_NOTIFY_TIMEOUT) {
|
* Configuration.UPNP_NOTIFY_TIMEOUT) { try {
|
||||||
try {
|
* sendUpnpNotify(socketAddress.getAddress()); } catch (IOException e) { log.
|
||||||
sendUpnpNotify(socketAddress.getAddress());
|
* warn("UpnpListener encountered an error sending upnp notify packets. IP: " +
|
||||||
} catch (IOException e) {
|
* packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
|
||||||
log.warn("UpnpListener encountered an error sending upnp notify packets. IP: "
|
* log.debug("UpnpListener send upnp notify exception: ", e); } previous =
|
||||||
+ packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
|
* Instant.now();
|
||||||
log.debug("UpnpListener send upnp notify exception: ", e);
|
*
|
||||||
}
|
* }
|
||||||
previous = Instant.now();
|
*/
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
} catch (SocketTimeoutException e) {
|
} catch (SocketTimeoutException e) {
|
||||||
try {
|
try {
|
||||||
sendUpnpNotify(socketAddress.getAddress());
|
sendUpnpNotify(socketAddress.getAddress());
|
||||||
@@ -277,7 +245,7 @@ public class UpnpListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
upnpMulticastSocket.close();
|
upnpMulticastSocket.close();
|
||||||
if(jmdns != null) {
|
if (jmdns != null) {
|
||||||
// Unregister all services
|
// Unregister all services
|
||||||
jmdns.unregisterAllServices();
|
jmdns.unregisterAllServices();
|
||||||
}
|
}
|
||||||
@@ -300,9 +268,8 @@ public class UpnpListener {
|
|||||||
String packetString = new String(packet.getData(), 0, packet.getLength());
|
String packetString = new String(packet.getData(), 0, packet.getLength());
|
||||||
if (packetString != null && packetString.startsWith("M-SEARCH * HTTP/1.1")
|
if (packetString != null && packetString.startsWith("M-SEARCH * HTTP/1.1")
|
||||||
&& packetString.contains("\"ssdp:discover\"")) {
|
&& packetString.contains("\"ssdp:discover\"")) {
|
||||||
if ((packetString.contains("ST: urn:schemas-upnp-org:device:basic:1") ||
|
if ((packetString.contains("ST: urn:schemas-upnp-org:device:basic:1")
|
||||||
packetString.contains("ST: upnp:rootdevice") ||
|
|| packetString.contains("ST: upnp:rootdevice") || packetString.contains("ST: ssdp:all"))) {
|
||||||
packetString.contains("ST: ssdp:all"))) {
|
|
||||||
if (traceupnp) {
|
if (traceupnp) {
|
||||||
log.info("Traceupnp: SSDP M-SEARCH packet from " + packet.getAddress().getHostAddress() + ":"
|
log.info("Traceupnp: SSDP M-SEARCH packet from " + packet.getAddress().getHostAddress() + ":"
|
||||||
+ packet.getPort());
|
+ packet.getPort());
|
||||||
@@ -328,16 +295,16 @@ public class UpnpListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void sendUpnpResponse(DatagramPacket aPacket) throws IOException {
|
protected void sendUpnpResponse(DatagramPacket aPacket) throws IOException {
|
||||||
SocketAddress requesterAddress = aPacket.getSocketAddress();
|
// SocketAddress requesterAddress = aPacket.getSocketAddress();
|
||||||
InetAddress requester = aPacket.getAddress();
|
InetAddress requester = aPacket.getAddress();
|
||||||
int sourcePort = aPacket.getPort();
|
int sourcePort = aPacket.getPort();
|
||||||
String discoveryResponse = null;
|
String discoveryResponse = null;
|
||||||
String httpLocationAddress = null;
|
String httpLocationAddress = null;
|
||||||
if(useUpnpIface) {
|
if (useUpnpIface) {
|
||||||
httpLocationAddress = upnpConfigIP;
|
httpLocationAddress = upnpConfigIP;
|
||||||
} else {
|
} else {
|
||||||
// refactored suggestion by https://github.com/pvint
|
// refactored suggestion by https://github.com/pvint
|
||||||
httpLocationAddress = AddressUtil.getOutboundAddress(requesterAddress).getHostAddress();
|
httpLocationAddress = AddressUtil.getOutboundAddress(requester.getHostAddress(), sourcePort).getHostAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -346,51 +313,53 @@ public class UpnpListener {
|
|||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
discoveryResponse = String.format(responseTemplate1, Configuration.UPNP_MULTICAST_ADDRESS,
|
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,
|
||||||
if (traceupnp) {
|
bridgeSNUUID);
|
||||||
log.info("Traceupnp: send upnp discovery template 1 with response address: " + httpLocationAddress + ":"
|
if (traceupnp) {
|
||||||
+ httpServerPort + " to address: " + requester + ":" + sourcePort);
|
log.info("Traceupnp: send upnp discovery template 1 with response address: " + httpLocationAddress + ":"
|
||||||
}
|
+ httpServerPort + " to address: " + requester.getHostAddress() + ":" + sourcePort);
|
||||||
log.debug("sendUpnpResponse to address: " + requester + ":" + sourcePort
|
}
|
||||||
+ " with discovery responseTemplate1 is <<<" + discoveryResponse + ">>>");
|
log.debug("sendUpnpResponse to address: " + requester.getHostAddress() + ":" + sourcePort
|
||||||
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
+ " with discovery responseTemplate1 is <<<" + discoveryResponse + ">>>");
|
||||||
|
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(theUpnpSendDelay);
|
Thread.sleep(theUpnpSendDelay);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
discoveryResponse = String.format(responseTemplate2, Configuration.UPNP_MULTICAST_ADDRESS,
|
discoveryResponse = String.format(responseTemplate2, Configuration.UPNP_MULTICAST_ADDRESS,
|
||||||
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId, bridgeSNUUID,
|
Configuration.UPNP_DISCOVERY_PORT, httpType, httpLocationAddress, httpServerPort, bridgeId,
|
||||||
bridgeSNUUID);
|
bridgeSNUUID, bridgeSNUUID);
|
||||||
if (traceupnp) {
|
if (traceupnp) {
|
||||||
log.info("Traceupnp: send upnp discovery template 2 with response address: " + httpLocationAddress + ":"
|
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 + ">>>");
|
+ " discovery responseTemplate2 is <<<" + discoveryResponse + ">>>");
|
||||||
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(theUpnpSendDelay);
|
Thread.sleep(theUpnpSendDelay);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
discoveryResponse = String.format(responseTemplate3,Configuration.UPNP_MULTICAST_ADDRESS,
|
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,
|
||||||
if (traceupnp) {
|
bridgeSNUUID);
|
||||||
log.info("Traceupnp: send upnp discovery template 3 with response address: " + httpLocationAddress + ":"
|
if (traceupnp) {
|
||||||
+ httpServerPort + " to address: " + requester + ":" + sourcePort);
|
log.info("Traceupnp: send upnp discovery template 3 with response address: " + httpLocationAddress + ":"
|
||||||
}
|
+ httpServerPort + " to address: " + requester.getHostAddress() + ":" + sourcePort);
|
||||||
log.debug("sendUpnpResponse to address: " + requester + ":" + sourcePort
|
}
|
||||||
+ " discovery responseTemplate3 is <<<" + discoveryResponse + ">>>");
|
log.debug("sendUpnpResponse to address: " + requester.getHostAddress() + ":" + sourcePort
|
||||||
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
+ " discovery responseTemplate3 is <<<" + discoveryResponse + ">>>");
|
||||||
|
sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendUDPResponse(byte[] udpMessage, InetAddress requester, int sourcePort) throws IOException {
|
private void sendUDPResponse(byte[] udpMessage, InetAddress requester, int sourcePort) throws IOException {
|
||||||
log.debug("Sending response string: <<<" + new String(udpMessage) + ">>>");
|
log.debug("Sending response string: <<<" + new String(udpMessage) + ">>>");
|
||||||
if(upnpOriginal) {
|
if (upnpOriginal) {
|
||||||
theUDPDatagramSender.sendUDPResponse(udpMessage, requester, sourcePort);
|
theUDPDatagramSender.sendUDPResponse(udpMessage, requester, sourcePort);
|
||||||
} else {
|
} else {
|
||||||
if (upnpMulticastSocket == null)
|
if (upnpMulticastSocket == null)
|
||||||
@@ -409,7 +378,8 @@ public class UpnpListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
notifyData = String.format(notifyTemplate1, Configuration.UPNP_MULTICAST_ADDRESS,
|
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) {
|
if (traceupnp) {
|
||||||
log.info("Traceupnp: sendUpnpNotify notifyTemplate1");
|
log.info("Traceupnp: sendUpnpNotify notifyTemplate1");
|
||||||
}
|
}
|
||||||
@@ -429,7 +399,7 @@ public class UpnpListener {
|
|||||||
}
|
}
|
||||||
log.debug("sendUpnpNotify notifyTemplate2 is <<<{}>>>", notifyData);
|
log.debug("sendUpnpNotify notifyTemplate2 is <<<{}>>>", notifyData);
|
||||||
sendUDPResponse(notifyData.getBytes(), aSocketAddress, Configuration.UPNP_DISCOVERY_PORT);
|
sendUDPResponse(notifyData.getBytes(), aSocketAddress, Configuration.UPNP_DISCOVERY_PORT);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(theUpnpSendDelay);
|
Thread.sleep(theUpnpSendDelay);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -8,14 +8,9 @@ import com.bwssystems.HABridge.BridgeSettingsDescriptor;
|
|||||||
import com.bwssystems.HABridge.api.hue.HueConstants;
|
import com.bwssystems.HABridge.api.hue.HueConstants;
|
||||||
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
|
||||||
import com.bwssystems.HABridge.util.AddressUtil;
|
import com.bwssystems.HABridge.util.AddressUtil;
|
||||||
import com.bwssystems.HABridge.util.ParseRoute;
|
|
||||||
|
|
||||||
import static spark.Spark.get;
|
import static spark.Spark.get;
|
||||||
|
|
||||||
import java.net.DatagramSocket;
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -16,24 +16,20 @@ public class AddressUtil {
|
|||||||
public static InetAddress getOutboundAddress(String remoteAddress, int remotePort) {
|
public static InetAddress getOutboundAddress(String remoteAddress, int remotePort) {
|
||||||
InetAddress localAddress = null;
|
InetAddress localAddress = null;
|
||||||
try {
|
try {
|
||||||
DatagramSocket sock = new DatagramSocket();
|
getOutboundAddress(new InetSocketAddress(remoteAddress, remotePort));
|
||||||
// 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;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ParseRoute theRoute = ParseRoute.getInstance();
|
ParseRoute theRoute = ParseRoute.getInstance();
|
||||||
try {
|
try {
|
||||||
localAddress = InetAddress.getByName(theRoute.getLocalIPAddress());
|
localAddress = InetAddress.getByName(theRoute.getLocalIPAddress());
|
||||||
|
log.warn("Error <" + e.getMessage() + "> on determining interface to reply for <" + remoteAddress
|
||||||
|
+ ">. Using default route IP Address of " + localAddress.getHostAddress());
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
|
log.error("Cannot find address for parsed local ip address: " + e.getMessage());
|
||||||
}
|
}
|
||||||
log.warn("Error <" + e.getMessage() + "> on determining interface to reply for <" + remoteAddress
|
|
||||||
+ ">. Using default route IP Address of " + localAddress.getHostAddress());
|
|
||||||
}
|
}
|
||||||
log.debug("getOutbountAddress returning IP Address of " + localAddress.getHostAddress());
|
|
||||||
|
if(localAddress != null)
|
||||||
|
log.debug("getOutbountAddress returning IP Address of " + localAddress.getHostAddress());
|
||||||
return localAddress;
|
return localAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user