2 * Copyright (c) 2010-2020 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 static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.DEFAULT_WAIT_BEFORE_INITIAL_REFRESH;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApi;
23 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BaseBridgeHandler;
29 import org.openhab.core.types.Command;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * @author Florian Schmidt - Refactoring
37 public abstract class OpenSprinklerBaseBridgeHandler extends BaseBridgeHandler {
38 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerBaseBridgeHandler.class);
41 private ScheduledFuture<?> pollingJob;
43 protected OpenSprinklerApi openSprinklerDevice;
45 public OpenSprinklerBaseBridgeHandler(Bridge bridge) {
49 public OpenSprinklerApi getApi() {
50 OpenSprinklerApi api = openSprinklerDevice;
52 throw new IllegalStateException();
58 public void initialize() {
59 pollingJob = scheduler.scheduleWithFixedDelay(this::refreshStations, DEFAULT_WAIT_BEFORE_INITIAL_REFRESH,
60 getRefreshInterval(), TimeUnit.SECONDS);
63 protected abstract long getRefreshInterval();
65 private void refreshStations() {
66 if (openSprinklerDevice != null) {
67 if (openSprinklerDevice.isManualModeEnabled()) {
68 updateStatus(ThingStatus.ONLINE);
70 this.getThing().getThings().forEach(thing -> {
71 OpenSprinklerBaseHandler handler = (OpenSprinklerBaseHandler) thing.getHandler();
72 if (handler != null) {
73 handler.updateChannels();
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
78 "Could not sync status with the OpenSprinkler.");
84 public void dispose() {
86 if (openSprinklerDevice != null) {
88 openSprinklerDevice.leaveManualMode();
89 } catch (CommunicationApiException e) {
90 logger.error("Could not close connection on teardown.", e);
92 openSprinklerDevice = null;
95 if (pollingJob != null) {
96 pollingJob.cancel(true);
102 public void handleCommand(ChannelUID channelUID, Command command) {
103 // Nothing to do for the bridge handler