Skip to content

Commit

Permalink
add request body, content type, and verb in creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Arm Suwarnaratana committed Aug 16, 2015
1 parent c190cea commit 28ba226
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.armzilla.ha</groupId>
<artifactId>amazon-echo-bridge</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>
<packaging>jar</packaging>

<name>Amazon Echo Bridge</name>
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/armzilla/ha/api/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ public class Device {
private String deviceType;
private String offUrl;
private String onUrl;
private String httpVerb;
private String contentType;
private String contentBody;

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

public String getName() {
return name;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/armzilla/ha/dao/DeviceDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@ public class DeviceDescriptor{
private String deviceType;
private String offUrl;
private String onUrl;
private String httpVerb;
private String contentType;
private String contentBody;

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

public String getName() {
return name;
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/armzilla/ha/devicemanagmeent/DeviceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.*;

/**
* Created by arm on 4/13/15.
Expand All @@ -23,17 +21,27 @@
@RequestMapping("/api/devices")
public class DeviceResource {

private static final Set<String> supportedVerbs = new HashSet<>(Arrays.asList("get", "put", "post"));

@Autowired
private DeviceRepository deviceRepository;

@RequestMapping(method = RequestMethod.POST, produces = "application/json", headers = "Accept=application/json")
public ResponseEntity<DeviceDescriptor> createDevice(@RequestBody Device device) {
if(device.getContentBody() != null ) {
if (device.getContentType() == null || device.getHttpVerb() == null || !supportedVerbs.contains(device.getHttpVerb().toLowerCase())) {
return new ResponseEntity<>(null, null, HttpStatus.BAD_REQUEST);
}
} //add more validation like content type
DeviceDescriptor deviceEntry = new DeviceDescriptor();
deviceEntry.setId(UUID.randomUUID().toString());
deviceEntry.setName(device.getName());
deviceEntry.setDeviceType(device.getDeviceType());
deviceEntry.setOnUrl(device.getOnUrl());
deviceEntry.setOffUrl(device.getOffUrl());
deviceEntry.setContentType(device.getContentType());
deviceEntry.setContentBody(device.getContentBody());
deviceEntry.setHttpVerb(device.getHttpVerb());

deviceRepository.save(deviceEntry);

Expand Down
71 changes: 51 additions & 20 deletions src/main/java/com/armzilla/ha/hue/HueMulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
import com.armzilla.ha.dao.*;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
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;
import org.apache.log4j.Logger;
Expand Down Expand Up @@ -139,38 +145,61 @@ public ResponseEntity<String> stateChange(@PathVariable(value = "lightId") Strin
url = device.getOffUrl();
}

//quick template
url = replaceIntensityValue(url, state.getBri());
String body = replaceIntensityValue(device.getContentBody(), state.getBri());
//make call
if(!doHttpRequest(url, device.getHttpVerb(), device.getContentType(), body)){
return new ResponseEntity<>(null, null, HttpStatus.SERVICE_UNAVAILABLE);
}

HttpHeaders headerMap = new HttpHeaders();

ResponseEntity<String> entity = new ResponseEntity<>(responseString, headerMap, HttpStatus.OK);
return entity;
}
protected String replaceIntensityValue(String request, int intensity){
/* 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);
}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);
if(request == null){
return "";
}

//make call
if(!doHttpGETRequest(url)){
return new ResponseEntity<>(null, null, HttpStatus.SERVICE_UNAVAILABLE);
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);
}

HttpHeaders headerMap = new HttpHeaders();

ResponseEntity<String> entity = new ResponseEntity<>(responseString, headerMap, HttpStatus.OK);
return entity;
return request;
}

protected boolean doHttpGETRequest(String url) {
log.info("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.info("Making outbound call: " + request);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpResponse response = httpClient.execute(request);
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
log.info("GET on URL responded: " + response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200){
Expand All @@ -180,5 +209,7 @@ protected boolean doHttpGETRequest(String url) {
log.error("Error calling out to HA gateway", e);
}
return false;

}

}

1 comment on commit 28ba226

@maddox
Copy link

@maddox maddox commented on 28ba226 Aug 16, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃🏻

Please sign in to comment.