2 * Copyright (c) 2010-2023 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.binder;
16 import java.util.concurrent.CopyOnWriteArraySet;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
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;
32 * The {@link NZWaterAlertsController} is responsible for handling the connection
33 * between the handler and API.
35 * @author Stewart Cossey - Initial contribution
38 public class NZWaterAlertsBinder {
39 private @Nullable WaterAlertWebClient webClient;
41 private final Logger logger = LoggerFactory.getLogger(NZWaterAlertsBinder.class);
43 private final Set<NZWaterAlertsBinderListener> listeners = new CopyOnWriteArraySet<>();
44 private @Nullable ScheduledFuture<?> future;
45 private final ScheduledExecutorService scheduler;
47 private int refreshInterval = 5;
49 public NZWaterAlertsBinder(final HttpClient httpClient, @Nullable final NZWaterAlertsConfiguration config,
50 final ScheduledExecutorService scheduler) {
51 this.scheduler = scheduler;
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.");
61 this.webClient = new WaterAlertWebClient(httpClient, localLocation);
62 refreshInterval = config.refreshInterval;
65 for (final NZWaterAlertsBinderListener listener : listeners) {
66 listener.updateBindingStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
67 "Could not create webClient, a parameter is null");
69 logger.debug("Create Binder failed due to null config item");
73 public void update() {
74 final WaterAlertWebClient localWebClient = webClient;
75 if (localWebClient != null) {
76 final Integer waterLevel = localWebClient.getLevel();
78 for (final NZWaterAlertsBinderListener listener : listeners) {
79 if (waterLevel == null) {
80 listener.updateBindingStatus(ThingStatus.OFFLINE);
82 listener.updateBindingStatus(ThingStatus.ONLINE);
83 listener.updateWaterLevel(waterLevel);
90 * Registers the given {@link NZWaterAlertsBinderListener}. If it is already
91 * registered, this method returns immediately.
93 * @param alertsBinderInterface The {@link NZWaterAlertsBinderListener} to be
96 public void registerListener(final NZWaterAlertsBinderListener alertsBinderInterface) {
97 final boolean isAdded = listeners.add(alertsBinderInterface);
104 * Unregisters the given {@link NZWaterAlertsBinderListener}. If it is already
105 * unregistered, this method returns immediately.
107 * @param alertsBinderInterface The {@link NZWaterAlertsBinderListener} to be
110 public void unregisterListener(final NZWaterAlertsBinderListener alertsBinderInterface) {
111 final boolean isRemoved = listeners.remove(alertsBinderInterface);
113 updatePollingState();
117 private void updatePollingState() {
118 final ScheduledFuture<?> localFuture = future;
120 if (localFuture != null && listeners.isEmpty()) {
121 localFuture.cancel(true);
126 if (localFuture == null && !listeners.isEmpty()) {
127 future = scheduler.scheduleWithFixedDelay(this::update, 0, refreshInterval, TimeUnit.HOURS);