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 sizeDataItem) {
89 logger.debug("Received TSP or FHB size message {} ({})", message.getID(), dataItem.getSubject());
91 verifyTspFhbChannels(sizeDataItem.getValueId(), message.getUInt(dataItem.getByteType()));
93 String channelId = dataItem.getChannelId(message);
95 if (thing.getChannel(channelId) == null || !dataItem.hasValidCodeType(message)) {
99 State state = dataItem.createState(message);
101 logger.debug("Received update for channel {}: {}", channelId, state);
102 updateState(channelId, state);
107 private void verifyTspFhbChannels(int id, int size) {
108 // Dynamically create TSP or FHB value channels based on TSP or FHB size message
109 ThingHandlerCallback callback = getCallback();
111 if (callback == null) {
112 logger.debug("Unable to get thing handler callback");
116 DataItem[] dataItems = DataItemGroup.DATAITEMGROUPS.get(id);
118 if (dataItems == null) {
119 logger.debug("Unable to find dataItem for id {}", id);
123 if (dataItems.length != 1) {
124 logger.debug("Found zero or multiple dataItems for id {}", id);
128 TspFhbValueDataItem dataItem = (TspFhbValueDataItem) dataItems[0];
130 logger.debug("Checking number of TSP or FHB channels for DATA-ID {}: {}", id, size);
132 // A generic Number:Dimensionless channel type for TSP and FHB values
133 ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, CHANNEL_TSPFHB);
135 List<Channel> channels = new ArrayList<>(getThing().getChannels());
137 boolean changed = false;
138 for (int i = 0; i < size; i++) {
139 String channelId = dataItem.getChannelId(i);
140 ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
142 if (!channels.stream().map(Channel::getUID).anyMatch(channelUID::equals)) {
143 String label = dataItem.getLabel(i);
145 logger.debug("Adding channel {}", channelId);
147 channels.add(callback.createChannelBuilder(channelUID, channelTypeUID).withKind(ChannelKind.STATE)
148 .withLabel(label).build());
151 logger.debug("Channel {} already exists", channelId);
156 logger.debug("Updating Thing with new channels");
157 updateThing(editThing().withChannels(channels).build());