2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.novafinedust.internal.sds011protocol.messages;
15 import java.util.Arrays;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.util.HexUtils;
21 * Base class holding information sent by the sensor to us
23 * @author Stefan Triller - Initial contribution
27 public class SensorReply {
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;
37 * Creates the container for data received from the sensor
39 * @param bytes the data received from the sensor
40 * @throws IllegalArgumentException Is thrown if less than 10 bytes are provided.
42 public SensorReply(byte[] bytes) {
43 if (bytes.length != 10) {
44 throw new IllegalArgumentException("was expecting 10 bytes, but received " + bytes.length);
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];
55 * Gets the commandID byte. However there is the first data byte which holds a kind of "sub command" that has to be
58 * @return byte representing the commandID
60 public byte getCommandID() {
61 return this.commandID;
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
68 * @return first byte from the data section of a reply
70 public byte getFirstDataByte() {
71 return this.payLoad[0];
74 protected byte calculateChecksum() {
76 for (byte b : payLoad) {
79 for (byte b : deviceID) {
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);