From 21e5dfb3383e27bf3df02e76ca19ced8ce0f49ba Mon Sep 17 00:00:00 2001
From: Admin
Date: Fri, 22 Apr 2016 13:59:12 -0500
Subject: [PATCH] added multiple command execution. Added commandline Exec.
NEeds testing
---
.../com/bwssystems/HABridge/api/CallItem.java | 13 +++
.../bwssystems/HABridge/hue/HueMulator.java | 86 ++++++++++++-------
src/main/resources/public/scripts/app.js | 2 +-
src/main/resources/public/views/editor.html | 19 +++-
4 files changed, 86 insertions(+), 34 deletions(-)
create mode 100644 src/main/java/com/bwssystems/HABridge/api/CallItem.java
diff --git a/src/main/java/com/bwssystems/HABridge/api/CallItem.java b/src/main/java/com/bwssystems/HABridge/api/CallItem.java
new file mode 100644
index 0000000..d0e8ff5
--- /dev/null
+++ b/src/main/java/com/bwssystems/HABridge/api/CallItem.java
@@ -0,0 +1,13 @@
+package com.bwssystems.HABridge.api;
+
+public class CallItem {
+ private String Item;
+
+ public String getItem() {
+ return Item;
+ }
+
+ public void setItem(String item) {
+ Item = item;
+ }
+}
diff --git a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java
index 2634e09..734ff89 100644
--- a/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java
+++ b/src/main/java/com/bwssystems/HABridge/hue/HueMulator.java
@@ -1,6 +1,7 @@
package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
+import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.UserCreateRequest;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.DeviceState;
@@ -510,44 +511,65 @@ public class HueMulator implements HueErrorStringSet {
}
}
}
- else if(url.startsWith("udp://"))
+ else if(device.getDeviceType().startsWith("Exec")) {
+ Process p = Runtime.getRuntime().exec(url);
+ }
+ else if(url.contains("udp://"))
{
+ if(!url.substring(0, 1).equalsIgnoreCase("[")) {
+ url = "[{\"item\":\"" + url +"\"}]";
+ }
+ CallItem[] callItems = new Gson().fromJson(url, CallItem[].class);
log.debug("executing HUE api request to UDP: " + url);
- try {
- String intermediate = url.substring(6);
- String ipAddr = intermediate.substring(0, intermediate.indexOf(':'));
- String port = intermediate.substring(intermediate.indexOf(':') + 1, intermediate.indexOf('/'));
- String theBody = intermediate.substring(intermediate.indexOf('/')+1);
- DatagramSocket responseSocket = new DatagramSocket(Integer.parseInt(port));
- if(theBody.startsWith("0x")) {
- sendData = DatatypeConverter.parseHexBinary(theBody.substring(2));
- }
- else
- sendData = theBody.getBytes();
- InetAddress IPAddress = InetAddress.getByName(ipAddr);
- DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Integer.parseInt(port));
- responseSocket.send(sendPacket);
- responseSocket.close();
- } catch (IOException e) {
- log.warn("Could not send UDP Datagram packet for request.", e);
- responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId + "state\"}}]";
- }
+ for(int i = 0; i < callItems.length; i++) {
+ if( i > 0) {
+ Thread.sleep(bridgeSettings.getButtonsleep());
+ }
+ try {
+ String intermediate = callItems[i].getItem().substring(6);
+ String ipAddr = intermediate.substring(0, intermediate.indexOf(':'));
+ String port = intermediate.substring(intermediate.indexOf(':') + 1, intermediate.indexOf('/'));
+ String theBody = intermediate.substring(intermediate.indexOf('/')+1);
+ DatagramSocket responseSocket = new DatagramSocket(Integer.parseInt(port));
+ if(theBody.startsWith("0x")) {
+ sendData = DatatypeConverter.parseHexBinary(theBody.substring(2));
+ }
+ else
+ sendData = theBody.getBytes();
+ InetAddress IPAddress = InetAddress.getByName(ipAddr);
+ DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Integer.parseInt(port));
+ responseSocket.send(sendPacket);
+ responseSocket.close();
+ } catch (IOException e) {
+ log.warn("Could not send UDP Datagram packet for request.", e);
+ responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId + "state\"}}]";
+ }
+ }
}
else
{
+ if(!url.substring(0, 1).equalsIgnoreCase("[")) {
+ url = "[{\"item\":\"" + url +"\"}]";
+ }
+ CallItem[] callItems = new Gson().fromJson(url, CallItem[].class);
log.debug("executing HUE api request to Http " + (device.getHttpVerb() == null?"GET":device.getHttpVerb()) + ": " + url);
- // quick template
- String body;
- url = replaceIntensityValue(url, state.getBri());
- if (state.isOn())
- body = replaceIntensityValue(device.getContentBody(), state.getBri());
- else
- body = replaceIntensityValue(device.getContentBodyOff(), state.getBri());
- // make call
- if (doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body) == null) {
- log.warn("Error on calling url to change device state: " + url);
- responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling url to change device state\", \"parameter\": \"/lights/" + lightId + "state\"}}]";
- }
+ for(int i = 0; i < callItems.length; i++) {
+ if( i > 0) {
+ Thread.sleep(bridgeSettings.getButtonsleep());
+ }
+ // quick template
+ String body;
+ String anUrl = replaceIntensityValue(callItems[i].getItem(), state.getBri());
+ if (state.isOn())
+ body = replaceIntensityValue(device.getContentBody(), state.getBri());
+ else
+ body = replaceIntensityValue(device.getContentBodyOff(), state.getBri());
+ // make call
+ if (doHttpRequest(anUrl, device.getHttpVerb(), device.getContentType(), body) == null) {
+ log.warn("Error on calling url to change device state: " + anUrl);
+ responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling url to change device state\", \"parameter\": \"/lights/" + lightId + "state\"}}]";
+ }
+ }
}
if(!responseString.contains("[{\"error\":")) {
diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js
index 3ed0454..5272e28 100644
--- a/src/main/resources/public/scripts/app.js
+++ b/src/main/resources/public/scripts/app.js
@@ -1147,7 +1147,7 @@ app.controller('HueController', function ($scope, $location, $http, bridgeServic
$scope.device.contentType = "application/json";
$scope.device.mapType = "hueDevice";
$scope.device.mapId = huedevice.device.uniqueid;
- $scope.device.onUrl = "{\"ipAddress\":\"" + huedevice.hueaddress + "\",\"deviceId\":\"" + huedevice.device.uniqueid +"\"}";
+ $scope.device.onUrl = "{\"ipAddress\":\"" + huedevice.hueaddress + "\",\"deviceId\":\"" + huedevice.huedeviceid +"\"}";
};
$scope.addDevice = function () {
diff --git a/src/main/resources/public/views/editor.html b/src/main/resources/public/views/editor.html
index 256643d..79c99c3 100644
--- a/src/main/resources/public/views/editor.html
+++ b/src/main/resources/public/views/editor.html
@@ -78,7 +78,10 @@
Add a new device
This area allows you to create any http or udp call to an endpoint. You can use the default GET or select
- the http verb type below and configure a payload for either on or off methods. Currently, https is not supported.
+ the http verb type below and configure a payload for either on, dim or off methods. Currently, https is not supported. For Execution of
+ a script or program, plese fill in the path. All manually entered calls can use Json notation of array with
+ [{"item":"the payload"},{"item":"another payload"}] to execute multiple entries. Adding the value replacements (${intensity..byte},${intensity.percent},${intensity.math(X*1)})
+ will also work.