2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.govee.internal.command.hygrometer;
15 import java.nio.ByteBuffer;
16 import java.nio.ByteOrder;
17 import java.util.concurrent.CompletableFuture;
19 import javax.measure.quantity.Dimensionless;
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;
27 * @author Connor Petty - Initial Contribution
31 public class GetOrSetHumCaliCommand extends GoveeCommand {
33 private final CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler;
34 private final @Nullable QuantityType<Dimensionless> value;
36 public GetOrSetHumCaliCommand(CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler) {
38 this.resultHandler = resultHandler;
41 public GetOrSetHumCaliCommand(QuantityType<Dimensionless> value,
42 CompletableFuture<@Nullable QuantityType<Dimensionless>> resultHandler) {
44 this.resultHandler = resultHandler;
48 public byte getCommandType() {
49 return value != null ? WRITE_TYPE : READ_TYPE;
53 public byte getCommandCode() {
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");
62 return (short) (percentQuantity.doubleValue() * 100);
66 protected byte @Nullable [] getData() {
69 return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(convertQuantity(v)).array();
75 public void handleResponse(byte @Nullable [] data, @Nullable Throwable th) {
77 resultHandler.completeExceptionally(th);
80 short hum = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getShort();
81 resultHandler.complete(new QuantityType<>(hum / 100.0, Units.PERCENT));
83 resultHandler.complete(null);