]> git.basschouten.com Git - openhab-addons.git/blob
b50c155262502d70d4cd1f1407f71c8c3c57281d
[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.rme.internal.handler;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
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;
29
30 /**
31  * The {@link RMEThingHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Karel Goderis - Initial contribution
35  */
36 public class RMEThingHandler extends SerialThingHandler {
37
38     private final Logger logger = LoggerFactory.getLogger(RMEThingHandler.class);
39
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");
44
45     public RMEThingHandler(Thing thing, SerialPortManager serialPortManager) {
46         super(thing, serialPortManager);
47     }
48
49     @Override
50     public void initialize() {
51         logger.debug("Initializing RME handler.");
52
53         if (getConfig().get(BAUD_RATE) == null) {
54             baud = 2400;
55         } else {
56             baud = (int) getConfig().get(BAUD_RATE);
57         }
58
59         if (getConfig().get(BUFFER_SIZE) == null) {
60             bufferSize = 1024;
61         } else {
62             bufferSize = (int) getConfig().get(BUFFER_SIZE);
63         }
64
65         port = (String) getConfig().get(PORT);
66
67         sleep = 250;
68
69         interval = 5000;
70
71         super.initialize();
72     }
73
74     @Override
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");
77     }
78
79     @Override
80     public void onDataReceived(String receivedLine) {
81         String line = StringUtils.chomp(receivedLine);
82
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(",", ".");
87         line = line.trim();
88
89         Pattern responsePattern = Pattern.compile("(.*);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1)");
90
91         try {
92             logger.trace("Processing '{}'", line);
93
94             Matcher matcher = responsePattern.matcher(line);
95             if (matcher.matches()) {
96                 for (int i = 1; i <= matcher.groupCount(); i++) {
97                     switch (DataField.get(i)) {
98                         case LEVEL: {
99                             DecimalType decimalType = new DecimalType(matcher.group(i));
100                             updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()), decimalType);
101                             break;
102                         }
103                         case MODE: {
104                             StringType stringType = null;
105                             if ("0".equals(matcher.group(i))) {
106                                 stringType = MANUAL;
107                             } else if ("1".equals(matcher.group(i))) {
108                                 stringType = AUTOMATIC;
109                             }
110                             if (stringType != null) {
111                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
112                                         stringType);
113                             }
114                             break;
115                         }
116                         case SOURCE: {
117                             StringType stringType = null;
118                             if ("0".equals(matcher.group(i))) {
119                                 stringType = RAIN;
120                             } else if ("1".equals(matcher.group(i))) {
121                                 stringType = CITY;
122                             }
123                             if (stringType != null) {
124                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
125                                         stringType);
126                             }
127                             break;
128                         }
129                         default:
130                             if ("0".equals(matcher.group(i))) {
131                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
132                                         OnOffType.OFF);
133                             } else if ("1".equals(matcher.group(i))) {
134                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
135                                         OnOffType.ON);
136                             }
137                             break;
138                     }
139                 }
140             }
141         } catch (Exception e) {
142             logger.error("An exception occurred while receiving data : '{}'", e.getMessage(), e);
143         }
144     }
145 }