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