]> git.basschouten.com Git - openhab-addons.git/blob
241adf1a37a3975474f2a98bf25d95fa896a84fd
[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.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         byte[] bEyeBrightness = mm.getValues().get(0x33).getRawValue();
43
44         if (bEyeBrightness == null || bEyeBrightness == null) {
45             setState(State.FAILED);
46             throw new MadokaParsingException("Incorrect eye brightness value");
47         }
48
49         Integer iEyeBrightness = Integer.valueOf(bEyeBrightness[0]);
50
51         if (iEyeBrightness != null) {
52             // The values accepted by the device are from 0 to 19 - integers so conversion needed for Dimmer channel
53             eyeBrightness = new PercentType((int) Math.round(iEyeBrightness / 0.19));
54         }
55
56         logger.debug("Eye Brightness: {}", eyeBrightness);
57
58         setState(State.SUCCEEDED);
59         executor.execute(() -> listener.receivedResponse(this));
60     }
61
62     @Override
63     public byte[][] getRequest() {
64         // We can call the function without parameters - but it will return all the display parameters, which makes a 3
65         // chunks return message. As such, specifying requested value 0x33 (eyeBrightness)
66         MadokaValue mv = new MadokaValue(0x33, 1, new byte[] { (byte) 0x00 });
67         return MadokaMessage.createRequest(this, mv);
68     }
69
70     @Override
71     public int getCommandId() {
72         return 770;
73     }
74
75     public @Nullable PercentType getEyeBrightness() {
76         return eyeBrightness;
77     }
78 }