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