From cd8015d8f311a273bad49257805130a8b26fc262 Mon Sep 17 00:00:00 2001 From: Laurent ARNAL Date: Sun, 26 Jan 2025 09:33:53 +0100 Subject: [PATCH] add possible fix for 500 Internal Server Error Signed-off-by: Laurent ARNAL --- .../linky/internal/api/EnedisHttpApi.java | 6 +++ .../linky/internal/handler/LinkyHandler.java | 49 ++++++++++++------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/api/EnedisHttpApi.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/api/EnedisHttpApi.java index fce46ced0a9..7f88d4ff4b9 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/api/EnedisHttpApi.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/api/EnedisHttpApi.java @@ -90,10 +90,16 @@ public class EnedisHttpApi { this.config = config; } + public void removeAllCookie() { + httpClient.getCookieStore().removeAll(); + } + public void initialize() throws LinkyException { logger.debug("Starting login process for user: {}", config.username); try { + removeAllCookie(); + addCookie(LinkyConfiguration.INTERNAL_AUTH_ID, config.internalAuthId); logger.debug("Step 1: getting authentification"); String data = getContent(URL_ENEDIS_AUTHENTICATE); diff --git a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java index 3775a6c5d8e..afb6bf1ba15 100644 --- a/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java +++ b/bundles/org.openhab.binding.linky/src/main/java/org/openhab/binding/linky/internal/handler/LinkyHandler.java @@ -180,19 +180,6 @@ public class LinkyHandler extends BaseThingHandler { api.initialize(); updateStatus(ThingStatus.ONLINE); - if (thing.getProperties().isEmpty()) { - UserInfo userInfo = api.getUserInfo(); - PrmInfo prmInfo = api.getPrmInfo(userInfo.userProperties.internId); - PrmDetail details = api.getPrmDetails(userInfo.userProperties.internId, prmInfo.idPrm); - updateProperties(Map.of(USER_ID, userInfo.userProperties.internId, PUISSANCE, - details.situationContractuelleDtos[0].structureTarifaire().puissanceSouscrite().valeur() - + " kVA", - PRM_ID, prmInfo.idPrm)); - } - - prmId = thing.getProperties().get(PRM_ID); - userId = thing.getProperties().get(USER_ID); - updateData(); disconnect(); @@ -214,17 +201,41 @@ public class LinkyHandler extends BaseThingHandler { } } + private synchronized void updateMetaData() throws LinkyException { + EnedisHttpApi api = this.enedisApi; + if (api != null) { + if (thing.getProperties().isEmpty()) { + UserInfo userInfo = api.getUserInfo(); + PrmInfo prmInfo = api.getPrmInfo(userInfo.userProperties.internId); + PrmDetail details = api.getPrmDetails(userInfo.userProperties.internId, prmInfo.idPrm); + updateProperties(Map.of(USER_ID, userInfo.userProperties.internId, PUISSANCE, + details.situationContractuelleDtos[0].structureTarifaire().puissanceSouscrite().valeur() + + " kVA", + PRM_ID, prmInfo.idPrm)); + } + + prmId = thing.getProperties().get(PRM_ID); + userId = thing.getProperties().get(USER_ID); + } + } + /** * Request new data and updates channels */ private synchronized void updateData() { boolean connectedBefore = isConnected(); - updatePowerData(); - updateDailyWeeklyData(); - updateMonthlyData(); - updateYearlyData(); - if (!connectedBefore && isConnected()) { - disconnect(); + try { + updateMetaData(); + updatePowerData(); + updateDailyWeeklyData(); + updateMonthlyData(); + updateYearlyData(); + if (!connectedBefore && isConnected()) { + disconnect(); + } + } catch (LinkyException e) { + logger.error("Exception occurs during data update", e); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); } }