]> git.basschouten.com Git - openhab-addons.git/blob
107489fba26018a79dca197d43c0215469e8db51
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.fronius.internal.handler;
14
15 import java.util.HashSet;
16 import java.util.Set;
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.fronius.internal.FroniusBridgeConfiguration;
23 import org.openhab.binding.fronius.internal.FroniusCommunicationException;
24 import org.openhab.binding.fronius.internal.FroniusHttpUtil;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
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.thing.binding.ThingHandler;
32 import org.openhab.core.types.Command;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Bridge for Fronius devices.
38  *
39  * @author Gerrit Beine - Initial contribution
40  * @author Thomas Rokohl - Refactoring to merge the concepts.
41  *         Check if host is reachable.
42  * @author Jimmy Tanagra - Refactor the child services registration
43  *         Refactor host online check
44  */
45 @NonNullByDefault
46 public class FroniusBridgeHandler extends BaseBridgeHandler {
47
48     private final Logger logger = LoggerFactory.getLogger(FroniusBridgeHandler.class);
49     private static final int DEFAULT_REFRESH_PERIOD = 10;
50     private final Set<FroniusBaseThingHandler> services = new HashSet<>();
51     private @Nullable ScheduledFuture<?> refreshJob;
52
53     public FroniusBridgeHandler(Bridge bridge) {
54         super(bridge);
55     }
56
57     @Override
58     public void handleCommand(ChannelUID channelUID, Command command) {
59     }
60
61     @Override
62     public void initialize() {
63         final FroniusBridgeConfiguration config = getConfigAs(FroniusBridgeConfiguration.class);
64
65         boolean validConfig = true;
66         String errorMsg = null;
67
68         String hostname = config.hostname;
69         if (hostname == null || hostname.isBlank()) {
70             errorMsg = "Parameter 'hostname' is mandatory and must be configured";
71             validConfig = false;
72         }
73
74         if (config.refreshInterval != null && config.refreshInterval <= 0) {
75             errorMsg = "Parameter 'refresh' must be at least 1 second";
76             validConfig = false;
77         }
78
79         if (validConfig) {
80             startAutomaticRefresh();
81         } else {
82             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMsg);
83         }
84     }
85
86     @Override
87     public void dispose() {
88         if (refreshJob != null) {
89             refreshJob.cancel(true);
90             refreshJob = null;
91         }
92     }
93
94     @Override
95     public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
96         if (childHandler instanceof FroniusBaseThingHandler handler) {
97             this.services.add(handler);
98             restartAutomaticRefresh();
99         } else {
100             logger.debug("Child handler {} not added because it is not an instance of FroniusBaseThingHandler",
101                     childThing.getUID().getId());
102         }
103     }
104
105     @Override
106     public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
107         this.services.remove((FroniusBaseThingHandler) childHandler);
108     }
109
110     private void restartAutomaticRefresh() {
111         if (refreshJob != null) { // refreshJob should be null if the config isn't valid
112             refreshJob.cancel(false);
113             startAutomaticRefresh();
114         }
115     }
116
117     /**
118      * Start the job refreshing the data
119      */
120     private void startAutomaticRefresh() {
121         if (refreshJob == null || refreshJob.isCancelled()) {
122             final FroniusBridgeConfiguration config = getConfigAs(FroniusBridgeConfiguration.class);
123             Runnable runnable = () -> {
124                 try {
125                     checkBridgeOnline(config);
126                     if (getThing().getStatus() != ThingStatus.ONLINE) {
127                         updateStatus(ThingStatus.ONLINE);
128                     }
129                     for (FroniusBaseThingHandler service : services) {
130                         service.refresh(config);
131                     }
132                 } catch (FroniusCommunicationException e) {
133                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
134                 }
135             };
136
137             int delay = (config.refreshInterval != null) ? config.refreshInterval.intValue() : DEFAULT_REFRESH_PERIOD;
138             refreshJob = scheduler.scheduleWithFixedDelay(runnable, 1, delay, TimeUnit.SECONDS);
139         }
140     }
141
142     private void checkBridgeOnline(FroniusBridgeConfiguration config) throws FroniusCommunicationException {
143         FroniusHttpUtil.executeUrl("http://" + config.hostname, 5000);
144     }
145 }