]> git.basschouten.com Git - openhab-addons.git/blob
33f14f44620e7a30856959d159b63425b6d6a0d1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 javax.measure.quantity.Temperature;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaMessage;
22 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaParsingException;
23 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaValue;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * This command returns the Indoor and Outdoor temperature. Outdoor is not always supported.
31  *
32  * @author Benjamin Lafois - Initial contribution
33  *
34  */
35 @NonNullByDefault
36 public class GetIndoorOutoorTemperatures extends BRC1HCommand {
37
38     private final Logger logger = LoggerFactory.getLogger(GetIndoorOutoorTemperatures.class);
39
40     private @Nullable QuantityType<Temperature> indoorTemperature;
41     private @Nullable QuantityType<Temperature> outdoorTemperature;
42
43     @Override
44     public byte[][] getRequest() {
45         return MadokaMessage.createRequest(this);
46     }
47
48     @Override
49     public void handleResponse(Executor executor, ResponseListener listener, MadokaMessage mm)
50             throws MadokaParsingException {
51         MadokaValue indoorValue = mm.getValues().get(0x40);
52         MadokaValue outdoorValue = mm.getValues().get(0x41);
53         if (outdoorValue == null || indoorValue == null) {
54             String message = "indoor or outdoor value is null when handling the response";
55             setState(State.FAILED);
56             throw new MadokaParsingException(message);
57         }
58
59         byte[] bIndoorTemperature = indoorValue.getRawValue();
60         byte[] bOutdoorTemperature = outdoorValue.getRawValue();
61         if (bIndoorTemperature == null || bOutdoorTemperature == null) {
62             setState(State.FAILED);
63             throw new MadokaParsingException("Incorrect indoor or outdoor temperature");
64         }
65
66         Integer iIndoorTemperature = Integer.valueOf(bIndoorTemperature[0]);
67         Integer iOutdoorTemperature = Integer.valueOf(bOutdoorTemperature[0]);
68
69         if (iOutdoorTemperature == -1) {
70             iOutdoorTemperature = null;
71         } else {
72             if (iOutdoorTemperature < 0) {
73                 iOutdoorTemperature = ((iOutdoorTemperature + 256) - 128) * -1;
74             }
75         }
76
77         indoorTemperature = new QuantityType<Temperature>(iIndoorTemperature, SIUnits.CELSIUS);
78
79         if (iOutdoorTemperature != null) {
80             outdoorTemperature = new QuantityType<Temperature>(iOutdoorTemperature, SIUnits.CELSIUS);
81         }
82
83         logger.debug("Indoor Temp: {}", indoorTemperature);
84         logger.debug("Outdoor Temp: {}", outdoorTemperature);
85
86         setState(State.SUCCEEDED);
87
88         try {
89             executor.execute(() -> listener.receivedResponse(this));
90         } catch (Exception e) {
91             setState(State.FAILED);
92             throw new MadokaParsingException(e);
93         }
94     }
95
96     public @Nullable QuantityType<Temperature> getIndoorTemperature() {
97         return indoorTemperature;
98     }
99
100     public @Nullable QuantityType<Temperature> getOutdoorTemperature() {
101         return outdoorTemperature;
102     }
103
104     @Override
105     public int getCommandId() {
106         return 272;
107     }
108 }