]> git.basschouten.com Git - openhab-addons.git/blob
403e1ebf262b06e8f95b7d152bff2b10d04822bb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nzwateralerts.internal.handler;
14
15 import static org.openhab.binding.nzwateralerts.internal.NZWaterAlertsBindingConstants.CHANNEL_ALERTLEVEL;
16
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;
32
33 /**
34  * The {@link NZWaterAlertsHandler} is responsible for handling commands, which are
35  * sent to one of the channels.
36  *
37  * @author Stewart Cossey - Initial contribution
38  */
39 @NonNullByDefault
40 public class NZWaterAlertsHandler extends BaseThingHandler implements NZWaterAlertsBinderListener {
41
42     private @Nullable NZWaterAlertsConfiguration config = null;
43     private HttpClient httpClient;
44     private @Nullable NZWaterAlertsBinder binder = null;
45
46     public NZWaterAlertsHandler(Thing thing, HttpClient httpClient) {
47         super(thing);
48
49         this.httpClient = httpClient;
50     }
51
52     @Override
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) {
58                     localBinder.update();
59                 }
60             }
61         }
62     }
63
64     @Override
65     public void initialize() {
66         config = getConfigAs(NZWaterAlertsConfiguration.class);
67
68         NZWaterAlertsBinder localBinder = binder = new NZWaterAlertsBinder(httpClient, config, scheduler);
69
70         updateStatus(ThingStatus.UNKNOWN);
71         localBinder.registerListener(this);
72     }
73
74     @Override
75     public void dispose() {
76         NZWaterAlertsBinder localBinder = binder;
77         if (localBinder != null) {
78             localBinder.unregisterListener(this);
79         }
80
81         super.dispose();
82     }
83
84     @Override
85     public void handleRemoval() {
86         NZWaterAlertsBinder localBinder = binder;
87         if (localBinder != null) {
88             localBinder.unregisterListener(this);
89         }
90
91         super.handleRemoval();
92     }
93
94     @Override
95     public void updateWaterLevel(int level) {
96         if (level == -1) {
97             updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), UnDefType.UNDEF);
98         } else {
99             updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), new DecimalType(level));
100         }
101     }
102
103     @Override
104     public void updateBindingStatus(ThingStatus thingStatus) {
105         updateStatus(thingStatus);
106     }
107
108     @Override
109     public void updateBindingStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail, String description) {
110         updateStatus(thingStatus, thingStatusDetail, description);
111     }
112 }