Commit 302ac45401144ca65c5ddc62a1a72f811f6755b1

Authored by 黄浪
1 parent 82e541cf
Exists in develop

1、初步完成RBM3000类型设备client端编码

@@ -81,7 +81,15 @@ @@ -81,7 +81,15 @@
81 <groupId>org.springframework.boot</groupId> 81 <groupId>org.springframework.boot</groupId>
82 <artifactId>spring-boot-starter-web</artifactId> 82 <artifactId>spring-boot-starter-web</artifactId>
83 </dependency> 83 </dependency>
84 - 84 + <dependency>
  85 + <groupId>org.springframework.boot</groupId>
  86 + <artifactId>spring-boot-starter-aop</artifactId>
  87 + </dependency>
  88 + <dependency>
  89 + <groupId>com.alibaba</groupId>
  90 + <artifactId>fastjson</artifactId>
  91 + <version>1.2.75</version>
  92 + </dependency>
85 </dependencies> 93 </dependencies>
86 94
87 <build> 95 <build>
src/main/java/com/example/mina/Application.java
@@ -49,44 +49,65 @@ public class Application { @@ -49,44 +49,65 @@ public class Application {
49 MatrixClient REC3000Client = clientManager.getOrCreateClient(REC3000); 49 MatrixClient REC3000Client = clientManager.getOrCreateClient(REC3000);
50 50
51 REC3000Client.setAttenuation(1, 2, 3, null);*/ 51 REC3000Client.setAttenuation(1, 2, 3, null);*/
  52 +// //------------------------------Qrb测试 start--------------------------
  53 +// ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
  54 +//
  55 +// ClientManager clientManager = run.getBean(ClientManager.class);
  56 +//
  57 +// MatrixConnectConfig connectConfig = MatrixConnectConfig.builder()
  58 +// .deviceId("15")
  59 +// .deviceType(MatrixConstants.MATRIX_TYPE_QRB)
  60 +// .host("127.0.0.1")
  61 +// .port(9103)
  62 +// .build();
  63 +//
  64 +// MatrixClient client = clientManager.getOrCreateClient(connectConfig);
  65 +//
  66 +//
  67 +// client.setAttenuation(1,2,3);
  68 +//// LockSupport.parkNanos(1000_000_000);
  69 +//
  70 +//// client.setOffset(1, 2);
  71 +//// client.getOffset(2);
  72 +// LockSupport.parkNanos(1000_000_000);
  73 +//
  74 +//// client.setOffset(1,2);
  75 +//// LockSupport.parkNanos(1000_000_000);
  76 +//
  77 +//
  78 +//// client.setAttenuation(1,2,3, MatrixCommand.Type.AERO_GET_ATTN);
  79 +// //------------------------------Qrb测试 end--------------------------
  80 +// MatrixConnectConfig REC3000 = MatrixConnectConfig.builder()
  81 +// .deviceId("158")
  82 +// .deviceType(MatrixConstants.MATRIX_TYPE_QRB)
  83 +// .host("127.0.0.1")
  84 +// .port(9099)
  85 +// .build();
  86 +//
  87 +// MatrixClient REC3000Client = clientManager.getOrCreateClient(REC3000);
  88 +//
  89 +// REC3000Client.setAttenuation(1, 2, 3, null);
52 90
  91 + //------------------------------Qrb测试 start--------------------------
53 ConfigurableApplicationContext run = SpringApplication.run(Application.class, args); 92 ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
54 -  
55 ClientManager clientManager = run.getBean(ClientManager.class); 93 ClientManager clientManager = run.getBean(ClientManager.class);
56 94
57 MatrixConnectConfig connectConfig = MatrixConnectConfig.builder() 95 MatrixConnectConfig connectConfig = MatrixConnectConfig.builder()
58 .deviceId("15") 96 .deviceId("15")
59 - .deviceType(MatrixConstants.MATRIX_TYPE_QRB) 97 + .deviceType(MatrixConstants.MATRIX_TYPE_RBM3000)
60 .host("127.0.0.1") 98 .host("127.0.0.1")
61 - .port(9103) 99 + .port(9101)
62 .build(); 100 .build();
63 101
64 MatrixClient client = clientManager.getOrCreateClient(connectConfig); 102 MatrixClient client = clientManager.getOrCreateClient(connectConfig);
65 -  
66 -  
67 - client.setAttenuation(1,2,3, null);  
68 -// LockSupport.parkNanos(1000_000_000);  
69 -  
70 -// client.setOffset(1, 2);  
71 -// client.getOffset(2); 103 + client.setAttenuation(1,2,3);
72 LockSupport.parkNanos(1000_000_000); 104 LockSupport.parkNanos(1000_000_000);
73 105
74 -// client.setOffset(1,2); 106 +// client.getAttenuation(3,null);
75 // LockSupport.parkNanos(1000_000_000); 107 // LockSupport.parkNanos(1000_000_000);
76 108
77 109
78 -// client.setAttenuation(1,2,3, MatrixCommand.Type.AERO_GET_ATTN);  
79 -  
80 -// MatrixConnectConfig REC3000 = MatrixConnectConfig.builder()  
81 -// .deviceId("158")  
82 -// .deviceType(MatrixConstants.MATRIX_TYPE_QRB)  
83 -// .host("127.0.0.1")  
84 -// .port(9099)  
85 -// .build();  
86 -//  
87 -// MatrixClient REC3000Client = clientManager.getOrCreateClient(REC3000);  
88 -//  
89 -// REC3000Client.setAttenuation(1, 2, 3, null); 110 + //------------------------------Qrb测试 end--------------------------
90 111
91 } 112 }
92 113
src/main/java/com/example/mina/client/base/MatrixClient.java
@@ -121,9 +121,16 @@ public class MatrixClient { @@ -121,9 +121,16 @@ public class MatrixClient {
121 sendCommand(command); 121 sendCommand(command);
122 } 122 }
123 123
124 - public void setAttenuation(int row, int col, int attn, MatrixCommand.Type type) { 124 + public void setAttenuation(int row, int col, int attn) {
125 MatrixCommand command = MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_ATTN) 125 MatrixCommand command = MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_ATTN)
126 - .col(col).row(row).attn(attn).type(type).build(); 126 + .col(col).row(row).attn(attn).build();
  127 +
  128 + sendCommand(command);
  129 + }
  130 +
  131 + public void getAttenuation(Integer row, Integer col) {
  132 + MatrixCommand command = MatrixCommand.builder().command(MatrixConstants.COMMAND_GET_ATTN)
  133 + .col(col).row(row).build();
127 134
128 sendCommand(command); 135 sendCommand(command);
129 } 136 }
src/main/java/com/example/mina/client/base/MatrixCommand.java
@@ -7,55 +7,35 @@ import lombok.Data; @@ -7,55 +7,35 @@ import lombok.Data;
7 @Builder 7 @Builder
8 public class MatrixCommand { 8 public class MatrixCommand {
9 /** 9 /**
10 - * 矩阵id 10 + * matrix id
11 */ 11 */
12 private String matrixId; 12 private String matrixId;
13 13
14 /** 14 /**
15 - * 指令类型,来自MatrixConstants 15 + * command type,come from MatrixConstants
16 */ 16 */
17 private Integer command; 17 private Integer command;
18 18
19 /** 19 /**
20 - * 矩阵行坐标 20 + * matrix row index
21 */ 21 */
22 private Integer row; 22 private Integer row;
23 23
24 /** 24 /**
25 - * 矩阵列坐标 25 + * matrix column index
26 */ 26 */
27 private Integer col; 27 private Integer col;
28 28
29 /** 29 /**
30 - * 矩阵指定行列位置的值,如衰减值 30 + * attenuation
31 */ 31 */
32 private Integer attn; 32 private Integer attn;
33 33
34 /** 34 /**
35 - * 相位值,偏移量 35 + * offset
36 */ 36 */
37 private Integer offset; 37 private Integer offset;
38 38
39 - private Type type;  
40 -  
41 - public enum Type {  
42 -  
43 - /**  
44 - * eg: "ATTN? 1"  
45 - */  
46 - AERO_GET_ATTN,  
47 -  
48 - /**  
49 - * ATTN 2 3;  
50 - */  
51 - AERO_SET_ATTN,  
52 -  
53 - /**  
54 - * eg: "ATTN ALL MAX"  
55 - */  
56 - AERO_ATTN_ALL_MAX,  
57 -  
58 - }  
59 39
60 } 40 }
61 41
src/main/java/com/example/mina/client/base/MatrixConstants.java
@@ -12,6 +12,8 @@ public class MatrixConstants { @@ -12,6 +12,8 @@ public class MatrixConstants {
12 12
13 public static final String MATRIX_TYPE_QRB = "QRB3000"; 13 public static final String MATRIX_TYPE_QRB = "QRB3000";
14 14
  15 + public static final String MATRIX_TYPE_RBM3000 = "RBM3000";
  16 +
15 public static final int COMMAND_RESET = 1; 17 public static final int COMMAND_RESET = 1;
16 18
17 public static final int COMMAND_SET_ATTN = 2; 19 public static final int COMMAND_SET_ATTN = 2;
src/main/java/com/example/mina/client/base/MatrixResponse.java
@@ -16,6 +16,10 @@ public class MatrixResponse { @@ -16,6 +16,10 @@ public class MatrixResponse {
16 16
17 private Integer attn; 17 private Integer attn;
18 18
  19 + private Integer rowCol;
  20 +
  21 + private Integer colRow;
  22 +
19 public MatrixResponse() { 23 public MatrixResponse() {
20 isSetAttn = false; 24 isSetAttn = false;
21 isSetOffset = false; 25 isSetOffset = false;
src/main/java/com/example/mina/client/box/aeroflex/AeroFlexProtocolFactory.java
1 package com.example.mina.client.box.aeroflex; 1 package com.example.mina.client.box.aeroflex;
2 2
3 import com.example.mina.client.base.MatrixCommand; 3 import com.example.mina.client.base.MatrixCommand;
  4 +import com.example.mina.client.base.MatrixConstants;
4 import com.example.mina.client.base.MatrixResponse; 5 import com.example.mina.client.base.MatrixResponse;
5 import lombok.extern.slf4j.Slf4j; 6 import lombok.extern.slf4j.Slf4j;
6 import org.apache.mina.core.buffer.IoBuffer; 7 import org.apache.mina.core.buffer.IoBuffer;
@@ -46,24 +47,23 @@ public class AeroFlexProtocolFactory implements ProtocolCodecFactory { @@ -46,24 +47,23 @@ public class AeroFlexProtocolFactory implements ProtocolCodecFactory {
46 mc.getCol(), 47 mc.getCol(),
47 mc.getCommand(), 48 mc.getCommand(),
48 mc.getRow(), 49 mc.getRow(),
49 - mc.getMatrixId(),  
50 - mc.getType() 50 + mc.getMatrixId()
51 ); 51 );
52 - MatrixCommand.Type mcType = mc.getType(); 52 + int cmd = mc.getCommand();
53 53
54 IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true); 54 IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
55 55
56 - if (MatrixCommand.Type.AERO_ATTN_ALL_MAX.equals(mcType)) { 56 + if (cmd == MatrixConstants.COMMAND_RESET) {
57 57
58 buffer.putString("ATTN ALL MAX", ENCODER); 58 buffer.putString("ATTN ALL MAX", ENCODER);
59 59
60 - } else if (MatrixCommand.Type.AERO_GET_ATTN.equals(mcType)) { 60 + } else if (cmd == MatrixConstants.COMMAND_GET_OFFSET) {
61 61
62 buffer.putString("ATTN?", ENCODER); 62 buffer.putString("ATTN?", ENCODER);
63 buffer.putString(" ", ENCODER); 63 buffer.putString(" ", ENCODER);
64 buffer.putString(mc.getRow().toString(), ENCODER); 64 buffer.putString(mc.getRow().toString(), ENCODER);
65 65
66 - } else if (MatrixCommand.Type.AERO_SET_ATTN.equals(mcType)) { 66 + } else if (cmd == MatrixConstants.COMMAND_SET_OFFSET) {
67 67
68 buffer.putString("ATTN", ENCODER); 68 buffer.putString("ATTN", ENCODER);
69 buffer.putString(" ", ENCODER); 69 buffer.putString(" ", ENCODER);
src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java
@@ -49,8 +49,7 @@ public class Qrb3000ProtocolFactory implements ProtocolCodecFactory { @@ -49,8 +49,7 @@ public class Qrb3000ProtocolFactory implements ProtocolCodecFactory {
49 mc.getCol(), 49 mc.getCol(),
50 mc.getCommand(), 50 mc.getCommand(),
51 mc.getRow(), 51 mc.getRow(),
52 - mc.getMatrixId(),  
53 - mc.getType() 52 + mc.getMatrixId()
54 ); 53 );
55 int cmd = mc.getCommand(); 54 int cmd = mc.getCommand();
56 VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg); 55 VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg);
src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ClientFactory.java 0 → 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +package com.example.mina.client.box.rbm3000;
  2 +
  3 +import com.example.mina.client.base.AbstractClientFactory;
  4 +import com.example.mina.client.base.MatrixConstants;
  5 +import com.example.mina.client.box.qrb3000.Qrb3000ClientIoHandler;
  6 +import com.example.mina.client.box.qrb3000.Qrb3000ProtocolFactory;
  7 +import org.apache.mina.core.filterchain.IoFilterChain;
  8 +import org.apache.mina.filter.codec.ProtocolCodecFilter;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +@Component(MatrixConstants.MATRIX_TYPE_RBM3000)
  12 +public class Rbm3000ClientFactory extends AbstractClientFactory {
  13 +
  14 + @Override
  15 + public Rbm3000ClientIoHandler getClientHandler() {
  16 + System.out.println("=============getClientHandler==================");
  17 + return new Rbm3000ClientIoHandler();
  18 + }
  19 +
  20 + @Override
  21 + public void buildFilterChain(IoFilterChain ioFilterChain) {
  22 + ioFilterChain.addLast("codec", new ProtocolCodecFilter(new Rbm3000ProtocolFactory()));
  23 + }
  24 +}
src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ClientIoHandler.java 0 → 100644
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +package com.example.mina.client.box.rbm3000;
  2 +
  3 +import com.example.mina.client.base.AbstractMatrixIoHandler;
  4 +import com.example.mina.client.base.MatrixDataProxy;
  5 +import com.example.mina.client.base.MatrixResponse;
  6 +import lombok.extern.slf4j.Slf4j;
  7 +import org.apache.mina.core.session.IoSession;
  8 +
  9 +/**
  10 + * @author hl
  11 + * @date 2021/3/10
  12 + */
  13 +@Slf4j
  14 +public class Rbm3000ClientIoHandler extends AbstractMatrixIoHandler {
  15 +
  16 + //public AeroflexClientIoHandler(MatrixDataProxy matrixDataProxy) {
  17 + // super(matrixDataProxy);
  18 + // }
  19 +
  20 + public Rbm3000ClientIoHandler() {
  21 + super(new MatrixDataProxy());
  22 + }
  23 +
  24 +
  25 + @Override
  26 + public boolean handleCommandResponse(MatrixResponse matrixResponse) {
  27 + log.info("------Rbm3000ClientIoHandler-----{}",matrixResponse);
  28 + return false;
  29 + }
  30 +
  31 + @Override
  32 + public void sessionCreated(IoSession session) {
  33 + System.out.println("=========111========");
  34 + }
  35 +
  36 +
  37 +
  38 +}
src/main/java/com/example/mina/client/box/rbm3000/Rbm3000ProtocolFactory.java 0 → 100644
@@ -0,0 +1,142 @@ @@ -0,0 +1,142 @@
  1 +package com.example.mina.client.box.rbm3000;
  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 Rbm3000ProtocolFactory 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 Qrb3000ProtocolEncoder();
  28 + }
  29 +
  30 + @Override
  31 + public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
  32 + return new Qrb3000ProtocolDecoder();
  33 + }
  34 +
  35 + public static class Qrb3000ProtocolEncoder 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 + // Len==8 cmd【0】 0x30 set row/col attn=1
  60 + 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);
  69 + }else if (cmd == MatrixConstants.COMMAND_GET_ATTN) {
  70 + buffer.put((byte)0x68);
  71 + buffer.put((byte)0xFE);
  72 + buffer.put((byte)0xFE);
  73 + if(mc.getRow() != null){
  74 + // row
  75 + buffer.put((byte)0x0A);
  76 + buffer.put(mc.getRow().byteValue());
  77 + } else {
  78 + // col
  79 + buffer.put((byte)0x0B);
  80 + buffer.put(mc.getCol().byteValue());
  81 + }
  82 + buffer.put((byte)0xFE);
  83 + buffer.put((byte)0xFE);
  84 + buffer.put((byte)0xFE);
  85 +
  86 + } 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);
  95 + }
  96 +
  97 + buffer.flip();
  98 + protocolEncoderOutput.write(buffer);
  99 + }
  100 + }
  101 +
  102 + public static class Qrb3000ProtocolDecoder extends ProtocolDecoderAdapter {
  103 +
  104 + @Override
  105 + public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput)
  106 + throws Exception {
  107 + log.info("--server返回给----client ------IoBuffer{}", ioBuffer);
  108 + MatrixResponse response = new MatrixResponse();
  109 + int len = ioBuffer.limit();
  110 + byte[] bytes = new byte[len];
  111 + ioBuffer.get(bytes);
  112 + int cmd = bytes[0];
  113 + if (cmd == 0x30 && len == 8) {
  114 + int row = CommandHelper.BCD_REV[bytes[3]];
  115 + int col = CommandHelper.BCD_REV[bytes[4]];
  116 + int rowCol = bytes[6];
  117 + int colRow = bytes[5];
  118 +
  119 + response.setRow(row);
  120 + response.setCol(col);
  121 + response.setRowCol(rowCol);
  122 + response.setColRow(colRow);
  123 + response.setIsSetAttn(true);
  124 +
  125 + }
  126 + if (cmd == 0x68 && len == 8) {
  127 + boolean isA = (bytes[3] == (byte) 0x0A);
  128 + int tmp = bytes[4];
  129 + if (isA) {
  130 + // col
  131 + response.setCol(tmp);
  132 + } else {
  133 + // row
  134 + response.setRow(tmp);
  135 + }
  136 + }
  137 + protocolDecoderOutput.write(response);
  138 + }
  139 +
  140 + }
  141 +
  142 +}
