]> git.basschouten.com Git - openhab-addons.git/blob
0d4ab3dd7b8a183c7866e97d5cf787a50a8e196f
[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 org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.unit.SIUnits;
23 import org.openhab.core.library.unit.Units;
24
25 /**
26  * @author Connor Petty - Initial Contribution
27  *
28  */
29 @NonNullByDefault
30 public class GetTemHumCommand extends GetCommand {
31
32     private CompletableFuture<@Nullable TemHumDTO> resultHandler;
33
34     public GetTemHumCommand(CompletableFuture<@Nullable TemHumDTO> resultHandler) {
35         this.resultHandler = resultHandler;
36     }
37
38     @Override
39     public byte getCommandCode() {
40         return 10;
41     }
42
43     @Override
44     public void handleResponse(byte @Nullable [] data, @Nullable Throwable th) {
45         if (th != null) {
46             resultHandler.completeExceptionally(th);
47         }
48         if (data != null) {
49             ByteBuffer buffer = ByteBuffer.wrap(data);
50             buffer.order(ByteOrder.LITTLE_ENDIAN);
51             int temp = buffer.getShort();
52             int hum = Short.toUnsignedInt(buffer.getShort());
53
54             TemHumDTO temhum = new TemHumDTO();
55             temhum.temperature = new QuantityType<>(temp / 100.0, SIUnits.CELSIUS);
56             temhum.humidity = new QuantityType<>(hum / 100.0, Units.PERCENT);
57             resultHandler.complete(temhum);
58         } else {
59             resultHandler.complete(null);
60         }
61     }
62 }