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.openhab.binding.rme.internal.RMEBindingConstants.DataField;
19 import org.openhab.core.io.transport.serial.SerialPortManager;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.util.StringUtils;
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);
85 // little hack to overcome Locale limits of the RME Rain Manager
86 // note to the attentive reader : should we add support for system
87 // locale's in the Type classes? ;-)
88 line = line.replace(",", ".");
91 Pattern responsePattern = Pattern.compile("(.*);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1)");
94 logger.trace("Processing '{}'", line);
96 Matcher matcher = responsePattern.matcher(line);
97 if (matcher.matches()) {
98 for (int i = 1; i <= matcher.groupCount(); i++) {
99 switch (DataField.get(i)) {
101 DecimalType decimalType = new DecimalType(matcher.group(i));
102 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()), decimalType);
106 StringType stringType = null;
107 if ("0".equals(matcher.group(i))) {
109 } else if ("1".equals(matcher.group(i))) {
110 stringType = AUTOMATIC;
112 if (stringType != null) {
113 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
119 StringType stringType = null;
120 if ("0".equals(matcher.group(i))) {
122 } else if ("1".equals(matcher.group(i))) {
125 if (stringType != null) {
126 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
132 if ("0".equals(matcher.group(i))) {
133 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
135 } else if ("1".equals(matcher.group(i))) {
136 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
143 } catch (Exception e) {
144 logger.error("An exception occurred while receiving data : '{}'", e.getMessage(), e);