From ed3642da84688802d93aeb5438c6c296d4859f36 Mon Sep 17 00:00:00 2001 From: Haavar Valeur Date: Sun, 21 Aug 2022 03:21:44 -0700 Subject: [PATCH] Setting auto discovered callback URL in configuration. Cleaned up callback URL handling (#13295) Signed-off-by: Haavar Valeur --- .../internal/KonnectedHandlerFactory.java | 37 +++++++++++-------- .../internal/handler/KonnectedHandler.java | 29 +++++++-------- .../main/resources/OH-INF/config/config.xml | 4 +- .../OH-INF/i18n/konnected.properties | 4 +- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/KonnectedHandlerFactory.java b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/KonnectedHandlerFactory.java index 27883b9a14..83fe16682c 100644 --- a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/KonnectedHandlerFactory.java +++ b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/KonnectedHandlerFactory.java @@ -65,7 +65,8 @@ public class KonnectedHandlerFactory extends BaseThingHandlerFactory { protected void activate(ComponentContext componentContext) { super.activate(componentContext); Dictionary properties = componentContext.getProperties(); - callbackUrl = (String) properties.get("callbackUrl"); + callbackUrl = (String) properties.get(CALLBACK_URL); + logger.debug("Callback URL from OSGI service: {}", callbackUrl); try { this.servlet = registerWebHookServlet(); } catch (KonnectedWebHookFail e) { @@ -81,8 +82,8 @@ public class KonnectedHandlerFactory extends BaseThingHandlerFactory { @Override protected @Nullable ThingHandler createHandler(Thing thing) { - KonnectedHandler thingHandler = new KonnectedHandler(thing, '/' + BINDING_ID, createCallbackUrl(), - createCallbackPort()); + + KonnectedHandler thingHandler = new KonnectedHandler(thing, getCallbackUrl()); if (servlet != null) { logger.debug("Adding thinghandler for thing {} to webhook.", thing.getUID().getId()); servlet.add(thingHandler); @@ -119,22 +120,28 @@ public class KonnectedHandlerFactory extends BaseThingHandlerFactory { this.httpService = null; } - private String createCallbackUrl() { - if (callbackUrl != null) { - logger.debug("The callback ip address from the OSGI is:{}", callbackUrl); - return callbackUrl; - } else { - final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress(); - if (ipAddress == null) { - logger.warn("No network interface could be found."); - return null; + private String getCallbackUrl() { + if (callbackUrl == null) { + String callbackIP = discoverCallbackIP(); + String callbackPort = discoverCallbackPort(); + if (callbackPort != null && callbackIP != null) { + callbackUrl = "http://" + discoverCallbackIP() + ":" + discoverCallbackPort() + '/' + BINDING_ID; } - logger.debug("The callback ip address obtained from the Network Address Service was:{}", ipAddress); - return ipAddress; } + return callbackUrl; + } + + private String discoverCallbackIP() { + final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress(); + if (ipAddress == null) { + logger.warn("No network interface could be found."); + return null; + } + logger.debug("The callback ip address obtained from the Network Address Service was:{}", ipAddress); + return ipAddress; } - private String createCallbackPort() { + private String discoverCallbackPort() { // we do not use SSL as it can cause certificate validation issues. final int port = HttpServiceUtil.getHttpServicePort(bundleContext); if (port == -1) { diff --git a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java index befa1e6118..9010e2a0f5 100644 --- a/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java +++ b/bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java @@ -53,9 +53,8 @@ import com.google.gson.GsonBuilder; public class KonnectedHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(KonnectedHandler.class); private KonnectedConfiguration config; - private final String konnectedServletPath; private final KonnectedHTTPUtils http = new KonnectedHTTPUtils(30); - private String callbackIpAddress = null; + private String callbackUrl; private String baseUrl; private final Gson gson = new GsonBuilder().create(); private int retryCount; @@ -71,11 +70,10 @@ public class KonnectedHandler extends BaseThingHandler { * @param hostAddress the webaddress of the openHAB server instance obtained by the runtime * @param port the port on which the openHAB instance is running that was obtained by the runtime. */ - public KonnectedHandler(Thing thing, String path, String hostAddress, String port) { + public KonnectedHandler(Thing thing, String callbackUrl) { super(thing); - this.konnectedServletPath = path; - callbackIpAddress = hostAddress + ":" + port; - logger.debug("The callback ip address is: {}", callbackIpAddress); + this.callbackUrl = callbackUrl; + logger.debug("The auto discovered callback URL is: {}", this.callbackUrl); retryCount = 2; thingID = getThing().getThingTypeUID().getId(); authToken = getThing().getUID().getAsString(); @@ -178,9 +176,16 @@ public class KonnectedHandler extends BaseThingHandler { String testRetryCount = testConfig.get(RETRY_COUNT).toString(); String testRequestTimeout = testConfig.get(REQUEST_TIMEOUT).toString(); baseUrl = testConfig.get(BASE_URL).toString(); + String configuredCallbackUrl = (String) getThing().getConfiguration().get(CALLBACK_URL); + if (configuredCallbackUrl != null) { + callbackUrl = configuredCallbackUrl; + } else { + getThing().getConfiguration().put(CALLBACK_URL, callbackUrl); + } logger.debug("The RequestTimeout Parameter is Configured as: {}", testRequestTimeout); logger.debug("The Retry Count Parameter is Configured as: {}", testRetryCount); logger.debug("Base URL is Configured as: {}", baseUrl); + logger.debug("The callback URL is: {}", callbackUrl); try { this.retryCount = Integer.parseInt(testRetryCount); } catch (NumberFormatException e) { @@ -197,9 +202,8 @@ public class KonnectedHandler extends BaseThingHandler { testRequestTimeout); } - if ((callbackIpAddress == null)) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, - "Unable to obtain hostaddress from OSGI service, please configure hostaddress"); + if ((callbackUrl == null)) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Unable to obtain callback URL"); } else { @@ -321,13 +325,8 @@ public class KonnectedHandler extends BaseThingHandler { * @return a json settings payload which can be sent to the Konnected Module based on the Thing */ private String constructSettingsPayload() { - String apiUrl = (String) getThing().getConfiguration().get(CALLBACK_URL); - if (apiUrl == null) { - apiUrl = "http://" + callbackIpAddress + this.konnectedServletPath; - } - logger.debug("The Auth_Token is: {}", authToken); - KonnectedModulePayload payload = new KonnectedModulePayload(authToken, apiUrl); + KonnectedModulePayload payload = new KonnectedModulePayload(authToken, callbackUrl); payload.setBlink(config.blink); payload.setDiscovery(config.discovery); this.getThing().getChannels().forEach(channel -> { diff --git a/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/config/config.xml index ed3bacd31a..500bc1a457 100644 --- a/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/config/config.xml @@ -46,8 +46,8 @@ - - The URI where the Konnected panel will make callbacks. The default is to auto detect the port and IP + + The URL where the Konnected panel will make callbacks. The default is to auto detect the port and IP address of openHAB. This may not work in case you use a reverse proxy or openHAB is running in Docker. Then the plugin diff --git a/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/i18n/konnected.properties b/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/i18n/konnected.properties index ed42b78553..d04280d50b 100644 --- a/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/i18n/konnected.properties +++ b/bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/i18n/konnected.properties @@ -16,8 +16,8 @@ thing-type.config.konnected.module.baseUrl.label = Base URL thing-type.config.konnected.module.baseUrl.description = The base URL of the Konnected Alarm Panel. thing-type.config.konnected.module.blink.label = Blink thing-type.config.konnected.module.blink.description = When set to false the Led on the device won't blink during transmission. -thing-type.config.konnected.module.callbackUrl.label = Callback URI -thing-type.config.konnected.module.callbackUrl.description = The URI where the Konnected panel will make callbacks. The default is to auto detect the port and IP address of openHAB. This may not work in case you use a reverse proxy or openHAB is running in Docker. Then the plugin is bound to the /konnected http context. +thing-type.config.konnected.module.callbackUrl.label = Callback URL +thing-type.config.konnected.module.callbackUrl.description = The URL where the Konnected panel will make callbacks. The default is to auto detect the port and IP address of openHAB. This may not work in case you use a reverse proxy or openHAB is running in Docker. Then the plugin is bound to the /konnected http context. thing-type.config.konnected.module.controller_removewifi.label = Factory Reset thing-type.config.konnected.module.controller_removewifi.description = Resets the module to Factory Conditions. thing-type.config.konnected.module.controller_sendConfig.label = Update Settings -- 2.47.3