]> git.basschouten.com Git - openhab-addons.git/blob
0fe14406c91b2f79a176babfa04b12d30e9b3896
[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.novafinedust.internal.sds011protocol.messages;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.util.HexUtils;
19
20 /**
21  * Base class holding information sent by the sensor to us
22  *
23  * @author Stefan Triller - Initial contribution
24  *
25  */
26 @NonNullByDefault
27 public class SensorReply {
28
29     protected final byte header;
30     protected final byte commandID;
31     protected final byte[] payLoad;
32     protected final byte[] deviceID;
33     protected final byte checksum;
34     protected final byte messageTail;
35
36     /**
37      * Creates the container for data received from the sensor
38      *
39      * @param bytes the data received from the sensor
40      * @throws IllegalArgumentException Is thrown if less than 10 bytes are provided.
41      */
42     public SensorReply(byte[] bytes) {
43         if (bytes.length != 10) {
44             throw new IllegalArgumentException("was expecting 10 bytes, but received " + bytes.length);
45         }
46         this.header = bytes[0];
47         this.commandID = bytes[1];
48         this.payLoad = Arrays.copyOfRange(bytes, 2, 6);
49         this.deviceID = Arrays.copyOfRange(bytes, 6, 8);
50         this.checksum = bytes[8];
51         this.messageTail = bytes[9];
52     }
53
54     /**
55      * Gets the commandID byte. However there is the first data byte which holds a kind of "sub command" that has to be
56      * evaluated too
57      *
58      * @return byte representing the commandID
59      */
60     public byte getCommandID() {
61         return this.commandID;
62     }
63
64     /**
65      * Gets the first byte from the data bytes (usually holds the {@link Command}) as a form of some sub command
66      *
67      * @return first byte from the data section of a reply
68      */
69     public byte getFirstDataByte() {
70         return this.payLoad[0];
71     }
72
73     protected byte calculateChecksum() {
74         byte sum = 0;
75         for (byte b : payLoad) {
76             sum += b;
77         }
78         for (byte b : deviceID) {
79             sum += b;
80         }
81         return sum;
82     }
83
84     @Override
85     public String toString() {
86         return String.format("GeneralReply: [head=%x, commandID=%x, payload=%s, deviceID=%s, checksum=%s, tail=%x",
87                 header, commandID, HexUtils.bytesToHex(payLoad), HexUtils.bytesToHex(deviceID), checksum, messageTail);
88     }
89 }