2 * Copyright (c) 2010-2024 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.serial.internal.handler;
15 import java.util.HashMap;
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.serial.internal.channel.ChannelConfig;
23 import org.openhab.binding.serial.internal.channel.DeviceChannel;
24 import org.openhab.binding.serial.internal.channel.DeviceChannelFactory;
25 import org.openhab.binding.serial.internal.transform.ValueTransformationProvider;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Channel;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.ThingStatusInfo;
34 import org.openhab.core.thing.binding.BaseThingHandler;
35 import org.openhab.core.thing.type.ChannelTypeUID;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
40 * The {@link SerialDeviceHandler} is responsible for handling commands, which are
41 * sent to one of the channels.
43 * @author Mike Major - Initial contribution
46 public class SerialDeviceHandler extends BaseThingHandler {
48 private final ValueTransformationProvider valueTransformationProvider;
50 private @Nullable Pattern devicePattern;
52 private @Nullable String lastValue;
54 private final Map<ChannelUID, DeviceChannel> channels = new HashMap<>();
56 public SerialDeviceHandler(final Thing thing, final ValueTransformationProvider valueTransformationProvider) {
58 this.valueTransformationProvider = valueTransformationProvider;
62 public void handleCommand(final ChannelUID channelUID, final Command command) {
63 if (command instanceof RefreshType) {
64 final String lastValue = this.lastValue;
66 if (lastValue != null) {
67 final DeviceChannel channel = channels.get(channelUID);
68 if (channel != null) {
69 refresh(channelUID, channel, lastValue);
73 final DeviceChannel channel = channels.get(channelUID);
74 if (channel != null) {
75 final Bridge bridge = getBridge();
77 final SerialBridgeHandler handler = (SerialBridgeHandler) bridge.getHandler();
78 if (handler != null) {
79 channel.mapCommand(command).ifPresent(value -> handler.writeString(value));
87 public void initialize() {
88 final SerialDeviceConfiguration config = getConfigAs(SerialDeviceConfiguration.class);
91 devicePattern = Pattern.compile(config.patternMatch);
92 } catch (final PatternSyntaxException e) {
93 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
94 "Invalid device pattern: " + e.getMessage());
98 for (final Channel c : getThing().getChannels()) {
99 final ChannelTypeUID type = c.getChannelTypeUID();
101 final ChannelConfig channelConfig = c.getConfiguration().as(ChannelConfig.class);
103 final DeviceChannel deviceChannel = DeviceChannelFactory
104 .createDeviceChannel(valueTransformationProvider, channelConfig, type.getId());
105 if (deviceChannel != null) {
106 channels.put(c.getUID(), deviceChannel);
108 } catch (final IllegalArgumentException e) {
109 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
110 "Configuration error for channel " + c.getUID().getId() + ": " + e.getMessage());
116 if (getBridgeStatus().getStatus() == ThingStatus.ONLINE) {
117 updateStatus(ThingStatus.ONLINE);
119 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
124 public void dispose() {
131 * Handle a line of data received from the bridge
133 * @param data the line of data
135 public void handleData(final String data) {
136 final Pattern devicePattern = this.devicePattern;
138 if (devicePattern != null && devicePattern.matcher(data).matches()) {
139 channels.forEach((channelUID, channel) -> refresh(channelUID, channel, data));
140 this.lastValue = data;
145 * Return the bridge status.
147 private ThingStatusInfo getBridgeStatus() {
148 final Bridge b = getBridge();
150 return b.getStatusInfo();
152 return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, null);
157 * Refreshes the channel with the last received data
159 * @param channelId the channel to refresh
161 private void refresh(final ChannelUID channelUID, final DeviceChannel channel, final String data) {
162 if (!isLinked(channelUID)) {
166 channel.transformData(data).ifPresent(value -> updateState(channelUID, new StringType(value)));