2 * Copyright (c) 2010-2023 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.satel.internal.handler;
15 import java.util.function.Consumer;
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;
31 * The {@link SatelThingHandler} is base thing handler class for all non-bridge things.
33 * @author Krzysztof Goworek - Initial contribution
36 public abstract class SatelThingHandler extends BaseThingHandler implements SatelEventListener {
38 private @Nullable SatelThingConfig thingConfig;
39 private @Nullable SatelBridgeHandler bridgeHandler;
41 public SatelThingHandler(Thing thing) {
46 public void dispose() {
47 final SatelBridgeHandler bridgeHandler = this.bridgeHandler;
48 if (bridgeHandler != null) {
49 bridgeHandler.removeEventListener(this);
54 public void initialize() {
55 thingConfig = getConfig().as(SatelThingConfig.class);
57 final Bridge bridge = getBridge();
59 final ThingHandler handler = bridge.getHandler();
60 if (handler instanceof SatelBridgeHandler satelBridgeHandler) {
61 satelBridgeHandler.addEventListener(this);
62 this.bridgeHandler = satelBridgeHandler;
64 if (bridge.getStatus() == ThingStatus.ONLINE) {
65 updateStatus(ThingStatus.ONLINE);
70 protected SatelThingConfig getThingConfig() {
71 final SatelThingConfig thingConfig = this.thingConfig;
72 if (thingConfig != null) {
75 throw new IllegalStateException("Thing handler is not initialized yet for thing " + getThing().getUID());
78 protected void withBridgeHandlerPresent(Consumer<SatelBridgeHandler> action) {
79 final SatelBridgeHandler bridgeHandler = this.bridgeHandler;
80 if (bridgeHandler != null) {
81 action.accept(bridgeHandler);
85 protected SatelBridgeHandler getBridgeHandler() {
86 final SatelBridgeHandler bridgeHandler = this.bridgeHandler;
87 if (bridgeHandler != null) {
90 throw new IllegalStateException("Bridge handler is not set for thing " + getThing().getUID());
94 * Updates switch channel with given state.
96 * @param channelID channel ID
97 * @param switchOn if <code>true</code> the channel is updated with ON state, with OFF state otherwise
99 protected void updateSwitch(String channelID, boolean switchOn) {
100 ChannelUID channelUID = new ChannelUID(this.getThing().getUID(), channelID);
101 updateSwitch(channelUID, switchOn);
105 * Updates switch channel with given state.
107 * @param channelUID channel UID
108 * @param switchOn if <code>true</code> the channel is updated with ON state, with OFF state otherwise
110 protected void updateSwitch(ChannelUID channelUID, boolean switchOn) {
111 State state = switchOn ? OnOffType.ON : OnOffType.OFF;
112 updateState(channelUID, state);
116 * Creates bitset of given size with particular bits set to 1.
118 * @param size bitset size in bytes
119 * @param ids bits to set, first bit is 1
120 * @return bitset as array of bytes
122 protected byte[] getObjectBitset(int size, int... ids) {
123 byte[] bitset = new byte[size];
126 bitset[bitNbr / 8] |= (byte) (1 << (bitNbr % 8));