From f4b6ba6af10e35aca2c8e2a429b1e38c87a021f2 Mon Sep 17 00:00:00 2001 From: Laurent ARNAL Date: Tue, 20 Aug 2024 21:52:07 +0200 Subject: [PATCH] fixes for @clinique review #3 Signed-off-by: Laurent ARNAL --- .../linky/internal/handler/EnedisBridgeHandler.java | 13 +++++++++++-- .../internal/handler/EnedisWebBridgeHandler.java | 13 +++++++++---- .../linky/internal/handler/LinkyBridgeHandler.java | 6 +++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisBridgeHandler.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisBridgeHandler.java index 71ad3637eb971..68cbb1adb804d 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisBridgeHandler.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisBridgeHandler.java @@ -15,6 +15,7 @@ import java.time.format.DateTimeFormatter; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.linky.internal.LinkyConfiguration; import org.openhab.binding.linky.internal.LinkyException; import org.openhab.core.auth.client.oauth2.AccessTokenResponse; import org.openhab.core.auth.client.oauth2.OAuthFactory; @@ -62,12 +63,20 @@ public EnedisBridgeHandler(Bridge bridge, final @Reference HttpClientFactory htt @Override public String getClientId() { - return config.clientId; + LinkyConfiguration lcConfig = config; + if (lcConfig != null) { + return lcConfig.clientId; + } + return ""; } @Override public String getClientSecret() { - return config.clientSecret; + LinkyConfiguration lcConfig = config; + if (lcConfig != null) { + return lcConfig.clientSecret; + } + return ""; } @Override diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java index 8ef80f640c6f1..d4bd73a140720 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/EnedisWebBridgeHandler.java @@ -167,10 +167,15 @@ public DateTimeFormatter getApiDateFormatYearsFirst() { @Override protected synchronized void connectionInit() throws LinkyException { - logger.debug("Starting login process for user : {}", config.username); + LinkyConfiguration lcConfig = config; + if (lcConfig == null) { + return; + } + + logger.debug("Starting login process for user : {}", lcConfig.username); try { - enedisApi.addCookie(LinkyConfiguration.INTERNAL_AUTH_ID, config.internalAuthId); + enedisApi.addCookie(LinkyConfiguration.INTERNAL_AUTH_ID, lcConfig.internalAuthId); logger.debug("Step 1 : getting authentification"); String data = enedisApi.getData(URL_ENEDIS_AUTHENTICATE); @@ -207,13 +212,13 @@ protected synchronized void connectionInit() throws LinkyException { AuthData authData = gson.fromJson(result.getContentAsString(), AuthData.class); if (authData == null || authData.callbacks.size() < 2 || authData.callbacks.get(0).input.isEmpty() - || authData.callbacks.get(1).input.isEmpty() || !config.username + || authData.callbacks.get(1).input.isEmpty() || !lcConfig.username .equals(Objects.requireNonNull(authData.callbacks.get(0).input.get(0)).valueAsString())) { logger.debug("auth1 - invalid template for auth data: {}", result.getContentAsString()); throw new LinkyException("Authentication error, the authentication_cookie is probably wrong"); } - authData.callbacks.get(1).input.get(0).value = config.password; + authData.callbacks.get(1).input.get(0).value = lcConfig.password; logger.debug("Step 4 : auth2 - send the auth data"); result = httpClient.POST(authenticateUrl).header(HttpHeader.CONTENT_TYPE, "application/json") .header("X-NoSession", "true").header("X-Password", "anonymous") diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyBridgeHandler.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyBridgeHandler.java index de07af29e08df..dce8b20d72ed3 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyBridgeHandler.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyBridgeHandler.java @@ -63,9 +63,9 @@ public abstract class LinkyBridgeHandler extends BaseBridgeHandler { protected final EnedisHttpApi enedisApi; protected final ThingRegistry thingRegistry; - protected final LinkyConfiguration config; protected final Gson gson; + protected @Nullable LinkyConfiguration config; protected boolean connected = false; private static final int REQUEST_BUFFER_SIZE = 8000; @@ -104,8 +104,6 @@ public LinkyBridgeHandler(Bridge bridge, final @Reference HttpClientFactory http this.enedisApi = new EnedisHttpApi(this, gson, this.httpClient); - config = getConfigAs(LinkyConfiguration.class); - updateStatus(ThingStatus.UNKNOWN); } @@ -117,6 +115,8 @@ public BundleContext getBundleContext() { public synchronized void initialize() { logger.debug("Initializing Linky API bridge handler."); + config = getConfigAs(LinkyConfiguration.class); + updateStatus(ThingStatus.UNKNOWN); scheduler.submit(() -> {