|
|
|
|
@@ -102,6 +102,7 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void getTibberParameters() {
|
|
|
|
|
String response = "";
|
|
|
|
|
try {
|
|
|
|
|
httpHeader.put("cache-control", "no-cache");
|
|
|
|
|
httpHeader.put("content-type", JSON_CONTENT_TYPE);
|
|
|
|
|
@@ -109,12 +110,10 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
|
|
|
|
|
TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
|
|
|
|
|
InputStream connectionStream = tibberQuery.connectionInputStream(tibberConfig.getHomeid());
|
|
|
|
|
String response = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, connectionStream, null,
|
|
|
|
|
REQUEST_TIMEOUT);
|
|
|
|
|
response = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, connectionStream, null, REQUEST_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
if (!response.contains("error") && !response.contains("<html>")) {
|
|
|
|
|
updateStatus(ThingStatus.ONLINE);
|
|
|
|
|
|
|
|
|
|
getURLInput(BASE_URL);
|
|
|
|
|
|
|
|
|
|
InputStream inputStream = tibberQuery.getRealtimeInputStream(tibberConfig.getHomeid());
|
|
|
|
|
@@ -141,10 +140,11 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void getURLInput(String url) throws IOException {
|
|
|
|
|
String jsonResponse = "";
|
|
|
|
|
TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
|
|
|
|
|
|
|
|
|
|
InputStream inputStream = tibberQuery.getInputStream(tibberConfig.getHomeid());
|
|
|
|
|
String jsonResponse = HttpUtil.executeUrl("POST", url, httpHeader, inputStream, null, REQUEST_TIMEOUT);
|
|
|
|
|
jsonResponse = HttpUtil.executeUrl("POST", url, httpHeader, inputStream, null, REQUEST_TIMEOUT);
|
|
|
|
|
logger.debug("API response: {}", jsonResponse);
|
|
|
|
|
|
|
|
|
|
if (!jsonResponse.contains("error") && !jsonResponse.contains("<html>")) {
|
|
|
|
|
@@ -163,13 +163,15 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
updateState(CURRENT_TOTAL, new DecimalType(myObject.get("total").toString()));
|
|
|
|
|
String timestamp = myObject.get("startsAt").toString().substring(1, 20);
|
|
|
|
|
updateState(CURRENT_STARTSAT, new DateTimeType(timestamp));
|
|
|
|
|
updateState(CURRENT_LEVEL, new StringType(myObject.get("level").toString()));
|
|
|
|
|
|
|
|
|
|
} catch (JsonSyntaxException e) {
|
|
|
|
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
|
|
|
|
"Error communicating with Tibber API: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (jsonResponse.contains("daily")) {
|
|
|
|
|
if (jsonResponse.contains("daily") && !jsonResponse.contains("\"daily\":{\"nodes\":[]")
|
|
|
|
|
&& !jsonResponse.contains("\"daily\":null")) {
|
|
|
|
|
try {
|
|
|
|
|
JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer")
|
|
|
|
|
.getAsJsonObject("home").getAsJsonObject("daily").getAsJsonArray("nodes").get(0);
|
|
|
|
|
@@ -188,7 +190,8 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
"Error communicating with Tibber API: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (jsonResponse.contains("hourly")) {
|
|
|
|
|
if (jsonResponse.contains("hourly") && !jsonResponse.contains("\"hourly\":{\"nodes\":[]")
|
|
|
|
|
&& !jsonResponse.contains("\"hourly\":null")) {
|
|
|
|
|
try {
|
|
|
|
|
JsonObject myObject = (JsonObject) object.getAsJsonObject("data").getAsJsonObject("viewer")
|
|
|
|
|
.getAsJsonObject("home").getAsJsonObject("hourly").getAsJsonArray("nodes").get(0);
|
|
|
|
|
@@ -257,7 +260,7 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.WATT));
|
|
|
|
|
} else if (channelID.contains("voltage")) {
|
|
|
|
|
updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.VOLT));
|
|
|
|
|
} else if (channelID.contains("live_current")) {
|
|
|
|
|
} else if (channelID.contains("current")) {
|
|
|
|
|
updateState(channelID, new QuantityType<>(new BigDecimal(channelValue), Units.AMPERE));
|
|
|
|
|
} else {
|
|
|
|
|
updateState(channelID, new DecimalType(channelValue));
|
|
|
|
|
@@ -307,6 +310,7 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
WebSocketClient client = this.client;
|
|
|
|
|
if (client == null) {
|
|
|
|
|
client = new WebSocketClient(sslContextFactory, websocketExecutor);
|
|
|
|
|
client.setMaxIdleTimeout(600 * 1000);
|
|
|
|
|
this.client = client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -352,7 +356,7 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.warn("Websocket Close Exception: {}", e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
session.close(0, "Tibber websocket disposed");
|
|
|
|
|
session.close();
|
|
|
|
|
this.session = null;
|
|
|
|
|
this.socket = null;
|
|
|
|
|
}
|
|
|
|
|
@@ -470,14 +474,14 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
if (myObject.has("voltagePhase3")) {
|
|
|
|
|
updateChannel(LIVE_VOLTAGE3, myObject.get("voltagePhase3").toString());
|
|
|
|
|
}
|
|
|
|
|
if (myObject.has("currentPhase1")) {
|
|
|
|
|
updateChannel(LIVE_CURRENT1, myObject.get("currentPhase1").toString());
|
|
|
|
|
if (myObject.has("currentL1")) {
|
|
|
|
|
updateChannel(LIVE_CURRENT1, myObject.get("currentL1").toString());
|
|
|
|
|
}
|
|
|
|
|
if (myObject.has("currentPhase2")) {
|
|
|
|
|
updateChannel(LIVE_CURRENT2, myObject.get("currentPhase2").toString());
|
|
|
|
|
if (myObject.has("currentL2")) {
|
|
|
|
|
updateChannel(LIVE_CURRENT2, myObject.get("currentL2").toString());
|
|
|
|
|
}
|
|
|
|
|
if (myObject.has("currentPhase3")) {
|
|
|
|
|
updateChannel(LIVE_CURRENT3, myObject.get("currentPhase3").toString());
|
|
|
|
|
if (myObject.has("currentL3")) {
|
|
|
|
|
updateChannel(LIVE_CURRENT3, myObject.get("currentL3").toString());
|
|
|
|
|
}
|
|
|
|
|
if (myObject.has("powerProduction")) {
|
|
|
|
|
updateChannel(LIVE_POWERPRODUCTION, myObject.get("powerProduction").toString());
|
|
|
|
|
@@ -508,7 +512,7 @@ public class TibberHandler extends BaseThingHandler {
|
|
|
|
|
String query = "{\"id\":\"1\",\"type\":\"start\",\"payload\":{\"variables\":{},\"extensions\":{},\"operationName\":null,\"query\":\"subscription {\\n liveMeasurement(homeId:\\\""
|
|
|
|
|
+ tibberConfig.getHomeid()
|
|
|
|
|
+ "\\\") {\\n timestamp\\n power\\n lastMeterConsumption\\n accumulatedConsumption\\n accumulatedCost\\n currency\\n minPower\\n averagePower\\n maxPower\\n"
|
|
|
|
|
+ "voltagePhase1\\n voltagePhase2\\n voltagePhase3\\n currentPhase1\\n currentPhase2\\n currentPhase3\\n powerProduction\\n accumulatedProduction\\n minPowerProduction\\n maxPowerProduction\\n }\\n }\\n\"}}";
|
|
|
|
|
+ "voltagePhase1\\n voltagePhase2\\n voltagePhase3\\n currentL1\\n currentL2\\n currentL3\\n powerProduction\\n accumulatedProduction\\n minPowerProduction\\n maxPowerProduction\\n }\\n }\\n\"}}";
|
|
|
|
|
try {
|
|
|
|
|
TibberWebSocketListener socket = TibberHandler.this.socket;
|
|
|
|
|
if (socket != null) {
|
|
|
|
|
|