]> git.basschouten.com Git - openhab-addons.git/blob
830d54863579d0338cdf22fae2e42fd1cd61fb51
[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.Dimensionless;
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.Units;
25
26 /**
27  * @author Connor Petty - Initial Contribution
28  *
29  */
30 @NonNullByDefault
31 public class GetOrSetHumCaliCommand extends GoveeCommand {
32
33     private final CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler;
34     private final @Nullable QuantityType<Dimensionless> value;
35
36     public GetOrSetHumCaliCommand(CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler) {
37         this.value = null;
38         this.resultHandler = resultHandler;
39     }
40
41     public GetOrSetHumCaliCommand(QuantityType<Dimensionless> value,
42             CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler) {
43         this.value = value;
44         this.resultHandler = resultHandler;
45     }
46
47     @Override
48     public byte getCommandType() {
49         return value != null ? WRITE_TYPE : READ_TYPE;
50     }
51
52     @Override
53     public byte getCommandCode() {
54         return 6;
55     }
56
57     private static short convertQuantity(QuantityType<Dimensionless> quantity) {
58         var percentQuantity = quantity.toUnit(Units.PERCENT);
59         if (percentQuantity == null) {
60             throw new IllegalArgumentException("Unable to convert quantity to percent");
61         }
62         return (short) (percentQuantity.doubleValue() * 100);
63     }
64
65     @Override
66     protected byte @Nullable [] getData() {
67         var v = value;
68         if (v != null) {
69             return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(convertQuantity(v)).array();
70         }
71         return null;
72     }
73
74     @Override
75     public void handleResponse(byte @Nullable [] data, @Nullable Throwable th) {
76         if (th != null) {
77             resultHandler.completeExceptionally(th);
78         }
79         if (data != null) {
80             short hum = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getShort();
81             resultHandler.complete(new QuantityType<>(hum / 100.0, Units.PERCENT));
82         } else {
83             resultHandler.complete(null);
84         }
85     }
86 }