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) {
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 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.
final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
if (port == -1) {

View 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 -> {

View 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

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.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