]> git.basschouten.com Git - openhab-addons.git/blob
fba16e3432dab1a5ae1f2e25c634b8a86863d074
[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.openthermgateway.handler;
14
15 import static org.openhab.binding.openthermgateway.internal.OpenThermGatewayBindingConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
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;
40
41 /**
42  * The {@link BaseDeviceHandler} is a base class for actual Things.
43  *
44  * @author Arjen Korevaar - Initial contribution
45  */
46 @NonNullByDefault
47 public abstract class BaseDeviceHandler extends BaseThingHandler {
48
49     private final Logger logger = LoggerFactory.getLogger(BaseDeviceHandler.class);
50
51     public BaseDeviceHandler(Thing thing) {
52         super(thing);
53     }
54
55     @Override
56     public void initialize() {
57         if (getBridge() == null) {
58             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
59         } else {
60             // note: the framework handles bridge configuration resp. offline errors
61             updateStatus(ThingStatus.ONLINE);
62         }
63     }
64
65     @Override
66     public void handleCommand(ChannelUID channelUID, Command command) {
67         Bridge bridge = getBridge();
68
69         if (bridge != null) {
70             OpenThermGatewayHandler handler = (OpenThermGatewayHandler) bridge.getHandler();
71             if (handler != null) {
72                 handler.handleCommand(channelUID, command);
73             }
74         } else {
75             logger.debug("Bridge is missing");
76         }
77     }
78
79     public void receiveMessage(Message message) {
80         DataItem[] dataItems = DataItemGroup.DATAITEMGROUPS.get(message.getID());
81
82         if (dataItems == null) {
83             logger.debug("No DataItem found for message id {}", message.getID());
84             return;
85         }
86
87         for (DataItem dataItem : dataItems) {
88             if (dataItem instanceof TspFhbSizeDataItem sizeDataItem) {
89                 logger.debug("Received TSP or FHB size message {} ({})", message.getID(), dataItem.getSubject());
90
91                 verifyTspFhbChannels(sizeDataItem.getValueId(), message.getUInt(dataItem.getByteType()));
92             } else {
93                 String channelId = dataItem.getChannelId(message);
94
95                 if (thing.getChannel(channelId) == null || !dataItem.hasValidCodeType(message)) {
96                     continue;
97                 }
98
99                 State state = dataItem.createState(message);
100
101                 logger.debug("Received update for channel {}: {}", channelId, state);
102                 updateState(channelId, state);
103             }
104         }
105     }
106
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();
110
111         if (callback == null) {
112             logger.debug("Unable to get thing handler callback");
113             return;
114         }
115
116         DataItem[] dataItems = DataItemGroup.DATAITEMGROUPS.get(id);
117
118         if (dataItems == null) {
119             logger.debug("Unable to find dataItem for id {}", id);
120             return;
121         }
122
123         if (dataItems.length != 1) {
124             logger.debug("Found zero or multiple dataItems for id {}", id);
125             return;
126         }
127
128         TspFhbValueDataItem dataItem = (TspFhbValueDataItem) dataItems[0];
129
130         logger.debug("Checking number of TSP or FHB channels for DATA-ID {}: {}", id, size);
131
132         // A generic Number:Dimensionless channel type for TSP and FHB values
133         ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, CHANNEL_TSPFHB);
134
135         List<Channel> channels = new ArrayList<>(getThing().getChannels());
136
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);
141
142             if (!channels.stream().map(Channel::getUID).anyMatch(channelUID::equals)) {
143                 String label = dataItem.getLabel(i);
144
145                 logger.debug("Adding channel {}", channelId);
146
147                 channels.add(callback.createChannelBuilder(channelUID, channelTypeUID).withKind(ChannelKind.STATE)
148                         .withLabel(label).build());
149                 changed = true;
150             } else {
151                 logger.debug("Channel {} already exists", channelId);
152             }
153         }
154
155         if (changed) {
156             logger.debug("Updating Thing with new channels");
157             updateThing(editThing().withChannels(channels).build());
158         }
159     }
160 }