Commit 8cb7be3cb3ec639d88a869404db2bb2c17deb030

Authored by 黄浪
1 parent 39eb8a55
Exists in develop

1、按照原有的hardware代码,修改RBM、LTE、QRB类型设备client端指令发送编码

src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientFactory.java
... ... @@ -3,6 +3,10 @@ package com.example.mina.client.box.lte3000;
3 3 import com.example.mina.client.base.AbstractClientFactory;
4 4 import com.example.mina.client.base.AbstractMatrixIoHandler;
5 5 import com.example.mina.client.base.MatrixConstants;
  6 +import com.example.mina.client.box.aeroflex.AeroFlexProtocolFactory;
  7 +import com.example.mina.client.box.aeroflex.AeroflexClientIoHandler;
  8 +import org.apache.mina.core.filterchain.IoFilterChain;
  9 +import org.apache.mina.filter.codec.ProtocolCodecFilter;
6 10 import org.springframework.stereotype.Component;
7 11  
8 12 @Component(MatrixConstants.MATRIX_TYPE_LTE3000)
... ... @@ -10,6 +14,12 @@ public class Lte3000ClientFactory extends AbstractClientFactory {
10 14  
11 15 @Override
12 16 public AbstractMatrixIoHandler getClientHandler() {
13   - return null;
  17 + System.out.println("=============getClientHandler==================");
  18 + return new Lte3000ClientIoHandler();
  19 + }
  20 +
  21 + @Override
  22 + public void buildFilterChain(IoFilterChain ioFilterChain) {
  23 + ioFilterChain.addLast("codec", new ProtocolCodecFilter(new Lte3000ProtocolFactory()));
14 24 }
15 25 }
... ...
src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java
... ... @@ -5,6 +5,7 @@ import com.example.mina.client.base.MatrixDataProxy;
5 5 import com.example.mina.client.base.MatrixResponse;
6 6 import com.example.mina.server.util.Lte3000CommandHelper;
7 7 import lombok.extern.slf4j.Slf4j;
  8 +import org.apache.mina.core.session.IoSession;
