Skip to content

Commit

Permalink
Merge pull request openhab#3 from lolodomo/sonnen_SonnenJSONCommunica…
Browse files Browse the repository at this point in the history
…tion

Fix handling of batteryData
  • Loading branch information
chingon007 authored Jan 8, 2022
2 parents 27f2970 + 9aa4ebf commit 47733e0
Showing 1 changed file with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

/**
* This class handles the JSON communication with the sonnen battery
Expand All @@ -41,7 +42,6 @@ public class SonnenJSONCommunication {

public SonnenJSONCommunication() {
gson = new Gson();
batteryData = new SonnenJsonDataDTO();
config = new SonnenConfiguration();
}

Expand All @@ -53,28 +53,24 @@ public SonnenJSONCommunication() {
* @return true if no error occurred, false otherwise.
*/
public boolean refreshBatteryConnection(Helper message, String thingUID) {
SonnenJsonDataDTO result = null;
boolean resultOk = false;
String errorDetail = "", statusDescr = "";
String statusDescr = "";
String urlStr = "http://" + config.hostIP + "/api/v1/status";

String response = null;
try {
response = HttpUtil.executeUrl("GET", urlStr, 10000);
String response = HttpUtil.executeUrl("GET", urlStr, 10000);
logger.debug("BatteryData = {}", response);
result = gson.fromJson(response, SonnenJsonDataDTO.class);
if (response == null) {
throw new IOException("HttpUtil.executeUrl returned null");
}
batteryData = gson.fromJson(response, SonnenJsonDataDTO.class);
resultOk = true;
} catch (IOException e) {
} catch (IOException | JsonSyntaxException e) {
logger.debug("Error processiong Get request {}", urlStr);
statusDescr = "Cannot find service on given IP" + config.hostIP + " Please verify the IP-Address!";
errorDetail = e.getMessage();
batteryData = null;
statusDescr = "Cannot find service on given IP " + config.hostIP + ". Please verify the IP address!";
resultOk = false;
}
if (resultOk) {
batteryData = result;
} else {
logger.debug("Setting thing '{}' to OFFLINE: Error '{}'", thingUID, errorDetail);
batteryData = new SonnenJsonDataDTO();
logger.debug("Setting thing '{}' to OFFLINE: Error '{}'", thingUID, e.getMessage());
}
message.setStatusDescription(statusDescr);
return resultOk;
Expand All @@ -92,10 +88,9 @@ public void setConfig(SonnenConfiguration config2) {
/**
* Returns the actual stored Battery Data
*
* @return JSON Data from the Battery
* @return JSON Data from the Battery or null if request failed
*/
@Nullable
public SonnenJsonDataDTO getBatteryData() {
public @Nullable SonnenJsonDataDTO getBatteryData() {
return this.batteryData;
}
}

0 comments on commit 47733e0

Please sign in to comment.