QrbCommandHelper.java
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.example.mina.client.box.qrb3000;
import org.apache.commons.lang3.ArrayUtils;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Stream;
/**
* @ProjectName: demo
* @Package: com.example.mina.client.box.qrb3000
* @ClassName: QrbCommandHelper
* @Author: HuangLang
* @Description: QrbCommandHelper
* @Date: 2021-04-02 下午 6:13
*/
public class QrbCommandHelper {
public static final byte STX = 0x02;
public static final byte ETX = 0x03;
/**
* 1234 => "1234" => ['4','3','2','1']
* 1 => [31]
* 12 => [32,31]
* 123 => [33,32,31]
* @param value value to convert
* @return result
*/
public static byte[] intToReversedBytes(int value) {
byte[] result = String.valueOf(value).getBytes(StandardCharsets.US_ASCII);
ArrayUtils.reverse(result);
return result;
}
public static byte[] addChecksumAndGenerateCommand(ArrayList<Byte> bytes) {
Optional<Byte> checksum = bytes.stream().reduce((x, y) -> (byte) (x ^ y));
checksum.ifPresent(bytes::add);
byte[] result = new byte[bytes.size()];
for(int i = 0; i < bytes.size(); i ++) result[i] = bytes.get(i);
return result;
}
/**
* XRI: RF Input Signal Level Check
* @param row starts from 1
* @return a command bytes
*/
public static byte[] genInputSignalLevelCheck(int row){
ArrayList<Byte> command = new ArrayList<Byte>();
byte[] vRow = intToReversedBytes(row);
command.add(STX);
command.add((byte)0x30);
command.add((byte)0x30);
command.add((byte)0x58); // X
command.add((byte)0x52); // R
command.add((byte)0x49); // I
command.add(vRow.length > 2 ? vRow[2] : (byte)0x30);
command.add(vRow.length > 1 ? vRow[1] : (byte)0x30);
command.add(vRow.length > 0 ? vRow[0] : (byte)0x30);
command.add(ETX);
return addChecksumAndGenerateCommand(command);
}
/**
* Not documented, from logs
* Request: set attenuation 13, 19
* 0x02 0x30 0x30 0x58 0x41 0x57 0x41 0x30 0x32 0x39 0x42 0x30 0x31 0x39 0x30 0x30 0x30 0x03 0x7f
* Request: set attenuation 13, 19
* 0x02 0x30 0x30 0x58 0x41 0x57 0x41 0x30 0x30 0x31 0x42 0x30 0x30 0x31 0x36 0x33 0x30 0x03 0x79
* Response:
* N/A
* @param row starts from 1
* @return a command bytes
*/
public static byte[] genSetAttenuation(int row, int col, int attn){
ArrayList<Byte> command = new ArrayList<Byte>();
byte[] vRow = intToReversedBytes(row);
byte[] vCol = intToReversedBytes(col);
byte[] vValue = intToReversedBytes(attn);
command.add(STX);
command.add((byte)0x30);
command.add((byte)0x30);
command.add((byte)0x58); // X
command.add((byte)0x41); // A
command.add((byte)0x57); // W
command.add((byte)0x41); // A
command.add(vRow.length > 2 ? vRow[2] : (byte)0x30);
command.add(vRow.length > 1 ? vRow[1] : (byte)0x30);
command.add(vRow.length > 0 ? vRow[0] : (byte)0x30);
command.add((byte)0x42); // B
command.add(vCol.length > 2 ? vCol[2] : (byte)0x30);
command.add(vCol.length > 1 ? vCol[1] : (byte)0x30);
command.add(vCol.length > 0 ? vCol[0] : (byte)0x30);
command.add(vValue.length > 1 ? vValue[1] : (byte)0x30);
command.add(vValue.length > 0 ? vValue[0] : (byte)0x30);
command.add((byte)0x30); // no decimal
command.add(ETX);
return addChecksumAndGenerateCommand(command);
}
}