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.openthermgateway.handler;
15 import static org.openhab.binding.openthermgateway.internal.OpenThermGatewayBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.openthermgateway.internal.DataItem;
22 import org.openhab.binding.openthermgateway.internal.DataItemGroup;
23 import org.openhab.binding.openthermgateway.internal.Message;
24 import org.openhab.binding.openthermgateway.internal.TspFhbSizeDataItem;
25 import org.openhab.binding.openthermgateway.internal.TspFhbValueDataItem;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.binding.BaseThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerCallback;
34 import org.openhab.core.thing.type.ChannelKind;
35 import org.openhab.core.thing.type.ChannelTypeUID;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.State;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link BaseDeviceHandler} is a base class for actual Things.
44 * @author Arjen Korevaar - Initial contribution
47 public abstract class BaseDeviceHandler extends BaseThingHandler {
49 private final Logger logger = LoggerFactory.getLogger(BaseDeviceHandler.class);
51 public BaseDeviceHandler(Thing thing) {
56 public void initialize() {
57 if (getBridge() == null) {
58 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
60 // note: the framework handles bridge configuration resp. offline errors
61 updateStatus(ThingStatus.ONLINE);
66 public void handleCommand(ChannelUID channelUID, Command command) {
67 Bridge bridge = getBridge();
70 OpenThermGatewayHandler handler = (OpenThermGatewayHandler) bridge.getHandler();
71 if (handler != null) {
72 handler.handleCommand(channelUID, command);
75 logger.debug("Bridge is missing");
79 public void receiveMessage(Message message) {
80 DataItem[] dataItems = DataItemGroup.DATAITEMGROUPS.get(message.getID());
82 if (dataItems == null) {
83 logger.debug("No DataItem found for message id {}", message.getID());
87 for (DataItem dataItem : dataItems) {
88 if (dataItem instanceof TspFhbSizeDataItem) {
89 logger.debug("Received TSP or FHB size message {} ({})", message.getID(), dataItem.getSubject());
91 verifyTspFhbChannels(((TspFhbSizeDataItem) dataItem).getValueId(),
92 message.getUInt(dataItem.getByteType()));
94 String channelId = dataItem.getChannelId(message);
96 if (thing.getChannel(channelId) == null || !dataItem.hasValidCodeType(message)) {
100 State state = dataItem.createState(message);
102 logger.debug("Received update for channel {}: {}", channelId, state);
103 updateState(channelId, state);
108 private void verifyTspFhbChannels(int id, int size) {
109 // Dynamically create TSP or FHB value channels based on TSP or FHB size message
110 ThingHandlerCallback callback = getCallback();
112 if (callback == null) {
113 logger.debug("Unable to get thing handler callback");
117 DataItem[] dataItems = DataItemGroup.DATAITEMGROUPS.get(id);
119 if (dataItems == null) {
120 logger.debug("Unable to find dataItem for id {}", id);
124 if (dataItems.length != 1) {
125 logger.debug("Found zero or multiple dataItems for id {}", id);
129 TspFhbValueDataItem dataItem = (TspFhbValueDataItem) dataItems[0];
131 logger.debug("Checking number of TSP or FHB channels for DATA-ID {}: {}", id, size);
133 // A generic Number:Dimensionless channel type for TSP and FHB values
134 ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, CHANNEL_TSPFHB);
136 List<Channel> channels = new ArrayList<>(getThing().getChannels());
138 boolean changed = false;
139 for (int i = 0; i < size; i++) {
140 String channelId = dataItem.getChannelId(i);
141 ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
143 if (!channels.stream().map(Channel::getUID).anyMatch(channelUID::equals)) {
144 String label = dataItem.getLabel(i);
146 logger.debug("Adding channel {}", channelId);
148 channels.add(callback.createChannelBuilder(channelUID, channelTypeUID).withKind(ChannelKind.STATE)
149 .withLabel(label).build());
152 logger.debug("Channel {} already exists", channelId);
157 logger.debug("Updating Thing with new channels");
158 updateThing(editThing().withChannels(channels).build());