8 9  
9 10 /**
10 11 * @author dy
... ... @@ -13,8 +14,8 @@ import lombok.extern.slf4j.Slf4j;
13 14 @Slf4j
14 15 public class Lte3000ClientIoHandler extends AbstractMatrixIoHandler {
15 16  
16   - public Lte3000ClientIoHandler(MatrixDataProxy matrixDataProxy) {
17   - super(matrixDataProxy);
  17 + public Lte3000ClientIoHandler() {
  18 + super(new MatrixDataProxy());
18 19 }
19 20  
20 21 protected boolean isSameValue(byte value, int expect) {
... ... @@ -23,84 +24,11 @@ public class Lte3000ClientIoHandler extends AbstractMatrixIoHandler {
23 24  
24 25 @Override
25 26 public boolean handleCommandResponse(MatrixResponse matrixResponse) {
26   -// int responseLen = response.length;
27   -// if (response[0] != Lte3000CommandHelper.ACK || responseLen < 6) {
28   -// return false;
29   -// }
30   -// //api document: "SRM - MRF - MRM - XRM - QRM - LTE Protocol v2.15 Rev FR.pdf"
31   -//
32   -// //O:Query Output Channel P.106
33   -// //0x4F
34   -// if (responseLen == 9 && isSameValue(response[3], 0x4F)) { // 'O'
35   -//// 0x6 0x46 0x46 0x4F 0x30 0x32 0x38 0x3 0x70
36   -// int col = Lte3000CommandHelper.getBcdPort(response[4], response[5], response[6]);
37   -// int row = Lte3000CommandHelper.getBcdPort(response[4], response[5], response[6]);
38   -// // limits only one set "ON" in a column, others should be OFF
39   -//// for (int i = 1; i <= responsePool.getMaxRow(); i++) {
40   -//// if (i == row) {
41   -//// responsePool.setAttenuation(i, col, 1);
42   -//// } else {
43   -//// responsePool.setAttenuation(i, col, 0);
44   -//// }
45   -//// } TODO 再次调用MatrixDataProxy的方法来设置衰减值
46   -// log.info("RESPONSE Ox4F. ROW = {}, COL = {}", row, col);
47   -// return true;
48   -// }
49   -//
50   -// //S: Set CrossPoint P.115
51   -// //0x53
52   -// if (responseLen == 6 && isSameValue(response[3], 0x53)) {
53   -// int col = Lte3000CommandHelper.getBcdPort(response[4], response[5], response[6]);
54   -// int row = Lte3000CommandHelper.getBcdPort(response[7], response[8], response[9]);
55   -// // limits only one set "ON" in a column, others should be OFF
56   -//// for (int i = 1; i <= responsePool.getMaxRow(); i++) {
57   -//// if (i == row) {
58   -//// responsePool.setAttenuation(i, col, 1);
59   -//// } else {
60   -//// responsePool.setAttenuation(i, col, 0);
61   -//// }
62   -//// } TODO 再次调用MatrixDataProxy的方法来设置衰减值
63   -// log.info("RESPONSE Ox53. ROW = {}, COL = {}", row, col);
64   -// return true;
65   -// }
66   -//
67   -// //XGMO: Set Gain Control to Manual Mode - Output p.137
68   -// //0x58 0x47 0x4D 0x4F
69   -// if (responseLen == 9 && response[3] == 'X' && response[4] == 'G' && response[5] == 'M' && response[6] == 'O') {
70   -//// receive - 0x2 0x30 0x30 0x58 0x47 0x4D 0x4F 0x30 0x30 0x31 0x40 0x2D 0x36 0x33 0x2E 0x30 0x3 0x5B
71   -//// 00XGMO001@-63.0[
72   -//// set col/attn 1/63
73   -//
74   -// int col = Lte3000CommandHelper.getBcdPort(response[7], response[8], response[9]);
75   -// int attn = Lte3000CommandHelper.getBcdAttn(response[11], response[12], response[13], response[14], response[15]);
76   -// //responsePool.setOffset(col, attn); TODO 再次调用MatrixDataProxy的方法来设置衰减值
77   -// log.info("RESPONSE XGMO. ATTN = {}, COL = {}", attn, col);
78   -// return true;
79   -// }
80   -//
81   -//
82   -// //XGRO: Gain Control Read Status - Output P.139
83   -// if (responseLen == 16 && response[3] == 'X' && response[4] == 'G' && response[5] == 'R' && response[6] == 'O') {
84   -// /*
85   -// Example Command:
86   -// 02 XX XX 58 46 52 4F 30 31 36 03 XX
87   -// STX ADR ADR X G R O O 1 6 ETX CHK
88   -// (Read attenuation control status of system output 16)
89   -// */
90   -// /*
91   -// Example Response:
92   -// 06 XX XX 58 46 52 4F 4D 40 2D 31 33 2E 35 03 XX
93   -// ACK ADR ADR X G R O M @ - 1 3 . 5 ETX CHX
94   -// (Output set at -13.5 dB of attenuation in manual mode. )
95   -// */
96   -// int col = Lte3000CommandHelper.getBcdPort(response[7], response[8], response[9]);
97   -// int attn = Lte3000CommandHelper.getBcdAttn(response[9], response[10], response[11], response[12], response[13]);
98   -// //responsePool.setOffset(col, attn); TODO 再次调用MatrixDataProxy的方法来设置衰减值
99   -// log.info("RESPONSE XGRO. ATTN = {}, COL = {}", attn, col);
100   -// return true;
101   -// }
102   -//
103   -// log.info("收到服务器回传的消息了");
  27 + log.info("------Qrb3000ClientIoHandler-----{}",matrixResponse);
104 28 return false;
105 29 }
  30 +
  31 + @Override
  32 + public void sessionCreated(IoSession session) {}
  33 +
