]> git.basschouten.com Git - openhab-addons.git/blob
97ad9d6cfc4cec193ddd643fb5801a0143f7f1f5
[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.openhab.core.library.types.PercentType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Command used to get the blue Eye brightness level
28  *
29  * @author Benjamin Lafois - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public class GetEyeBrightnessCommand extends BRC1HCommand {
34
35     private final Logger logger = LoggerFactory.getLogger(GetEyeBrightnessCommand.class);
36
37     private @Nullable PercentType eyeBrightness;
38
39     @Override
40     public void handleResponse(Executor executor, ResponseListener listener, MadokaMessage mm)
41             throws MadokaParsingException {
42         MadokaValue mValue = mm.getValues().get(0x33);
43         if (mValue == null) {
44             String message = "eye brightness is null when handling the response";
45             setState(State.FAILED);
46             throw new MadokaParsingException(message);
47         }
48
49         byte[] bEyeBrightness = mValue.getRawValue();
50         if (bEyeBrightness == null) {
51             setState(State.FAILED);
52             throw new MadokaParsingException("Incorrect eye brightness value");
53         }
54
55         Integer iEyeBrightness = Integer.valueOf(bEyeBrightness[0]);
56         // The values accepted by the device are from 0 to 19 - integers so conversion needed for Dimmer channel
57         eyeBrightness = new PercentType((int) Math.round(iEyeBrightness / 0.19));
58
59         logger.debug("Eye Brightness: {}", eyeBrightness);
60
61         setState(State.SUCCEEDED);
62         try {
63             executor.execute(() -> listener.receivedResponse(this));
64         } catch (Exception e) {
65             setState(State.FAILED);
66             throw new MadokaParsingException(e);
67         }
68     }
69
70     @Override
71     public byte[][] getRequest() {
72         // We can call the function without parameters - but it will return all the display parameters, which makes a 3
73         // chunks return message. As such, specifying requested value 0x33 (eyeBrightness)
74         MadokaValue mv = new MadokaValue(0x33, 1, new byte[] { (byte) 0x00 });
75         return MadokaMessage.createRequest(this, mv);
76     }
77
78     @Override
79     public int getCommandId() {
80         return 770;
81     }
82
83     public @Nullable PercentType getEyeBrightness() {
84         return eyeBrightness;
85     }
86 }