]> git.basschouten.com Git - openhab-addons.git/blob
b05fb7ede0884e55d35907573e7d8687a23932d6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.binder;
14
15 import java.util.Set;
16 import java.util.concurrent.CopyOnWriteArraySet;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.nzwateralerts.internal.NZWaterAlertsConfiguration;
25 import org.openhab.binding.nzwateralerts.internal.api.WaterAlertWebClient;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link NZWaterAlertsBinder} is responsible for handling the connection
33  * between the handler and API.
34  *
35  * @author Stewart Cossey - Initial contribution
36  */
37 @NonNullByDefault
38 public class NZWaterAlertsBinder {
39     private @Nullable WaterAlertWebClient webClient;
40
41     private final Logger logger = LoggerFactory.getLogger(NZWaterAlertsBinder.class);
42
43     private final Set<NZWaterAlertsBinderListener> listeners = new CopyOnWriteArraySet<>();
44     private @Nullable ScheduledFuture<?> future;
45     private final ScheduledExecutorService scheduler;
46
47     private int refreshInterval = 5;
48
49     public NZWaterAlertsBinder(final HttpClient httpClient, @Nullable final NZWaterAlertsConfiguration config,
50             final ScheduledExecutorService scheduler) {
51         this.scheduler = scheduler;
52
53         if (config != null) {
54             final String localLocation = config.location;
55             if (localLocation == null) {
56                 for (final NZWaterAlertsBinderListener listener : listeners) {
57                     listener.updateBindingStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
58                             "Location is not set.");
59                 }
60             } else {
61                 this.webClient = new WaterAlertWebClient(httpClient, localLocation);
62                 refreshInterval = config.refreshInterval;
63             }
64         } else {
65             for (final NZWaterAlertsBinderListener listener : listeners) {
66                 listener.updateBindingStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
67                         "Could not create webClient, a parameter is null");
68             }
69             logger.debug("Create Binder failed due to null config item");
70         }
71     }
72
73     public void update() {
74         final WaterAlertWebClient localWebClient = webClient;
75         if (localWebClient != null) {
76             final Integer waterLevel = localWebClient.getLevel();
77
78             for (final NZWaterAlertsBinderListener listener : listeners) {
79                 if (waterLevel == null) {
80                     listener.updateBindingStatus(ThingStatus.OFFLINE);
81                 } else {
82                     listener.updateBindingStatus(ThingStatus.ONLINE);
83                     listener.updateWaterLevel(waterLevel);
84                 }
85             }
86         }
87     }
88
89     /**
90      * Registers the given {@link NZWaterAlertsBinderListener}. If it is already
91      * registered, this method returns immediately.
92      *
93      * @param alertsBinderInterface The {@link NZWaterAlertsBinderListener} to be
94      *            registered.
95      */
96     public void registerListener(final NZWaterAlertsBinderListener alertsBinderInterface) {
97         final boolean isAdded = listeners.add(alertsBinderInterface);
98         if (isAdded) {
99             updatePollingState();
100         }
101     }
102
103     /**
104      * Unregisters the given {@link NZWaterAlertsBinderListener}. If it is already
105      * unregistered, this method returns immediately.
106      *
107      * @param alertsBinderInterface The {@link NZWaterAlertsBinderListener} to be
108      *            unregistered.
109      */
110     public void unregisterListener(final NZWaterAlertsBinderListener alertsBinderInterface) {
111         final boolean isRemoved = listeners.remove(alertsBinderInterface);
112         if (isRemoved) {
113             updatePollingState();
114         }
115     }
116
117     private void updatePollingState() {
118         final ScheduledFuture<?> localFuture = future;
119
120         if (localFuture != null && listeners.isEmpty()) {
121             localFuture.cancel(true);
122             future = null;
123             return;
124         }
125
126         if (localFuture == null && !listeners.isEmpty()) {
127             future = scheduler.scheduleWithFixedDelay(this::update, 0, refreshInterval, TimeUnit.HOURS);
128         }
129     }
130 }