106 34 }
... ...
src/main/java/com/example/mina/client/box/lte3000/Lte3000CommandHelper.java 0 → 100644
... ... @@ -0,0 +1,157 @@
  1 +package com.example.mina.client.box.lte3000;
  2 +
  3 +import org.apache.commons.lang3.ArrayUtils;
  4 +
  5 +import java.nio.charset.StandardCharsets;
  6 +
  7 +/**
  8 + * @ProjectName: demo
  9 + * @Package: com.example.mina.client.box.lte3000
  10 + * @ClassName: Lte3000CommandHelper
  11 + * @Author: HuangLang
  12 + * @Description: Lte command helper
  13 + * @Date: 2021-04-02 下午 5:12
  14 + */
  15 +public class Lte3000CommandHelper {
  16 + public static final byte STX = 0X02;
  17 + public static final byte ETX = 0X03;
  18 + public static final byte ACK = 0X06;
  19 + public static final byte NAK = 0X15;
  20 +
  21 + public static void setCHK(byte[] bs) {
  22 + bs[bs.length - 1] = 0;
  23 +
  24 + for (int i = 0; i < bs.length - 1; i++) {
  25 + bs[bs.length - 1] ^= bs[i];
  26 + }
  27 + }
  28 +
  29 + public static int getBcdPort(byte a, byte b, byte c) {
  30 + String port = new StringBuilder().append(a - 0x30).append(b - 0x30).append(c - 0x30).toString();
  31 + return Integer.parseInt(port);
  32 + }
  33 +
  34 + /**
  35 + * input: "-12.2"
  36 + * @param a sign
  37 + * @param b tenth
  38 + * @param c single
  39 + * @param d dot
  40 + * @param e tenths
  41 + * @return int -12 if -12.2
  42 + */
  43 + public static int getBcdAttn(byte a, byte b, byte c, byte d, byte e) {
  44 + String port = new StringBuilder()
  45 + //.append((char)a) // sign -/+
  46 + .append(b - 0x30).append(c - 0x30) // integer
  47 + //.append((char)d) // .
  48 + //.append(e - 0x30)
  49 + .toString(); // tenths.
  50 + return Integer.parseInt(port);
  51 + }
  52 +
  53 + public static byte[] genCommandSetAttn(int col, int val){
  54 + byte[] command = new byte[18];
  55 + byte[] vCol = intToReversedBytes(col);
  56 + byte[] vVal = intToReversedBytes(val);
  57 + command[0] = STX;
  58 + command[1] = 0x30;
  59 + command[2] = 0x30;
  60 + command[3] = 'X';
  61 + command[4] = 'G';
  62 + command[5] = 'M';
  63 + command[6] = 'O';
  64 + command[7] = vCol.length > 2 ? vCol[2] : 0x30;
  65 + command[8] = vCol.length > 1 ? vCol[1] : 0x30;
  66 + command[9] = vCol.length > 0 ? vCol[0] : 0x30;
  67 + command[10] = '@';
  68 + command[11] = '-';
  69 + command[12] = vVal.length > 1 ? vVal[1] : 0x30;
  70 + command[13] = vVal.length > 0 ? vVal[0] : 0x30;
  71 + command[14] = '.';
  72 + command[15] = '0';
  73 + command[16] = ETX;
  74 + command[17] = 0;
  75 +
  76 + setCHK(command);
  77 +
  78 + return command;
  79 + }
  80 +
  81 + public static byte[] genCommandGetAttn(int col){
  82 + byte[] command = new byte[12];
  83 + byte[] vCol = intToReversedBytes(col);
  84 + command[0] = STX;
  85 + command[1] = 0x30;
  86 + command[2] = 0x30;
  87 + command[3] = 'X';
  88 + command[4] = 'G';
  89 + command[5] = 'R';
  90 + command[6] = 'O';
  91 + command[7] = vCol.length > 2 ? vCol[2] : 0x30;
  92 + command[8] = vCol.length > 1 ? vCol[1] : 0x30;
  93 + command[9] = vCol.length > 0 ? vCol[0] : 0x30;
  94 + command[10] = ETX;
  95 + command[11] = 0;
  96 +
  97 + setCHK(command);
  98 +
  99 + return command;
  100 + }
  101 +
  102 +
  103 + public static byte[] genCommandSetCross(int row, int col){
  104 + byte[] command = new byte[12];
  105 + byte[] vRow = intToReversedBytes(row);
  106 + byte[] vCol = intToReversedBytes(col);
  107 + command[0] = STX;
  108 + command[1] = 0x30;
  109 + command[2] = 0x30;
  110 + command[3] = 'S';
  111 + command[4] = vCol.length > 2 ? vCol[2] : 0x30;
  112 + command[5] = vCol.length > 1 ? vCol[1] : 0x30;
  113 + command[6] = vCol.length > 0 ? vCol[0] : 0x30;
  114 + command[7] = vRow.length > 2 ? vRow[2] : 0x30;
  115 + command[8] = vRow.length > 1 ? vRow[1] : 0x30;
  116 + command[9] = vRow.length > 0 ? vRow[0] : 0x30;
  117 + command[10] = ETX;
  118 + command[11] = 0;
  119 +
  120 + setCHK(command);
  121 +
  122 + return command;
  123 + }
  124 +
  125 + /**
  126 + * 1234 => "1234" => ['4','3','2','1']
  127 + * 1 => [31]
  128 + * 12 => [32,31]
  129 + * 123 => [33,32,31]
  130 + * @param value value to convert
  131 + * @return result
  132 + */
  133 + public static byte[] intToReversedBytes(int value) {
  134 + byte[] result = String.valueOf(value).getBytes(StandardCharsets.US_ASCII);
  135 + ArrayUtils.reverse(result);
  136 + return result;
  137 + }
  138 +
  139 + public static byte[] genCommandGetCross(int col){
  140 + byte[] command = new byte[9];
  141 + byte[] value = String.valueOf(col).getBytes(StandardCharsets.US_ASCII);
  142 + ArrayUtils.reverse(value);
  143 + command[0] = STX;
  144 + command[1] = 0x30;
  145 + command[2] = 0x30;
  146 + command[3] = 'O';
  147 + command[4] = value.length >= 3 ? value[2] : 0x30;
  148 + command[5] = value.length >= 2 ? value[1] : 0x30;
  149 + command[6] = value.length >= 1 ? value[0] : 0x30;
  150 + command[7] = ETX;
  151 + command[8] = 0;
  152 +
  153 + setCHK(command);
  154 +
  155 + return command;
  156 + }
  157 +}
