]> git.basschouten.com Git - openhab-addons.git/blob
051bbd61f877677025a7e7cd8fbd537c90fe95f0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.govee.internal.command.hygrometer;
14
15 import java.nio.ByteBuffer;
16 import java.nio.ByteOrder;
17 import java.util.concurrent.CompletableFuture;
18
19 import javax.measure.quantity.Temperature;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.SIUnits;
25
26 /**
27  * @author Connor Petty - Initial Contribution
28  *
29  */
30 @NonNullByDefault
31 public class GetOrSetTemCaliCommand extends GoveeCommand {
32     private final CompletableFuture<@Nullable QuantityType<Temperature>> resultHandler;
33     private final @Nullable QuantityType<Temperature> value;
34
35     public GetOrSetTemCaliCommand(CompletableFuture<@Nullable QuantityType<Temperature>> resultHandler) {
36         this.value = null;
37         this.resultHandler = resultHandler;
38     }
39
40     public GetOrSetTemCaliCommand(QuantityType<Temperature> value,
41             CompletableFuture<@Nullable QuantityType<Temperature>> resultHandler) {
42         this.value = value;
43         this.resultHandler = resultHandler;
44     }
45
46     @Override
47     public byte getCommandType() {
48         return value != null ? WRITE_TYPE : READ_TYPE;
49     }
50
51     @Override
52     public byte getCommandCode() {
53         return 7;
54     }
55
56     private static short convertQuantity(QuantityType<Temperature> quantity) {
57         var celciusQuantity = quantity.toUnit(SIUnits.CELSIUS);
58         if (celciusQuantity == null) {
59             throw new IllegalArgumentException("Unable to convert quantity to celcius");
60         }
61         return (short) (celciusQuantity.doubleValue() * 100);
62     }
63
64     @Override
65     protected byte @Nullable [] getData() {
66         var v = value;
67         if (v != null) {
68             return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(convertQuantity(v)).array();
69         }
70         return null;
71     }
72
73     @Override
74     public void handleResponse(byte @Nullable [] data, @Nullable Throwable th) {
75         if (th != null) {
76             resultHandler.completeExceptionally(th);
77         }
78         if (data != null) {
79             short tem = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getShort();
80             resultHandler.complete(new QuantityType<>(tem / 100.0, SIUnits.CELSIUS));
81         } else {
82             resultHandler.complete(null);
83         }
84     }
85 }