]> git.basschouten.com Git - openhab-addons.git/blob
ef3f444c73425e2005950f317627aad42723e441
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.*;
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     @Override
64     public void initialize() {
65         config = getConfigAs(NZWaterAlertsConfiguration.class);
66
67         NZWaterAlertsBinder localBinder = binder = new NZWaterAlertsBinder(httpClient, config, scheduler);
68
69         updateStatus(ThingStatus.UNKNOWN);
70         localBinder.registerListener(this);
71     }
72
73     @Override
74     public void dispose() {
75         NZWaterAlertsBinder localBinder = binder;
76         if (localBinder != null) {
77             localBinder.unregisterListener(this);
78         }
79
80         super.dispose();
81     }
82
83     @Override
84     public void handleRemoval() {
85         NZWaterAlertsBinder localBinder = binder;
86         if (localBinder != null) {
87             localBinder.unregisterListener(this);
88         }
89
90         super.handleRemoval();
91     }
92
93     @Override
94     public void updateWaterLevel(int level) {
95         if (level == -1) {
96             updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), UnDefType.UNDEF);
97         } else {
98             updateState(new ChannelUID(getThing().getUID(), CHANNEL_ALERTLEVEL), new DecimalType(level));
99         }
100     }
101
102     @Override
103     public void updateBindingStatus(ThingStatus thingStatus) {
104         updateStatus(thingStatus);
105     }
106
107     @Override
108     public void updateBindingStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail, String description) {
109         updateStatus(thingStatus, thingStatusDetail, description);
110     }
111 }