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.bsblan.internal.handler;
15 import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.*;
17 import java.util.HashSet;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.bsblan.internal.api.BsbLanApiCaller;
26 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterQueryResponseDTO;
27 import org.openhab.binding.bsblan.internal.configuration.BsbLanBridgeConfiguration;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.binding.BaseBridgeHandler;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Bridge for BSB-LAN devices.
40 * @author Peter Schraffl - Initial contribution
43 public class BsbLanBridgeHandler extends BaseBridgeHandler {
45 private final Logger logger = LoggerFactory.getLogger(BsbLanBridgeHandler.class);
46 private final Set<BsbLanBaseThingHandler> things = new HashSet<>();
47 private BsbLanBridgeConfiguration bridgeConfig = new BsbLanBridgeConfiguration();
48 private @Nullable ScheduledFuture<?> refreshJob;
49 private @Nullable ScheduledFuture<?> debouncedInit;
50 private @Nullable BsbLanApiParameterQueryResponseDTO cachedParameterQueryResponse;
52 public BsbLanBridgeHandler(Bridge bridge) {
57 public void handleCommand(ChannelUID channelUID, Command command) {
60 public void registerThing(final BsbLanBaseThingHandler parameter) {
61 this.things.add(parameter);
63 // To avoid having to wait up to refreshInterval seconds until values are updated
64 // for the added thing, we trigger a debounced refresh to shorten the delay.
65 // Alternatively the thing itself could make an additional REST call
66 // on initialization but this would flood the device when lots of parameters are setup.
68 // use a local variable to avoid the build warning "Potential null pointer access"
69 ScheduledFuture<?> localDebouncedInit = debouncedInit;
70 if (localDebouncedInit == null || localDebouncedInit.isCancelled() || localDebouncedInit.isDone()) {
71 debouncedInit = scheduler.schedule(this::doRefresh, 2, TimeUnit.SECONDS);
76 public void initialize() {
77 bridgeConfig = getConfigAs(BsbLanBridgeConfiguration.class);
79 // validate 'host' configuration
80 String host = bridgeConfig.host;
81 if (host == null || host.isBlank()) {
82 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
83 "Parameter 'host' is mandatory and must be configured");
87 // validate 'refreshInterval' configuration
88 if (bridgeConfig.refreshInterval != null && bridgeConfig.refreshInterval < MIN_REFRESH_INTERVAL) {
89 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
90 String.format("Parameter 'refreshInterval' must be at least %d seconds", MIN_REFRESH_INTERVAL));
94 if (bridgeConfig.port == null) {
95 bridgeConfig.port = DEFAULT_API_PORT;
98 // all checks succeeded, start refreshing
99 startAutomaticRefresh(bridgeConfig);
103 public void dispose() {
104 // use a local variable to avoid the build warning "Potential null pointer access"
105 ScheduledFuture<?> localRefreshJob = refreshJob;
106 if (localRefreshJob != null) {
107 localRefreshJob.cancel(true);
109 // use a local variable to avoid the build warning "Potential null pointer access"
110 ScheduledFuture<?> localDebouncedInit = debouncedInit;
111 if (localDebouncedInit != null) {
112 localDebouncedInit.cancel(true);
117 public @Nullable BsbLanApiParameterQueryResponseDTO getCachedParameterQueryResponse() {
118 return cachedParameterQueryResponse;
121 public BsbLanBridgeConfiguration getBridgeConfiguration() {
125 private void doRefresh() {
126 logger.trace("Refreshing parameter values");
128 BsbLanApiCaller apiCaller = new BsbLanApiCaller(bridgeConfig);
130 // refresh all parameters
131 Set<Integer> parameterIds = things.stream() //
132 .filter(thing -> thing instanceof BsbLanParameterHandler) //
133 .map(thing -> (BsbLanParameterHandler) thing) //
134 .map(thing -> thing.getParameterId()) //
135 .collect(Collectors.toSet());
137 cachedParameterQueryResponse = apiCaller.queryParameters(parameterIds);
139 // InetAddress.isReachable(...) check returned false on RPi although the device is reachable (worked on
141 // Therefore we check status depending on the response.
142 if (cachedParameterQueryResponse == null) {
143 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
144 "Did not receive a response from BSB-LAN device. Check your configuration and if device is online.");
145 // continue processing, so things can go to OFFLINE too
147 // response received, tread device as reachable, refresh state now
148 updateStatus(ThingStatus.ONLINE);
151 for (BsbLanBaseThingHandler parameter : things) {
152 parameter.refresh(bridgeConfig);
157 * Start the job refreshing the data
159 private void startAutomaticRefresh(BsbLanBridgeConfiguration config) {
160 // use a local variable to avoid the build warning "Potential null pointer access"
161 ScheduledFuture<?> localRefreshJob = refreshJob;
162 if (localRefreshJob == null || localRefreshJob.isCancelled()) {
163 int interval = (config.refreshInterval != null) ? config.refreshInterval.intValue()
164 : DEFAULT_REFRESH_INTERVAL;
165 refreshJob = scheduler.scheduleWithFixedDelay(this::doRefresh, 0, interval, TimeUnit.SECONDS);