From 1ba85da335f58b3cd5942e67ed6bc0f04a1bba47 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Sun, 25 Apr 2021 18:24:37 +0200 Subject: [PATCH] add lastSuccess and lastFailure channels Signed-off-by: Jan N. Klug --- .../http/internal/HttpBindingConstants.java | 3 +++ .../http/internal/HttpThingHandler.java | 9 +++++++-- .../resources/OH-INF/thing/thing-types.xml | 19 +++++++++++++++++++ .../src/main/resources/update/url.update | 3 +++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 bundles/org.smarthomej.binding.http/src/main/resources/update/url.update diff --git a/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpBindingConstants.java b/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpBindingConstants.java index b1ecf41cd1..be4dfe772b 100644 --- a/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpBindingConstants.java +++ b/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpBindingConstants.java @@ -28,4 +28,7 @@ public class HttpBindingConstants { private static final String BINDING_ID = "http"; public static final ThingTypeUID THING_TYPE_URL = new ThingTypeUID(BINDING_ID, "url"); + + public static final String CHANNEL_LAST_SUCCESS = "lastSuccess"; + public static final String CHANNEL_LAST_FAILURE = "lastFailure"; } diff --git a/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpThingHandler.java b/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpThingHandler.java index 371b1d63e8..ff2727fa7a 100644 --- a/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpThingHandler.java +++ b/bundles/org.smarthomej.binding.http/src/main/java/org/smarthomej/binding/http/internal/HttpThingHandler.java @@ -13,6 +13,9 @@ */ package org.smarthomej.binding.http.internal; +import static org.smarthomej.binding.http.internal.HttpBindingConstants.CHANNEL_LAST_FAILURE; +import static org.smarthomej.binding.http.internal.HttpBindingConstants.CHANNEL_LAST_SUCCESS; + import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -40,7 +43,6 @@ import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; -import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; import org.openhab.core.types.State; @@ -55,6 +57,7 @@ import org.smarthomej.binding.http.internal.http.RateLimitedHttpClient; import org.smarthomej.binding.http.internal.http.RefreshingUrlCache; import org.smarthomej.commons.SimpleDynamicStateDescriptionProvider; +import org.smarthomej.commons.UpdatingBaseThingHandler; import org.smarthomej.commons.itemvalueconverter.ChannelMode; import org.smarthomej.commons.itemvalueconverter.ContentWrapper; import org.smarthomej.commons.itemvalueconverter.ItemValueConverter; @@ -76,7 +79,7 @@ * @author Jan N. Klug - Initial contribution */ @NonNullByDefault -public class HttpThingHandler extends BaseThingHandler implements HttpStatusListener { +public class HttpThingHandler extends UpdatingBaseThingHandler implements HttpStatusListener { private static final Set URL_PART_DELIMITER = Set.of('/', '?', '&'); private final Logger logger = LoggerFactory.getLogger(HttpThingHandler.class); @@ -322,12 +325,14 @@ private void createChannel(Channel channel) { @Override public void onHttpError(@Nullable String message) { + updateState(CHANNEL_LAST_FAILURE, new DateTimeType()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, Objects.requireNonNullElse(message, "")); } @Override public void onHttpSuccess() { + updateState(CHANNEL_LAST_SUCCESS, new DateTimeType()); updateStatus(ThingStatus.ONLINE); } diff --git a/bundles/org.smarthomej.binding.http/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.smarthomej.binding.http/src/main/resources/OH-INF/thing/thing-types.xml index 7f6484c0b9..68d5f3732b 100644 --- a/bundles/org.smarthomej.binding.http/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.smarthomej.binding.http/src/main/resources/OH-INF/thing/thing-types.xml @@ -9,6 +9,19 @@ Represents a base URL and all associated requests. + + + + + + + + + + + 1 + + @@ -121,6 +134,12 @@ + + DateTime + Time + + + Color diff --git a/bundles/org.smarthomej.binding.http/src/main/resources/update/url.update b/bundles/org.smarthomej.binding.http/src/main/resources/update/url.update new file mode 100644 index 0000000000..c2c8f9c673 --- /dev/null +++ b/bundles/org.smarthomej.binding.http/src/main/resources/update/url.update @@ -0,0 +1,3 @@ +# add last success and last failure channels +1;ADD_CHANNEL;lastSuccess,DateTime,http:requestDateTime,Last Success +1;ADD_CHANNEL;lastFailure,DateTime,http:requestDateTime,Last Failure \ No newline at end of file