Search in sources :

Example 1 with SuccessUserResponse

use of com.bwssystems.HABridge.api.SuccessUserResponse in project ha-bridge by bwssytems.

the class HueInfo method registerWithHue.

public String registerWithHue() {
    UserCreateRequest theLogin = new UserCreateRequest();
    theLogin.setDevicetype("HABridge#MyMachine");
    HttpPost postRequest = new HttpPost("http://" + hueAddress.getIp() + HUE_REQUEST);
    ContentType parsedContentType = ContentType.parse("application/json");
    StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
    HttpResponse response = null;
    postRequest.setEntity(requestBody);
    HttpClient anHttpClient = httpClient.getHttpClient();
    try {
        response = anHttpClient.execute(postRequest);
        log.debug("registerWithHue - POST execute on " + hueAddress.getName() + "URL responded: " + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
            String theBody = EntityUtils.toString(response.getEntity());
            log.debug("registerWithHue response data: " + theBody);
            if (theBody.contains("[{\"error\":")) {
                if (theBody.contains("link button not")) {
                    log.warn("registerWithHue needs link button pressed on HUE bridge: " + hueAddress.getName());
                } else
                    log.warn("registerWithHue returned an unexpected error: " + theBody);
            } else {
                //read content for data, SuccessUserResponse[].class);
                SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class);
                hueAddress.setUsername(theResponses[0].getSuccess().getUsername());
            }
        }
        //close out inputstream ignore content
        EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        log.warn("Error logging into HUE: IOException in log", e);
    }
    return hueAddress.getUsername();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) SuccessUserResponse(com.bwssystems.HABridge.api.SuccessUserResponse) ContentType(org.apache.http.entity.ContentType) HttpClient(org.apache.http.client.HttpClient) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UserCreateRequest(com.bwssystems.HABridge.api.UserCreateRequest)

Example 2 with SuccessUserResponse

use of com.bwssystems.HABridge.api.SuccessUserResponse in project ha-bridge by bwssytems.

the class HueUtil method registerWithHue.

public static final String registerWithHue(HTTPHandler anHttpHandler, String ipAddress, String aName, String theUser) {
    UserCreateRequest theLogin = new UserCreateRequest();
    theLogin.setDevicetype("HABridge#MyMachine");
    HttpPost postRequest = new HttpPost("http://" + ipAddress + HUE_REQUEST);
    ContentType parsedContentType = ContentType.parse("application/json");
    StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
    HttpResponse response = null;
    postRequest.setEntity(requestBody);
    HttpClient anHttpClient = anHttpHandler.getHttpClient();
    try {
        response = anHttpClient.execute(postRequest);
        log.debug("POST execute on URL responded: " + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
            String theBody = EntityUtils.toString(response.getEntity());
            log.debug("registerWithHue response data: " + theBody);
            if (theBody.contains("[{\"error\":")) {
                if (theBody.contains("link button not")) {
                    log.warn("registerWithHue needs link button pressed on HUE bridge: " + aName);
                } else
                    log.warn("registerWithHue returned an unexpected error: " + theBody);
            } else {
                //read content for data, SuccessUserResponse[].class);
                SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class);
                theUser = theResponses[0].getSuccess().getUsername();
            }
        }
        //close out inputstream ignore content
        EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        log.warn("Error logging into HUE: IOException in log", e);
    }
    return theUser;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) SuccessUserResponse(com.bwssystems.HABridge.api.SuccessUserResponse) ContentType(org.apache.http.entity.ContentType) HttpClient(org.apache.http.client.HttpClient) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UserCreateRequest(com.bwssystems.HABridge.api.UserCreateRequest)

Aggregations

SuccessUserResponse (com.bwssystems.HABridge.api.SuccessUserResponse)2 UserCreateRequest (com.bwssystems.HABridge.api.UserCreateRequest)2 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 HttpResponse (org.apache.http.HttpResponse)2 HttpClient (org.apache.http.client.HttpClient)2 HttpPost (org.apache.http.client.methods.HttpPost)2 ContentType (org.apache.http.entity.ContentType)2 StringEntity (org.apache.http.entity.StringEntity)2