mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-02-04 03:14:07 +01:00
[Tibber] Improve connection logic (#17344)
* Fixes #14735 Optimized Logging and sending Ping Frames. See also PR #17214 Signed-off-by: christian <dev@bestof5.de> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
5286950eba
commit
6679a00585
@ -19,7 +19,10 @@ import java.io.InputStream;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
@ -87,6 +90,7 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
private String rtEnabled = "false";
|
private String rtEnabled = "false";
|
||||||
private @Nullable String subscriptionURL;
|
private @Nullable String subscriptionURL;
|
||||||
private @Nullable String versionString;
|
private @Nullable String versionString;
|
||||||
|
private @Nullable LocalDateTime lastWebSocketMessage;
|
||||||
|
|
||||||
public TibberHandler(Thing thing) {
|
public TibberHandler(Thing thing) {
|
||||||
super(thing);
|
super(thing);
|
||||||
@ -170,7 +174,10 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (liveChannelsLinked() && "true".equals(rtEnabled)) {
|
if (liveChannelsLinked() && "true".equals(rtEnabled)) {
|
||||||
|
logger.debug("Pulse associated with HomeId: Live stream will be started");
|
||||||
startLiveStream();
|
startLiveStream();
|
||||||
|
} else {
|
||||||
|
logger.debug("No Pulse associated with HomeId: No live stream will be started");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
||||||
@ -320,8 +327,17 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
|
|
||||||
public void updateRequest() throws IOException {
|
public void updateRequest() throws IOException {
|
||||||
getURLInput(BASE_URL);
|
getURLInput(BASE_URL);
|
||||||
if (liveChannelsLinked() && "true".equals(rtEnabled) && !isConnected()) {
|
if (liveChannelsLinked() && "true".equals(rtEnabled)) {
|
||||||
startLiveStream();
|
if (lastWebSocketMessage != null && lastWebSocketMessage.plusMinutes(5).isBefore(LocalDateTime.now())) {
|
||||||
|
logger.debug("Last data from tibber on {}. Reconnecting WebSocket.", lastWebSocketMessage);
|
||||||
|
close();
|
||||||
|
startLiveStream();
|
||||||
|
} else if (isConnected()) {
|
||||||
|
logger.debug("Sending Ping Message");
|
||||||
|
session.getRemote().sendPing(ByteBuffer.wrap("openHAB Ping".getBytes(StandardCharsets.UTF_8)));
|
||||||
|
} else if (!isConnected()) {
|
||||||
|
startLiveStream();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,7 +422,7 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||||
"Unexpected subscription result from the server");
|
"Unexpected subscription result from the server");
|
||||||
} else {
|
} else {
|
||||||
logger.debug("Connecting Subscription to: {}", subscriptionURL);
|
logger.debug("Reconnecting Subscription to: {}", subscriptionURL);
|
||||||
open();
|
open();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -552,19 +568,21 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
@OnWebSocketError
|
@OnWebSocketError
|
||||||
public void onWebSocketError(Throwable e) {
|
public void onWebSocketError(Throwable e) {
|
||||||
String message = e.getMessage();
|
String message = e.getMessage();
|
||||||
logger.debug("Error during websocket communication: {}", message);
|
logger.warn("Error during websocket communication: {}", message);
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnWebSocketMessage
|
@OnWebSocketMessage
|
||||||
public void onMessage(String message) {
|
public void onMessage(String message) {
|
||||||
if (message.contains("connection_ack")) {
|
if (message.contains("connection_ack")) {
|
||||||
logger.debug("Connected to Server");
|
logger.debug("WebSocket connected to Server");
|
||||||
startSubscription();
|
startSubscription();
|
||||||
} else if (message.contains("error") || message.contains("terminate")) {
|
} else if (message.contains("error") || message.contains("terminate")) {
|
||||||
logger.debug("Error/terminate received from server: {}", message);
|
logger.debug("Error/terminate received from server: {}", message);
|
||||||
close();
|
close();
|
||||||
} else if (message.contains("liveMeasurement")) {
|
} else if (message.contains("liveMeasurement")) {
|
||||||
|
logger.debug("Received liveMeasurement message.");
|
||||||
|
lastWebSocketMessage = LocalDateTime.now();
|
||||||
JsonObject object = (JsonObject) JsonParser.parseString(message);
|
JsonObject object = (JsonObject) JsonParser.parseString(message);
|
||||||
JsonObject myObject = object.getAsJsonObject("payload").getAsJsonObject("data")
|
JsonObject myObject = object.getAsJsonObject("payload").getAsJsonObject("data")
|
||||||
.getAsJsonObject("liveMeasurement");
|
.getAsJsonObject("liveMeasurement");
|
||||||
@ -641,7 +659,7 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
updateChannel(LIVE_MAXPOWERPRODUCTION, myObject.get("maxPowerProduction").toString());
|
updateChannel(LIVE_MAXPOWERPRODUCTION, myObject.get("maxPowerProduction").toString());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.debug("Unknown live response from Tibber");
|
logger.debug("Unknown live response from Tibber. Message: {}", message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user