]> git.basschouten.com Git - openhab-addons.git/blob
1dcbcfcbd9ab0686fb76381a9d3e4d855e7cfd9a
[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 setpoint, whatever is the current mode.
31  *
32  * @author Benjamin Lafois - Initial contribution
33  *
34  */
35 @NonNullByDefault
36 public class GetSetpointCommand extends BRC1HCommand {
37
38     private final Logger logger = LoggerFactory.getLogger(GetSetpointCommand.class);
39
40     private @Nullable QuantityType<Temperature> heatingSetpoint;
41     private @Nullable QuantityType<Temperature> coolingSetpoint;
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 heatValue = mm.getValues().get(0x21);
52         MadokaValue coolValue = mm.getValues().get(0x20);
53         if (heatValue == null || coolValue == null) {
54             String message = "heatingSetpoint or coolingSetpoint is null when handling the response";
55             setState(State.FAILED);
56             throw new MadokaParsingException(message);
57         }
58
59         Integer iHeatingSetpoint = (int) (heatValue.getComputedValue() / 128.);
60         Integer iCoolingSetpoint = (int) (coolValue.getComputedValue() / 128.);
61
62         this.heatingSetpoint = new QuantityType<Temperature>(iHeatingSetpoint, SIUnits.CELSIUS);
63         this.coolingSetpoint = new QuantityType<Temperature>(iCoolingSetpoint, SIUnits.CELSIUS);
64
65         logger.debug("heatingSetpoint: {}", heatingSetpoint);
66         logger.debug("coolingSetpoint: {}", coolingSetpoint);
67
68         setState(State.SUCCEEDED);
69         try {
70             executor.execute(() -> listener.receivedResponse(this));
71         } catch (Exception e) {
72             setState(State.FAILED);
73             throw new MadokaParsingException(e);
74         }
75     }
76
77     @Override
78     public int getCommandId() {
79         return 64;
80     }
81
82     public @Nullable QuantityType<Temperature> getHeatingSetpoint() {
83         return heatingSetpoint;
84     }
85
86     public @Nullable QuantityType<Temperature> getCoolingSetpoint() {
87         return coolingSetpoint;
88     }
89 }