Updated upnp response to be closer to the hue bridge v2 spec. Updated

handling for is on when bri is 0. Updated exec to check for empty call.
This commit is contained in:
Admin
2016-09-15 15:41:59 -05:00
parent 20dedec8ab
commit c20d046b30
2 changed files with 51 additions and 21 deletions

View File

@@ -490,8 +490,12 @@ public class HueMulator implements HueErrorStringSet {
return responseString; return responseString;
} }
if (request.body().contains("\"bri\"")) if (request.body().contains("\"bri\"")) {
stateHasBri = true; if(theStateChanges.isOn() && theStateChanges.getBri() == 0)
stateHasBri = false;
else
stateHasBri = true;
}
if (request.body().contains("\"bri_inc\"")) if (request.body().contains("\"bri_inc\""))
stateHasBriInc = true; stateHasBriInc = true;
@@ -576,8 +580,12 @@ public class HueMulator implements HueErrorStringSet {
return responseString; return responseString;
} }
if (request.body().contains("\"bri\"")) if (request.body().contains("\"bri\"")) {
stateHasBri = true; if(theStateChanges.isOn() && theStateChanges.getBri() == 0)
stateHasBri = false;
else
stateHasBri = true;
}
if (request.body().contains("\"bri_inc\"")) if (request.body().contains("\"bri_inc\""))
stateHasBriInc = true; stateHasBriInc = true;
@@ -1025,19 +1033,30 @@ public class HueMulator implements HueErrorStringSet {
return theContent; return theContent;
} }
private String doExecRequest(String anItem, int intensity, String lightId) { private String doExecRequest(String anItem, int intensity, String lightId) {
log.debug("Executing request: " + anItem); log.debug("Executing request: " + anItem);
String responseString = null; String responseString = null;
try { if(anItem != null && !anItem.equalsIgnoreCase("")) {
Process p = Runtime.getRuntime().exec(replaceIntensityValue(anItem, intensity, false)); try {
log.debug("Process running: " + p.isAlive()); Process p = Runtime.getRuntime().exec(replaceIntensityValue(anItem, intensity, false));
} catch (IOException e) { log.debug("Process running: " + p.isAlive());
log.warn("Could not execute request: " + anItem, e); } catch (IOException e) {
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; log.warn("Could not execute request: " + anItem, e);
} responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
return responseString; + "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId
} + "state\"}}]";
}
}
else {
log.warn("Could not execute request. Request is empty.");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId
+ "state\"}}]";
}
return responseString;
}
private String formatSuccessHueResponse(StateChangeBody state, String body, String lightId, DeviceState deviceState) { private String formatSuccessHueResponse(StateChangeBody state, String body, String lightId, DeviceState deviceState) {
String responseString = "["; String responseString = "[";

View File

@@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeControlDescriptor; import com.bwssystems.HABridge.BridgeControlDescriptor;
import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Configuration; import com.bwssystems.HABridge.Configuration;
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.*;
@@ -24,6 +25,15 @@ public class UpnpListener {
private BridgeControlDescriptor bridgeControl; private BridgeControlDescriptor bridgeControl;
private boolean discoveryTemplateLatest; private boolean discoveryTemplateLatest;
private String discoveryTemplate = "HTTP/1.1 200 OK\r\n" + private String discoveryTemplate = "HTTP/1.1 200 OK\r\n" +
"HOST: %s:%s" +
"CACHE-CONTROL: max-age=86400\r\n" +
"EXT:\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" +
"SERVER: FreeRTOS/7.4.2 UPnP/1.0 IpBridge/1.10.0\r\n" +
"hue-bridgeid: %s\r\n" +
"ST: upnp:rootdevice\r\n" +
"USN: uuid:2f402f80-da50-11e1-9b23-001788102201::upnp:rootdevice\r\n\r\n";
private String discoveryTemplate091516 = "HTTP/1.1 200 OK\r\n" +
"CACHE-CONTROL: max-age=86400\r\n" + "CACHE-CONTROL: max-age=86400\r\n" +
"EXT:\r\n" + "EXT:\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" + "LOCATION: http://%s:%s/description.xml\r\n" +
@@ -33,10 +43,11 @@ public class UpnpListener {
private String discoveryTemplateOld = "HTTP/1.1 200 OK\r\n" + private String discoveryTemplateOld = "HTTP/1.1 200 OK\r\n" +
"CACHE-CONTROL: max-age=86400\r\n" + "CACHE-CONTROL: max-age=86400\r\n" +
"EXT:\r\n" + "EXT:\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" + "LOCATION: http://%s:%s/upnp/amazon-ha-bridge/setup.xml\r\n" +
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\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" + "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"; "USN: uuid:Socket-1_0-221438K0100073::urn:Belkin:device:**\r\n\r\n";
public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl) { public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl) {
super(); super();
@@ -208,9 +219,9 @@ public class UpnpListener {
protected void sendUpnpResponse(DatagramSocket socket, InetAddress requester, int sourcePort) throws IOException { protected void sendUpnpResponse(DatagramSocket socket, InetAddress requester, int sourcePort) throws IOException {
String discoveryResponse = null; String discoveryResponse = null;
if(discoveryTemplateLatest) if(discoveryTemplateLatest)
discoveryResponse = String.format(discoveryTemplate, responseAddress, httpServerPort); discoveryResponse = String.format(discoveryTemplate, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, HuePublicConfig.createConfig("temp", responseAddress).getBridgeid());
else else
discoveryResponse = String.format(discoveryTemplateOld, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort); discoveryResponse = String.format(discoveryTemplate091516, responseAddress, httpServerPort);
if(traceupnp) if(traceupnp)
log.info("Traceupnp: sendUpnpResponse discovery template with address: " + responseAddress + " and port: " + httpServerPort); log.info("Traceupnp: sendUpnpResponse discovery template with address: " + responseAddress + " and port: " + httpServerPort);
else else