Skip to content

Commit

Permalink
Implemented some improvement suggestions from PR code review #3
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Pecta <[email protected]>
  • Loading branch information
octa22 committed Aug 13, 2018
1 parent e45f6d4 commit a6655f1
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ private String getGroups() {
url = TAHOMA_URL + "getActionGroups";
String urlParameters = "";

byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
InputStream response = sendDataToTahomaWithCookie(url, postData);
InputStream response = sendDataToTahomaWithCookie(url, urlParameters);

return readResponse(response);

Expand All @@ -240,8 +239,7 @@ private void listDevices() {
url = TAHOMA_URL + "getSetup";
String urlParameters = "";

byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
InputStream response = sendDataToTahomaWithCookie(url, postData);
InputStream response = sendDataToTahomaWithCookie(url, urlParameters);

String line = readResponse(response);

Expand Down Expand Up @@ -287,9 +285,7 @@ private State getState(SomfyTahomaThingHandler handler, String deviceUrl) {
url = TAHOMA_URL + "getStates";
String urlParameters = "[{\"deviceURL\": \"" + deviceUrl + "\", \"states\": [{\"name\": \"" + handler.getStateName() + "\"}]}]";

byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

InputStream response = sendDataToTahomaWithCookie(url, postData);
InputStream response = sendDataToTahomaWithCookie(url, urlParameters);
String line = readResponse(response);

SomfyTahomaStatesResponse data = gson.fromJson(line, SomfyTahomaStatesResponse.class);
Expand Down Expand Up @@ -409,17 +405,17 @@ public InputStream sendToTahomaWithCookie(String url) throws Exception {
return connection.getInputStream();
}

public InputStream sendDataToTahomaWithCookie(String url, byte[] postData) throws Exception {
public InputStream sendDataToTahomaWithCookie(String url, String postData) throws Exception {
logger.trace("Sending POST to Tahoma to url: {} with data: {}", url, postData);
URL cookieUrl = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) cookieUrl.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
setConnectionDefaults(connection);
connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
connection.setRequestProperty("Content-Length", Integer.toString(postData.length()));
connection.setRequestProperty("Cookie", cookie);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(postData);
wr.write(postData.getBytes(StandardCharsets.UTF_8));
}

return connection.getInputStream();
Expand Down Expand Up @@ -453,9 +449,8 @@ public void sendCommand(String io, String command, String params) {

String urlParameters = "{\"actions\": [{\"deviceURL\": \"" + io + "\", \"commands\": [{ \"name\": \"" + command + "\", \"parameters\": " + params + "}]}]}";
logger.debug("Sending apply: {}", urlParameters);
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

InputStream response = sendDataToTahomaWithCookie(url, postData);
InputStream response = sendDataToTahomaWithCookie(url, urlParameters);
String line = readResponse(response);

SomfyTahomaApplyResponse data = gson.fromJson(line, SomfyTahomaApplyResponse.class);
Expand Down Expand Up @@ -484,11 +479,9 @@ public String getCurrentExecutions(String type) {

try {
url = TAHOMA_URL + "getCurrentExecutions";

String urlParameters = "";
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

InputStream response = sendDataToTahomaWithCookie(url, postData);
InputStream response = sendDataToTahomaWithCookie(url, urlParameters);
String line = readResponse(response);

SomfyTahomaExecutionsResponse data = gson.fromJson(line, SomfyTahomaExecutionsResponse.class);
Expand Down

0 comments on commit a6655f1

Please sign in to comment.