[tibber] Modified Websocket connectivity/reconnect (#11462)

Signed-off-by: Stian Kjoglum <stiankj@online.no>
This commit is contained in:
Stian Kjoglum 2021-10-31 09:18:11 +01:00 committed by GitHub
parent 1d07dbe1f5
commit 20114a3685
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -125,10 +125,10 @@ public class TibberHandler extends BaseThingHandler {
.getAsJsonObject("features").get("realTimeConsumptionEnabled").toString(); .getAsJsonObject("features").get("realTimeConsumptionEnabled").toString();
if ("true".equals(rtEnabled)) { if ("true".equals(rtEnabled)) {
logger.debug("Pulse associated with HomeId: Live stream will be started"); logger.info("Pulse associated with HomeId: Live stream will be started");
open(); open();
} else { } else {
logger.debug("No Pulse associated with HomeId: No live stream will be started"); logger.info("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,
@ -246,7 +246,7 @@ public class TibberHandler extends BaseThingHandler {
public void updateRequest() throws IOException { public void updateRequest() throws IOException {
getURLInput(BASE_URL); getURLInput(BASE_URL);
if ("true".equals(rtEnabled) && !isConnected()) { if ("true".equals(rtEnabled) && !isConnected()) {
logger.debug("Attempting to reopen Websocket connection"); logger.info("Attempting to reopen Websocket connection");
open(); open();
} }
} }
@ -288,7 +288,7 @@ public class TibberHandler extends BaseThingHandler {
WebSocketClient client = this.client; WebSocketClient client = this.client;
if (client != null) { if (client != null) {
try { try {
logger.debug("Stopping and Terminating Websocket connection"); logger.warn("Stopping and Terminating Websocket connection");
client.stop(); client.stop();
client.destroy(); client.destroy();
} catch (Exception e) { } catch (Exception e) {
@ -301,18 +301,21 @@ public class TibberHandler extends BaseThingHandler {
} }
public void open() { public void open() {
if (isConnected()) { WebSocketClient client = this.client;
logger.debug("Open: connection is already open"); if (client == null || !client.isRunning()) {
} else { if (client != null) {
try {
client.stop();
} catch (Exception e) {
logger.warn("Failed to stop websocket client: {}", e.getMessage());
}
}
sslContextFactory.setTrustAll(true); sslContextFactory.setTrustAll(true);
sslContextFactory.setEndpointIdentificationAlgorithm(null); sslContextFactory.setEndpointIdentificationAlgorithm(null);
WebSocketClient client = this.client; client = new WebSocketClient(sslContextFactory, websocketExecutor);
if (client == null) { client.setMaxIdleTimeout(600 * 1000);
client = new WebSocketClient(sslContextFactory, websocketExecutor); this.client = client;
client.setMaxIdleTimeout(600 * 1000);
this.client = client;
}
TibberWebSocketListener socket = this.socket; TibberWebSocketListener socket = this.socket;
if (socket == null) { if (socket == null) {
@ -325,19 +328,21 @@ public class TibberHandler extends BaseThingHandler {
newRequest.setSubProtocols("graphql-subscriptions"); newRequest.setSubProtocols("graphql-subscriptions");
try { try {
logger.debug("Starting Websocket connection"); logger.info("Starting Websocket connection");
client.start(); client.start();
} catch (Exception e) { } catch (Exception e) {
logger.warn("Websocket Start Exception: {}", e.getMessage()); logger.warn("Websocket Start Exception: {}", e.getMessage());
} }
try { try {
logger.debug("Connecting Websocket connection"); logger.info("Connecting Websocket connection");
sessionFuture = client.connect(socket, new URI(SUBSCRIPTION_URL), newRequest); sessionFuture = client.connect(socket, new URI(SUBSCRIPTION_URL), newRequest);
} catch (IOException e) { } catch (IOException e) {
logger.warn("Websocket Connect Exception: {}", e.getMessage()); logger.warn("Websocket Connect Exception: {}", e.getMessage());
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
logger.warn("Websocket URI Exception: {}", e.getMessage()); logger.warn("Websocket URI Exception: {}", e.getMessage());
} }
} else {
logger.warn("Open: Websocket client already running");
} }
} }
@ -348,7 +353,7 @@ public class TibberHandler extends BaseThingHandler {
try { try {
TibberWebSocketListener socket = this.socket; TibberWebSocketListener socket = this.socket;
if (socket != null) { if (socket != null) {
logger.debug("Sending websocket disconnect message"); logger.info("Sending websocket disconnect message");
socket.sendMessage(disconnect); socket.sendMessage(disconnect);
} else { } else {
logger.debug("Socket unable to send disconnect message: Socket is null"); logger.debug("Socket unable to send disconnect message: Socket is null");
@ -390,7 +395,7 @@ public class TibberHandler extends BaseThingHandler {
String connection = "{\"type\":\"connection_init\", \"payload\":\"token=" + tibberConfig.getToken() + "\"}"; String connection = "{\"type\":\"connection_init\", \"payload\":\"token=" + tibberConfig.getToken() + "\"}";
try { try {
if (socket != null) { if (socket != null) {
logger.debug("Sending websocket connect message"); logger.info("Sending websocket connect message");
socket.sendMessage(connection); socket.sendMessage(connection);
} else { } else {
logger.debug("Socket unable to send connect message: Socket is null"); logger.debug("Socket unable to send connect message: Socket is null");
@ -402,20 +407,16 @@ public class TibberHandler extends BaseThingHandler {
@OnWebSocketClose @OnWebSocketClose
public void onClose(int statusCode, String reason) { public void onClose(int statusCode, String reason) {
logger.debug("Closing a WebSocket due to {}", reason); logger.info("Closing a WebSocket due to {}", reason);
WebSocketClient client = TibberHandler.this.client; WebSocketClient client = TibberHandler.this.client;
if (client != null && client.isRunning()) { if (client != null && client.isRunning()) {
try { try {
logger.debug("Stopping and Terminating Websocket connection"); logger.info("Stopping and Terminating Websocket connection");
client.stop(); client.stop();
client.destroy();
} catch (Exception e) { } catch (Exception e) {
logger.warn("Websocket Client Stop Exception: {}", e.getMessage()); logger.warn("Websocket Client Stop Exception: {}", e.getMessage());
} }
} }
TibberHandler.this.session = null;
TibberHandler.this.client = null;
TibberHandler.this.socket = null;
} }
@OnWebSocketError @OnWebSocketError
@ -428,7 +429,7 @@ public class TibberHandler extends BaseThingHandler {
@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.info("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);