]> git.basschouten.com Git - openhab-addons.git/blob
55ec19c2de652e539162e74b2b09a538e2c50a02
[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.globalcache.internal.command;
14
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.openhab.binding.globalcache.internal.GlobalCacheBindingConstants.CommandType;
20 import org.openhab.core.thing.Thing;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * The {@link CommandSetserial} class implements the GlobalCache set_SERIAL command, which sets the serial
26  * port parameters (baud, flow control, and parity) on the device.
27  *
28  * @author Mark Hilbush - Initial contribution
29  */
30 public class CommandSetserial extends AbstractCommand {
31
32     private final Logger logger = LoggerFactory.getLogger(CommandSetserial.class);
33
34     private String baud;
35     private String flow;
36     private String parity;
37
38     public CommandSetserial(Thing thing, LinkedBlockingQueue<RequestMessage> requestQueue, String mod, String con,
39             String baud, String flowcontrol, String parity) {
40         super(thing, requestQueue, "set_SERIAL", CommandType.COMMAND);
41
42         deviceCommand = "set_SERIAL," + mod + ":" + con + "," + baud + "," + flowcontrol + "," + parity;
43     }
44
45     @Override
46     public void parseSuccessfulReply() {
47         if (deviceReply == null) {
48             return;
49         }
50
51         // decode response of form SERIAL,1:1,<baudrate>,<flowcontrol>,<parity
52         Pattern p = Pattern.compile("SERIAL,\\d:\\d,\\S+,\\S+,\\S+");
53         Matcher m = p.matcher(deviceReply);
54         if (!m.matches()) {
55             return;
56         }
57
58         String fields[] = deviceReply.split(",");
59         if (fields.length != 5) {
60             return;
61         }
62
63         setModule(fields[1].substring(0, 1));
64         setConnector(fields[1].substring(2));
65         baud = fields[2];
66         flow = fields[3];
67         parity = fields[4];
68     }
69
70     public String getBaud() {
71         return baud;
72     }
73
74     public String getFlowcontrol() {
75         return flow;
76     }
77
78     public String getParity() {
79         return parity;
80     }
81
82     @Override
83     public void logSuccess() {
84         logger.debug("Execute '{}' succeeded on thing {} at {}", commandName, thing.getUID().getId(), ipAddress);
85     }
86
87     @Override
88     public void logFailure() {
89         logger.error("Execute '{}' failed on thing {} at: ipAdress={}, errorCode={}, errorMessage={}", commandName,
90                 thing.getUID().getId(), ipAddress, errorCode, errorMessage);
91     }
92 }