... ...
src/main/java/com/example/mina/client/box/lte3000/Lte3000ProtocolFactory.java 0 → 100644
... ... @@ -0,0 +1,112 @@
  1 +package com.example.mina.client.box.lte3000;
  2 +
  3 +import com.example.mina.client.base.MatrixCommand;
  4 +import com.example.mina.client.base.MatrixConstants;
  5 +import com.example.mina.client.base.MatrixResponse;
  6 +import com.example.mina.processor.VbLogUtils;
  7 +import com.example.mina.server.util.CommandHelper;
  8 +import lombok.extern.slf4j.Slf4j;
  9 +import org.apache.mina.core.buffer.IoBuffer;
  10 +import org.apache.mina.core.session.IoSession;
  11 +import org.apache.mina.filter.codec.*;
  12 +
  13 +import java.nio.charset.CharacterCodingException;
  14 +import java.nio.charset.CharsetDecoder;
  15 +import java.nio.charset.CharsetEncoder;
  16 +import java.nio.charset.StandardCharsets;
  17 +
  18 +@Slf4j
  19 +public class Lte3000ProtocolFactory implements ProtocolCodecFactory {
  20 +
  21 + private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder();
  22 +
  23 + private static final CharsetEncoder ENCODER = StandardCharsets.UTF_8.newEncoder();
  24 +
  25 + @Override
  26 + public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
  27 + return new Lte3000ProtocolEncoder();
  28 + }
  29 +
  30 + @Override
  31 + public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
  32 + return new Lte3000ProtocolDecoder();
  33 + }
  34 +
  35 + public static class Lte3000ProtocolEncoder extends ProtocolEncoderAdapter {
  36 +
  37 + @Override
  38 + public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput)
  39 + throws CharacterCodingException {
  40 +
  41 + if (!(msg instanceof MatrixCommand)) {
  42 + log.error("error msg, msg is: {}", msg);
  43 + return;
  44 + }
  45 +
  46 + MatrixCommand mc = (MatrixCommand) msg;
  47 + log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {},type = {}",
  48 + mc.getAttn(),
  49 + mc.getCol(),
  50 + mc.getCommand(),
  51 + mc.getRow(),
  52 + mc.getMatrixId()
  53 + );
  54 + int cmd = mc.getCommand();
  55 + VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg);
  56 +
  57 + IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
  58 +
  59 + int row = mc.getRow();
  60 + byte[] command;
  61 + if (row == 0) {
  62 + command = Lte3000CommandHelper.genCommandSetAttn(mc.getCol(), mc.getAttn());
  63 + }else{
  64 + command = Lte3000CommandHelper.genCommandSetCross(row, mc.getCol());
  65 + }
  66 +
  67 + buffer.put(command);
  68 + buffer.flip();
  69 + protocolEncoderOutput.write(buffer);
  70 + }
  71 + }
  72 +
  73 + public static class Lte3000ProtocolDecoder extends ProtocolDecoderAdapter {
  74 +
  75 + @Override
  76 + public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput)
  77 + throws Exception {
  78 + log.info("--server返回给----client ------IoBuffer{}", ioBuffer);
  79 + MatrixResponse response = new MatrixResponse();
  80 + int len = ioBuffer.limit();
  81 + byte[] bytes = new byte[len];
  82 + ioBuffer.get(bytes);
  83 + int cmd = bytes[0];
  84 + if(cmd == 0x30 && len==8) {
  85 + int row = CommandHelper.BCD_REV[bytes[3]];
  86 + int col = CommandHelper.BCD_REV[bytes[4]];
  87 + int attn = bytes[5];
  88 +
  89 + response.setRow(row);
  90 + response.setCol(col);
  91 + response.setIsSetAttn(true);
  92 + response.setAttn(attn);
  93 +
  94 + } else if (cmd == 0x68 && len ==8) {
  95 + int offset = bytes[6];
  96 + int col = CommandHelper.BCD_REV[bytes[3]];
  97 + response.setIsSetOffset(true);
  98 + response.setCol(col);
  99 + response.setOffset(offset);
  100 + } else {
  101 + int offset = bytes[6];
  102 + response.setOffset(offset);
  103 + }
  104 + protocolDecoderOutput.write(response);
  105 + }
  106 +
  107 + }
  108 +
  109 +
  110 +
  111 +
  112 +}
