From b36877ec6d5ce8e7bb25d2d979b7e24eda466e9c Mon Sep 17 00:00:00 2001 From: lsiepel Date: Mon, 7 Oct 2024 23:36:26 +0200 Subject: [PATCH] [pushover] Fix idle connection causing EOF exception (#17348) Signed-off-by: Leo Siepel --- .../org.openhab.binding.pushover/README.md | 2 +- .../internal/PushoverBindingConstants.java | 2 +- .../config/PushoverAccountConfiguration.java | 1 + .../factory/PushoverHandlerFactory.java | 21 ++++++++++++++++++- .../handler/PushoverAccountHandler.java | 2 +- .../main/resources/OH-INF/config/config.xml | 6 ++++++ .../resources/OH-INF/i18n/pushover.properties | 2 ++ 7 files changed, 32 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.pushover/README.md b/bundles/org.openhab.binding.pushover/README.md index 2b7b93eb2c7..228071ee3c7 100644 --- a/bundles/org.openhab.binding.pushover/README.md +++ b/bundles/org.openhab.binding.pushover/README.md @@ -26,6 +26,7 @@ You can reach it via [https://pushover.net/apps/clone/openHAB](https://pushover. | `retry` | integer | The retry parameter specifies how often (in seconds) the Pushover servers will send the same emergency-priority notification to the user (default: `300`). **advanced** | | `expire` | integer | The expire parameter specifies how long (in seconds) your emergency-priority notification will continue to be retried (default: `3600`). **advanced** | | `timeout` | integer | The timeout parameter specifies maximum number of seconds a request to Pushover can take. **advanced** | +| `idleTimeout` | integer | The idle-timeout parameter specifies maximum number of seconds a connection with Pushover can be idle (default: `300`). **advanced** | ## Channels @@ -74,7 +75,6 @@ For priority `2` only, the action returns a `String` value (the `receipt`) if th For other priorities, the action always returns an empty `String`. - `cancelPriorityMessage(String receipt)` - This method is used to cancel an emergency priority message. - The action returns a `Boolean` value to indicate if the message was cancelled successfully or not. ## Full Example diff --git a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/PushoverBindingConstants.java b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/PushoverBindingConstants.java index d041f14736f..342f07989e0 100644 --- a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/PushoverBindingConstants.java +++ b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/PushoverBindingConstants.java @@ -23,7 +23,7 @@ import org.openhab.core.thing.ThingTypeUID; @NonNullByDefault public class PushoverBindingConstants { - private static final String BINDING_ID = "pushover"; + public static final String BINDING_ID = "pushover"; public static final ThingTypeUID PUSHOVER_ACCOUNT = new ThingTypeUID(BINDING_ID, "pushover-account"); diff --git a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/config/PushoverAccountConfiguration.java b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/config/PushoverAccountConfiguration.java index c6dbb57a33e..a8f280bb87d 100644 --- a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/config/PushoverAccountConfiguration.java +++ b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/config/PushoverAccountConfiguration.java @@ -47,4 +47,5 @@ public class PushoverAccountConfiguration { public int retry = 300; public int expire = 3600; public int timeout = 10; + public int idleTimeout = 300; } diff --git a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/factory/PushoverHandlerFactory.java b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/factory/PushoverHandlerFactory.java index 7d9927eb435..161940e36fa 100644 --- a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/factory/PushoverHandlerFactory.java +++ b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/factory/PushoverHandlerFactory.java @@ -19,13 +19,16 @@ import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; +import org.openhab.binding.pushover.internal.PushoverBindingConstants; import org.openhab.binding.pushover.internal.handler.PushoverAccountHandler; import org.openhab.core.io.net.http.HttpClientFactory; +import org.openhab.core.io.net.http.HttpClientInitializationException; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.binding.BaseThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandlerFactory; +import org.osgi.service.component.ComponentContext; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -45,7 +48,23 @@ public class PushoverHandlerFactory extends BaseThingHandlerFactory { @Activate public PushoverHandlerFactory(final @Reference HttpClientFactory httpClientFactory) { - this.httpClient = httpClientFactory.getCommonHttpClient(); + httpClient = httpClientFactory.createHttpClient(PushoverBindingConstants.BINDING_ID); + try { + httpClient.start(); + } catch (final Exception e) { + throw new HttpClientInitializationException("Could not start HttpClient", e); + } + } + + @Override + protected void deactivate(final ComponentContext componentContext) { + try { + httpClient.stop(); + } catch (final Exception e) { + // Eat http client stop exception. + } finally { + super.deactivate(componentContext); + } } @Override diff --git a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/handler/PushoverAccountHandler.java b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/handler/PushoverAccountHandler.java index c7d501bda22..a75a504031d 100644 --- a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/handler/PushoverAccountHandler.java +++ b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/handler/PushoverAccountHandler.java @@ -83,7 +83,7 @@ public class PushoverAccountHandler extends BaseThingHandler { if (configValid) { updateStatus(ThingStatus.UNKNOWN); - + httpClient.setIdleTimeout(config.idleTimeout * 1000); connection = new PushoverAPIConnection(httpClient, config); scheduler.submit(this::asyncValidateUser); } diff --git a/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/config/config.xml index 9f4be6e4134..01084a05bf6 100644 --- a/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/config/config.xml @@ -55,6 +55,12 @@ take. 10 + + true + + The idle-timeout parameter specifies maximum number of seconds a connection with Pushover can be idle. + 300 + diff --git a/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover.properties b/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover.properties index be47aaa7cdb..68b5215d18f 100644 --- a/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover.properties +++ b/bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover.properties @@ -19,6 +19,8 @@ thing-type.config.pushover.pushover-account.format.description = The default for thing-type.config.pushover.pushover-account.format.option.none = None thing-type.config.pushover.pushover-account.format.option.html = HTML thing-type.config.pushover.pushover-account.format.option.monospace = monospace +thing-type.config.pushover.pushover-account.idleTimeout.label = Idle Timeout +thing-type.config.pushover.pushover-account.idleTimeout.description = The idle-timeout parameter specifies maximum number of seconds a connection with Pushover can be idle. thing-type.config.pushover.pushover-account.retry.label = Retry thing-type.config.pushover.pushover-account.retry.description = The retry parameter specifies how often the Pushover servers will send the same notification to the user. thing-type.config.pushover.pushover-account.sound.label = Notification Sound