Added the Post Put capabilities for the http eecution. cleaned up css

for list ordering. Cleaned up device management calls.
This commit is contained in:
Admin
2015-09-01 13:40:53 -05:00
parent 6c99358f95
commit 7294dbf175
13 changed files with 196 additions and 102 deletions

View File

@@ -8,6 +8,9 @@ public class Device {
private String deviceType;
private String offUrl;
private String onUrl;
private String httpVerb;
private String contentType;
private String contentBody;
public String getName() {
return name;
@@ -40,4 +43,30 @@ public class Device {
public void setOnUrl(String onUrl) {
this.onUrl = onUrl;
}
public String getHttpVerb() {
return httpVerb;
}
public void setHttpVerb(String httpVerb) {
this.httpVerb = httpVerb;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentBody() {
return contentBody;
}
public void setContentBody(String contentBody) {
this.contentBody = contentBody;
}
}

View File

@@ -8,6 +8,9 @@ public class DeviceDescriptor{
private String deviceType;
private String offUrl;
private String onUrl;
private String httpVerb;
private String contentType;
private String contentBody;
public String getName() {
return name;
@@ -48,4 +51,30 @@ public class DeviceDescriptor{
public void setId(String id) {
this.id = id;
}
public String getHttpVerb() {
return httpVerb;
}
public void setHttpVerb(String httpVerb) {
this.httpVerb = httpVerb;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentBody() {
return contentBody;
}
public void setContentBody(String contentBody) {
this.contentBody = contentBody;
}
}

View File

@@ -179,6 +179,15 @@ public class DeviceRepository {
} else if (name.equals("onUrl")) {
deviceEntry.setOnUrl(reader.nextString());
log.debug("Read a Device - device json on URL:" + deviceEntry.getOnUrl());
} else if (name.equals("httpVerb")) {
deviceEntry.setHttpVerb(reader.nextString());
log.debug("Read a Device - device json httpVerb:" + deviceEntry.getHttpVerb());
} else if (name.equals("contentType")) {
deviceEntry.setContentType(reader.nextString());
log.debug("Read a Device - device json contentType:" + deviceEntry.getContentType());
} else if (name.equals("contentBody")) {
deviceEntry.setContentBody(reader.nextString());
log.debug("Read a Device - device json contentBody:" + deviceEntry.getContentBody());
} else {
reader.skipValue();
}

View File

@@ -4,9 +4,13 @@ import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.put;
import static spark.Spark.delete;
import java.util.List;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -27,7 +31,7 @@ public class DeviceResource {
private DeviceRepository deviceRepository;
private VeraInfo veraInfo;
private static final Set<String> supportedVerbs = new HashSet<>(Arrays.asList("get", "put", "post"));
public DeviceResource(BridgeSettings theSettings) {
super();
@@ -45,39 +49,44 @@ public class DeviceResource {
post(API_CONTEXT, "application/json", (request, response) -> {
log.debug("Create a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
DeviceDescriptor deviceEntry = new DeviceDescriptor();
deviceEntry.setName(device.getName());
log.debug("Create a Device - device json name: " + deviceEntry.getName());
deviceEntry.setDeviceType(device.getDeviceType());
log.debug("Create a Device - device json type:" + deviceEntry.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
log.debug("Create a Device - device json on URL:" + deviceEntry.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());
log.debug("Create a Device - device json off URL:" + deviceEntry.getOffUrl());
deviceRepository.save(deviceEntry);
log.debug("Created a Device: " + request.body());
response.status(201);
return deviceEntry;
if(device.getContentBody() != null ) {
if (device.getContentType() == null || device.getHttpVerb() == null || !supportedVerbs.contains(device.getHttpVerb().toLowerCase())) {
device = null;
response.status(HttpStatus.SC_BAD_REQUEST);
log.debug("Created a Device: " + request.body());
}
}
else
{
deviceRepository.save(device);
log.debug("Created a Device: " + request.body());
response.status(HttpStatus.SC_OK);
}
return device;
}, new JsonTransformer());
put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Edit a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id"));
if(deviceEntry == null){
log.debug("Could not save an edited Device Id: " + request.params(":id"));
return null;
response.status(HttpStatus.SC_BAD_REQUEST);
}
else
{
log.debug("Saving an edited Device: " + deviceEntry.getName());
deviceEntry.setName(device.getName());
if (device.getDeviceType() != null)
deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());
deviceRepository.save(deviceEntry);
response.status(HttpStatus.SC_OK);
}
log.debug("Saving an edited Device: " + deviceEntry.getName());
deviceEntry.setName(device.getName());
if(device.getDeviceType() != null)
deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());
deviceRepository.save(deviceEntry);
return deviceEntry;
}, new JsonTransformer());
@@ -87,25 +96,30 @@ public class DeviceResource {
JsonTransformer aRenderer = new JsonTransformer();
String theStream = aRenderer.render(deviceList);
log.debug("The Device List: " + theStream);
response.status(HttpStatus.SC_OK);
return deviceList;
}, new JsonTransformer());
get (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Get a device");
DeviceDescriptor descriptor = deviceRepository.findOne(request.params(":id"));
if(descriptor == null){
return null;
}
if(descriptor == null)
response.status(HttpStatus.SC_NOT_FOUND);
else
response.status(HttpStatus.SC_OK);
return descriptor;
}, new JsonTransformer());
delete (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Delete a device");
DeviceDescriptor deleted = deviceRepository.findOne(request.params(":id"));
if(deleted == null){
return null;
if(deleted == null)
response.status(HttpStatus.SC_NOT_FOUND);
else
{
deviceRepository.delete(deleted);
response.status(HttpStatus.SC_OK);
}
deviceRepository.delete(deleted);
return null;
}, new JsonTransformer());
@@ -113,8 +127,11 @@ public class DeviceResource {
log.debug("Get vera devices");
Sdata sData = veraInfo.getSdata();
if(sData == null){
return null;
response.status(HttpStatus.SC_NOT_FOUND);
return null;
}
response.status(HttpStatus.SC_OK);
return sData.getDevices();
}, new JsonTransformer());
@@ -122,10 +139,12 @@ public class DeviceResource {
log.debug("Get vera scenes");
Sdata sData = veraInfo.getSdata();
if(sData == null){
response.status(HttpStatus.SC_NOT_FOUND);
return null;
}
response.status(HttpStatus.SC_OK);
return sData.getScenes();
}, new JsonTransformer());
}
}
}