... ...
src/main/java/com/example/mina/client/box/qrb3000/Qrb3000CommandHelper.java 0 → 100644
... ... @@ -0,0 +1,186 @@
  1 +package com.example.mina.client.box.qrb3000;
  2 +
  3 +import com.example.mina.server.util.CommandHelper;
  4 +
  5 +/**
  6 + * @ProjectName: demo
  7 + * @Package: com.example.mina.client.box.qrb3000
  8 + * @ClassName: Qrb3000CommandHelper
  9 + * @Author: HuangLang
  10 + * @Description: Qrb command helper
  11 + * @Date: 2021-04-02 下午 5:55
  12 + */
  13 +public class Qrb3000CommandHelper {
  14 +
  15 + public static byte[] genCommand30(int row, int col, int val){
  16 + byte[] command = new byte[8];
  17 +
  18 + command[0] = 0x30;
  19 + command[1] = 0x40;
  20 + command[2] = 0x00;
  21 + command[3] = CommandHelper.BCD[row];
  22 + command[4] = CommandHelper.BCD[col];
  23 + command[5] = (byte)val;
  24 + command[6] = 0x00;
  25 + command[7] = 0x00;
  26 +
  27 + return command;
  28 + }
  29 +
  30 + public static byte[] genCommand31(int row, int col, int val){
  31 + byte[] command = new byte[8];
  32 +
  33 + command[0] = 0x31;
  34 + command[1] = 0x40;
  35 + command[2] = 0x00;
  36 + command[3] = CommandHelper.BCD[row];
  37 + command[4] = CommandHelper.BCD[col];
  38 + command[5] = (byte)val;
  39 + command[6] = 0x00;
  40 + command[7] = 0x00;
  41 +
  42 + return command;
  43 + }
  44 +
  45 +
  46 + public static byte[] genCommand32(){
  47 + byte[] command = new byte[8];
  48 +
  49 + command[0] = 0x32;
  50 + command[1] = 0x00;
  51 + command[2] = 0x00;
  52 + command[3] = 0x00;
  53 + command[4] = 0x00;
  54 + command[5] = 0x00;
  55 + command[6] = 0x00;
  56 + command[7] = 0x00;
  57 +
  58 + return command;
  59 + }
  60 +
  61 +
  62 +
  63 + public static byte[] genCommand4D(byte val){
  64 + byte[] command = new byte[1028];
  65 +
  66 + command[0] = 0x4D;
  67 + command[1] = 0x40;
  68 + command[2] = 0x00;
  69 +
  70 + for(int i = 0; i < 1024; i++) {
  71 + command[ 3 + i] = val;
  72 + }
  73 + command[1027] = 0x00;
  74 +
  75 +
  76 + return command;
  77 + }
  78 +
  79 + public static byte[] genCommand62(int row){
  80 + byte[] command = new byte[8];
  81 +
  82 + command[0] = 0x62;
  83 + command[1] = 0x40;
  84 + command[2] = 0x00;
  85 + command[3] = CommandHelper.BCD[row];
  86 + command[4] = 0x00;
  87 + command[5] = 0x00;
  88 + command[6] = 0x00;
  89 + command[7] = 0x00;
  90 +
  91 + return command;
  92 + }
  93 +
  94 + public static byte[] genCommand68(int row){
  95 + byte[] command = new byte[8];
  96 +
  97 + command[0] = 0x68;
  98 + command[1] = 0x40;
  99 + command[2] = 0x00;
  100 + command[3] = CommandHelper.BCD[row];
  101 + command[4] = 0x00;
  102 + command[5] = 0x00;
  103 + command[6] = 0x00;
  104 + command[7] = 0x00;
  105 +
  106 + return command;
  107 + }
  108 +
  109 + @Deprecated
  110 + public static int parseCommand68_Response(byte[] b){//TODO : need double check
  111 + int b7 = 0xFF & b[6];
  112 + int b8 = 0xFF & b[7];
  113 +
  114 + int rec = 0;
  115 +
  116 + if(b7 == 0xFE || b7 == 0xFF || b8 == 0xEE){
  117 + rec = -99;
  118 + }
  119 +// else if(b7 ==0xF9){
  120 +// rec = 31;
  121 +// }
  122 + else if(b7 == 0x80){
  123 + rec = 0;
  124 + }
  125 + else if(b7 < 0x80){
  126 + rec = (b7 / 16) * 10 + (0x0F & b7);
  127 + }
  128 + else{
  129 + b7= 0x7f & b7;
  130 + rec = - ( (b7 / 16) * 10 + (0x0F & b7));
  131 + }
  132 +
  133 + return rec;
  134 + }
  135 +
  136 + public static byte[] genCommand4F(int row){
  137 + byte[] command = new byte[8];
  138 +
  139 + command[0] = 0x4F;
  140 + command[1] = 0x40;
  141 + command[2] = 0x00;
  142 + command[3] = CommandHelper.BCD[row];
  143 + command[4] = 0x00;
  144 + command[5] = 0x00;
  145 + command[6] = 0x00;
  146 + command[7] = 0x00;
  147 +
  148 + return command;
  149 + }
  150 +
  151 + public static byte[] genCommandEA(){
  152 + byte[] command = new byte[8];
  153 +
  154 + int b = 0xEA;
  155 +
  156 + command[0] = (byte)b;
  157 + command[1] = 0x40;
  158 + command[2] = 0x00;
  159 + command[3] = 0x00;
  160 + command[4] = 0x00;
  161 + command[5] = 0x00;
  162 + command[6] = 0x00;
  163 + command[7] = 0x00;
  164 +
  165 + return command;
  166 + }
  167 +
  168 +
  169 + public static byte[] genCommandEE(){
  170 + byte[] command = new byte[8];
  171 +
  172 + int b = 0xEE;
  173 +
  174 + command[0] = (byte)b;
  175 + command[1] = 0x40;
  176 + command[2] = 0x00;
  177 + command[3] = 0x00;
  178 + command[4] = 0x00;
  179 + command[5] = 0x00;
  180 + command[6] = 0x00;
  181 + command[7] = 0x00;
  182 +
  183 + return command;
  184 + }
  185 +
  186 +}
