]> git.basschouten.com Git - openhab-addons.git/blob
8ad9273cafebb9bd38d87295adf32290770dcc6c
[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.library.types.OnOffType;
21 import org.openhab.core.thing.Thing;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link CommandGetstate} class implements the GlobalCache getstate command, which retrieves the
27  * current state of the contact closure on the device.
28  *
29  * @author Mark Hilbush - Initial contribution
30  */
31 public class CommandGetstate extends AbstractCommand {
32
33     private final Logger logger = LoggerFactory.getLogger(CommandGetstate.class);
34
35     private OnOffType state;
36
37     public CommandGetstate(Thing thing, LinkedBlockingQueue<RequestMessage> requestQueue, String mod, String con) {
38         super(thing, requestQueue, "getstate", CommandType.COMMAND);
39         deviceCommand = "getstate," + mod + ":" + con;
40     }
41
42     @Override
43     public void parseSuccessfulReply() {
44         if (deviceReply == null) {
45             return;
46         }
47
48         // decode response of form state,1:3,0
49         Pattern p = Pattern.compile("state,\\d:\\d,[01]");
50         Matcher m = p.matcher(deviceReply);
51         if (!m.matches()) {
52             logger.warn("Successful reply from device can't be matched: {}", deviceReply);
53             state = OnOffType.OFF;
54             return;
55         }
56
57         setModule(deviceReply.substring(6, 7));
58         setConnector(deviceReply.substring(8, 9));
59         setState(OnOffType.from(deviceReply.charAt(10) != '0'));
60     }
61
62     private void setState(OnOffType s) {
63         state = s;
64     }
65
66     public OnOffType state() {
67         return state;
68     }
69
70     @Override
71     public void logSuccess() {
72         logger.debug("Execute '{}' succeeded on thing {} at {}, state={}", commandName, thing.getUID().getId(),
73                 ipAddress, state);
74     }
75
76     @Override
77     public void logFailure() {
78         logger.error("Execute '{}' failed on thing {} at {}: errorCode={}, errorMessage={}", commandName,
79                 thing.getUID().getId(), ipAddress, errorCode, errorMessage);
80     }
81 }