]> git.basschouten.com Git - openhab-addons.git/blob
ae90530ad27999a4144a582bd512d17e153ad13f
[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.bluetooth.daikinmadoka.internal.model.commands;
14
15 import java.util.concurrent.Executor;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaMessage;
20 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaParsingException;
21 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaProperties.OperationMode;
22 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaValue;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * This command returns the current AC operation mode
28  *
29  * @author Benjamin Lafois - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public class GetOperationmodeCommand extends BRC1HCommand {
34
35     private final Logger logger = LoggerFactory.getLogger(GetOperationmodeCommand.class);
36
37     private @Nullable OperationMode operationMode;
38
39     @Override
40     public byte[][] getRequest() {
41         return MadokaMessage.createRequest(this);
42     }
43
44     @Override
45     public void handleResponse(Executor executor, ResponseListener listener, MadokaMessage mm)
46             throws MadokaParsingException {
47         @Nullable
48         MadokaValue mode = mm.getValues().get(0x20);
49         if (mode == null) {
50             setState(State.FAILED);
51             throw new MadokaParsingException("Incorrect operation mode");
52         }
53         byte[] bOperationMode = mode.getRawValue();
54         if (bOperationMode == null) {
55             setState(State.FAILED);
56             throw new MadokaParsingException("Incorrect operation mode");
57         }
58
59         operationMode = OperationMode.valueOf(bOperationMode[0]);
60
61         logger.debug("operationMode: {}", operationMode);
62
63         setState(State.SUCCEEDED);
64         try {
65             executor.execute(() -> listener.receivedResponse(this));
66         } catch (Exception e) {
67             setState(State.FAILED);
68             throw new MadokaParsingException(e);
69         }
70     }
71
72     @Override
73     public int getCommandId() {
74         return 48;
75     }
76
77     public @Nullable OperationMode getOperationMode() {
78         return operationMode;
79     }
80 }