]> git.basschouten.com Git - openhab-addons.git/blob
297ac7849c0ffed75fbacfb30c86af3646c8e6a1
[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.TimeUnit;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.bluetooth.gattserial.SimpleMessageServicer;
20
21 /**
22  * @author Connor Petty - Initial Contribution
23  *
24  */
25 @NonNullByDefault
26 public abstract class GoveeCommand implements SimpleMessageServicer<GoveeMessage> {
27
28     public static final byte READ_TYPE = -86;
29     public static final byte WRITE_TYPE = 51;
30
31     public abstract byte getCommandType();
32
33     public abstract byte getCommandCode();
34
35     protected abstract byte @Nullable [] getData();
36
37     @Override
38     public long getTimeout(TimeUnit unit) {
39         return unit.convert(60, TimeUnit.SECONDS);
40     }
41
42     @Override
43     public GoveeMessage createMessage() {
44         return new GoveeMessage(getCommandType(), getCommandCode(), getData());
45     }
46
47     @Override
48     public boolean handleFailedMessage(GoveeMessage message, Throwable th) {
49         if (matches(message)) {
50             handleResponse(null, th);
51             return true;
52         }
53         return false;
54     }
55
56     @Override
57     public boolean handleReceivedMessage(GoveeMessage message) {
58         if (matches(message)) {
59             handleResponse(message.getData(), null);
60             return true;
61         }
62         return false;
63     }
64
65     public abstract void handleResponse(byte @Nullable [] data, @Nullable Throwable th);
66
67     protected boolean matches(GoveeMessage message) {
68         return message.getCommandType() == getCommandType() && message.getCommandCode() == getCommandCode();
69     }
70 }