2 * Copyright (c) 2010-2020 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.*;
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)
64 public void initialize() {
65 config = getConfigAs(NZWaterAlertsConfiguration.class);
67 NZWaterAlertsBinder localBinder = binder = new NZWaterAlertsBinder(httpClient, config, scheduler);
69 updateStatus(ThingStatus.UNKNOWN);
70 localBinder.registerListener(this);
74 public void dispose() {
75 NZWaterAlertsBinder localBinder = binder;
76 if (localBinder != null) {
77 localBinder.unregisterListener(this);
84 public void handleRemoval() {
85 NZWaterAlertsBinder localBinder = binder;
86 if (localBinder != null) {
87 localBinder.unregisterListener(this);
90 super.handleRemoval();
94 public void updateWaterLevel(int level) {
96 updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), UnDefType.UNDEF);
98 updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), new DecimalType(level));
103 public void updateBindingStatus(ThingStatus thingStatus) {
104 updateStatus(thingStatus);
108 public void updateBindingStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail, String description) {
109 updateStatus(thingStatus, thingStatusDetail, description);