Skip to content

Commit

Permalink
fixes #2 when Vera responds back with bad content type, BAD VERA!
Browse files Browse the repository at this point in the history
  • Loading branch information
armzilla committed Apr 30, 2015
1 parent 6274bbe commit 5076ce1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
</dependencies>

<build>
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/com/armzilla/ha/hue/HueMulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.armzilla.ha.api.hue.DeviceResponse;
import com.armzilla.ha.api.hue.HueApiResponse;
import com.armzilla.ha.dao.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
Expand All @@ -16,6 +21,7 @@
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,6 +36,10 @@ public class HueMulator {
protected static RestTemplate restTemplate = new RestTemplate();
@Autowired
private DeviceRepository repository;
private HttpClient httpClient;
public HueMulator(){
httpClient = HttpClients.createDefault(); //patched for now, moving away from HueMulator doing work
}


@RequestMapping(value = "/{userId}/lights", method = RequestMethod.GET, produces = "application/json")
Expand Down Expand Up @@ -120,8 +130,10 @@ public ResponseEntity<String> stateChange(@PathVariable(value = "lightId") Strin
} else {
url = device.getOffUrl();
}

String response = restTemplate.getForObject(url, String.class);
//make call
if(!doHttpGETRequest(url)){
return new ResponseEntity<>(null, null, HttpStatus.SERVICE_UNAVAILABLE);
}

HttpHeaders headerMap = new HttpHeaders();
headerMap.set("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Expand All @@ -140,4 +152,18 @@ public ResponseEntity<String> stateChange(@PathVariable(value = "lightId") Strin
ResponseEntity<String> entity = new ResponseEntity<>(setting, headerMap, HttpStatus.OK);
return entity;
}

protected boolean doHttpGETRequest(String url){
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200){
EntityUtils.consume(response.getEntity()); //close out inputstream
return true;
}
} catch (IOException e) {
log.error("Error calling out to HA gateway", e);
}
return false;
}
}

0 comments on commit 5076ce1

Please sign in to comment.