2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nzwateralerts.internal.handler;
15 import static org.openhab.binding.nzwateralerts.internal.NZWaterAlertsBindingConstants.CHANNEL_ALERTLEVEL;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.openhab.binding.nzwateralerts.internal.NZWaterAlertsConfiguration;
21 import org.openhab.binding.nzwateralerts.internal.binder.NZWaterAlertsBinder;
22 import org.openhab.binding.nzwateralerts.internal.binder.NZWaterAlertsBinderListener;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BaseThingHandler;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.openhab.core.types.UnDefType;
34 * The {@link NZWaterAlertsHandler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Stewart Cossey - Initial contribution
40 public class NZWaterAlertsHandler extends BaseThingHandler implements NZWaterAlertsBinderListener {
42 private @Nullable NZWaterAlertsConfiguration config = null;
43 private HttpClient httpClient;
44 private @Nullable NZWaterAlertsBinder binder = null;
46 public NZWaterAlertsHandler(Thing thing, HttpClient httpClient) {
49 this.httpClient = httpClient;
53 public void handleCommand(ChannelUID channelUID, Command command) {
54 final NZWaterAlertsBinder localBinder = binder;
55 if (CHANNEL_ALERTLEVEL.equals(channelUID.getId())) {
56 if (command instanceof RefreshType) {
57 if (localBinder != null) {
65 public void initialize() {
66 config = getConfigAs(NZWaterAlertsConfiguration.class);
68 NZWaterAlertsBinder localBinder = binder = new NZWaterAlertsBinder(httpClient, config, scheduler);
70 updateStatus(ThingStatus.UNKNOWN);
71 localBinder.registerListener(this);
75 public void dispose() {
76 NZWaterAlertsBinder localBinder = binder;
77 if (localBinder != null) {
78 localBinder.unregisterListener(this);
85 public void handleRemoval() {
86 NZWaterAlertsBinder localBinder = binder;
87 if (localBinder != null) {
88 localBinder.unregisterListener(this);
91 super.handleRemoval();
95 public void updateWaterLevel(int level) {
97 updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), UnDefType.UNDEF);
99 updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), new DecimalType(level));
104 public void updateBindingStatus(ThingStatus thingStatus) {
105 updateStatus(thingStatus);
109 public void updateBindingStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail, String description) {
110 updateStatus(thingStatus, thingStatusDetail, description);