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.opensprinkler.internal.handler;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApi;
21 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiFactory;
22 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
23 import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
24 import org.openhab.binding.opensprinkler.internal.api.exception.UnauthorizedApiException;
25 import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerHttpInterfaceConfig;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseBridgeHandler;
31 import org.openhab.core.types.Command;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * @author Chris Graham - Initial contribution
37 * @author Florian Schmidt - Refactoring
40 public class OpenSprinklerHttpBridgeHandler extends BaseBridgeHandler {
41 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerHttpBridgeHandler.class);
42 private @Nullable ScheduledFuture<?> pollingJob;
43 private @Nullable ScheduledFuture<?> delayedJob;
44 private @Nullable OpenSprinklerApi openSprinklerDevice;
45 private OpenSprinklerHttpInterfaceConfig openSprinklerConfig = new OpenSprinklerHttpInterfaceConfig();
46 private OpenSprinklerApiFactory apiFactory;
48 public OpenSprinklerHttpBridgeHandler(Bridge bridge, OpenSprinklerApiFactory apiFactory) {
50 this.apiFactory = apiFactory;
53 public OpenSprinklerApi getApi() {
54 OpenSprinklerApi api = openSprinklerDevice;
56 throw new IllegalStateException();
61 public void communicationError(Exception e) {
62 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
63 "Communication Error with the OpenSprinkler: " + e.getMessage());
66 public void refreshStations() {
67 OpenSprinklerApi localApi = openSprinklerDevice;
68 if (localApi == null || !localApi.isManualModeEnabled()) {
70 localApi = openSprinklerDevice;
72 if (localApi != null) {
75 updateStatus(ThingStatus.ONLINE);
76 this.getThing().getThings().forEach(thing -> {
77 OpenSprinklerBaseHandler handler = (OpenSprinklerBaseHandler) thing.getHandler();
78 if (handler != null) {
79 handler.updateChannels();
82 } catch (CommunicationApiException e) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
84 "Could not sync status with the OpenSprinkler. " + e.getMessage());
85 } catch (UnauthorizedApiException e) {
86 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
87 "Unauthorized, check your password is correct");
92 public void delayedRefresh() {
93 ScheduledFuture<?> localFuture = delayedJob;
94 if (localFuture == null || localFuture.isDone()) {
95 delayedJob = scheduler.schedule(this::refreshStations, 3, TimeUnit.SECONDS);
96 } else {// User has sent multiple commands quickly, only need to update the controls once.
97 localFuture.cancel(true);
98 delayedJob = scheduler.schedule(this::refreshStations, 3, TimeUnit.SECONDS);
102 private void setupAPI() {
103 logger.debug("Initializing OpenSprinkler with config (Hostname: {}, Port: {}, Refresh: {}).",
104 openSprinklerConfig.hostname, openSprinklerConfig.port, openSprinklerConfig.refresh);
106 openSprinklerDevice = apiFactory.getHttpApi(openSprinklerConfig);
107 OpenSprinklerApi localApi = openSprinklerDevice;
108 localApi.enterManualMode();
109 if (!localApi.isManualModeEnabled()) {
110 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
111 "Could not initialize the connection to the OpenSprinkler.");
113 } catch (CommunicationApiException | GeneralApiException exp) {
114 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
115 "Could not create an API connection to the OpenSprinkler. Error received: " + exp);
121 public void handleCommand(ChannelUID channelUID, Command command) {
122 // Nothing to do for the bridge handler
126 public void initialize() {
127 openSprinklerConfig = getConfig().as(OpenSprinklerHttpInterfaceConfig.class);
128 pollingJob = scheduler.scheduleWithFixedDelay(this::refreshStations, 2, openSprinklerConfig.refresh,
133 public void dispose() {
134 OpenSprinklerApi localApi = openSprinklerDevice;
135 if (localApi != null) {
137 localApi.leaveManualMode();
138 } catch (CommunicationApiException | UnauthorizedApiException e) {
139 logger.warn("Could not close connection on teardown.");
141 openSprinklerDevice = null;
143 ScheduledFuture<?> localFuture = pollingJob;
144 if (localFuture != null) {
145 localFuture.cancel(true);
148 localFuture = delayedJob;
149 if (localFuture != null) {
150 localFuture.cancel(true);