... ...
src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java
... ... @@ -52,39 +52,48 @@ public class Qrb3000ProtocolFactory implements ProtocolCodecFactory {
52 52 mc.getMatrixId()
53 53 );
54 54 int cmd = mc.getCommand();
  55 + int row = mc.getRow();
  56 + int col = mc.getCol();
  57 + int attn = mc.getAttn();
55 58 VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg);
56 59  
57 60 IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
58 61  
  62 + byte[] command;
  63 +
59 64 if (cmd == MatrixConstants.COMMAND_SET_ATTN) {
60   - buffer.put((byte)0x30);
61   - buffer.put((byte)0xFE);
62   - buffer.put((byte)0xFE);
63   - buffer.put(mc.getRow().byteValue());
64   - buffer.put(mc.getCol().byteValue());
65   - buffer.put(mc.getAttn().byteValue());
66   - buffer.put((byte)0xFE);
67   - buffer.put((byte)0xFE);
  65 +// buffer.put((byte)0x30);
  66 +// buffer.put((byte)0xFE);
  67 +// buffer.put((byte)0xFE);
  68 +// buffer.put(mc.getRow().byteValue());
  69 +// buffer.put(mc.getCol().byteValue());
  70 +// buffer.put(mc.getAttn().byteValue());
  71 +// buffer.put((byte)0xFE);
  72 +// buffer.put((byte)0xFE);
  73 + command = QrbCommandHelper.genSetAttenuation(row, col, attn);
68 74 }
69 75 if (cmd == MatrixConstants.COMMAND_SET_OFFSET) {
70   - buffer.put((byte)0x68);
71   - buffer.put((byte)0xFE);
72   - buffer.put((byte)0xFE);
73   - buffer.put(mc.getCol().byteValue());
74   - buffer.put(mc.getOffset().byteValue());
75   - buffer.put((byte)0xFE);
76   - buffer.put((byte)0xFE);
77   - buffer.put((byte)0xFE);
  76 + // 0x68
  77 + command = Qrb3000CommandHelper.genCommand30(row, col, attn);
  78 +// buffer.put((byte)0x68);
  79 +// buffer.put((byte)0xFE);
  80 +// buffer.put((byte)0xFE);
  81 +// buffer.put(mc.getCol().byteValue());
  82 +// buffer.put(mc.getOffset().byteValue());
  83 +// buffer.put((byte)0xFE);
  84 +// buffer.put((byte)0xFE);
  85 +// buffer.put((byte)0xFE);
78 86  
79 87 }
80 88 if (cmd == MatrixConstants.COMMAND_GET_OFFSET) {
81   - buffer.put((byte)0xFE);
82   - buffer.put((byte)0xFE);
83   - buffer.put((byte)0xFE);
84   - buffer.put(mc.getCol().byteValue());
85   - buffer.put((byte)0xFE);
86   - buffer.put((byte)0xFE);
87   - buffer.put((byte)0xFE);
  89 + command = QrbCommandHelper.genSetAttenuation(row, col, attn);
  90 +// buffer.put((byte)0xFE);
  91 +// buffer.put((byte)0xFE);
  92 +// buffer.put((byte)0xFE);
  93 +// buffer.put(mc.getCol().byteValue());
  94 +// buffer.put((byte)0xFE);
  95 +// buffer.put((byte)0xFE);
  96 +// buffer.put((byte)0xFE);
88 97 }
89 98  
90 99 buffer.flip();
... ...
src/main/java/com/example/mina/client/box/qrb3000/QrbCommandHelper.java 0 → 100644
... ... @@ -0,0 +1,110 @@
  1 +package com.example.mina.client.box.qrb3000;
  2 +
  3 +import org.apache.commons.lang3.ArrayUtils;
  4 +
  5 +import java.nio.charset.StandardCharsets;
  6 +import java.util.ArrayList;
  7 +import java.util.Optional;
  8 +import java.util.stream.Stream;
  9 +
  10 +/**
  11 + * @ProjectName: demo
  12 + * @Package: com.example.mina.client.box.qrb3000
  13 + * @ClassName: QrbCommandHelper
  14 + * @Author: HuangLang
  15 + * @Description: QrbCommandHelper
  16 + * @Date: 2021-04-02 下午 6:13
  17 + */
  18 +public class QrbCommandHelper {
  19 +
  20 + public static final byte STX = 0x02;
  21 + public static final byte ETX = 0x03;
  22 +
  23 + /**
  24 + * 1234 => "1234" => ['4','3','2','1']
  25 + * 1 => [31]
  26 + * 12 => [32,31]
  27 + * 123 => [33,32,31]
  28 + * @param value value to convert
  29 + * @return result
  30 + */
  31 + public static byte[] intToReversedBytes(int value) {
  32 + byte[] result = String.valueOf(value).getBytes(StandardCharsets.US_ASCII);
  33 + ArrayUtils.reverse(result);
  34 + return result;
  35 + }
  36 +
  37 + public static byte[] addChecksumAndGenerateCommand(ArrayList<Byte> bytes) {
  38 + Optional<Byte> checksum = bytes.stream().reduce((x, y) -> (byte) (x ^ y));
  39 + checksum.ifPresent(bytes::add);
  40 + byte[] result = new byte[bytes.size()];
  41 + for(int i = 0; i < bytes.size(); i ++) result[i] = bytes.get(i);
  42 + return result;
  43 + }
  44 +
  45 + /**
  46 + * XRI: RF Input Signal Level Check
  47 + * @param row starts from 1
  48 + * @return a command bytes
  49 + */
  50 + public static byte[] genInputSignalLevelCheck(int row){
  51 + ArrayList<Byte> command = new ArrayList<Byte>();
  52 + byte[] vRow = intToReversedBytes(row);
  53 +
  54 + command.add(STX);
  55 + command.add((byte)0x30);
  56 + command.add((byte)0x30);
  57 + command.add((byte)0x58); // X
  58 + command.add((byte)0x52); // R
  59 + command.add((byte)0x49); // I
  60 + command.add(vRow.length > 2 ? vRow[2] : (byte)0x30);
  61 + command.add(vRow.length > 1 ? vRow[1] : (byte)0x30);
  62 + command.add(vRow.length > 0 ? vRow[0] : (byte)0x30);
  63 + command.add(ETX);
  64 +
  65 + return addChecksumAndGenerateCommand(command);
  66 +
  67 + }
  68 +
  69 + /**
  70 + * Not documented, from logs
  71 + * Request: set attenuation 13, 19
  72 + * 0x02 0x30 0x30 0x58 0x41 0x57 0x41 0x30 0x32 0x39 0x42 0x30 0x31 0x39 0x30 0x30 0x30 0x03 0x7f
  73 + * Request: set attenuation 13, 19
  74 + * 0x02 0x30 0x30 0x58 0x41 0x57 0x41 0x30 0x30 0x31 0x42 0x30 0x30 0x31 0x36 0x33 0x30 0x03 0x79
  75 + * Response:
  76 + * N/A
  77 + * @param row starts from 1
  78 + * @return a command bytes
  79 + */
  80 + public static byte[] genSetAttenuation(int row, int col, int attn){
  81 + ArrayList<Byte> command = new ArrayList<Byte>();
  82 + byte[] vRow = intToReversedBytes(row);
  83 + byte[] vCol = intToReversedBytes(col);
  84 + byte[] vValue = intToReversedBytes(attn);
  85 +
  86 + command.add(STX);
  87 + command.add((byte)0x30);
  88 + command.add((byte)0x30);
  89 + command.add((byte)0x58); // X
  90 + command.add((byte)0x41); // A
  91 + command.add((byte)0x57); // W
  92 + command.add((byte)0x41); // A
  93 + command.add(vRow.length > 2 ? vRow[2] : (byte)0x30);
  94 + command.add(vRow.length > 1 ? vRow[1] : (byte)0x30);
  95 + command.add(vRow.length > 0 ? vRow[0] : (byte)0x30);
  96 + command.add((byte)0x42); // B
  97 + command.add(vCol.length > 2 ? vCol[2] : (byte)0x30);
  98 + command.add(vCol.length > 1 ? vCol[1] : (byte)0x30);
  99 + command.add(vCol.length > 0 ? vCol[0] : (byte)0x30);
  100 + command.add(vValue.length > 1 ? vValue[1] : (byte)0x30);
  101 + command.add(vValue.length > 0 ? vValue[0] : (byte)0x30);
  102 + command.add((byte)0x30); // no decimal
  103 + command.add(ETX);
  104 +
  105 + return addChecksumAndGenerateCommand(command);
  106 +
  107 + }
  108 +
  109 +
  110 +}
