Setting auto discovered callback URL in configuration. Cleaned up callback URL handling (#13295)

Signed-off-by: Haavar Valeur <haavar@haavar.com>
This commit is contained in:
Haavar Valeur 2022-08-21 03:21:44 -07:00 committed by GitHub
parent 1b5e766b55
commit ed3642da84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 34 deletions

View File

@ -65,7 +65,8 @@ public class KonnectedHandlerFactory extends BaseThingHandlerFactory {
protected void activate(ComponentContext componentContext) { protected void activate(ComponentContext componentContext) {
super.activate(componentContext); super.activate(componentContext);
Dictionary<String, Object> properties = componentContext.getProperties(); 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 { try {
this.servlet = registerWebHookServlet(); this.servlet = registerWebHookServlet();
} catch (KonnectedWebHookFail e) { } catch (KonnectedWebHookFail e) {
@ -81,8 +82,8 @@ public class KonnectedHandlerFactory extends BaseThingHandlerFactory {
@Override @Override
protected @Nullable ThingHandler createHandler(Thing thing) { protected @Nullable ThingHandler createHandler(Thing thing) {
KonnectedHandler thingHandler = new KonnectedHandler(thing, '/' + BINDING_ID, createCallbackUrl(),
createCallbackPort()); KonnectedHandler thingHandler = new KonnectedHandler(thing, getCallbackUrl());
if (servlet != null) { if (servlet != null) {
logger.debug("Adding thinghandler for thing {} to webhook.", thing.getUID().getId()); logger.debug("Adding thinghandler for thing {} to webhook.", thing.getUID().getId());
servlet.add(thingHandler); servlet.add(thingHandler);
@ -119,22 +120,28 @@ public class KonnectedHandlerFactory extends BaseThingHandlerFactory {
this.httpService = null; this.httpService = null;
} }
private String createCallbackUrl() { private String getCallbackUrl() {
if (callbackUrl != null) { if (callbackUrl == null) {
logger.debug("The callback ip address from the OSGI is:{}", callbackUrl); String callbackIP = discoverCallbackIP();
return callbackUrl; String callbackPort = discoverCallbackPort();
} else { if (callbackPort != null && callbackIP != null) {
final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress(); callbackUrl = "http://" + discoverCallbackIP() + ":" + discoverCallbackPort() + '/' + BINDING_ID;
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;
} }
return callbackUrl;
} }
private String createCallbackPort() { 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 discoverCallbackPort() {
// we do not use SSL as it can cause certificate validation issues. // we do not use SSL as it can cause certificate validation issues.
final int port = HttpServiceUtil.getHttpServicePort(bundleContext); final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
if (port == -1) { if (port == -1) {

View File

@ -53,9 +53,8 @@ import com.google.gson.GsonBuilder;
public class KonnectedHandler extends BaseThingHandler { public class KonnectedHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(KonnectedHandler.class); private final Logger logger = LoggerFactory.getLogger(KonnectedHandler.class);
private KonnectedConfiguration config; private KonnectedConfiguration config;
private final String konnectedServletPath;
private final KonnectedHTTPUtils http = new KonnectedHTTPUtils(30); private final KonnectedHTTPUtils http = new KonnectedHTTPUtils(30);
private String callbackIpAddress = null; private String callbackUrl;
private String baseUrl; private String baseUrl;
private final Gson gson = new GsonBuilder().create(); private final Gson gson = new GsonBuilder().create();
private int retryCount; 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 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. * @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); super(thing);
this.konnectedServletPath = path; this.callbackUrl = callbackUrl;
callbackIpAddress = hostAddress + ":" + port; logger.debug("The auto discovered callback URL is: {}", this.callbackUrl);
logger.debug("The callback ip address is: {}", callbackIpAddress);
retryCount = 2; retryCount = 2;
thingID = getThing().getThingTypeUID().getId(); thingID = getThing().getThingTypeUID().getId();
authToken = getThing().getUID().getAsString(); authToken = getThing().getUID().getAsString();
@ -178,9 +176,16 @@ public class KonnectedHandler extends BaseThingHandler {
String testRetryCount = testConfig.get(RETRY_COUNT).toString(); String testRetryCount = testConfig.get(RETRY_COUNT).toString();
String testRequestTimeout = testConfig.get(REQUEST_TIMEOUT).toString(); String testRequestTimeout = testConfig.get(REQUEST_TIMEOUT).toString();
baseUrl = testConfig.get(BASE_URL).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 RequestTimeout Parameter is Configured as: {}", testRequestTimeout);
logger.debug("The Retry Count Parameter is Configured as: {}", testRetryCount); logger.debug("The Retry Count Parameter is Configured as: {}", testRetryCount);
logger.debug("Base URL is Configured as: {}", baseUrl); logger.debug("Base URL is Configured as: {}", baseUrl);
logger.debug("The callback URL is: {}", callbackUrl);
try { try {
this.retryCount = Integer.parseInt(testRetryCount); this.retryCount = Integer.parseInt(testRetryCount);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -197,9 +202,8 @@ public class KonnectedHandler extends BaseThingHandler {
testRequestTimeout); testRequestTimeout);
} }
if ((callbackIpAddress == null)) { if ((callbackUrl == null)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Unable to obtain callback URL");
"Unable to obtain hostaddress from OSGI service, please configure hostaddress");
} }
else { 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 * @return a json settings payload which can be sent to the Konnected Module based on the Thing
*/ */
private String constructSettingsPayload() { 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); logger.debug("The Auth_Token is: {}", authToken);
KonnectedModulePayload payload = new KonnectedModulePayload(authToken, apiUrl); KonnectedModulePayload payload = new KonnectedModulePayload(authToken, callbackUrl);
payload.setBlink(config.blink); payload.setBlink(config.blink);
payload.setDiscovery(config.discovery); payload.setDiscovery(config.discovery);
this.getThing().getChannels().forEach(channel -> { this.getThing().getChannels().forEach(channel -> {

View File

@ -46,8 +46,8 @@
</parameter> </parameter>
<parameter name="callbackUrl" type="text"> <parameter name="callbackUrl" type="text">
<label>Callback URI</label> <label>Callback URL</label>
<description>The URI where the Konnected panel will make callbacks. The default is to auto detect the port and IP <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 address of openHAB. This may not work in case you use a reverse proxy or openHAB
is running in Docker. Then the is running in Docker. Then the
plugin plugin

View 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.baseUrl.description = The base URL of the Konnected Alarm Panel.
thing-type.config.konnected.module.blink.label = Blink 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.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.label = Callback URL
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.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.label = Factory Reset
thing-type.config.konnected.module.controller_removewifi.description = Resets the module to Factory Conditions. thing-type.config.konnected.module.controller_removewifi.description = Resets the module to Factory Conditions.
thing-type.config.konnected.module.controller_sendConfig.label = Update Settings thing-type.config.konnected.module.controller_sendConfig.label = Update Settings