]> git.basschouten.com Git - openhab-addons.git/blob
0528ec5acc66e42a02dba77e3880b35d4ce2517a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.IOException;
16 import java.net.InetAddress;
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration;
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  * Bridge for Fronius devices.
35  *
36  * @author Gerrit Beine - Initial contribution
37  * @author Thomas Rokohl - Refactoring to merge the concepts.
38  *         Check if host is reachable.
39  */
40 public class FroniusBridgeHandler extends BaseBridgeHandler {
41
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;
46
47     public FroniusBridgeHandler(Bridge bridge) {
48         super(bridge);
49     }
50
51     @Override
52     public void handleCommand(ChannelUID channelUID, Command command) {
53     }
54
55     public void registerService(final FroniusBaseThingHandler service) {
56         this.services.add(service);
57     }
58
59     @Override
60     public void initialize() {
61         final FroniusBridgeConfiguration config = getConfigAs(FroniusBridgeConfiguration.class);
62
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";
67             validConfig = false;
68         }
69         if (config.refreshInterval != null && config.refreshInterval <= 0) {
70             errorMsg = "Parameter 'refresh' must be at least 1 second";
71             validConfig = false;
72         }
73
74         if (validConfig) {
75             startAutomaticRefresh(config);
76         } else {
77             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMsg);
78         }
79     }
80
81     @Override
82     public void dispose() {
83         if (refreshJob != null) {
84             refreshJob.cancel(true);
85         }
86         services.clear();
87     }
88
89     /**
90      * Start the job refreshing the data
91      */
92     private void startAutomaticRefresh(FroniusBridgeConfiguration config) {
93         if (refreshJob == null || refreshJob.isCancelled()) {
94             Runnable runnable = () -> {
95                 boolean online = false;
96                 try {
97                     InetAddress inet;
98                     inet = InetAddress.getByName(config.hostname);
99                     if (inet.isReachable(5000)) {
100                         online = true;
101                     }
102                 } catch (IOException e) {
103                     logger.debug("Connection Error: {}", e.getMessage());
104                     return;
105                 }
106
107                 if (!online) {
108                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
109                             "hostname or ip is not reachable");
110                 } else {
111                     updateStatus(ThingStatus.ONLINE);
112                     for (FroniusBaseThingHandler service : services) {
113                         service.refresh(config);
114                     }
115                 }
116             };
117
118             int delay = (config.refreshInterval != null) ? config.refreshInterval.intValue() : DEFAULT_REFRESH_PERIOD;
119             refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, delay, TimeUnit.SECONDS);
120         }
121     }
122 }