]> git.basschouten.com Git - openhab-addons.git/blob
85a9b81d99b7182a088c9cc997b7948b3cb3d704
[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.satel.internal.handler;
14
15 import java.util.function.Consumer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.satel.internal.config.SatelThingConfig;
20 import org.openhab.binding.satel.internal.event.SatelEventListener;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.types.State;
29
30 /**
31  * The {@link SatelThingHandler} is base thing handler class for all non-bridge things.
32  *
33  * @author Krzysztof Goworek - Initial contribution
34  */
35 @NonNullByDefault
36 public abstract class SatelThingHandler extends BaseThingHandler implements SatelEventListener {
37
38     private @Nullable SatelThingConfig thingConfig;
39     private @Nullable SatelBridgeHandler bridgeHandler;
40
41     public SatelThingHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     public void dispose() {
47         final SatelBridgeHandler bridgeHandler = this.bridgeHandler;
48         if (bridgeHandler != null) {
49             bridgeHandler.removeEventListener(this);
50         }
51     }
52
53     @Override
54     public void initialize() {
55         thingConfig = getConfig().as(SatelThingConfig.class);
56
57         final Bridge bridge = getBridge();
58         if (bridge != null) {
59             final ThingHandler handler = bridge.getHandler();
60             if (handler instanceof SatelBridgeHandler satelBridgeHandler) {
61                 satelBridgeHandler.addEventListener(this);
62                 this.bridgeHandler = satelBridgeHandler;
63             }
64             if (bridge.getStatus() == ThingStatus.ONLINE) {
65                 updateStatus(ThingStatus.ONLINE);
66             }
67         }
68     }
69
70     protected SatelThingConfig getThingConfig() {
71         final SatelThingConfig thingConfig = this.thingConfig;
72         if (thingConfig != null) {
73             return thingConfig;
74         }
75         throw new IllegalStateException("Thing handler is not initialized yet for thing " + getThing().getUID());
76     }
77
78     protected void withBridgeHandlerPresent(Consumer<SatelBridgeHandler> action) {
79         final SatelBridgeHandler bridgeHandler = this.bridgeHandler;
80         if (bridgeHandler != null) {
81             action.accept(bridgeHandler);
82         }
83     }
84
85     protected SatelBridgeHandler getBridgeHandler() {
86         final SatelBridgeHandler bridgeHandler = this.bridgeHandler;
87         if (bridgeHandler != null) {
88             return bridgeHandler;
89         }
90         throw new IllegalStateException("Bridge handler is not set for thing " + getThing().getUID());
91     }
92
93     /**
94      * Updates switch channel with given state.
95      *
96      * @param channelID channel ID
97      * @param switchOn if <code>true</code> the channel is updated with ON state, with OFF state otherwise
98      */
99     protected void updateSwitch(String channelID, boolean switchOn) {
100         ChannelUID channelUID = new ChannelUID(this.getThing().getUID(), channelID);
101         updateSwitch(channelUID, switchOn);
102     }
103
104     /**
105      * Updates switch channel with given state.
106      *
107      * @param channelUID channel UID
108      * @param switchOn if <code>true</code> the channel is updated with ON state, with OFF state otherwise
109      */
110     protected void updateSwitch(ChannelUID channelUID, boolean switchOn) {
111         State state = switchOn ? OnOffType.ON : OnOffType.OFF;
112         updateState(channelUID, state);
113     }
114
115     /**
116      * Creates bitset of given size with particular bits set to 1.
117      *
118      * @param size bitset size in bytes
119      * @param ids bits to set, first bit is 1
120      * @return bitset as array of bytes
121      */
122     protected byte[] getObjectBitset(int size, int... ids) {
123         byte[] bitset = new byte[size];
124         for (int id : ids) {
125             int bitNbr = id - 1;
126             bitset[bitNbr / 8] |= (byte) (1 << (bitNbr % 8));
127         }
128         return bitset;
129     }
130 }