]> git.basschouten.com Git - openhab-addons.git/blob
c96750b1b206a59c7be3ea63a04bbc8fa57360a9
[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
66      * {@link org.openhab.binding.novafinedust.internal.sds011protocol.Command}) as a form of some sub command
67      *
68      * @return first byte from the data section of a reply
69      */
70     public byte getFirstDataByte() {
71         return this.payLoad[0];
72     }
73
74     protected byte calculateChecksum() {
75         byte sum = 0;
76         for (byte b : payLoad) {
77             sum += b;
78         }
79         for (byte b : deviceID) {
80             sum += b;
81         }
82         return sum;
83     }
84
85     @Override
86     public String toString() {
87         return String.format("GeneralReply: [head=%x, commandID=%x, payload=%s, deviceID=%s, checksum=%s, tail=%x",
88                 header, commandID, HexUtils.bytesToHex(payLoad), HexUtils.bytesToHex(deviceID), checksum, messageTail);
89     }
90 }