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.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.apache.commons.lang.StringUtils;
22 import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration;
23 import org.openhab.core.io.net.http.HttpUtil;
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 * Bridge for Fronius devices.
36 * @author Gerrit Beine - Initial contribution
37 * @author Thomas Rokohl - Refactoring to merge the concepts.
38 * Check if host is reachable.
40 public class FroniusBridgeHandler extends BaseBridgeHandler {
42 private final Logger logger = LoggerFactory.getLogger(FroniusBridgeHandler.class);
43 private static final int DEFAULT_REFRESH_PERIOD = 10;
44 private final Set<FroniusBaseThingHandler> services = new HashSet<>();
45 private ScheduledFuture<?> refreshJob;
47 public FroniusBridgeHandler(Bridge bridge) {
52 public void handleCommand(ChannelUID channelUID, Command command) {
55 public void registerService(final FroniusBaseThingHandler service) {
56 this.services.add(service);
60 public void initialize() {
61 final FroniusBridgeConfiguration config = getConfigAs(FroniusBridgeConfiguration.class);
63 boolean validConfig = true;
64 String errorMsg = null;
65 if (StringUtils.trimToNull(config.hostname) == null) {
66 errorMsg = "Parameter 'hostname' is mandatory and must be configured";
69 if (config.refreshInterval != null && config.refreshInterval <= 0) {
70 errorMsg = "Parameter 'refresh' must be at least 1 second";
75 startAutomaticRefresh(config);
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMsg);
82 public void dispose() {
83 if (refreshJob != null) {
84 refreshJob.cancel(true);
90 * Start the job refreshing the data
92 private void startAutomaticRefresh(FroniusBridgeConfiguration config) {
93 if (refreshJob == null || refreshJob.isCancelled()) {
94 Runnable runnable = () -> {
95 boolean online = false;
97 if (HttpUtil.executeUrl("GET", "http://" + config.hostname, 5000) != null) {
100 } catch (IOException e) {
101 logger.debug("Connection Error: {}", e.getMessage());
105 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
106 "hostname or ip is not reachable");
108 updateStatus(ThingStatus.ONLINE);
109 for (FroniusBaseThingHandler service : services) {
110 service.refresh(config);
115 int delay = (config.refreshInterval != null) ? config.refreshInterval.intValue() : DEFAULT_REFRESH_PERIOD;
116 refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, delay, TimeUnit.SECONDS);