... ...
src/main/java/com/example/mina/client/box/rbm3000/Rbm3000CommandHelper.java 0 → 100644
... ... @@ -0,0 +1,67 @@
  1 +package com.example.mina.client.box.rbm3000;
  2 +
  3 +import com.example.mina.server.util.CommandHelper;
  4 +
  5 +/**
  6 + * @ProjectName: demo
  7 + * @Package: com.example.mina.client.box.rbm3000
  8 + * @ClassName: Rbm3000CommandHelper
  9 + * @Author: HuangLang
  10 + * @Description: Rbm command helper
  11 + * @Date: 2021-04-02 下午 5:30
  12 + */
  13 +public class Rbm3000CommandHelper {
  14 +
  15 + public static byte[] genCommand30(int row, int col){
  16 + byte[] command = new byte[8];
  17 +
  18 + command[0] = 0x30;
  19 + command[1] = 0x50;
  20 + command[2] = 0x00;
  21 + command[3] = CommandHelper.BCD[row];
  22 + command[4] = CommandHelper.BCD[col];
  23 + command[5] = 0x00;
  24 + command[6] = 0x00;
  25 + command[7] = 0x00;
  26 +
  27 + return command;
  28 + }
  29 +
  30 +
  31 + public static byte[] genCommand68(boolean isA, int port){
  32 + byte[] command = new byte[8];
  33 +
  34 + command[0] = 0x68;
  35 + command[1] = 0x50;
  36 + command[2] = 0x00;
  37 + if(isA){
  38 + command[3] = 0x0A;
  39 + }
  40 + else{
  41 + command[3] = 0x0B;
  42 + }
  43 +
  44 + command[4] = CommandHelper.BCD[port];
  45 + command[5] = 0x00;
  46 + command[6] = 0x00;
  47 + command[7] = 0x00;
  48 +
  49 + return command;
  50 + }
  51 +
  52 +
  53 + public static byte[] genCommand62(){
  54 + byte[] command = new byte[8];
  55 +
  56 + command[0] = 0x62;
  57 + command[1] = 0x50;
  58 + command[2] = 0x00;
  59 + command[3] = 0x00;
  60 + command[4] = 0x00;
  61 + command[5] = 0x00;
  62 + command[6] = 0x00;
  63 + command[7] = 0x00;
  64 +
  65 + return command;
  66 + }
  67 +}
