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