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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * The {@link CommandGetstate} class implements the GlobalCache getstate command, which retrieves the
27 * current state of the contact closure on the device.
29 * @author Mark Hilbush - Initial contribution
31 public class CommandGetstate extends AbstractCommand {
33 private final Logger logger = LoggerFactory.getLogger(CommandGetstate.class);
35 private OnOffType state;
37 public CommandGetstate(Thing thing, LinkedBlockingQueue<RequestMessage> requestQueue, String mod, String con) {
38 super(thing, requestQueue, "getstate", CommandType.COMMAND);
39 deviceCommand = "getstate," + mod + ":" + con;
43 public void parseSuccessfulReply() {
44 if (deviceReply == null) {
48 // decode response of form state,1:3,0
49 Pattern p = Pattern.compile("state,\\d:\\d,[01]");
50 Matcher m = p.matcher(deviceReply);
52 logger.warn("Successful reply from device can't be matched: {}", deviceReply);
53 state = OnOffType.OFF;
57 setModule(deviceReply.substring(6, 7));
58 setConnector(deviceReply.substring(8, 9));
59 setState((deviceReply.charAt(10) == '0' ? OnOffType.OFF : OnOffType.ON));
62 private void setState(OnOffType s) {
66 public OnOffType state() {
71 public void logSuccess() {
72 logger.debug("Execute '{}' succeeded on thing {} at {}, state={}", commandName, thing.getUID().getId(),
77 public void logFailure() {
78 logger.error("Execute '{}' failed on thing {} at {}: errorCode={}, errorMessage={}", commandName,
79 thing.getUID().getId(), ipAddress, errorCode, errorMessage);