added multiple command execution. Added commandline Exec. NEeds testing

This commit is contained in:
Admin
2016-04-22 13:59:12 -05:00
parent b73a4cd666
commit 21e5dfb338
4 changed files with 86 additions and 34 deletions

View File

@@ -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;
}
}

View File

@@ -1,6 +1,7 @@
package com.bwssystems.HABridge.hue; package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.UserCreateRequest; import com.bwssystems.HABridge.api.UserCreateRequest;
import com.bwssystems.HABridge.api.hue.DeviceResponse; import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.DeviceState; 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); log.debug("executing HUE api request to UDP: " + url);
try { for(int i = 0; i < callItems.length; i++) {
String intermediate = url.substring(6); if( i > 0) {
String ipAddr = intermediate.substring(0, intermediate.indexOf(':')); Thread.sleep(bridgeSettings.getButtonsleep());
String port = intermediate.substring(intermediate.indexOf(':') + 1, intermediate.indexOf('/')); }
String theBody = intermediate.substring(intermediate.indexOf('/')+1); try {
DatagramSocket responseSocket = new DatagramSocket(Integer.parseInt(port)); String intermediate = callItems[i].getItem().substring(6);
if(theBody.startsWith("0x")) { String ipAddr = intermediate.substring(0, intermediate.indexOf(':'));
sendData = DatatypeConverter.parseHexBinary(theBody.substring(2)); String port = intermediate.substring(intermediate.indexOf(':') + 1, intermediate.indexOf('/'));
} String theBody = intermediate.substring(intermediate.indexOf('/')+1);
else DatagramSocket responseSocket = new DatagramSocket(Integer.parseInt(port));
sendData = theBody.getBytes(); if(theBody.startsWith("0x")) {
InetAddress IPAddress = InetAddress.getByName(ipAddr); sendData = DatatypeConverter.parseHexBinary(theBody.substring(2));
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Integer.parseInt(port)); }
responseSocket.send(sendPacket); else
responseSocket.close(); sendData = theBody.getBytes();
} catch (IOException e) { InetAddress IPAddress = InetAddress.getByName(ipAddr);
log.warn("Could not send UDP Datagram packet for request.", e); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Integer.parseInt(port));
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; 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 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); log.debug("executing HUE api request to Http " + (device.getHttpVerb() == null?"GET":device.getHttpVerb()) + ": " + url);
// quick template for(int i = 0; i < callItems.length; i++) {
String body; if( i > 0) {
url = replaceIntensityValue(url, state.getBri()); Thread.sleep(bridgeSettings.getButtonsleep());
if (state.isOn()) }
body = replaceIntensityValue(device.getContentBody(), state.getBri()); // quick template
else String body;
body = replaceIntensityValue(device.getContentBodyOff(), state.getBri()); String anUrl = replaceIntensityValue(callItems[i].getItem(), state.getBri());
// make call if (state.isOn())
if (doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body) == null) { body = replaceIntensityValue(device.getContentBody(), state.getBri());
log.warn("Error on calling url to change device state: " + url); else
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId + "\",\"description\": \"Error on calling url to change device state\", \"parameter\": \"/lights/" + lightId + "state\"}}]"; 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\":")) { if(!responseString.contains("[{\"error\":")) {

View File

@@ -1147,7 +1147,7 @@ app.controller('HueController', function ($scope, $location, $http, bridgeServic
$scope.device.contentType = "application/json"; $scope.device.contentType = "application/json";
$scope.device.mapType = "hueDevice"; $scope.device.mapType = "hueDevice";
$scope.device.mapId = huedevice.device.uniqueid; $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 () { $scope.addDevice = function () {

View File

@@ -78,7 +78,10 @@
<h2 class="panel-title">Add a new device</h2> <h2 class="panel-title">Add a new device</h2>
</div> </div>
<p class="text-muted">This area allows you to create any http or udp call to an endpoint. You can use the default GET or select <p class="text-muted">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.</p> 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.</p>
<ul class="list-group"> <ul class="list-group">
<li class="list-group-item"> <li class="list-group-item">
<form class="form-horizontal"> <form class="form-horizontal">
@@ -95,6 +98,20 @@
Add Bridge Device</button> Add Bridge Device</button>
</div> </div>
</div> </div>
<div class="form-group">
<div class="row">
<label class="col-xs-12 col-sm-2 control-label" for="device-type">Device Type
</label>
<div class="col-xs-8 col-sm-7">
<select name="device-type" id="device-type" ng-model="device.deviceType">
<option value="">---Types if needed---</option> <!-- not selected / blank option -->
<option value="UDP">UDP</option>
<option value="Exec">Execute Script/Program</option>
</select>
</div>
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
<label class="col-xs-12 col-sm-2 control-label" for="device-on-url">On <label class="col-xs-12 col-sm-2 control-label" for="device-on-url">On