Skip to content

Commit

Permalink
add lastSuccess and lastFailure channels
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
  • Loading branch information
J-N-K committed Apr 25, 2021
1 parent 77fa24d commit 1ba85da
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Character> URL_PART_DELIMITER = Set.of('/', '?', '&');

private final Logger logger = LoggerFactory.getLogger(HttpThingHandler.class);
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
<label>HTTP URL Thing</label>
<description>Represents a base URL and all associated requests.</description>

<channels>
<channel type="requestDateTime" id="lastFailure">
<label>Last Failure</label>
</channel>
<channel type="requestDateTime" id="lastSuccess">
<label>Last Success</label>
</channel>
</channels>

<properties>
<property name="thingTypeVersion">1</property>
</properties>

<config-description>
<parameter name="baseURL" type="text" required="true">
<label>Base URL</label>
Expand Down Expand Up @@ -121,6 +134,12 @@
</config-description>
</thing-type>

<channel-type id="stateDatetime">
<item-type>DateTime</item-type>
<category>Time</category>
<state readOnly="true" pattern="%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"/>
</channel-type>

<channel-type id="color">
<item-type>Color</item-type>
<label>Color Channel</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1ba85da

Please sign in to comment.