]> git.basschouten.com Git - openhab-addons.git/blob
f4dcbfd0fee94cc3502f0f346cc063a2421de01f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.bsblan.internal.handler;
14
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;
30
31 /**
32  * Basic Handler class for all BSB-LAN services.
33  *
34  * @author Peter Schraffl - Initial contribution
35  */
36 @NonNullByDefault
37 public abstract class BsbLanBaseThingHandler extends BaseThingHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(BsbLanBaseThingHandler.class);
40     private @Nullable BsbLanBridgeHandler bridgeHandler;
41
42     public BsbLanBaseThingHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public void handleCommand(ChannelUID channelUID, Command command) {
48         if (command instanceof RefreshType) {
49             updateChannel(channelUID.getId());
50         } else {
51             setChannel(channelUID.getId(), command);
52         }
53     }
54
55     @Override
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);
61             return;
62         }
63         logger.trace("Initializing '{}' thing", getDescription());
64         bridgeHandler.registerThing(this);
65     }
66
67     protected synchronized @Nullable BsbLanBridgeHandler getBridgeHandler() {
68         if (this.bridgeHandler == null) {
69             Bridge bridge = getBridge();
70             if (bridge == null) {
71                 return null;
72             }
73             ThingHandler handler = bridge.getHandler();
74             if (handler instanceof BsbLanBridgeHandler lanBridgeHandler) {
75                 this.bridgeHandler = lanBridgeHandler;
76             }
77         }
78         return this.bridgeHandler;
79     }
80
81     /**
82      * Update all channels
83      */
84     protected void updateChannels() {
85         for (Channel channel : getThing().getChannels()) {
86             updateChannel(channel.getUID().getId());
87         }
88     }
89
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) {
94             return null;
95         }
96         return new BsbLanApiCaller(localBridgeHandler.getBridgeConfiguration());
97     }
98
99     /**
100      * Update the channel from the last data
101      *
102      * @param channelId the id identifying the channel to be updated
103      */
104     protected abstract void updateChannel(String channelId);
105
106     /**
107      * Set new value for channel
108      *
109      * @param channelId the id identifying the channel
110      */
111     protected abstract void setChannel(String channelId, Command command);
112
113     /**
114      * return an internal description for logging
115      *
116      * @return the description of the thing
117      */
118     protected abstract String getDescription();
119
120     /**
121      * do whatever a thing must do to update the values
122      * this function is called from the bridge in a given interval
123      *
124      * @param bridgeConfiguration the connected bridge configuration
125      */
126     public abstract void refresh(BsbLanBridgeConfiguration bridgeConfiguration);
127 }