]> git.basschouten.com Git - openhab-addons.git/blob
eaf5ee7f0400f09b5422d801ea19f58cfaed4d78
[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.openhab.core.types.Command;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link CommandSetstate} class implements the GlobalCache setstate command for devices that support contact
28  * closure.
29  *
30  * @author Mark Hilbush - Initial contribution
31  */
32 public class CommandSetstate extends AbstractCommand {
33
34     private final Logger logger = LoggerFactory.getLogger(CommandSetstate.class);
35
36     private Command command;
37     private OnOffType state;
38
39     public CommandSetstate(Thing thing, Command command, LinkedBlockingQueue<RequestMessage> queue, String mod,
40             String con) {
41         super(thing, queue, "setstate", CommandType.COMMAND);
42
43         this.command = command;
44         if (command instanceof OnOffType) {
45             deviceCommand = "setstate," + mod + ":" + con + "," + (command.equals(OnOffType.ON) ? "1" : "0");
46         }
47     }
48
49     @Override
50     public void parseSuccessfulReply() {
51         if (deviceReply == null) {
52             return;
53         }
54
55         // Match on response of form setstate,1:3,0 or state,1:3,0
56         if (!matchSetstate()) {
57             logger.warn("Successful reply from device can't be matched: {}", deviceReply);
58             setState(OnOffType.OFF);
59             return;
60         }
61     }
62
63     private boolean matchSetstate() {
64         // Matches both iTach and GC-100 responses
65         Pattern p = Pattern.compile("(setstate|state),(\\d):(\\d),([01])");
66         Matcher m = p.matcher(deviceReply);
67         if (m.matches()) {
68             logger.trace("Matched setstate response: g2={}, g3={}, g4={}", m.group(2), m.group(3), m.group(4));
69             if (m.groupCount() == 4) {
70                 setModule(m.group(2));
71                 setConnector(m.group(3));
72                 setState(m.group(4).equals("0") ? OnOffType.OFF : OnOffType.ON);
73                 return true;
74             }
75         }
76         return false;
77     }
78
79     private void setState(OnOffType s) {
80         state = s;
81     }
82
83     public OnOffType state() {
84         return state;
85     }
86
87     @Override
88     public void logSuccess() {
89         logger.debug("Execute '{}' succeeded for command {} on thing {} at {}", commandName, command,
90                 thing.getUID().getId(), ipAddress);
91     }
92
93     @Override
94     public void logFailure() {
95         logger.error("Execute '{}' failed on thing {} at {}: errorCode={}, errorMessage={}", commandName,
96                 thing.getUID().getId(), ipAddress, errorCode, errorMessage);
97     }
98 }