From 4da5f65d897f88e51f773d23ae19120a923af23c Mon Sep 17 00:00:00 2001 From: Admin Date: Mon, 26 Sep 2016 17:03:30 -0500 Subject: [PATCH] Updatting upnp responses --- pom.xml | 2 +- .../HABridge/api/hue/DeviceState.java | 14 +++++++------- .../HABridge/api/hue/GroupResponse.java | 15 +++++++++++++-- .../HABridge/api/hue/HuePublicConfig.java | 18 +++++++++--------- .../bwssystems/HABridge/hue/HueMulator.java | 17 +++++------------ .../bwssystems/HABridge/upnp/UpnpListener.java | 14 +++++++++----- 6 files changed, 44 insertions(+), 36 deletions(-) diff --git a/pom.xml b/pom.xml index 29fddc2..8f2546c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 3.1.0a + 3.1.0b jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/DeviceState.java b/src/main/java/com/bwssystems/HABridge/api/hue/DeviceState.java index ef13110..f01e980 100644 --- a/src/main/java/com/bwssystems/HABridge/api/hue/DeviceState.java +++ b/src/main/java/com/bwssystems/HABridge/api/hue/DeviceState.java @@ -17,7 +17,7 @@ public class DeviceState { private String colormode; private boolean reachable; private List xy; - private int transitiontime; +// private int transitiontime; public boolean isOn() { return on; @@ -98,13 +98,13 @@ public class DeviceState { public void setXy(List xy) { this.xy = xy; } - public int getTransitiontime() { - return transitiontime; - } +// public int getTransitiontime() { +// return transitiontime; +// } - public void setTransitiontime(int transitiontime) { - this.transitiontime = transitiontime; - } +// public void setTransitiontime(int transitiontime) { +// this.transitiontime = transitiontime; +// } public static DeviceState createDeviceState() { DeviceState newDeviceState = new DeviceState(); diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/GroupResponse.java b/src/main/java/com/bwssystems/HABridge/api/hue/GroupResponse.java index f4f8890..c037560 100644 --- a/src/main/java/com/bwssystems/HABridge/api/hue/GroupResponse.java +++ b/src/main/java/com/bwssystems/HABridge/api/hue/GroupResponse.java @@ -1,5 +1,10 @@ package com.bwssystems.HABridge.api.hue; +import java.util.List; + +import com.bwssystems.HABridge.dao.DeviceDescriptor; +import com.bwssystems.HABridge.dao.DeviceRepository; + public class GroupResponse { private DeviceState action; private String[] lights; @@ -23,11 +28,17 @@ public class GroupResponse { this.name = name; } - public static GroupResponse createGroupResponse(String[] theLights) { + public static GroupResponse createGroupResponse(List deviceList) { + String[] theList = new String[deviceList.size()]; + int i = 0; + for (DeviceDescriptor device : deviceList) { + theList[i] = device.getId(); + i++; + } GroupResponse theResponse = new GroupResponse(); theResponse.setAction(DeviceState.createDeviceState()); theResponse.setName("Lightset 0"); - theResponse.setLights(theLights); + theResponse.setLights(theList); return theResponse; } } diff --git a/src/main/java/com/bwssystems/HABridge/api/hue/HuePublicConfig.java b/src/main/java/com/bwssystems/HABridge/api/hue/HuePublicConfig.java index b731d92..ac1b1b6 100644 --- a/src/main/java/com/bwssystems/HABridge/api/hue/HuePublicConfig.java +++ b/src/main/java/com/bwssystems/HABridge/api/hue/HuePublicConfig.java @@ -71,18 +71,18 @@ public class HuePublicConfig { StringTokenizer st = new StringTokenizer(macAddr, ":"); String bridgeId = ""; - String port = null; +// String port = null; while(st.hasMoreTokens()) { bridgeId = bridgeId + st.nextToken(); } - if(ipAddr.contains(":")) { - port = ipAddr.substring(ipAddr.indexOf(":")); - BigInteger bigInt = BigInteger.valueOf(Integer.getInteger(port).intValue()); - byte[] theBytes = bigInt.toByteArray(); - bridgeId = bridgeId + DatatypeConverter.printHexBinary(theBytes); - } - else - bridgeId = bridgeId + "0800"; +// if(ipAddr.contains(":")) { +// port = ipAddr.substring(ipAddr.indexOf(":")); +// BigInteger bigInt = BigInteger.valueOf(Integer.getInteger(port).intValue()); +// byte[] theBytes = bigInt.toByteArray(); +// bridgeId = bridgeId + DatatypeConverter.printHexBinary(theBytes); +// } +// else +// bridgeId = bridgeId + "0800"; return bridgeId; } diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java index b99303e..34d5ad1 100644 --- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java +++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java @@ -173,14 +173,7 @@ public class HueMulator implements HueErrorStringSet { } if(groupId.equalsIgnoreCase("0")) { - List deviceList = repository.findAll(); - String[] theList = new String[deviceList.size()]; - int i = 0; - for (DeviceDescriptor device : deviceList) { - theList[i] = device.getId(); - i++; - } - GroupResponse theResponse = GroupResponse.createGroupResponse(theList); + GroupResponse theResponse = GroupResponse.createGroupResponse(repository.findAll()); return new Gson().toJson(theResponse, GroupResponse.class); } @@ -443,9 +436,9 @@ public class HueMulator implements HueErrorStringSet { return theErrorResp.getTheErrors(); } + List descriptorList = repository.findAll(); HueApiResponse apiResponse = new HueApiResponse("Philips hue", bridgeSettings.getUpnpConfigAddress(), bridgeSettings.getWhitelist()); Map deviceList = new HashMap<>(); - List descriptorList = repository.findAll(); if (descriptorList != null) { descriptorList.forEach(descriptor -> { DeviceResponse deviceResponse = DeviceResponse.createResponse(descriptor); @@ -1256,8 +1249,8 @@ public class HueMulator implements HueErrorStringSet { if(notFirstChange) responseString = responseString + ","; responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/transitiontime\":" + state.getTransitiontime() + "}}"; - if(deviceState != null) - deviceState.setTransitiontime(state.getTransitiontime()); +// if(deviceState != null) +// deviceState.setTransitiontime(state.getTransitiontime()); notFirstChange = true; } @@ -1289,7 +1282,7 @@ public class HueMulator implements HueErrorStringSet { private String validateWhitelistUser(String aUser, boolean strict) { if(aUser == null ||aUser.equalsIgnoreCase("undefined") || aUser.equalsIgnoreCase("null") || aUser.equalsIgnoreCase("")) return null; - + String validUser = null; boolean found = false; if(bridgeSettings.getWhitelist() != null) { diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java index d99157d..436371c 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java @@ -25,14 +25,14 @@ public class UpnpListener { private BridgeControlDescriptor bridgeControl; private boolean discoveryTemplateLatest; private String discoveryTemplate = "HTTP/1.1 200 OK\r\n" + - "HOST: %s:%s" + + "HOST: %s:%s\r\n" + "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"; + "USN: uuid:2f402f80-da50-11e1-9b23-001788102201\r\n\r\n"; private String discoveryTemplate091516 = "HTTP/1.1 200 OK\r\n" + "CACHE-CONTROL: max-age=86400\r\n" + "EXT:\r\n" + @@ -199,7 +199,8 @@ public class UpnpListener { log.info("Traceupnp: isSSDPDiscovery found message to be valid under strict rules - strict: " + strict); log.info("Traceupnp: SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + ", body: " + packetString); } - log.debug("isSSDPDiscovery found message to be valid under strict rules - strict: " + strict); + else + log.debug("isSSDPDiscovery found message to be valid under strict rules - strict: " + strict); return true; } else if (!strict) @@ -209,7 +210,8 @@ public class UpnpListener { log.info("Traceupnp: isSSDPDiscovery found message to be valid under loose rules - strict: " + strict); log.info("Traceupnp: SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + ", body: " + packetString); } - log.debug("isSSDPDiscovery found message to be valid under loose rules - strict: " + strict); + else + log.debug("isSSDPDiscovery found message to be valid under loose rules - strict: " + strict); return true; } } @@ -226,8 +228,10 @@ public class UpnpListener { discoveryResponse = String.format(discoveryTemplate, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, HuePublicConfig.createConfig("temp", responseAddress).getBridgeid()); else 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: discoveryResponse is <<<" + discoveryResponse + ">>>"); + } else log.debug("sendUpnpResponse discovery template with address: " + responseAddress + " and port: " + httpServerPort); DatagramPacket response = new DatagramPacket(discoveryResponse.getBytes(), discoveryResponse.length(), requester, sourcePort);