From 8cb7be3cb3ec639d88a869404db2bb2c17deb030 Mon Sep 17 00:00:00 2001 From: huanglang <1037084308@qq.com> Date: Fri, 2 Apr 2021 19:45:32 +0800 Subject: [PATCH] 1、按照原有的hardware代码,修改RBM、LTE、QRB类型设备client端指令发送编码 --- src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientFactory.java | 12 +++++++++++- src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java | 88 ++++++++-------------------------------------------------------------------------------- src/main/java/com/example/mina/client/box/lte3000/Lte3000CommandHelper.java | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/example/mina/client/box/lte3000/Lte3000ProtocolFactory.java | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/example/mina/client/box/qrb3000/Qrb3000CommandHelper.java | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java | 55 ++++++++++++++++++++++++++++++++----------------------- src/main/java/com/example/mina/client/box/qrb3000/QrbCommandHelper.java | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/example/mina/client/box/rbm3000/Rbm3000CommandHelper.java | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ProtocolFactory.java | 21 +++++---------------- 9 files changed, 688 insertions(+), 120 deletions(-) create mode 100644 src/main/java/com/example/mina/client/box/lte3000/Lte3000CommandHelper.java create mode 100644 src/main/java/com/example/mina/client/box/lte3000/Lte3000ProtocolFactory.java create mode 100644 src/main/java/com/example/mina/client/box/qrb3000/Qrb3000CommandHelper.java create mode 100644 src/main/java/com/example/mina/client/box/qrb3000/QrbCommandHelper.java create mode 100644 src/main/java/com/example/mina/client/box/rbm3000/Rbm3000CommandHelper.java diff --git a/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientFactory.java b/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientFactory.java index bb0eace..4195055 100644 --- a/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientFactory.java +++ b/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientFactory.java @@ -3,6 +3,10 @@ package com.example.mina.client.box.lte3000; import com.example.mina.client.base.AbstractClientFactory; import com.example.mina.client.base.AbstractMatrixIoHandler; import com.example.mina.client.base.MatrixConstants; +import com.example.mina.client.box.aeroflex.AeroFlexProtocolFactory; +import com.example.mina.client.box.aeroflex.AeroflexClientIoHandler; +import org.apache.mina.core.filterchain.IoFilterChain; +import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.springframework.stereotype.Component; @Component(MatrixConstants.MATRIX_TYPE_LTE3000) @@ -10,6 +14,12 @@ public class Lte3000ClientFactory extends AbstractClientFactory { @Override public AbstractMatrixIoHandler getClientHandler() { - return null; + System.out.println("=============getClientHandler=================="); + return new Lte3000ClientIoHandler(); + } + + @Override + public void buildFilterChain(IoFilterChain ioFilterChain) { + ioFilterChain.addLast("codec", new ProtocolCodecFilter(new Lte3000ProtocolFactory())); } } diff --git a/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java b/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java index ade7108..5a0d521 100644 --- a/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java +++ b/src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java @@ -5,6 +5,7 @@ import com.example.mina.client.base.MatrixDataProxy; import com.example.mina.client.base.MatrixResponse; import com.example.mina.server.util.Lte3000CommandHelper; import lombok.extern.slf4j.Slf4j; +import org.apache.mina.core.session.IoSession; /** * @author dy @@ -13,8 +14,8 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class Lte3000ClientIoHandler extends AbstractMatrixIoHandler { - public Lte3000ClientIoHandler(MatrixDataProxy matrixDataProxy) { - super(matrixDataProxy); + public Lte3000ClientIoHandler() { + super(new MatrixDataProxy()); } protected boolean isSameValue(byte value, int expect) { @@ -23,84 +24,11 @@ public class Lte3000ClientIoHandler extends AbstractMatrixIoHandler { @Override public boolean handleCommandResponse(MatrixResponse matrixResponse) { -// int responseLen = response.length; -// if (response[0] != Lte3000CommandHelper.ACK || responseLen < 6) { -// return false; -// } -// //api document: "SRM - MRF - MRM - XRM - QRM - LTE Protocol v2.15 Rev FR.pdf" -// -// //O:Query Output Channel P.106 -// //0x4F -// if (responseLen == 9 && isSameValue(response[3], 0x4F)) { // 'O' -//// 0x6 0x46 0x46 0x4F 0x30 0x32 0x38 0x3 0x70 -// int col = Lte3000CommandHelper.getBcdPort(response[4], response[5], response[6]); -// int row = Lte3000CommandHelper.getBcdPort(response[4], response[5], response[6]); -// // limits only one set "ON" in a column, others should be OFF -//// for (int i = 1; i <= responsePool.getMaxRow(); i++) { -//// if (i == row) { -//// responsePool.setAttenuation(i, col, 1); -//// } else { -//// responsePool.setAttenuation(i, col, 0); -//// } -//// } TODO 再次调用MatrixDataProxy的方法来设置衰减值 -// log.info("RESPONSE Ox4F. ROW = {}, COL = {}", row, col); -// return true; -// } -// -// //S: Set CrossPoint P.115 -// //0x53 -// if (responseLen == 6 && isSameValue(response[3], 0x53)) { -// int col = Lte3000CommandHelper.getBcdPort(response[4], response[5], response[6]); -// int row = Lte3000CommandHelper.getBcdPort(response[7], response[8], response[9]); -// // limits only one set "ON" in a column, others should be OFF -//// for (int i = 1; i <= responsePool.getMaxRow(); i++) { -//// if (i == row) { -//// responsePool.setAttenuation(i, col, 1); -//// } else { -//// responsePool.setAttenuation(i, col, 0); -//// } -//// } TODO 再次调用MatrixDataProxy的方法来设置衰减值 -// log.info("RESPONSE Ox53. ROW = {}, COL = {}", row, col); -// return true; -// } -// -// //XGMO: Set Gain Control to Manual Mode - Output p.137 -// //0x58 0x47 0x4D 0x4F -// if (responseLen == 9 && response[3] == 'X' && response[4] == 'G' && response[5] == 'M' && response[6] == 'O') { -//// receive - 0x2 0x30 0x30 0x58 0x47 0x4D 0x4F 0x30 0x30 0x31 0x40 0x2D 0x36 0x33 0x2E 0x30 0x3 0x5B -//// 00XGMO001@-63.0[ -//// set col/attn 1/63 -// -// int col = Lte3000CommandHelper.getBcdPort(response[7], response[8], response[9]); -// int attn = Lte3000CommandHelper.getBcdAttn(response[11], response[12], response[13], response[14], response[15]); -// //responsePool.setOffset(col, attn); TODO 再次调用MatrixDataProxy的方法来设置衰减值 -// log.info("RESPONSE XGMO. ATTN = {}, COL = {}", attn, col); -// return true; -// } -// -// -// //XGRO: Gain Control Read Status - Output P.139 -// if (responseLen == 16 && response[3] == 'X' && response[4] == 'G' && response[5] == 'R' && response[6] == 'O') { -// /* -// Example Command: -// 02 XX XX 58 46 52 4F 30 31 36 03 XX -// STX ADR ADR X G R O O 1 6 ETX CHK -// (Read attenuation control status of system output 16) -// */ -// /* -// Example Response: -// 06 XX XX 58 46 52 4F 4D 40 2D 31 33 2E 35 03 XX -// ACK ADR ADR X G R O M @ - 1 3 . 5 ETX CHX -// (Output set at -13.5 dB of attenuation in manual mode. ) -// */ -// int col = Lte3000CommandHelper.getBcdPort(response[7], response[8], response[9]); -// int attn = Lte3000CommandHelper.getBcdAttn(response[9], response[10], response[11], response[12], response[13]); -// //responsePool.setOffset(col, attn); TODO 再次调用MatrixDataProxy的方法来设置衰减值 -// log.info("RESPONSE XGRO. ATTN = {}, COL = {}", attn, col); -// return true; -// } -// -// log.info("收到服务器回传的消息了"); + log.info("------Qrb3000ClientIoHandler-----{}",matrixResponse); return false; } + + @Override + public void sessionCreated(IoSession session) {} + } diff --git a/src/main/java/com/example/mina/client/box/lte3000/Lte3000CommandHelper.java b/src/main/java/com/example/mina/client/box/lte3000/Lte3000CommandHelper.java new file mode 100644 index 0000000..0831712 --- /dev/null +++ b/src/main/java/com/example/mina/client/box/lte3000/Lte3000CommandHelper.java @@ -0,0 +1,157 @@ +package com.example.mina.client.box.lte3000; + +import org.apache.commons.lang3.ArrayUtils; + +import java.nio.charset.StandardCharsets; + +/** + * @ProjectName: demo + * @Package: com.example.mina.client.box.lte3000 + * @ClassName: Lte3000CommandHelper + * @Author: HuangLang + * @Description: Lte command helper + * @Date: 2021-04-02 下午 5:12 + */ +public class Lte3000CommandHelper { + public static final byte STX = 0X02; + public static final byte ETX = 0X03; + public static final byte ACK = 0X06; + public static final byte NAK = 0X15; + + public static void setCHK(byte[] bs) { + bs[bs.length - 1] = 0; + + for (int i = 0; i < bs.length - 1; i++) { + bs[bs.length - 1] ^= bs[i]; + } + } + + public static int getBcdPort(byte a, byte b, byte c) { + String port = new StringBuilder().append(a - 0x30).append(b - 0x30).append(c - 0x30).toString(); + return Integer.parseInt(port); + } + + /** + * input: "-12.2" + * @param a sign + * @param b tenth + * @param c single + * @param d dot + * @param e tenths + * @return int -12 if -12.2 + */ + public static int getBcdAttn(byte a, byte b, byte c, byte d, byte e) { + String port = new StringBuilder() + //.append((char)a) // sign -/+ + .append(b - 0x30).append(c - 0x30) // integer + //.append((char)d) // . + //.append(e - 0x30) + .toString(); // tenths. + return Integer.parseInt(port); + } + + public static byte[] genCommandSetAttn(int col, int val){ + byte[] command = new byte[18]; + byte[] vCol = intToReversedBytes(col); + byte[] vVal = intToReversedBytes(val); + command[0] = STX; + command[1] = 0x30; + command[2] = 0x30; + command[3] = 'X'; + command[4] = 'G'; + command[5] = 'M'; + command[6] = 'O'; + command[7] = vCol.length > 2 ? vCol[2] : 0x30; + command[8] = vCol.length > 1 ? vCol[1] : 0x30; + command[9] = vCol.length > 0 ? vCol[0] : 0x30; + command[10] = '@'; + command[11] = '-'; + command[12] = vVal.length > 1 ? vVal[1] : 0x30; + command[13] = vVal.length > 0 ? vVal[0] : 0x30; + command[14] = '.'; + command[15] = '0'; + command[16] = ETX; + command[17] = 0; + + setCHK(command); + + return command; + } + + public static byte[] genCommandGetAttn(int col){ + byte[] command = new byte[12]; + byte[] vCol = intToReversedBytes(col); + command[0] = STX; + command[1] = 0x30; + command[2] = 0x30; + command[3] = 'X'; + command[4] = 'G'; + command[5] = 'R'; + command[6] = 'O'; + command[7] = vCol.length > 2 ? vCol[2] : 0x30; + command[8] = vCol.length > 1 ? vCol[1] : 0x30; + command[9] = vCol.length > 0 ? vCol[0] : 0x30; + command[10] = ETX; + command[11] = 0; + + setCHK(command); + + return command; + } + + + public static byte[] genCommandSetCross(int row, int col){ + byte[] command = new byte[12]; + byte[] vRow = intToReversedBytes(row); + byte[] vCol = intToReversedBytes(col); + command[0] = STX; + command[1] = 0x30; + command[2] = 0x30; + command[3] = 'S'; + command[4] = vCol.length > 2 ? vCol[2] : 0x30; + command[5] = vCol.length > 1 ? vCol[1] : 0x30; + command[6] = vCol.length > 0 ? vCol[0] : 0x30; + command[7] = vRow.length > 2 ? vRow[2] : 0x30; + command[8] = vRow.length > 1 ? vRow[1] : 0x30; + command[9] = vRow.length > 0 ? vRow[0] : 0x30; + command[10] = ETX; + command[11] = 0; + + setCHK(command); + + return command; + } + + /** + * 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[] genCommandGetCross(int col){ + byte[] command = new byte[9]; + byte[] value = String.valueOf(col).getBytes(StandardCharsets.US_ASCII); + ArrayUtils.reverse(value); + command[0] = STX; + command[1] = 0x30; + command[2] = 0x30; + command[3] = 'O'; + command[4] = value.length >= 3 ? value[2] : 0x30; + command[5] = value.length >= 2 ? value[1] : 0x30; + command[6] = value.length >= 1 ? value[0] : 0x30; + command[7] = ETX; + command[8] = 0; + + setCHK(command); + + return command; + } +} diff --git a/src/main/java/com/example/mina/client/box/lte3000/Lte3000ProtocolFactory.java b/src/main/java/com/example/mina/client/box/lte3000/Lte3000ProtocolFactory.java new file mode 100644 index 0000000..dd3d199 --- /dev/null +++ b/src/main/java/com/example/mina/client/box/lte3000/Lte3000ProtocolFactory.java @@ -0,0 +1,112 @@ +package com.example.mina.client.box.lte3000; + +import com.example.mina.client.base.MatrixCommand; +import com.example.mina.client.base.MatrixConstants; +import com.example.mina.client.base.MatrixResponse; +import com.example.mina.processor.VbLogUtils; +import com.example.mina.server.util.CommandHelper; +import lombok.extern.slf4j.Slf4j; +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.filter.codec.*; + +import java.nio.charset.CharacterCodingException; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.StandardCharsets; + +@Slf4j +public class Lte3000ProtocolFactory implements ProtocolCodecFactory { + + private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder(); + + private static final CharsetEncoder ENCODER = StandardCharsets.UTF_8.newEncoder(); + + @Override + public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception { + return new Lte3000ProtocolEncoder(); + } + + @Override + public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception { + return new Lte3000ProtocolDecoder(); + } + + public static class Lte3000ProtocolEncoder extends ProtocolEncoderAdapter { + + @Override + public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput) + throws CharacterCodingException { + + if (!(msg instanceof MatrixCommand)) { + log.error("error msg, msg is: {}", msg); + return; + } + + MatrixCommand mc = (MatrixCommand) msg; + log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {},type = {}", + mc.getAttn(), + mc.getCol(), + mc.getCommand(), + mc.getRow(), + mc.getMatrixId() + ); + int cmd = mc.getCommand(); + VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg); + + IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true); + + int row = mc.getRow(); + byte[] command; + if (row == 0) { + command = Lte3000CommandHelper.genCommandSetAttn(mc.getCol(), mc.getAttn()); + }else{ + command = Lte3000CommandHelper.genCommandSetCross(row, mc.getCol()); + } + + buffer.put(command); + buffer.flip(); + protocolEncoderOutput.write(buffer); + } + } + + public static class Lte3000ProtocolDecoder extends ProtocolDecoderAdapter { + + @Override + public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput) + throws Exception { + log.info("--server返回给----client ------IoBuffer{}", ioBuffer); + MatrixResponse response = new MatrixResponse(); + int len = ioBuffer.limit(); + byte[] bytes = new byte[len]; + ioBuffer.get(bytes); + int cmd = bytes[0]; + if(cmd == 0x30 && len==8) { + int row = CommandHelper.BCD_REV[bytes[3]]; + int col = CommandHelper.BCD_REV[bytes[4]]; + int attn = bytes[5]; + + response.setRow(row); + response.setCol(col); + response.setIsSetAttn(true); + response.setAttn(attn); + + } else if (cmd == 0x68 && len ==8) { + int offset = bytes[6]; + int col = CommandHelper.BCD_REV[bytes[3]]; + response.setIsSetOffset(true); + response.setCol(col); + response.setOffset(offset); + } else { + int offset = bytes[6]; + response.setOffset(offset); + } + protocolDecoderOutput.write(response); + } + + } + + + + +} diff --git a/src/main/java/com/example/mina/client/box/qrb3000/Qrb3000CommandHelper.java b/src/main/java/com/example/mina/client/box/qrb3000/Qrb3000CommandHelper.java new file mode 100644 index 0000000..d295810 --- /dev/null +++ b/src/main/java/com/example/mina/client/box/qrb3000/Qrb3000CommandHelper.java @@ -0,0 +1,186 @@ +package com.example.mina.client.box.qrb3000; + +import com.example.mina.server.util.CommandHelper; + +/** + * @ProjectName: demo + * @Package: com.example.mina.client.box.qrb3000 + * @ClassName: Qrb3000CommandHelper + * @Author: HuangLang + * @Description: Qrb command helper + * @Date: 2021-04-02 下午 5:55 + */ +public class Qrb3000CommandHelper { + + public static byte[] genCommand30(int row, int col, int val){ + byte[] command = new byte[8]; + + command[0] = 0x30; + command[1] = 0x40; + command[2] = 0x00; + command[3] = CommandHelper.BCD[row]; + command[4] = CommandHelper.BCD[col]; + command[5] = (byte)val; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + public static byte[] genCommand31(int row, int col, int val){ + byte[] command = new byte[8]; + + command[0] = 0x31; + command[1] = 0x40; + command[2] = 0x00; + command[3] = CommandHelper.BCD[row]; + command[4] = CommandHelper.BCD[col]; + command[5] = (byte)val; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + + public static byte[] genCommand32(){ + byte[] command = new byte[8]; + + command[0] = 0x32; + command[1] = 0x00; + command[2] = 0x00; + command[3] = 0x00; + command[4] = 0x00; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + + + public static byte[] genCommand4D(byte val){ + byte[] command = new byte[1028]; + + command[0] = 0x4D; + command[1] = 0x40; + command[2] = 0x00; + + for(int i = 0; i < 1024; i++) { + command[ 3 + i] = val; + } + command[1027] = 0x00; + + + return command; + } + + public static byte[] genCommand62(int row){ + byte[] command = new byte[8]; + + command[0] = 0x62; + command[1] = 0x40; + command[2] = 0x00; + command[3] = CommandHelper.BCD[row]; + command[4] = 0x00; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + public static byte[] genCommand68(int row){ + byte[] command = new byte[8]; + + command[0] = 0x68; + command[1] = 0x40; + command[2] = 0x00; + command[3] = CommandHelper.BCD[row]; + command[4] = 0x00; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + @Deprecated + public static int parseCommand68_Response(byte[] b){//TODO : need double check + int b7 = 0xFF & b[6]; + int b8 = 0xFF & b[7]; + + int rec = 0; + + if(b7 == 0xFE || b7 == 0xFF || b8 == 0xEE){ + rec = -99; + } +// else if(b7 ==0xF9){ +// rec = 31; +// } + else if(b7 == 0x80){ + rec = 0; + } + else if(b7 < 0x80){ + rec = (b7 / 16) * 10 + (0x0F & b7); + } + else{ + b7= 0x7f & b7; + rec = - ( (b7 / 16) * 10 + (0x0F & b7)); + } + + return rec; + } + + public static byte[] genCommand4F(int row){ + byte[] command = new byte[8]; + + command[0] = 0x4F; + command[1] = 0x40; + command[2] = 0x00; + command[3] = CommandHelper.BCD[row]; + command[4] = 0x00; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + public static byte[] genCommandEA(){ + byte[] command = new byte[8]; + + int b = 0xEA; + + command[0] = (byte)b; + command[1] = 0x40; + command[2] = 0x00; + command[3] = 0x00; + command[4] = 0x00; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + + public static byte[] genCommandEE(){ + byte[] command = new byte[8]; + + int b = 0xEE; + + command[0] = (byte)b; + command[1] = 0x40; + command[2] = 0x00; + command[3] = 0x00; + command[4] = 0x00; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + +} diff --git a/src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java b/src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java index e1ce702..e11c00e 100644 --- a/src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java +++ b/src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java @@ -52,39 +52,48 @@ public class Qrb3000ProtocolFactory implements ProtocolCodecFactory { mc.getMatrixId() ); int cmd = mc.getCommand(); + int row = mc.getRow(); + int col = mc.getCol(); + int attn = mc.getAttn(); VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg); IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true); + byte[] command; + if (cmd == MatrixConstants.COMMAND_SET_ATTN) { - buffer.put((byte)0x30); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put(mc.getRow().byteValue()); - buffer.put(mc.getCol().byteValue()); - buffer.put(mc.getAttn().byteValue()); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); +// buffer.put((byte)0x30); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); +// buffer.put(mc.getRow().byteValue()); +// buffer.put(mc.getCol().byteValue()); +// buffer.put(mc.getAttn().byteValue()); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); + command = QrbCommandHelper.genSetAttenuation(row, col, attn); } if (cmd == MatrixConstants.COMMAND_SET_OFFSET) { - buffer.put((byte)0x68); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put(mc.getCol().byteValue()); - buffer.put(mc.getOffset().byteValue()); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); + // 0x68 + command = Qrb3000CommandHelper.genCommand30(row, col, attn); +// buffer.put((byte)0x68); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); +// buffer.put(mc.getCol().byteValue()); +// buffer.put(mc.getOffset().byteValue()); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); } if (cmd == MatrixConstants.COMMAND_GET_OFFSET) { - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put(mc.getCol().byteValue()); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); + command = QrbCommandHelper.genSetAttenuation(row, col, attn); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); +// buffer.put(mc.getCol().byteValue()); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); +// buffer.put((byte)0xFE); } buffer.flip(); diff --git a/src/main/java/com/example/mina/client/box/qrb3000/QrbCommandHelper.java b/src/main/java/com/example/mina/client/box/qrb3000/QrbCommandHelper.java new file mode 100644 index 0000000..81d942d --- /dev/null +++ b/src/main/java/com/example/mina/client/box/qrb3000/QrbCommandHelper.java @@ -0,0 +1,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 bytes) { + Optional 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 command = new ArrayList(); + 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 command = new ArrayList(); + 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); + + } + + +} diff --git a/src/main/java/com/example/mina/client/box/rbm3000/Rbm3000CommandHelper.java b/src/main/java/com/example/mina/client/box/rbm3000/Rbm3000CommandHelper.java new file mode 100644 index 0000000..3d6e244 --- /dev/null +++ b/src/main/java/com/example/mina/client/box/rbm3000/Rbm3000CommandHelper.java @@ -0,0 +1,67 @@ +package com.example.mina.client.box.rbm3000; + +import com.example.mina.server.util.CommandHelper; + +/** + * @ProjectName: demo + * @Package: com.example.mina.client.box.rbm3000 + * @ClassName: Rbm3000CommandHelper + * @Author: HuangLang + * @Description: Rbm command helper + * @Date: 2021-04-02 下午 5:30 + */ +public class Rbm3000CommandHelper { + + public static byte[] genCommand30(int row, int col){ + byte[] command = new byte[8]; + + command[0] = 0x30; + command[1] = 0x50; + command[2] = 0x00; + command[3] = CommandHelper.BCD[row]; + command[4] = CommandHelper.BCD[col]; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + + public static byte[] genCommand68(boolean isA, int port){ + byte[] command = new byte[8]; + + command[0] = 0x68; + command[1] = 0x50; + command[2] = 0x00; + if(isA){ + command[3] = 0x0A; + } + else{ + command[3] = 0x0B; + } + + command[4] = CommandHelper.BCD[port]; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } + + + public static byte[] genCommand62(){ + byte[] command = new byte[8]; + + command[0] = 0x62; + command[1] = 0x50; + command[2] = 0x00; + command[3] = 0x00; + command[4] = 0x00; + command[5] = 0x00; + command[6] = 0x00; + command[7] = 0x00; + + return command; + } +} diff --git a/src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ProtocolFactory.java b/src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ProtocolFactory.java index a01afc1..81c28d6 100644 --- a/src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ProtocolFactory.java +++ b/src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ProtocolFactory.java @@ -56,17 +56,12 @@ public class Rbm3000ProtocolFactory implements ProtocolCodecFactory { IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true); + byte[] comand = null; // Len==8 cmd【0】 0x30 set row/col attn=1 if (cmd == MatrixConstants.COMMAND_SET_ATTN) { - buffer.put((byte)0x30); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put(mc.getRow().byteValue()); - buffer.put(mc.getCol().byteValue()); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); + comand = Rbm3000CommandHelper.genCommand30(mc.getRow(), mc.getCol()); }else if (cmd == MatrixConstants.COMMAND_GET_ATTN) { + // todo 业务需求需要进一步明确 buffer.put((byte)0x68); buffer.put((byte)0xFE); buffer.put((byte)0xFE); @@ -84,16 +79,10 @@ public class Rbm3000ProtocolFactory implements ProtocolCodecFactory { buffer.put((byte)0xFE); } else { - buffer.put((byte)0x62); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); - buffer.put((byte)0xFE); + comand = Rbm3000CommandHelper.genCommand62(); } + buffer.put(comand); buffer.flip(); protocolEncoderOutput.write(buffer); } -- libgit2 0.21.2