]> git.basschouten.com Git - openhab-addons.git/blob
d352802e6eb55cc1d9649f5fbcd9d146c8650fcb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.serial.internal.handler;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
19
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;
38
39 /**
40  * The {@link SerialDeviceHandler} is responsible for handling commands, which are
41  * sent to one of the channels.
42  *
43  * @author Mike Major - Initial contribution
44  */
45 @NonNullByDefault
46 public class SerialDeviceHandler extends BaseThingHandler {
47
48     private final ValueTransformationProvider valueTransformationProvider;
49
50     private @Nullable Pattern devicePattern;
51
52     private @Nullable String lastValue;
53
54     private final Map<ChannelUID, DeviceChannel> channels = new HashMap<>();
55
56     public SerialDeviceHandler(final Thing thing, final ValueTransformationProvider valueTransformationProvider) {
57         super(thing);
58         this.valueTransformationProvider = valueTransformationProvider;
59     }
60
61     @Override
62     public void handleCommand(final ChannelUID channelUID, final Command command) {
63         if (command instanceof RefreshType) {
64             final String lastValue = this.lastValue;
65
66             if (lastValue != null) {
67                 final DeviceChannel channel = channels.get(channelUID);
68                 if (channel != null) {
69                     refresh(channelUID, channel, lastValue);
70                 }
71             }
72         } else {
73             final DeviceChannel channel = channels.get(channelUID);
74             if (channel != null) {
75                 final Bridge bridge = getBridge();
76                 if (bridge != null) {
77                     final SerialBridgeHandler handler = (SerialBridgeHandler) bridge.getHandler();
78                     if (handler != null) {
79                         channel.mapCommand(command).ifPresent(value -> handler.writeString(value));
80                     }
81                 }
82             }
83         }
84     }
85
86     @Override
87     public void initialize() {
88         final SerialDeviceConfiguration config = getConfigAs(SerialDeviceConfiguration.class);
89
90         try {
91             devicePattern = Pattern.compile(config.patternMatch);
92         } catch (final PatternSyntaxException e) {
93             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
94                     "Invalid device pattern: " + e.getMessage());
95             return;
96         }
97
98         for (final Channel c : getThing().getChannels()) {
99             final ChannelTypeUID type = c.getChannelTypeUID();
100             if (type != null) {
101                 final ChannelConfig channelConfig = c.getConfiguration().as(ChannelConfig.class);
102                 try {
103                     final DeviceChannel deviceChannel = DeviceChannelFactory
104                             .createDeviceChannel(valueTransformationProvider, channelConfig, type.getId());
105                     if (deviceChannel != null) {
106                         channels.put(c.getUID(), deviceChannel);
107                     }
108                 } catch (final IllegalArgumentException e) {
109                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
110                             "Configuration error for channel " + c.getUID().getId() + ": " + e.getMessage());
111                     return;
112                 }
113             }
114         }
115
116         if (getBridgeStatus().getStatus() == ThingStatus.ONLINE) {
117             updateStatus(ThingStatus.ONLINE);
118         } else {
119             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
120         }
121     }
122
123     @Override
124     public void dispose() {
125         channels.clear();
126         lastValue = null;
127         super.dispose();
128     }
129
130     /**
131      * Handle a line of data received from the bridge
132      *
133      * @param data the line of data
134      */
135     public void handleData(final String data) {
136         final Pattern devicePattern = this.devicePattern;
137
138         if (devicePattern != null && devicePattern.matcher(data).matches()) {
139             channels.forEach((channelUID, channel) -> refresh(channelUID, channel, data));
140             this.lastValue = data;
141         }
142     }
143
144     /**
145      * Return the bridge status.
146      */
147     private ThingStatusInfo getBridgeStatus() {
148         final Bridge b = getBridge();
149         if (b != null) {
150             return b.getStatusInfo();
151         } else {
152             return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, null);
153         }
154     }
155
156     /**
157      * Refreshes the channel with the last received data
158      *
159      * @param channelId the channel to refresh
160      */
161     private void refresh(final ChannelUID channelUID, final DeviceChannel channel, final String data) {
162         if (!isLinked(channelUID)) {
163             return;
164         }
165
166         channel.transformData(data).ifPresent(value -> updateState(channelUID, new StringType(value)));
167     }
168 }