2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.globalcache.internal.command;
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
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;
27 * The {@link CommandSetstate} class implements the GlobalCache setstate command for devices that support contact
30 * @author Mark Hilbush - Initial contribution
32 public class CommandSetstate extends AbstractCommand {
34 private final Logger logger = LoggerFactory.getLogger(CommandSetstate.class);
36 private Command command;
37 private OnOffType state;
39 public CommandSetstate(Thing thing, Command command, LinkedBlockingQueue<RequestMessage> queue, String mod,
41 super(thing, queue, "setstate", CommandType.COMMAND);
43 this.command = command;
44 if (command instanceof OnOffType) {
45 deviceCommand = "setstate," + mod + ":" + con + "," + (command.equals(OnOffType.ON) ? "1" : "0");
50 public void parseSuccessfulReply() {
51 if (deviceReply == null) {
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);
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);
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("0".equals(m.group(4)) ? OnOffType.OFF : OnOffType.ON);
79 private void setState(OnOffType s) {
83 public OnOffType state() {
88 public void logSuccess() {
89 logger.debug("Execute '{}' succeeded for command {} on thing {} at {}", commandName, command,
90 thing.getUID().getId(), ipAddress);
94 public void logFailure() {
95 logger.error("Execute '{}' failed on thing {} at {}: errorCode={}, errorMessage={}", commandName,
96 thing.getUID().getId(), ipAddress, errorCode, errorMessage);