]> git.basschouten.com Git - openhab-addons.git/commitdiff
Setting auto discovered callback URL in configuration. Cleaned up callback URL handli...
authorHaavar Valeur <github.com@haavar.com>
Sun, 21 Aug 2022 10:21:44 +0000 (03:21 -0700)
committerGitHub <noreply@github.com>
Sun, 21 Aug 2022 10:21:44 +0000 (12:21 +0200)
Signed-off-by: Haavar Valeur <haavar@haavar.com>
bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/KonnectedHandlerFactory.java
bundles/org.openhab.binding.konnected/src/main/java/org/openhab/binding/konnected/internal/handler/KonnectedHandler.java
bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/config/config.xml
bundles/org.openhab.binding.konnected/src/main/resources/OH-INF/i18n/konnected.properties

index 27883b9a143b3d8ba5e75db3ce5477fe36b6c295..83fe16682ca9340e18dbb1b6967c8203b841667a 100644 (file)
@@ -65,7 +65,8 @@ public class KonnectedHandlerFactory extends BaseThingHandlerFactory {
     protected void activate(ComponentContext componentContext) {
         super.activate(componentContext);
         Dictionary<String, Object> 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) {
index befa1e6118c1585300f0fcebbff3d47862b3a54a..9010e2a0f5ef9af96504dfcf13c25ff9a3e6cb08 100644 (file)
@@ -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 -> {
index ed3bacd31a35340ef12e8288369d5a805ae4c91e..500bc1a45741b2ba923224e108a0add541403798 100644 (file)
@@ -46,8 +46,8 @@
                </parameter>
 
                <parameter name="callbackUrl" type="text">
-                       <label>Callback URI</label>
-                       <description>The URI where the Konnected panel will make callbacks. The default is to auto detect the port and IP
+                       <label>Callback URL</label>
+                       <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
index ed42b785532bbf42a8087f45d7e4c90e8f5ab41d..d04280d50b1c277acfd49219a7a3be1571590c37 100644 (file)
@@ -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