2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.daikinmadoka.internal.model.commands;
15 import java.util.concurrent.Executor;
17 import javax.measure.quantity.Temperature;
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;
30 * This command returns the Indoor and Outdoor temperature. Outdoor is not always supported.
32 * @author Benjamin Lafois - Initial contribution
36 public class GetIndoorOutoorTemperatures extends BRC1HCommand {
38 private final Logger logger = LoggerFactory.getLogger(GetIndoorOutoorTemperatures.class);
40 private @Nullable QuantityType<Temperature> indoorTemperature;
41 private @Nullable QuantityType<Temperature> outdoorTemperature;
44 public byte[][] getRequest() {
45 return MadokaMessage.createRequest(this);
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);
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");
66 Integer iIndoorTemperature = Integer.valueOf(bIndoorTemperature[0]);
67 Integer iOutdoorTemperature = Integer.valueOf(bOutdoorTemperature[0]);
69 if (iOutdoorTemperature == -1) {
70 iOutdoorTemperature = null;
72 if (iOutdoorTemperature < 0) {
73 iOutdoorTemperature = ((iOutdoorTemperature + 256) - 128) * -1;
77 indoorTemperature = new QuantityType<Temperature>(iIndoorTemperature, SIUnits.CELSIUS);
79 if (iOutdoorTemperature != null) {
80 outdoorTemperature = new QuantityType<Temperature>(iOutdoorTemperature, SIUnits.CELSIUS);
83 logger.debug("Indoor Temp: {}", indoorTemperature);
84 logger.debug("Outdoor Temp: {}", outdoorTemperature);
86 setState(State.SUCCEEDED);
89 executor.execute(() -> listener.receivedResponse(this));
90 } catch (Exception e) {
91 setState(State.FAILED);
92 throw new MadokaParsingException(e);
96 public @Nullable QuantityType<Temperature> getIndoorTemperature() {
97 return indoorTemperature;
100 public @Nullable QuantityType<Temperature> getOutdoorTemperature() {
101 return outdoorTemperature;
105 public int getCommandId() {