From eee0394f2057c3d77473e6f7d0fdd1e4ff7ff555 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 28 Aug 2015 15:56:36 -0500 Subject: [PATCH] updated upnp discovery calls to be more like the hue. --- pom.xml | 2 +- .../HABridge/api/hue/HueApiResponse.java | 37 +++- .../HABridge/api/hue/HueConfig.java | 198 ++++++++++++++++++ .../bwssystems/HABridge/api/hue/Swupdate.java | 52 +++++ .../HABridge/api/hue/WhitelistEntry.java | 49 +++++ .../bwssystems/HABridge/hue/HueMulator.java | 7 +- .../HABridge/upnp/UpnpSettingsResource.java | 21 +- src/main/resources/public/index.html | 2 +- 8 files changed, 359 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/bwssystems/HABridge/api/hue/HueConfig.java create mode 100644 src/main/java/com/bwssystems/HABridge/api/hue/Swupdate.java create mode 100644 src/main/java/com/bwssystems/HABridge/api/hue/WhitelistEntry.java diff --git a/pom.xml b/pom.xml index d1e8824..5049512 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 0.3.3 + 0.3.5 jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java b/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java index 12d7bb9..1134c93 100644 --- a/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java +++ b/src/main/java/com/bwssystems/HABridge/api/hue/HueApiResponse.java @@ -1,5 +1,6 @@ package com.bwssystems.HABridge.api.hue; +import java.util.HashMap; import java.util.Map; import com.bwssystems.HABridge.api.hue.DeviceResponse; @@ -9,12 +10,46 @@ import com.bwssystems.HABridge.api.hue.DeviceResponse; */ public class HueApiResponse { private Map lights; + private Map scenes; + private Map groups; + private HueConfig config; - public Map getLights() { + public HueApiResponse(String name, String ipaddress, String username, String userid) { + super(); + this.setConfig(HueConfig.createConfig(name, ipaddress, username, userid)); + this.setGroups(new HashMap<>()); + this.setScenes(new HashMap<>()); + } + + public Map getLights() { return lights; } public void setLights(Map lights) { this.lights = lights; } + + public Map getScenes() { + return scenes; + } + + public void setScenes(Map scenes) { + this.scenes = scenes; + } + + public Map getGroups() { + return groups; + } + + public void setGroups(Map groups) { + this.groups = groups; + } + + public HueConfig getConfig() { + return config; + } + + public void setConfig(HueConfig config) { + this.config = config; + } } diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/HueConfig.java b/src/main/java/com/bwssystems/HABridge/api/hue/HueConfig.java new file mode 100644 index 0000000..5b8cf4f --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/api/hue/HueConfig.java @@ -0,0 +1,198 @@ +package com.bwssystems.HABridge.api.hue; + +import java.util.HashMap; +import java.util.Map; + +public class HueConfig +{ + private Boolean portalservices; + private String gateway; + private String mac; + private String swversion; + private String apiversion; + private Boolean linkbutton; + private String ipaddress; + private Integer proxyport; + private Swupdate swupdate; + private String netmask; + private String name; + private Boolean dhcp; + private String UTC; + private String proxyaddress; + private String localtime; + private String timezone; + private String zigbeechannel; + private Map whitelist; + + public static HueConfig createConfig(String name, String ipaddress, String devicetype, String userid) { + HueConfig aConfig = new HueConfig(); + aConfig.setApiversion("1.4.0"); + aConfig.setPortalservices(false); + aConfig.setGateway("192.168.1.1"); + aConfig.setMac("00:00:88:00:bb:ee"); + aConfig.setSwversion("01005215"); + aConfig.setLinkbutton(false); + aConfig.setIpaddress(ipaddress); + aConfig.setProxyport(0); + aConfig.setSwupdate(Swupdate.createSwupdate()); + aConfig.setNetmask("255.255.255.0"); + aConfig.setName(name); + aConfig.setDhcp(true); + aConfig.setUtc("2014-07-17T09:27:35"); + aConfig.setProxyaddress("0.0.0.0"); + aConfig.setLocaltime("2014-07-17T11:27:35"); + aConfig.setTimezone("America/Chicago"); + aConfig.setZigbeechannel("6"); + Map awhitelist = new HashMap<>(); + awhitelist.put(userid, WhitelistEntry.createEntry(devicetype)); + aConfig.setWhitelist(awhitelist); + + + return aConfig; + } + + + public Boolean getPortalservices() { + return portalservices; + } + + public void setPortalservices(Boolean portalservices) { + this.portalservices = portalservices; + } + + public String getGateway() { + return gateway; + } + + public void setGateway(String gateway) { + this.gateway = gateway; + } + + public String getMac() { + return mac; + } + + public void setMac(String mac) { + this.mac = mac; + } + + public String getSwversion() { + return swversion; + } + + public void setSwversion(String swversion) { + this.swversion = swversion; + } + + public Boolean getLinkbutton() { + return linkbutton; + } + + public void setLinkbutton(Boolean linkbutton) { + this.linkbutton = linkbutton; + } + + public String getIpaddress() { + return ipaddress; + } + + public void setIpaddress(String ipaddress) { + this.ipaddress = ipaddress; + } + + public Integer getProxyport() { + return proxyport; + } + + public void setProxyport(Integer proxyport) { + this.proxyport = proxyport; + } + + public Swupdate getSwupdate() { + return swupdate; + } + + public void setSwupdate(Swupdate swupdate) { + this.swupdate = swupdate; + } + + public String getNetmask() { + return netmask; + } + + public void setNetmask(String netmask) { + this.netmask = netmask; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Boolean getDhcp() { + return dhcp; + } + + public void setDhcp(Boolean dhcp) { + this.dhcp = dhcp; + } + + public String getUtc() { + return UTC; + } + + public void setUtc(String utc) { + this.UTC = utc; + } + + public String getProxyaddress() { + return proxyaddress; + } + + public void setProxyaddress(String proxyaddress) { + this.proxyaddress = proxyaddress; + } + + public Map getWhitelist() { + return whitelist; + } + + public void setWhitelist(Map whitelist) { + this.whitelist = whitelist; + } + + public String getApiversion() { + return apiversion; + } + + public void setApiversion(String apiversion) { + this.apiversion = apiversion; + } + + public String getLocaltime() { + return localtime; + } + + public void setLocaltime(String localtime) { + this.localtime = localtime; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + + public String getZigbeechannel() { + return zigbeechannel; + } + + public void setZigbeechannel(String zigbeechannel) { + this.zigbeechannel = zigbeechannel; + } +} diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/Swupdate.java b/src/main/java/com/bwssystems/HABridge/api/hue/Swupdate.java new file mode 100644 index 0000000..9824095 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/api/hue/Swupdate.java @@ -0,0 +1,52 @@ +package com.bwssystems.HABridge.api.hue; + + +public class Swupdate +{ + private String text; + private Boolean notify; + private Integer updatestate; + private String url; + + public static Swupdate createSwupdate() { + Swupdate aSwupdate = new Swupdate(); + aSwupdate.setNotify(false); + aSwupdate.setText(""); + aSwupdate.setUpdatestate(0); + aSwupdate.setUrl(""); + return aSwupdate; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Boolean getNotify() { + return notify; + } + + public void setNotify(Boolean notify) { + this.notify = notify; + } + + public Integer getUpdatestate() { + return updatestate; + } + + public void setUpdatestate(Integer updatestate) { + this.updatestate = updatestate; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + +} diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/WhitelistEntry.java b/src/main/java/com/bwssystems/HABridge/api/hue/WhitelistEntry.java new file mode 100644 index 0000000..ffd6801 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/api/hue/WhitelistEntry.java @@ -0,0 +1,49 @@ +package com.bwssystems.HABridge.api.hue; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class WhitelistEntry +{ + private String lastUseDate; + private String createDate; + private String name; + private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + + public static WhitelistEntry createEntry(String devicetype) { + WhitelistEntry anEntry = new WhitelistEntry(); + anEntry.setName(devicetype); + anEntry.setCreateDate(getCurrentDate()); + anEntry.setLastUseDate(getCurrentDate()); + return anEntry; + } + + public static String getCurrentDate() { + return dateFormat.format(new Date()); + } + + public String getLastUseDate() { + return lastUseDate; + } + + public void setLastUseDate(String lastUseDate) { + this.lastUseDate = lastUseDate; + } + + public String getCreateDate() { + return createDate; + } + + public void setCreateDate(String createDate) { + this.createDate = createDate; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index 12b2c40..555c017 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -64,6 +64,7 @@ public class HueMulator { DeviceResponse deviceResponse = DeviceResponse.createResponse(device.getName(), device.getId()); deviceResponseMap.put(device.getId(), deviceResponse); } + response.type("application/json; charset=utf-8"); response.status(200); return deviceResponseMap; } , new JsonTransformer()); @@ -77,6 +78,7 @@ public class HueMulator { newUser = "lightssystem"; log.debug("hue api user create requested for device type: " + aNewUser.getDevicetype() + " and username: " + newUser); + response.type("application/json; charset=utf-8"); response.status(200); return "[{\"success\":{\"username\":\"" + newUser + "\"}}]"; } ); @@ -97,9 +99,10 @@ public class HueMulator { deviceList.put(descriptor.getId(), deviceResponse); } ); - HueApiResponse apiResponse = new HueApiResponse(); + HueApiResponse apiResponse = new HueApiResponse("Philips hue", request.ip(), "My App", userId); apiResponse.setLights(deviceList); + response.type("application/json; charset=utf-8"); response.status(200); return apiResponse; }, new JsonTransformer()); @@ -118,6 +121,7 @@ public class HueMulator { } DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId()); + response.type("application/json; charset=utf-8"); response.status(200); return lightResponse; }, new JsonTransformer()); @@ -183,6 +187,7 @@ public class HueMulator { return null; } + response.type("application/json; charset=utf-8"); response.status(200); return responseString; }); diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java index 9477185..a692a2f 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java @@ -16,7 +16,7 @@ public class UpnpSettingsResource { private Logger log = LoggerFactory.getLogger(UpnpSettingsResource.class); - private String hueTemplate = "\n" + "\n" + 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" @@ -26,7 +26,7 @@ public class UpnpSettingsResource { + "Hue Emulator for HA bridge\n" + "Philips hue bridge 2012\n" + "929000226503\n" + "http://www.bwssystems.com/apps.html\n" - + "01189998819991197253\n" + + "0017880ae670\n" + "uuid:88f6698f-2c83-4393-bd03-cd54a9f8595\n" + "\n" + "\n" + "(null)\n" + "(null)\n" + "(null)\n" + "(null)\n" @@ -46,12 +46,23 @@ public class UpnpSettingsResource { private void setupListener (BridgeSettings theSettings) { 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", (request, response) -> { + 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()); log.debug("upnp device settings response: " + filledTemplate); - response.status(201); +// 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; } ); @@ -60,7 +71,7 @@ public class UpnpSettingsResource { get(UPNP_CONTEXT + "/settings", "application/json", (request, response) -> { log.debug("bridge settings requested from " + request.ip()); - response.status(201); + response.status(200); return theSettings; }, new JsonTransformer()); diff --git a/src/main/resources/public/index.html b/src/main/resources/public/index.html index 923ee5a..b4dae70 100644 --- a/src/main/resources/public/index.html +++ b/src/main/resources/public/index.html @@ -41,7 +41,7 @@