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.bluetooth.daikinmadoka.internal.model.commands;
15 import java.util.concurrent.Executor;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.locks.Condition;
18 import java.util.concurrent.locks.Lock;
19 import java.util.concurrent.locks.ReentrantLock;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaMessage;
23 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaParsingException;
26 * Abstract class for all BLE commands sent to the controller
28 * @author Benjamin Lafois - Initial contribution
32 public abstract class BRC1HCommand {
42 private volatile State state = State.NEW;
44 private final Lock stateLock = new ReentrantLock();
46 private final Condition stateCondition = stateLock.newCondition();
48 public abstract void handleResponse(Executor executor, ResponseListener listener, MadokaMessage mm)
49 throws MadokaParsingException;
52 * THis command returns the message to be sent
56 public abstract byte[][] getRequest();
59 * This is the command number, in the protocol
63 public abstract int getCommandId();
66 * Returns current state of the command.
68 * @return current state
70 public State getState() {
75 * Sets state of the command.
77 * @param state new state
79 public void setState(State state) {
83 stateCondition.signalAll();
89 public boolean awaitStateChange(long timeout, TimeUnit unit, State... expectedStates) throws InterruptedException {
92 long nanosTimeout = unit.toNanos(timeout);
93 while (!isInAnyState(expectedStates)) {
94 if (nanosTimeout <= 0L) {
97 nanosTimeout = stateCondition.awaitNanos(nanosTimeout);
105 private boolean isInAnyState(State[] acceptedStates) {
106 for (State acceptedState : acceptedStates) {
107 if (acceptedState == state) {