... ...
src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ProtocolFactory.java
... ... @@ -56,17 +56,12 @@ public class Rbm3000ProtocolFactory implements ProtocolCodecFactory {
56 56  
57 57 IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
58 58  
  59 + byte[] comand = null;
59 60 // Len==8 cmd【0】 0x30 set row/col attn=1
60 61 if (cmd == MatrixConstants.COMMAND_SET_ATTN) {
61   - buffer.put((byte)0x30);
62   - buffer.put((byte)0xFE);
63   - buffer.put((byte)0xFE);
64   - buffer.put(mc.getRow().byteValue());
65   - buffer.put(mc.getCol().byteValue());
66   - buffer.put((byte)0xFE);
67   - buffer.put((byte)0xFE);
68   - buffer.put((byte)0xFE);
  62 + comand = Rbm3000CommandHelper.genCommand30(mc.getRow(), mc.getCol());
69 63 }else if (cmd == MatrixConstants.COMMAND_GET_ATTN) {
  64 + // todo 业务需求需要进一步明确
70 65 buffer.put((byte)0x68);
71 66 buffer.put((byte)0xFE);
72 67 buffer.put((byte)0xFE);
... ... @@ -84,16 +79,10 @@ public class Rbm3000ProtocolFactory implements ProtocolCodecFactory {
84 79 buffer.put((byte)0xFE);
85 80  
86 81 } else {
87   - buffer.put((byte)0x62);
88   - buffer.put((byte)0xFE);
89   - buffer.put((byte)0xFE);
90   - buffer.put((byte)0xFE);
91   - buffer.put((byte)0xFE);
92   - buffer.put((byte)0xFE);
93   - buffer.put((byte)0xFE);
94   - buffer.put((byte)0xFE);
  82 + comand = Rbm3000CommandHelper.genCommand62();
95 83 }
96 84  
  85 + buffer.put(comand);
97 86 buffer.flip();
98 87 protocolEncoderOutput.write(buffer);
99 88 }
... ...