mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-21 09:13:21 +00:00
Updtated bridge to be able to emulate the armzilla upnp responses.
Adding optional field edits - in progress.
This commit is contained in:
@@ -7,6 +7,8 @@ public class BridgeSettings {
|
||||
private String upnpdevicedb;
|
||||
private String veraaddress;
|
||||
private boolean upnpStrict;
|
||||
private boolean traceupnp;
|
||||
private boolean vtwocompatibility;
|
||||
|
||||
public String getUpnpConfigAddress() {
|
||||
return upnpconfigaddress;
|
||||
@@ -45,4 +47,18 @@ public class BridgeSettings {
|
||||
public void setUpnpStrict(boolean upnpStrict) {
|
||||
this.upnpStrict = upnpStrict;
|
||||
}
|
||||
public boolean isTraceupnp() {
|
||||
return traceupnp;
|
||||
}
|
||||
public void setTraceupnp(boolean traceupnp) {
|
||||
this.traceupnp = traceupnp;
|
||||
}
|
||||
|
||||
public boolean isVtwocompatibility() {
|
||||
return vtwocompatibility;
|
||||
}
|
||||
public void setVtwocompatibility(boolean vtwocompatibility) {
|
||||
this.vtwocompatibility = vtwocompatibility;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,6 +55,8 @@ public class HABridge {
|
||||
bridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", "50000"));
|
||||
bridgeSettings.setVeraAddress(System.getProperty("vera.address", "192.168.1.100"));
|
||||
bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("upnp.strict", "true")));
|
||||
bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("trace.upnp", "false")));
|
||||
bridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("vtwo.compatibility", "true")));
|
||||
|
||||
// sparkjava config directive to set ip address for the web server to listen on
|
||||
// ipAddress("0.0.0.0"); // not used
|
||||
|
||||
@@ -61,7 +61,7 @@ public class DeviceResource {
|
||||
deviceRepository.save(device);
|
||||
log.debug("Created a Device: " + request.body());
|
||||
|
||||
response.status(HttpStatus.SC_OK);
|
||||
response.status(HttpStatus.SC_CREATED);
|
||||
}
|
||||
return device;
|
||||
}, new JsonTransformer());
|
||||
|
||||
@@ -24,6 +24,10 @@ public class UpnpListener {
|
||||
private String responseAddress;
|
||||
|
||||
private boolean strict;
|
||||
|
||||
private boolean traceupnp;
|
||||
|
||||
private boolean vTwoCompatibility;
|
||||
|
||||
public UpnpListener(BridgeSettings theSettings) {
|
||||
super();
|
||||
@@ -31,6 +35,8 @@ public class UpnpListener {
|
||||
httpServerPort = Integer.valueOf(theSettings.getServerPort());
|
||||
responseAddress = theSettings.getUpnpConfigAddress();
|
||||
strict = theSettings.isUpnpStrict();
|
||||
traceupnp = theSettings.isTraceupnp();
|
||||
vTwoCompatibility = theSettings.isVtwocompatibility();
|
||||
}
|
||||
|
||||
public void startListening(){
|
||||
@@ -66,8 +72,12 @@ public class UpnpListener {
|
||||
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||
upnpMulticastSocket.receive(packet);
|
||||
String packetString = new String(packet.getData());
|
||||
if(packetString != null && packetString.startsWith("M-SEARCH * HTTP/1.1"))
|
||||
log.debug("Got SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + " body : " + packetString);
|
||||
if(packetString != null && packetString.startsWith("M-SEARCH * HTTP/1.1")) {
|
||||
if(traceupnp)
|
||||
log.info("Trace SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + " body : " + packetString);
|
||||
else
|
||||
log.debug("Got SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + " body : " + packetString);
|
||||
}
|
||||
if(isSSDPDiscovery(packetString)){
|
||||
sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort());
|
||||
}
|
||||
@@ -92,7 +102,7 @@ public class UpnpListener {
|
||||
if(body != null && body.startsWith("M-SEARCH * HTTP/1.1") && body.contains("MAN: \"ssdp:discover\"")){
|
||||
if(strict && body.contains("ST: urn:schemas-upnp-org:device:basic:1"))
|
||||
return true;
|
||||
else if (!strict)
|
||||
else if (!strict || vTwoCompatibility)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -105,8 +115,20 @@ public class UpnpListener {
|
||||
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\r\n" +
|
||||
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
|
||||
"USN: uuid:Socket-1_0-221438K0100073::urn:schemas-upnp-org:device:basic:1\r\n\r\n";
|
||||
String discoveryTemplateVTwo = "HTTP/1.1 200 OK\r\n" +
|
||||
"CACHE-CONTROL: max-age=86400\r\n" +
|
||||
"EXT:\r\n" +
|
||||
"LOCATION: http://%s:%s/description.xml\r\n" +
|
||||
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n" +
|
||||
"01-NLS: %s\r\n" +
|
||||
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
|
||||
"USN: uuid:Socket-1_0-221438K0100073::urn:Belkin:device:**\r\n\r\n";
|
||||
protected void sendUpnpResponse(DatagramSocket socket, InetAddress requester, int sourcePort) throws IOException {
|
||||
String discoveryResponse = String.format(discoveryTemplate, responseAddress, httpServerPort, getRandomUUIDString());
|
||||
String discoveryResponse = null;
|
||||
if(vTwoCompatibility)
|
||||
discoveryResponse = String.format(discoveryTemplateVTwo, responseAddress, httpServerPort, getRandomUUIDString());
|
||||
else
|
||||
discoveryResponse = String.format(discoveryTemplate, responseAddress, httpServerPort, getRandomUUIDString());
|
||||
log.debug("sndUpnpResponse: " + discoveryResponse);
|
||||
DatagramPacket response = new DatagramPacket(discoveryResponse.getBytes(), discoveryResponse.length(), requester, sourcePort);
|
||||
socket.send(response);
|
||||
|
||||
@@ -38,6 +38,53 @@ public class UpnpSettingsResource {
|
||||
+ "<depth>24</depth>\n" + "<url>hue_logo_3.png</url>\n" + "</icon>\n" + "</iconList>\n" + "</device>\n"
|
||||
+ "</root>\n";
|
||||
|
||||
private String hueTemplateVTwo = "<?xml version=\"1.0\"?>\n" +
|
||||
"<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" +
|
||||
"<specVersion>\n" +
|
||||
"<major>1</major>\n" +
|
||||
"<minor>0</minor>\n" +
|
||||
"</specVersion>\n" +
|
||||
"<URLBase>http://%s:%s/</URLBase>\n" + //hostname string
|
||||
"<device>\n" +
|
||||
"<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>\n" +
|
||||
"<friendlyName>Amazon-Echo-HA-Bridge (%s)</friendlyName>\n" +
|
||||
"<manufacturer>Royal Philips Electronics</manufacturer>\n" +
|
||||
"<manufacturerURL>http://www.bwssystems.com</manufacturerURL>\n" +
|
||||
"<modelDescription>Hue Emulator for Amazon Echo bridge</modelDescription>\n" +
|
||||
"<modelName>Philips hue bridge 2012</modelName>\n" +
|
||||
"<modelNumber>929000226503</modelNumber>\n" +
|
||||
"<modelURL>http://www.bwssystems.com/apps.html</modelURL>\n" +
|
||||
"<serialNumber>01189998819991197253</serialNumber>\n" +
|
||||
"<UDN>uuid:88f6698f-2c83-4393-bd03-cd54a9f8595</UDN>\n" +
|
||||
"<serviceList>\n" +
|
||||
"<service>\n" +
|
||||
"<serviceType>(null)</serviceType>\n" +
|
||||
"<serviceId>(null)</serviceId>\n" +
|
||||
"<controlURL>(null)</controlURL>\n" +
|
||||
"<eventSubURL>(null)</eventSubURL>\n" +
|
||||
"<SCPDURL>(null)</SCPDURL>\n" +
|
||||
"</service>\n" +
|
||||
"</serviceList>\n" +
|
||||
"<presentationURL>index.html</presentationURL>\n" +
|
||||
"<iconList>\n" +
|
||||
"<icon>\n" +
|
||||
"<mimetype>image/png</mimetype>\n" +
|
||||
"<height>48</height>\n" +
|
||||
"<width>48</width>\n" +
|
||||
"<depth>24</depth>\n" +
|
||||
"<url>hue_logo_0.png</url>\n" +
|
||||
"</icon>\n" +
|
||||
"<icon>\n" +
|
||||
"<mimetype>image/png</mimetype>\n" +
|
||||
"<height>120</height>\n" +
|
||||
"<width>120</width>\n" +
|
||||
"<depth>24</depth>\n" +
|
||||
"<url>hue_logo_3.png</url>\n" +
|
||||
"</icon>\n" +
|
||||
"</iconList>\n" +
|
||||
"</device>\n" +
|
||||
"</root>\n";
|
||||
|
||||
public UpnpSettingsResource(BridgeSettings theSettings) {
|
||||
super();
|
||||
setupListener(theSettings);
|
||||
@@ -49,7 +96,11 @@ public class UpnpSettingsResource {
|
||||
get("/description.xml", "application/xml; charset=utf-8", (request, response) -> {
|
||||
log.debug("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
|
||||
String portNumber = Integer.toString(request.port());
|
||||
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
|
||||
String filledTemplate = null;
|
||||
if(theSettings.isVtwocompatibility())
|
||||
filledTemplate = String.format(hueTemplateVTwo, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
|
||||
else
|
||||
filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
|
||||
log.debug("upnp device settings response: " + filledTemplate);
|
||||
// response.header("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
|
||||
// response.header("Pragma", "no-cache");
|
||||
|
||||
Reference in New Issue
Block a user