]> git.basschouten.com Git - openhab-addons.git/blob
88a0033b593055e5c8d12decdea7c3ed9a265424
[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.util.concurrent.CompletableFuture;
16
17 import javax.measure.quantity.Dimensionless;
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.Units;
23
24 /**
25  * @author Connor Petty - Initial Contribution
26  *
27  */
28 @NonNullByDefault
29 public class GetBatteryCommand extends GetCommand {
30
31     private CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler;
32
33     public GetBatteryCommand(CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler) {
34         this.resultHandler = resultHandler;
35     }
36
37     @Override
38     public byte getCommandCode() {
39         return 8;
40     }
41
42     @Override
43     public void handleResponse(byte @Nullable [] data, @Nullable Throwable th) {
44         if (th != null) {
45             resultHandler.completeExceptionally(th);
46         }
47         if (data != null) {
48             int value = data[0] & 0xFF;
49             resultHandler.complete(new QuantityType<Dimensionless>(value, Units.PERCENT));
50         } else {
51             resultHandler.complete(null);
52         }
53     }
54 }