From 998721531bfd23e80b3e300d4858007dc7d9de95 Mon Sep 17 00:00:00 2001 From: Christoph <36006493+fd0cwp@users.noreply.github.com> Date: Tue, 5 Mar 2024 21:40:22 +0100 Subject: [PATCH] [intesis] Improve session handling (#16476) * [intesis] SESSION ID HANDLING IMPROVED Signed-off-by: Christoph --- .../internal/gson/IntesisHomeJSonDTO.java | 6 +++ .../internal/handler/IntesisHomeHandler.java | 45 ++++++++++++++----- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/gson/IntesisHomeJSonDTO.java b/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/gson/IntesisHomeJSonDTO.java index f36dffc0ec316..44f5b2e47a241 100644 --- a/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/gson/IntesisHomeJSonDTO.java +++ b/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/gson/IntesisHomeJSonDTO.java @@ -25,6 +25,7 @@ public class IntesisHomeJSonDTO { public static class Response { public boolean success; public JsonElement data; + public JsonElement error; } public static class Data { @@ -36,6 +37,11 @@ public static class Data { public JsonElement dpval; } + public static class ResponseError { + public int code; + public String message; + } + public static class Id { public String sessionID; // Session ID } diff --git a/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/handler/IntesisHomeHandler.java b/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/handler/IntesisHomeHandler.java index e8ad4c3ce1801..ed3762897d249 100644 --- a/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/handler/IntesisHomeHandler.java +++ b/bundles/org.openhab.binding.intesis/src/main/java/org/openhab/binding/intesis/internal/handler/IntesisHomeHandler.java @@ -41,6 +41,7 @@ import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Id; import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Info; import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.Response; +import org.openhab.binding.intesis.internal.gson.IntesisHomeJSonDTO.ResponseError; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.QuantityType; @@ -86,6 +87,8 @@ public class IntesisHomeHandler extends BaseThingHandler { private IntesisHomeConfiguration config = new IntesisHomeConfiguration(); + private String sessionId = ""; + private @Nullable ScheduledFuture refreshJob; public IntesisHomeHandler(final Thing thing, final HttpClient httpClient, @@ -109,6 +112,9 @@ public void initialize() { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Password not set"); return; } else { + logger.trace("trying to log in - current session ID: {}", sessionId); + login(); + // start background initialization: scheduler.submit(() -> { populateProperties(); @@ -129,6 +135,8 @@ public void dispose() { refreshJob.cancel(true); this.refreshJob = null; } + + logout(sessionId); } @Override @@ -216,26 +224,29 @@ public void handleCommand(ChannelUID channelUID, Command command) { } public @Nullable String login() { - // lambda's can't modify local variables, so we use an array here to get around the issue - String[] sessionId = new String[1]; postRequest( "{\"command\":\"login\",\"data\":{\"username\":\"Admin\",\"password\":\"" + config.password + "\"}}", resp -> { Data data = gson.fromJson(resp.data, Data.class); + ResponseError error = gson.fromJson(resp.error, ResponseError.class); + if (error != null) { + logger.debug("Login - Error: {}", error); + } if (data != null) { Id id = gson.fromJson(data.id, Id.class); if (id != null) { - sessionId[0] = id.sessionID; + sessionId = id.sessionID.toString(); } } }); - if (sessionId[0] != null && !sessionId[0].isEmpty()) { + logger.trace("Login - received session ID: {}", sessionId); + if (sessionId != null && !sessionId.isEmpty()) { updateStatus(ThingStatus.ONLINE); - return sessionId[0]; } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "SessionId not received"); - return null; + sessionId = ""; } + return sessionId; } public @Nullable String logout(String sessionId) { @@ -309,18 +320,34 @@ public void addChannel(String channelId, String itemType, @Nullable final Collec } private void postRequest(String request, Consumer handler) { + postRequest(request, handler, false); + } + + private void postRequest(String request, Consumer handler, boolean retry) { try { logger.trace("request : '{}'", request); String response = api.postRequest(config.ipAddress, request); if (response != null) { Response resp = gson.fromJson(response, Response.class); if (resp != null) { - boolean success = resp.success; - if (success) { + if (resp.success) { handler.accept(resp); } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Request unsuccessful"); + ResponseError respError = gson.fromJson(resp.error, ResponseError.class); + if (respError != null) { + logger.warn("postRequest failed - respErrorCode: {} / respErrorMessage: {} / retry {}", + respError.code, respError.message, retry); + if (!retry && respError.code == 1) { + logger.debug( + "postRequest: trying to log in and retry request - respErrorCode: {} / respErrorMessage: {} / retry {}", + respError.code, respError.message, retry); + login(); + postRequest(request, handler, true); + } + } + } } } else { @@ -332,13 +359,11 @@ private void postRequest(String request, Consumer handler) { } private void postRequestInSession(UnaryOperator requestFactory, Consumer handler) { - String sessionId = login(); if (sessionId != null) { try { String request = requestFactory.apply(sessionId); postRequest(request, handler); } finally { - logout(sessionId); } } }