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