From a3325aa048bee4b35a3871f88b980b7a33d1aa16 Mon Sep 17 00:00:00 2001 From: bwssystems Date: Wed, 28 Dec 2016 18:42:08 -0600 Subject: [PATCH] refactoring.... --- pom.xml | 2 +- .../HABridge/plugins/udp/UDPHome.java | 2 +- .../HABridge/upnp/UpnpListener.java | 6 +-- .../HABridge/util/UDPDatagramSender.java | 6 +-- src/main/resources/public/scripts/app.js | 43 +++++++++++-------- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 2dce267..eede78d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 3.5.1q + 3.5.1r jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java b/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java index 6cdfa08..02a0482 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/udp/UDPHome.java @@ -74,7 +74,7 @@ public class UDPHome implements Home { sendData = theUrlBody.getBytes(); } try { - theUDPDatagramSender.sendUDPResponse(new String(sendData), IPAddress, + theUDPDatagramSender.sendUDPResponse(sendData, IPAddress, Integer.parseInt(port)); } catch (NumberFormatException e) { // noop diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java index 8a34c6c..1d0898c 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java @@ -209,7 +209,7 @@ public class UpnpListener { } else log.debug("sendUpnpResponse discovery responseTemplate1 is <<<" + discoveryResponse + ">>>"); - theUDPDatagramSender.sendUDPResponse(discoveryResponse, requester, sourcePort); + theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); discoveryResponse = String.format(responseTemplate2, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID, bridgeSNUUID); if(traceupnp) { @@ -217,7 +217,7 @@ public class UpnpListener { } else log.debug("sendUpnpResponse discovery responseTemplate2 is <<<" + discoveryResponse + ">>>"); - theUDPDatagramSender.sendUDPResponse(discoveryResponse, requester, sourcePort); + theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); discoveryResponse = String.format(responseTemplate3, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID); if(traceupnp) { @@ -225,6 +225,6 @@ public class UpnpListener { } else log.debug("sendUpnpResponse discovery responseTemplate3 is <<<" + discoveryResponse + ">>>"); - theUDPDatagramSender.sendUDPResponse(discoveryResponse, requester, sourcePort); + theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort); } } diff --git a/src/main/java/com/bwssystems/HABridge/util/UDPDatagramSender.java b/src/main/java/com/bwssystems/HABridge/util/UDPDatagramSender.java index 895c3d0..0cb8860 100644 --- a/src/main/java/com/bwssystems/HABridge/util/UDPDatagramSender.java +++ b/src/main/java/com/bwssystems/HABridge/util/UDPDatagramSender.java @@ -62,11 +62,11 @@ public class UDPDatagramSender { responseSocket.close(); } - public void sendUDPResponse(String udpResponse, InetAddress requester, int sourcePort) throws IOException { - log.debug("Sending response string: <<<" + udpResponse + ">>>"); + public void sendUDPResponse(byte[] udpMessage, InetAddress requester, int sourcePort) throws IOException { + log.debug("Sending response string: <<<" + new String(udpMessage) + ">>>"); if(responseSocket == null) throw new IOException("Socket not initialized"); - DatagramPacket response = new DatagramPacket(udpResponse.getBytes(), udpResponse.length(), requester, sourcePort); + DatagramPacket response = new DatagramPacket(udpMessage, udpMessage.length, requester, sourcePort); responseSocket.send(response); } } diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index 3770c90..0f3a3fd 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -1036,7 +1036,7 @@ app.controller('VeraController', function ($scope, $location, $http, bridgeServi $scope.device.dimUrl = "[{\"item\":\"http://" + veradevice.veraaddress + ":" + $scope.vera.port + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=" + veradevice.id + "\"},\"type\":\"veraDevice\"}]"; - $scope.device.onUrl = "[{\"item\":{\"clientId\":\"http://" + veradevice.veraaddress + ":" + $scope.vera.port + $scope.device.onUrl = "[{\"item\":\"http://" + veradevice.veraaddress + ":" + $scope.vera.port + "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=" + veradevice.id + "\"},\"type\":\"veraDevice\"}]"; $scope.device.offUrl = "[{\"item\":\"http://" + veradevice.veraaddress + ":" + $scope.vera.port @@ -1052,10 +1052,10 @@ app.controller('VeraController', function ($scope, $location, $http, bridgeServi $scope.device.mapId = $scope.device.mapId + "-" + verascene.id; $scope.device.onUrl = currentOn.substr(0, currentOn.indexOf("]")) + ",{\"item\":\"http://" + verascene.veraaddress + ":" + $scope.vera.port + "/data_request?id=action&output_format=json&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=" - + verascene.id+ "\"},\"type\":\"veraDevice\"}]"; + + verascene.id + "\"},\"type\":\"veraScene\"}]"; $scope.device.offUrl = currentOff.substr(0, currentOff.indexOf("]")) + ",{\"item\":\"http://" + verascene.veraaddress + ":" + $scope.vera.port + "/data_request?id=action&output_format=json&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=" - + verascene.id+ "\"},\"type\":\"veraDevice\"}]"; + + verascene.id + "\"},\"type\":\"veraScene\"}]"; } else if ($scope.device.mapType === undefined || $scope.device.mapType == null || $scope.device.mapType == "") { bridgeService.clearDevice(); @@ -1066,10 +1066,10 @@ app.controller('VeraController', function ($scope, $location, $http, bridgeServi $scope.device.mapId = verascene.id; $scope.device.onUrl = "[{\"item\":\"http://" + verascene.veraaddress + ":" + $scope.vera.port + "/data_request?id=action&output_format=json&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=" - + verascene.id+ "\"},\"type\":\"veraDevice\"}]"; + + verascene.id + "\"},\"type\":\"veraScene\"}]"; $scope.device.offUrl = "[{\"item\":\"http://" + verascene.veraaddress + ":" + $scope.vera.port + "/data_request?id=action&output_format=json&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=" - + verascene.id+ "\"},\"type\":\"veraDevice\"}]"; + + verascene.id + "\"},\"type\":\"veraScene\"}]"; } }; @@ -1194,13 +1194,22 @@ app.controller('HarmonyController', function ($scope, $location, $http, bridgeSe $scope.buildActivityUrls = function (harmonyactivity) { bridgeService.clearDevice(); - $scope.device.deviceType = "activity"; - $scope.device.targetDevice = harmonyactivity.hub; - $scope.device.name = harmonyactivity.activity.label; - $scope.device.mapType = "harmonyActivity"; - $scope.device.mapId = harmonyactivity.activity.id; - $scope.device.onUrl = "{\"name\":\"" + harmonyactivity.activity.id + "\"}"; - $scope.device.offUrl = "{\"name\":\"-1\"}"; + var currentOn = $scope.device.onUrl; + var currentOff = $scope.device.offUrl; + if($scope.device.mapType !== undefined && $scope.device.mapType != null && $scope.device.mapType != "") { + $scope.device.mapId = $scope.device.mapId + "-" + harmonyactivity.activity.id; + $scope.device.onUrl = currentOn.substr(0, currentOn.indexOf("]")) + ",{\"item\":{\"name\":\"" + harmonyactivity.activity.id + "\"}" + "\"},\"type\":\"harmonyActivity\"}]"; + $scope.device.offUrl = currentOn.substr(0, currentOn.indexOf("]")) + ",{\"item\":{\"name\":\"-1\"}" + "\"},\"type\":\"harmonyActivity\"}]"; + } + else if ($scope.device.mapType == null || $scope.device.mapType == "") { + $scope.device.deviceType = "activity"; + $scope.device.targetDevice = harmonyactivity.hub; + $scope.device.name = harmonyactivity.activity.label; + $scope.device.mapType = "harmonyActivity"; + $scope.device.mapId = harmonyactivity.activity.id; + $scope.device.onUrl = "[{\"item\":{\"name\":\"" + harmonyactivity.activity.id + "\"}" + "\"},\"type\":\"harmonyActivity\"}]"; + $scope.device.offUrl = "[{\"item\":{\"name\":\"-1\"}" + "\"},\"type\":\"harmonyActivity\"}]"; + } }; $scope.buildButtonUrls = function (harmonydevice, onbutton, offbutton) { @@ -1208,10 +1217,10 @@ app.controller('HarmonyController', function ($scope, $location, $http, bridgeSe var currentOff = $scope.device.offUrl; var actionOn = angular.fromJson(onbutton); var actionOff = angular.fromJson(offbutton); - if( $scope.device.mapType == "harmonyButton") { + if($scope.device.mapType !== undefined && $scope.device.mapType != null && $scope.device.mapType != "") { $scope.device.mapId = $scope.device.mapId + "-" + actionOn.command; - $scope.device.onUrl = currentOn.substr(0, currentOn.indexOf("]")) + ",{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOn.command + "\"}]"; - $scope.device.offUrl = currentOff.substr(0, currentOff.indexOf("]")) + ",{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOff.command + "\"}]"; + $scope.device.onUrl = currentOn.substr(0, currentOn.indexOf("]")) + ",{\"item\":{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOn.command + "\"}]"; + $scope.device.offUrl = currentOff.substr(0, currentOff.indexOf("]")) + ",{\"item\":{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOff.command + "\"}]"; } else if ($scope.device.mapType == null || $scope.device.mapType == "") { bridgeService.clearDevice(); @@ -1220,8 +1229,8 @@ app.controller('HarmonyController', function ($scope, $location, $http, bridgeSe $scope.device.name = harmonydevice.device.label; $scope.device.mapType = "harmonyButton"; $scope.device.mapId = harmonydevice.device.id + "-" + actionOn.command; - $scope.device.onUrl = "[{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOn.command + "\"}]"; - $scope.device.offUrl = "[{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOff.command + "\"}]"; + $scope.device.onUrl = "[{\"item\":{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOn.command + "\"}]"; + $scope.device.offUrl = "[{\"item\":{\"device\":\"" + harmonydevice.device.id + "\",\"button\":\"" + actionOff.command + "\"}]"; } };