]> git.basschouten.com Git - openhab-addons.git/blob
177d7a7fa1a9f8c75e3d375f10e7bf9034445363
[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.opensprinkler.internal.handler;
14
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.DEFAULT_WAIT_BEFORE_INITIAL_REFRESH;
16
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19
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;
32
33 /**
34  * @author Florian Schmidt - Refactoring
35  */
36 @NonNullByDefault
37 public abstract class OpenSprinklerBaseBridgeHandler extends BaseBridgeHandler {
38     private final Logger logger = LoggerFactory.getLogger(OpenSprinklerBaseBridgeHandler.class);
39
40     @Nullable
41     private ScheduledFuture<?> pollingJob;
42     @Nullable
43     protected OpenSprinklerApi openSprinklerDevice;
44
45     public OpenSprinklerBaseBridgeHandler(Bridge bridge) {
46         super(bridge);
47     }
48
49     public OpenSprinklerApi getApi() {
50         OpenSprinklerApi api = openSprinklerDevice;
51         if (api == null) {
52             throw new IllegalStateException();
53         }
54         return api;
55     }
56
57     @Override
58     public void initialize() {
59         pollingJob = scheduler.scheduleWithFixedDelay(this::refreshStations, DEFAULT_WAIT_BEFORE_INITIAL_REFRESH,
60                 getRefreshInterval(), TimeUnit.SECONDS);
61     }
62
63     protected abstract long getRefreshInterval();
64
65     private void refreshStations() {
66         if (openSprinklerDevice != null) {
67             if (openSprinklerDevice.isManualModeEnabled()) {
68                 updateStatus(ThingStatus.ONLINE);
69
70                 this.getThing().getThings().forEach(thing -> {
71                     OpenSprinklerBaseHandler handler = (OpenSprinklerBaseHandler) thing.getHandler();
72                     if (handler != null) {
73                         handler.updateChannels();
74                     }
75                 });
76             } else {
77                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
78                         "Could not sync status with the OpenSprinkler.");
79             }
80         }
81     }
82
83     @Override
84     public void dispose() {
85         super.dispose();
86         if (openSprinklerDevice != null) {
87             try {
88                 openSprinklerDevice.leaveManualMode();
89             } catch (CommunicationApiException e) {
90                 logger.error("Could not close connection on teardown.", e);
91             }
92             openSprinklerDevice = null;
93         }
94
95         if (pollingJob != null) {
96             pollingJob.cancel(true);
97             pollingJob = null;
98         }
99     }
100
101     @Override
102     public void handleCommand(ChannelUID channelUID, Command command) {
103         // Nothing to do for the bridge handler
104     }
105 }