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.fronius.internal.handler;
15 import java.util.HashSet;
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.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;
37 * Bridge for Fronius devices.
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
46 public class FroniusBridgeHandler extends BaseBridgeHandler {
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;
53 public FroniusBridgeHandler(Bridge bridge) {
58 public void handleCommand(ChannelUID channelUID, Command command) {
62 public void initialize() {
63 final FroniusBridgeConfiguration config = getConfigAs(FroniusBridgeConfiguration.class);
65 boolean validConfig = true;
66 String errorMsg = null;
68 String hostname = config.hostname;
69 if (hostname == null || hostname.isBlank()) {
70 errorMsg = "Parameter 'hostname' is mandatory and must be configured";
74 if (config.refreshInterval != null && config.refreshInterval <= 0) {
75 errorMsg = "Parameter 'refresh' must be at least 1 second";
80 startAutomaticRefresh();
82 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMsg);
87 public void dispose() {
88 if (refreshJob != null) {
89 refreshJob.cancel(true);
95 public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
96 if (childHandler instanceof FroniusBaseThingHandler) {
97 this.services.add((FroniusBaseThingHandler) childHandler);
98 restartAutomaticRefresh();
100 logger.debug("Child handler {} not added because it is not an instance of FroniusBaseThingHandler",
101 childThing.getUID().getId());
106 public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
107 this.services.remove((FroniusBaseThingHandler) childHandler);
110 private void restartAutomaticRefresh() {
111 if (refreshJob != null) { // refreshJob should be null if the config isn't valid
112 refreshJob.cancel(false);
113 startAutomaticRefresh();
118 * Start the job refreshing the data
120 private void startAutomaticRefresh() {
121 if (refreshJob == null || refreshJob.isCancelled()) {
122 final FroniusBridgeConfiguration config = getConfigAs(FroniusBridgeConfiguration.class);
123 Runnable runnable = () -> {
125 checkBridgeOnline(config);
126 if (getThing().getStatus() != ThingStatus.ONLINE) {
127 updateStatus(ThingStatus.ONLINE);
129 for (FroniusBaseThingHandler service : services) {
130 service.refresh(config);
132 } catch (FroniusCommunicationException e) {
133 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
137 int delay = (config.refreshInterval != null) ? config.refreshInterval.intValue() : DEFAULT_REFRESH_PERIOD;
138 refreshJob = scheduler.scheduleWithFixedDelay(runnable, 1, delay, TimeUnit.SECONDS);
142 private void checkBridgeOnline(FroniusBridgeConfiguration config) throws FroniusCommunicationException {
143 FroniusHttpUtil.executeUrl("http://" + config.hostname, 5000);