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.rme.internal.handler;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.apache.commons.lang3.StringUtils;
19 import org.openhab.binding.rme.internal.RMEBindingConstants.DataField;
20 import org.openhab.core.io.transport.serial.SerialPortManager;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link RMEThingHandler} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Karel Goderis - Initial contribution
36 public class RMEThingHandler extends SerialThingHandler {
38 private final Logger logger = LoggerFactory.getLogger(RMEThingHandler.class);
40 private static final StringType AUTOMATIC = new StringType("Automatic");
41 private static final StringType CITY = new StringType("City");
42 private static final StringType MANUAL = new StringType("Manual");
43 private static final StringType RAIN = new StringType("Rain");
45 public RMEThingHandler(Thing thing, SerialPortManager serialPortManager) {
46 super(thing, serialPortManager);
50 public void initialize() {
51 logger.debug("Initializing RME handler.");
53 if (getConfig().get(BAUD_RATE) == null) {
56 baud = (int) getConfig().get(BAUD_RATE);
59 if (getConfig().get(BUFFER_SIZE) == null) {
62 bufferSize = (int) getConfig().get(BUFFER_SIZE);
65 port = (String) getConfig().get(PORT);
75 public void handleCommand(ChannelUID channelUID, Command command) {
76 logger.warn("The RME Rain Manager is a read-only device and can not handle commands");
80 public void onDataReceived(String receivedLine) {
81 String line = StringUtils.chomp(receivedLine);
83 // little hack to overcome Locale limits of the RME Rain Manager
84 // note to the attentive reader : should we add support for system
85 // locale's in the Type classes? ;-)
86 line = line.replace(",", ".");
89 Pattern responsePattern = Pattern.compile("(.*);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1)");
92 logger.trace("Processing '{}'", line);
94 Matcher matcher = responsePattern.matcher(line);
95 if (matcher.matches()) {
96 for (int i = 1; i <= matcher.groupCount(); i++) {
97 switch (DataField.get(i)) {
99 DecimalType decimalType = new DecimalType(matcher.group(i));
100 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()), decimalType);
104 StringType stringType = null;
105 if ("0".equals(matcher.group(i))) {
107 } else if ("1".equals(matcher.group(i))) {
108 stringType = AUTOMATIC;
110 if (stringType != null) {
111 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
117 StringType stringType = null;
118 if ("0".equals(matcher.group(i))) {
120 } else if ("1".equals(matcher.group(i))) {
123 if (stringType != null) {
124 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
130 if ("0".equals(matcher.group(i))) {
131 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
133 } else if ("1".equals(matcher.group(i))) {
134 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
141 } catch (Exception e) {
142 logger.error("An exception occurred while receiving data : '{}'", e.getMessage(), e);