]> git.basschouten.com Git - openhab-addons.git/blob
820a015c883103c105174ec7a882ca26a30dd85a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26
27 /**
28  * @author Connor Petty - Initial Contribution
29  *
30  */
31 @NonNullByDefault
32 public class GetOrSetHumWarningCommand extends GoveeCommand {
33
34     private final @Nullable WarningSettingsDTO<Dimensionless> settings;
35     private final CompletableFuture<@Nullable WarningSettingsDTO<Dimensionless>> resultHandler;
36
37     public GetOrSetHumWarningCommand(CompletableFuture<@Nullable WarningSettingsDTO<Dimensionless>> resultHandler) {
38         this.settings = null;
39         this.resultHandler = resultHandler;
40     }
41
42     public GetOrSetHumWarningCommand(WarningSettingsDTO<Dimensionless> settings,
43             CompletableFuture<@Nullable WarningSettingsDTO<Dimensionless>> resultHandler) {
44         this.settings = settings;
45         this.resultHandler = resultHandler;
46     }
47
48     @Override
49     public byte getCommandType() {
50         return settings == null ? READ_TYPE : WRITE_TYPE;
51     }
52
53     @Override
54     public byte getCommandCode() {
55         return 3;
56     }
57
58     private static short convertQuantity(@Nullable QuantityType<Dimensionless> quantity) {
59         if (quantity == null) {
60             throw new IllegalArgumentException("Unable to convert quantity to percent");
61         }
62         QuantityType<Dimensionless> percentQuantity = quantity.toUnit(Units.PERCENT);
63         if (percentQuantity == null) {
64             throw new IllegalArgumentException("Unable to convert quantity to percent");
65         }
66         return (short) (percentQuantity.doubleValue() * 100);
67     }
68
69     @Override
70     protected byte @Nullable [] getData() {
71         WarningSettingsDTO<Dimensionless> localSettins = settings;
72         if (localSettins == null || localSettins.min == null || localSettins.max == null) {
73             return null;
74         }
75
76         ByteBuffer buffer = ByteBuffer.allocate(5).order(ByteOrder.LITTLE_ENDIAN);
77         buffer.put(localSettins.enableAlarm == OnOffType.ON ? (byte) 1 : 0);
78         buffer.putShort(convertQuantity(localSettins.min));
79         buffer.putShort(convertQuantity(localSettins.max));
80         return buffer.array();
81     }
82
83     @Override
84     public void handleResponse(byte @Nullable [] data, @Nullable Throwable th) {
85         if (th != null) {
86             resultHandler.completeExceptionally(th);
87         }
88         if (data != null) {
89             WarningSettingsDTO<Dimensionless> result = new WarningSettingsDTO<Dimensionless>();
90
91             ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
92             result.enableAlarm = OnOffType.from(buffer.get() == 1);
93             result.min = new QuantityType<Dimensionless>(buffer.getShort() / 100.0, Units.PERCENT);
94             result.max = new QuantityType<Dimensionless>(buffer.getShort() / 100.0, Units.PERCENT);
95
96             resultHandler.complete(result);
97         } else {
98             resultHandler.complete(null);
99         }
100     }
101 }