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