]> git.basschouten.com Git - openhab-addons.git/blob
efbcd47a090a1e543764e5bf6ac643316dc9f82e
[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.MadokaValue;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * This command returns the current AC power state (on or off)
27  *
28  * @author Benjamin Lafois - Initial contribution
29  *
30  */
31 @NonNullByDefault
32 public class GetPowerstateCommand extends BRC1HCommand {
33
34     private final Logger logger = LoggerFactory.getLogger(GetPowerstateCommand.class);
35
36     private @Nullable Boolean powerState;
37
38     @Override
39     public byte[][] getRequest() {
40         return MadokaMessage.createRequest(this);
41     }
42
43     @Override
44     public void handleResponse(Executor executor, ResponseListener listener, MadokaMessage mm)
45             throws MadokaParsingException {
46         MadokaValue mValue = mm.getValues().get(0x20);
47         if (mValue == null) {
48             String message = "powerstate is null when handling the response";
49             setState(State.FAILED);
50             throw new MadokaParsingException(message);
51         }
52
53         byte[] powerStateValue = mValue.getRawValue();
54         if (powerStateValue == null || powerStateValue.length != 1) {
55             setState(State.FAILED);
56             throw new MadokaParsingException("Incorrect value for PowerState");
57         }
58
59         powerState = Integer.valueOf(powerStateValue[0]) == 1;
60
61         logger.debug("PowerState: {}", powerState);
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 32;
75     }
76
77     public @Nullable Boolean isPowerState() {
78         return powerState;
79     }
80 }