]> git.basschouten.com Git - openhab-addons.git/blob
36b4f0103961d494867de4092800ba36c4d1ddde
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.io.ByteArrayOutputStream;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.util.HexUtils;
19
20 /**
21  * Message to be send to the device
22  *
23  * @author Stefan Triller - Initial contribution
24  *
25  */
26 @NonNullByDefault
27 public class CommandMessage {
28     private static final byte HEAD = -86; // AA
29     private static final byte COMMAND_ID = -76; // B4
30     private static final byte TAIL = -85; // AB
31
32     private static final int DATA_BYTES_AFTER_FIRST_DATA_BYTE = 12;
33
34     private final byte firstDataByte;
35     private byte[] payLoad = new byte[DATA_BYTES_AFTER_FIRST_DATA_BYTE];
36     private byte[] targetDevice = new byte[] { -1, -1 }; // FF FF = all devices
37
38     public CommandMessage(byte command, byte[] payLoad) {
39         this.firstDataByte = command;
40         this.payLoad = payLoad;
41     }
42
43     public CommandMessage(byte command, byte[] payLoad, byte[] targetDevice) {
44         this.firstDataByte = command;
45         this.payLoad = payLoad;
46         this.targetDevice = targetDevice;
47     }
48
49     /**
50      * Get the raw bytes to be send out to the device
51      *
52      * @return ByteArray containing the bytes for a message to the device
53      */
54     public byte[] getBytes() {
55         ByteArrayOutputStream message = new ByteArrayOutputStream(19);
56
57         message.write(HEAD);
58         message.write(COMMAND_ID);
59         message.write(firstDataByte);
60
61         for (byte b : payLoad) {
62             message.write(b);
63         }
64         int padding = DATA_BYTES_AFTER_FIRST_DATA_BYTE - payLoad.length;
65         for (int i = 0; i < padding; i++) {
66             message.write(0x00);
67         }
68
69         for (byte b : targetDevice) {
70             message.write(b);
71         }
72         message.write(calculateCheckSum(message.toByteArray()));
73         message.write(TAIL);
74
75         return message.toByteArray();
76     }
77
78     private byte calculateCheckSum(byte[] data) {
79         int checksum = 0;
80         for (int i = 2; i <= 14; i++) {
81             checksum += data[i];
82         }
83         checksum = (checksum - 2) % 256;
84
85         return (byte) checksum;
86     }
87
88     @Override
89     public String toString() {
90         StringBuilder sb = new StringBuilder();
91         sb.append("Message: ");
92         sb.append("Command=" + firstDataByte);
93         sb.append(" Target Device=" + HexUtils.bytesToHex(targetDevice));
94         sb.append(" Payload=" + HexUtils.bytesToHex(payLoad));
95         return sb.toString();
96     }
97 }