2 * Copyright (c) 2010-2021 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.io.IOException;
16 import java.util.HashSet;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration;
22 import org.openhab.core.io.net.http.HttpUtil;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.thing.binding.BaseBridgeHandler;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Bridge for Fronius devices.
35 * @author Gerrit Beine - Initial contribution
36 * @author Thomas Rokohl - Refactoring to merge the concepts.
37 * Check if host is reachable.
39 public class FroniusBridgeHandler extends BaseBridgeHandler {
41 private final Logger logger = LoggerFactory.getLogger(FroniusBridgeHandler.class);
42 private static final int DEFAULT_REFRESH_PERIOD = 10;
43 private final Set<FroniusBaseThingHandler> services = new HashSet<>();
44 private ScheduledFuture<?> refreshJob;
46 public FroniusBridgeHandler(Bridge bridge) {
51 public void handleCommand(ChannelUID channelUID, Command command) {
54 public void registerService(final FroniusBaseThingHandler service) {
55 this.services.add(service);
59 public void initialize() {
60 final FroniusBridgeConfiguration config = getConfigAs(FroniusBridgeConfiguration.class);
62 boolean validConfig = true;
63 String errorMsg = null;
65 String hostname = config.hostname;
66 if (hostname == null || hostname.isBlank()) {
67 errorMsg = "Parameter 'hostname' is mandatory and must be configured";
71 if (config.refreshInterval != null && config.refreshInterval <= 0) {
72 errorMsg = "Parameter 'refresh' must be at least 1 second";
77 startAutomaticRefresh(config);
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMsg);
84 public void dispose() {
85 if (refreshJob != null) {
86 refreshJob.cancel(true);
92 * Start the job refreshing the data
94 private void startAutomaticRefresh(FroniusBridgeConfiguration config) {
95 if (refreshJob == null || refreshJob.isCancelled()) {
96 Runnable runnable = () -> {
97 boolean online = false;
99 if (HttpUtil.executeUrl("GET", "http://" + config.hostname, 5000) != null) {
102 } catch (IOException e) {
103 logger.debug("Connection Error: {}", e.getMessage());
107 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
108 "hostname or ip is not reachable");
110 updateStatus(ThingStatus.ONLINE);
111 for (FroniusBaseThingHandler service : services) {
112 service.refresh(config);
117 int delay = (config.refreshInterval != null) ? config.refreshInterval.intValue() : DEFAULT_REFRESH_PERIOD;
118 refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, delay, TimeUnit.SECONDS);