2 * Copyright (c) 2010-2022 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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.bsblan.internal.api.BsbLanApiCaller;
18 import org.openhab.binding.bsblan.internal.configuration.BsbLanBridgeConfiguration;
19 import org.openhab.core.thing.Bridge;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.binding.BaseThingHandler;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Basic Handler class for all BSB-LAN services.
34 * @author Peter Schraffl - Initial contribution
37 public abstract class BsbLanBaseThingHandler extends BaseThingHandler {
39 private final Logger logger = LoggerFactory.getLogger(BsbLanBaseThingHandler.class);
40 private @Nullable BsbLanBridgeHandler bridgeHandler;
42 public BsbLanBaseThingHandler(Thing thing) {
47 public void handleCommand(ChannelUID channelUID, Command command) {
48 if (command instanceof RefreshType) {
49 updateChannel(channelUID.getId());
51 setChannel(channelUID.getId(), command);
56 public void initialize() {
57 BsbLanBridgeHandler bridgeHandler = getBridgeHandler();
58 if (bridgeHandler == null) {
59 logger.debug("Initializing '{}': thing is only supported within a bridge", getDescription());
60 updateStatus(ThingStatus.OFFLINE);
63 logger.trace("Initializing '{}' thing", getDescription());
64 bridgeHandler.registerThing(this);
67 protected synchronized @Nullable BsbLanBridgeHandler getBridgeHandler() {
68 if (this.bridgeHandler == null) {
69 Bridge bridge = getBridge();
73 ThingHandler handler = bridge.getHandler();
74 if (handler instanceof BsbLanBridgeHandler) {
75 this.bridgeHandler = (BsbLanBridgeHandler) handler;
78 return this.bridgeHandler;
84 protected void updateChannels() {
85 for (Channel channel : getThing().getChannels()) {
86 updateChannel(channel.getUID().getId());
90 protected @Nullable BsbLanApiCaller getApiCaller() {
91 // use a local variable to avoid the build warning "Potential null pointer access"
92 BsbLanBridgeHandler localBridgeHandler = bridgeHandler;
93 if (localBridgeHandler == null) {
96 return new BsbLanApiCaller(localBridgeHandler.getBridgeConfiguration());
100 * Update the channel from the last data
102 * @param channelId the id identifying the channel to be updated
104 protected abstract void updateChannel(String channelId);
107 * Set new value for channel
109 * @param channelId the id identifying the channel
111 protected abstract void setChannel(String channelId, Command command);
114 * return an internal description for logging
116 * @return the description of the thing
118 protected abstract String getDescription();
121 * do whatever a thing must do to update the values
122 * this function is called from the bridge in a given interval
124 * @param bridgeConfiguration the connected bridge configuration
126 public abstract void refresh(BsbLanBridgeConfiguration bridgeConfiguration);