]> git.basschouten.com Git - openhab-addons.git/blob
20201abdc2fe3187224c1cf42aa17a929576568f
[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.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;
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         if (line == null) {
83             line = "";
84         }
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(",", ".");
89         line = line.trim();
90
91         Pattern responsePattern = Pattern.compile("(.*);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1);(0|1)");
92
93         try {
94             logger.trace("Processing '{}'", line);
95
96             Matcher matcher = responsePattern.matcher(line);
97             if (matcher.matches()) {
98                 for (int i = 1; i <= matcher.groupCount(); i++) {
99                     switch (DataField.get(i)) {
100                         case LEVEL: {
101                             DecimalType decimalType = new DecimalType(matcher.group(i));
102                             updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()), decimalType);
103                             break;
104                         }
105                         case MODE: {
106                             StringType stringType = null;
107                             if ("0".equals(matcher.group(i))) {
108                                 stringType = MANUAL;
109                             } else if ("1".equals(matcher.group(i))) {
110                                 stringType = AUTOMATIC;
111                             }
112                             if (stringType != null) {
113                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
114                                         stringType);
115                             }
116                             break;
117                         }
118                         case SOURCE: {
119                             StringType stringType = null;
120                             if ("0".equals(matcher.group(i))) {
121                                 stringType = RAIN;
122                             } else if ("1".equals(matcher.group(i))) {
123                                 stringType = CITY;
124                             }
125                             if (stringType != null) {
126                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
127                                         stringType);
128                             }
129                             break;
130                         }
131                         default:
132                             if ("0".equals(matcher.group(i))) {
133                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
134                                         OnOffType.OFF);
135                             } else if ("1".equals(matcher.group(i))) {
136                                 updateState(new ChannelUID(getThing().getUID(), DataField.get(i).channelID()),
137                                         OnOffType.ON);
138                             }
139                             break;
140                     }
141                 }
142             }
143         } catch (Exception e) {
144             logger.error("An exception occurred while receiving data : '{}'", e.getMessage(), e);
145         }
146     }
147 }