package com.bwssystems.HABridge.upnp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettings;
import com.bwssystems.HABridge.JsonTransformer;
import static spark.Spark.get;
/**
*
*/
public class UpnpSettingsResource {
private static final String UPNP_CONTEXT = "/upnp";
private Logger log = LoggerFactory.getLogger(UpnpSettingsResource.class);
private BridgeSettings theSettings;
private String hueTemplate = "\n" + "\n"
+ "\n" + "1\n" + "0\n" + "\n"
+ "http://%s:%s/\n" + // hostname string
"\n" + "urn:schemas-upnp-org:device:Basic:1\n"
+ "HA-Bridge (%s)\n"
+ "Royal Philips Electronics\n"
+ "http://www.bwssystems.com\n"
+ "Hue Emulator for HA bridge\n"
+ "Philips hue bridge 2012\n" + "929000226503\n"
+ "http://www.bwssystems.com/apps.html\n"
+ "0017880ae670\n"
+ "uuid:88f6698f-2c83-4393-bd03-cd54a9f8595\n" + "\n" + "\n"
+ "(null)\n" + "(null)\n"
+ "(null)\n" + "(null)\n"
+ "(null)\n" + "\n" + "\n"
+ "index.html\n" + "\n" + "\n"
+ "image/png\n" + "48\n" + "48\n"
+ "24\n" + "hue_logo_0.png\n" + "\n" + "\n"
+ "image/png\n" + "120\n" + "120\n"
+ "24\n" + "hue_logo_3.png\n" + "\n" + "\n" + "\n"
+ "\n";
private String hueTemplateVTwo = "\n" +
"\n" +
"\n" +
"1\n" +
"0\n" +
"\n" +
"http://%s:%s/\n" + //hostname string
"\n" +
"urn:schemas-upnp-org:device:Basic:1\n" +
"Amazon-Echo-HA-Bridge (%s)\n" +
"Royal Philips Electronics\n" +
"http://www.bwssystems.com\n" +
"Hue Emulator for Amazon Echo bridge\n" +
"Philips hue bridge 2012\n" +
"929000226503\n" +
"http://www.bwssystems.com/apps.html\n" +
"01189998819991197253\n" +
"uuid:88f6698f-2c83-4393-bd03-cd54a9f8595\n" +
"\n" +
"\n" +
"(null)\n" +
"(null)\n" +
"(null)\n" +
"(null)\n" +
"(null)\n" +
"\n" +
"\n" +
"index.html\n" +
"\n" +
"\n" +
"image/png\n" +
"48\n" +
"48\n" +
"24\n" +
"hue_logo_0.png\n" +
"\n" +
"\n" +
"image/png\n" +
"120\n" +
"120\n" +
"24\n" +
"hue_logo_3.png\n" +
"\n" +
"\n" +
"\n" +
"\n";
public UpnpSettingsResource(BridgeSettings theSettings) {
super();
this.theSettings = theSettings;
}
public void setupServer() {
log.info("Hue description service started....");
// http://ip_adress:port/description.xml which returns the xml configuration for the hue emulator
get("/description.xml", "application/xml; charset=utf-8", (request, response) -> {
if(theSettings.isTraceupnp())
log.info("Traceupnp: upnp device settings requested: " + request.params(":id") + " from " + request.ip() + ":" + request.port());
else
log.debug("upnp device settings requested: " + request.params(":id") + " from " + request.ip() + ":" + request.port());
String portNumber = Integer.toString(request.port());
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());
if(theSettings.isTraceupnp())
log.info("Traceupnp: upnp device settings response: " + filledTemplate);
else
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");
// response.header("Expires", "Mon, 1 Aug 2011 09:00:00 GMT");
// response.header("Connection", "close"); // Not sure if the server will actually close the connections by just setting the header
// response.header("Access-Control-Max-Age", "0");
// response.header("Access-Control-Allow-Origin", "*");
// response.header("Access-Control-Allow-Credentials", "true");
// response.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
// response.header("Access-Control-Allow-Headers", "Content-Type");
// response.header("Content-Type", "application/xml; charset=utf-8");
response.type("application/xml; charset=utf-8");
response.status(200);
return filledTemplate;
} );
// http://ip_address:port/upnp/settings which returns the bridge configuration settings
get(UPNP_CONTEXT + "/settings", "application/json", (request, response) -> {
log.debug("bridge settings requested from " + request.ip());
response.status(200);
return theSettings;
}, new JsonTransformer());
}
}