]> git.basschouten.com Git - openhab-addons.git/blob
1257b9bb75394f8cc04f55988d86d7af30d67f84
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.bluetooth.gattserial.GattMessage;
18
19 /**
20  * @author Connor Petty - Initial contribution
21  *
22  */
23 @NonNullByDefault
24 public class GoveeMessage implements GattMessage {
25
26     private byte[] payload;
27
28     public GoveeMessage(byte[] payload) {
29         this.payload = payload;
30     }
31
32     public GoveeMessage(byte commandType, byte commandCode, byte @Nullable [] data) {
33         payload = new byte[20];
34         payload[0] = commandType;
35         payload[1] = commandCode;
36         if (data != null) {
37             System.arraycopy(data, 0, payload, 2, data.length);
38         }
39         payload[19] = calculateCrc(payload, 19);
40     }
41
42     public byte getCommandType() {
43         return payload[0];
44     }
45
46     public byte getCommandCode() {
47         return payload[1];
48     }
49
50     protected static byte calculateCrc(byte[] bArr, int i) {
51         byte b = bArr[0];
52         for (int i2 = 1; i2 < i; i2++) {
53             b = (byte) (b ^ bArr[i2]);
54         }
55         return b;
56     }
57
58     public byte @Nullable [] getData() {
59         byte[] data = new byte[17];
60         System.arraycopy(payload, 2, data, 0, Math.min(payload.length - 2, 17));
61         return data;
62     }
63
64     @Override
65     public byte[] getPayload() {
66         return payload;
67     }
68 }