View File

@@ -17,6 +17,11 @@ import static spark.Spark.put;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
@@ -162,26 +167,11 @@ public class HueMulator {
url = device.getOffUrl();
}
/* light weight templating here, was going to use free marker but it was a bit too
* heavy for what we were trying to do.
*
* currently provides only two variables:
* intensity.byte : 0-255 brightness. this is raw from the echo
* intensity.percent : 0-100, adjusted for the vera
*/
if(url.contains(INTENSITY_BYTE)){
String intensityByte = String.valueOf(state.getBri());
url = url.replace(INTENSITY_BYTE, intensityByte);
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/bri\":"+ String.valueOf(state.getBri()) + "}}]";
}else if(url.contains(INTENSITY_PERCENT)){
int percentBrightness = (int) Math.round(state.getBri()/255.0*100);
String intensityPercent = String.valueOf(percentBrightness);
url = url.replace(INTENSITY_PERCENT, intensityPercent);
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/bri\":"+ String.valueOf(state.getBri()) + "}}]";
}
//quick template
url = replaceIntensityValue(url, state.getBri());
String body = replaceIntensityValue(device.getContentBody(), state.getBri());
//make call
if(!doHttpGETRequest(url)){
if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){
response.status(503);
log.error("Error on calling url to change device state: " + url);
return null;
@@ -193,14 +183,52 @@ public class HueMulator {
});
}
/* light weight templating here, was going to use free marker but it was a bit too
* heavy for what we were trying to do.
*
* currently provides only two variables:
* intensity.byte : 0-255 brightness. this is raw from the echo
* intensity.percent : 0-100, adjusted for the vera
*/
protected String replaceIntensityValue(String request, int intensity){
if(request == null){
return "";
}
if(request.contains(INTENSITY_BYTE)){
String intensityByte = String.valueOf(intensity);
request = request.replace(INTENSITY_BYTE, intensityByte);
}else if(request.contains(INTENSITY_PERCENT)){
int percentBrightness = (int) Math.round(intensity/255.0*100);
String intensityPercent = String.valueOf(percentBrightness);
request = request.replace(INTENSITY_PERCENT, intensityPercent);
}
return request;
}
// This function executes the url from the device repository against the vera
protected boolean doHttpGETRequest(String url) {
log.debug("calling GET on URL: " + url);
HttpGet httpGet = new HttpGet(url);
protected boolean doHttpRequest(String url, String httpVerb, String contentType, String body) {
HttpUriRequest request = null;
if(HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb) || httpVerb == null) {
request = new HttpGet(url);
}else if(HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)){
HttpPost postRequest = new HttpPost(url);
ContentType parsedContentType = ContentType.parse(contentType);
StringEntity requestBody = new StringEntity(body, parsedContentType);
postRequest.setEntity(requestBody);
request = postRequest;
}else if(HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)){
HttpPut putRequest = new HttpPut(url);
ContentType parsedContentType = ContentType.parse(contentType);
StringEntity requestBody = new StringEntity(body, parsedContentType);
putRequest.setEntity(requestBody);
request = putRequest;
}
log.debug("Making outbound call in doHttpRequest: " + request);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpResponse response = httpClient.execute(request);
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
log.debug("GET on URL responded: " + response.getStatusLine().getStatusCode());
log.debug("Execute on URL responded: " + response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200){
return true;
}