src/main/java/com/example/mina/client/box/rec6000/REC6000ProtocolFactory.java
@@ -47,8 +47,7 @@ public class REC6000ProtocolFactory implements ProtocolCodecFactory { @@ -47,8 +47,7 @@ public class REC6000ProtocolFactory implements ProtocolCodecFactory {
47 mc.getCol(), 47 mc.getCol(),
48 mc.getCommand(), 48 mc.getCommand(),
49 mc.getRow(), 49 mc.getRow(),
50 - mc.getMatrixId(),  
51 - mc.getType() 50 + mc.getMatrixId()
52 ); 51 );
53 52
54 // REC6000 rest 开关 53 // REC6000 rest 开关
src/main/resources/logback-spring.xml
1 <configuration> 1 <configuration>
2 - 2 + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  3 + <!-- encoder 默认配置为PatternLayoutEncoder -->
  4 + <encoder>
  5 + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
  6 + </encoder>
  7 + </appender>
3 <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> 8 <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
4 <!-- in the absence of the class attribute, it is assumed that the 9 <!-- in the absence of the class attribute, it is assumed that the
5 desired discriminator type is 10 desired discriminator type is
@@ -21,5 +26,6 @@ @@ -21,5 +26,6 @@
21 26
22 <root level="DEBUG"> 27 <root level="DEBUG">
23 <appender-ref ref="SIFT" /> 28 <appender-ref ref="SIFT" />
  29 + <appender-ref ref="STDOUT" />
24 </root> 30 </root>
25 </configuration> 31 </configuration>
26 \ No newline at end of file 32 \ No newline at end of file