Commit e3df6564fb04e329899e1ced2b096fd6ed4f818a

Authored by 黄浪
1 parent c2997e70
Exists in develop

1、POM文件修改;2、QRB类型虚拟设备CS端第一版联调基本完成。

@@ -44,6 +44,12 @@ @@ -44,6 +44,12 @@
44 <groupId>org.apache.mina</groupId> 44 <groupId>org.apache.mina</groupId>
45 <artifactId>mina-http</artifactId> 45 <artifactId>mina-http</artifactId>
46 <version>2.1.4</version> 46 <version>2.1.4</version>
  47 + <exclusions>
  48 + <exclusion>
  49 + <artifactId>mina-core</artifactId>
  50 + <groupId>org.apache.mina</groupId>
  51 + </exclusion>
  52 + </exclusions>
47 </dependency> 53 </dependency>
48 54
49 <dependency> 55 <dependency>
src/main/java/com/example/mina/Application.java
@@ -17,7 +17,7 @@ public class Application { @@ -17,7 +17,7 @@ public class Application {
17 public static void main(String[] args) { 17 public static void main(String[] args) {
18 // SpringApplication.run(Application.class, args); 18 // SpringApplication.run(Application.class, args);
19 19
20 - ConfigurableApplicationContext run = SpringApplication.run(Application.class, args); 20 + /*ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
21 21
22 ClientManager clientManager = run.getBean(ClientManager.class); 22 ClientManager clientManager = run.getBean(ClientManager.class);
23 23
@@ -48,7 +48,45 @@ public class Application { @@ -48,7 +48,45 @@ public class Application {
48 48
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 +
  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, null);
  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 +
  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
53 } 91 }
54 92
src/main/java/com/example/mina/client/base/AbstractMatrixIoHandler.java
@@ -36,13 +36,13 @@ public abstract class AbstractMatrixIoHandler extends IoHandlerAdapter { @@ -36,13 +36,13 @@ public abstract class AbstractMatrixIoHandler extends IoHandlerAdapter {
36 36
37 @Override 37 @Override
38 public void messageReceived(IoSession session, Object message) { 38 public void messageReceived(IoSession session, Object message) {
39 - if (!(message instanceof IoBuffer)) { 39 + if (!(message instanceof MatrixResponse)) {
40 log.error("客户端接收到的消息不为定义的响应类! message:" + message); 40 log.error("客户端接收到的消息不为定义的响应类! message:" + message);
41 throw new RuntimeException("Unsupported response message"); 41 throw new RuntimeException("Unsupported response message");
42 } 42 }
43 43
44 //TODO 客户端将数据服务器的字符串数组读取出来 44 //TODO 客户端将数据服务器的字符串数组读取出来
45 - byte[] response = null; 45 + MatrixResponse response = (MatrixResponse) message;
46 log.info("the client recieved the device response, device:{}, response is: {}", 46 log.info("the client recieved the device response, device:{}, response is: {}",
47 getConnectConfig(session), response); 47 getConnectConfig(session), response);
48 handleCommandResponse(response); 48 handleCommandResponse(response);
@@ -76,5 +76,5 @@ public abstract class AbstractMatrixIoHandler extends IoHandlerAdapter { @@ -76,5 +76,5 @@ public abstract class AbstractMatrixIoHandler extends IoHandlerAdapter {
76 log.info("the system has connected to device, device is {}", getConnectConfig(session)); 76 log.info("the system has connected to device, device is {}", getConnectConfig(session));
77 } 77 }
78 78
79 - abstract public boolean handleCommandResponse(byte[] response); 79 + abstract public boolean handleCommandResponse(MatrixResponse response);
80 } 80 }
src/main/java/com/example/mina/client/base/MatrixClient.java
@@ -128,9 +128,16 @@ public class MatrixClient { @@ -128,9 +128,16 @@ public class MatrixClient {
128 sendCommand(command); 128 sendCommand(command);
129 } 129 }
130 130
131 - public void setOffset(int row, int offset) { 131 + public void setOffset(int col, int offset) {
132 MatrixCommand command = MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_OFFSET) 132 MatrixCommand command = MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_OFFSET)
133 - .row(row).offset(offset).build(); 133 + .col(col).offset(offset).build();
  134 +
  135 + sendCommand(command);
  136 + }
  137 +
  138 + public void getOffset(int col) {
  139 + MatrixCommand command = MatrixCommand.builder().command(MatrixConstants.COMMAND_GET_OFFSET)
  140 + .col(col).build();
134 141
135 sendCommand(command); 142 sendCommand(command);
136 } 143 }
src/main/java/com/example/mina/client/base/MatrixCommand.java
@@ -6,17 +6,34 @@ import lombok.Data; @@ -6,17 +6,34 @@ import lombok.Data;
6 @Data 6 @Data
7 @Builder 7 @Builder
8 public class MatrixCommand { 8 public class MatrixCommand {
9 - 9 + /**
  10 + * 矩阵id
  11 + */
10 private String matrixId; 12 private String matrixId;
11 13
  14 + /**
  15 + * 指令类型,来自MatrixConstants
  16 + */
12 private Integer command; 17 private Integer command;
13 18
  19 + /**
  20 + * 矩阵行坐标
  21 + */
14 private Integer row; 22 private Integer row;
15 23
  24 + /**
  25 + * 矩阵列坐标
  26 + */
16 private Integer col; 27 private Integer col;
17 28
  29 + /**
  30 + * 矩阵指定行列位置的值,如衰减值
  31 + */
18 private Integer attn; 32 private Integer attn;
19 33
  34 + /**
  35 + * 相位值,偏移量
  36 + */
20 private Integer offset; 37 private Integer offset;
21 38
22 private Type type; 39 private Type type;
src/main/java/com/example/mina/client/base/MatrixConstants.java
@@ -10,6 +10,8 @@ public class MatrixConstants { @@ -10,6 +10,8 @@ public class MatrixConstants {
10 10
11 public static final String MATRIX_TYPE_REC3000 = "REC3000"; 11 public static final String MATRIX_TYPE_REC3000 = "REC3000";
12 12
  13 + public static final String MATRIX_TYPE_QRB = "QRB3000";
  14 +
13 public static final int COMMAND_RESET = 1; 15 public static final int COMMAND_RESET = 1;
14 16
15 public static final int COMMAND_SET_ATTN = 2; 17 public static final int COMMAND_SET_ATTN = 2;
src/main/java/com/example/mina/client/base/MatrixResponse.java
1 package com.example.mina.client.base; 1 package com.example.mina.client.base;
2 2
  3 +import lombok.Data;
  4 +
  5 +@Data
3 public class MatrixResponse { 6 public class MatrixResponse {
  7 + private Integer row;
  8 +
  9 + private Integer col;
  10 +
  11 + private Boolean isSetOffset;
  12 +
  13 + private Integer offset;
  14 +
  15 + private Boolean isSetAttn;
  16 +
  17 + private Integer attn;
  18 +
  19 + public MatrixResponse() {
  20 + isSetAttn = false;
  21 + isSetOffset = false;
  22 +
  23 + offset = -1;
  24 + attn = -1;
  25 + }
4 } 26 }
src/main/java/com/example/mina/client/box/aeroflex/AeroflexClientIoHandler.java
@@ -2,6 +2,7 @@ package com.example.mina.client.box.aeroflex; @@ -2,6 +2,7 @@ package com.example.mina.client.box.aeroflex;
2 2
3 import com.example.mina.client.base.AbstractMatrixIoHandler; 3 import com.example.mina.client.base.AbstractMatrixIoHandler;
4 import com.example.mina.client.base.MatrixDataProxy; 4 import com.example.mina.client.base.MatrixDataProxy;
  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.session.IoSession; 7 import org.apache.mina.core.session.IoSession;
7 8
@@ -22,8 +23,8 @@ public class AeroflexClientIoHandler extends AbstractMatrixIoHandler { @@ -22,8 +23,8 @@ public class AeroflexClientIoHandler extends AbstractMatrixIoHandler {
22 23
23 24
24 @Override 25 @Override
25 - public boolean handleCommandResponse(byte[] response) {  
26 - log.info("------AeroflexClientIoHandler-----{}",response); 26 + public boolean handleCommandResponse(MatrixResponse matrixResponse) {
  27 + log.info("------AeroflexClientIoHandler-----{}",matrixResponse);
27 return false; 28 return false;
28 } 29 }
29 30
src/main/java/com/example/mina/client/box/lte3000/Lte3000ClientIoHandler.java
No preview for this file type
src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ClientFactory.java 0 → 100644
@@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
  1 +package com.example.mina.client.box.qrb3000;
  2 +
  3 +import com.example.mina.client.base.AbstractClientFactory;
  4 +import com.example.mina.client.base.MatrixConstants;
  5 +import org.apache.mina.core.filterchain.IoFilterChain;
  6 +import org.apache.mina.filter.codec.ProtocolCodecFilter;
  7 +import org.springframework.stereotype.Component;
  8 +
  9 +@Component(MatrixConstants.MATRIX_TYPE_QRB)
  10 +public class Qrb3000ClientFactory extends AbstractClientFactory {
  11 +
  12 + @Override
  13 + public Qrb3000ClientIoHandler getClientHandler() {
  14 + System.out.println("=============getClientHandler==================");
  15 + return new Qrb3000ClientIoHandler();
  16 + }
  17 +
  18 + @Override
  19 + public void buildFilterChain(IoFilterChain ioFilterChain) {
  20 + ioFilterChain.addLast("codec", new ProtocolCodecFilter(new Qrb3000ProtocolFactory()));
  21 + }
  22 +}
src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ClientIoHandler.java 0 → 100644
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +package com.example.mina.client.box.qrb3000;
  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 Qrb3000ClientIoHandler extends AbstractMatrixIoHandler {
  15 +
  16 + //public AeroflexClientIoHandler(MatrixDataProxy matrixDataProxy) {
  17 + // super(matrixDataProxy);
  18 + // }
  19 +
  20 + public Qrb3000ClientIoHandler() {
  21 + super(new MatrixDataProxy());
  22 + }
  23 +
  24 +
  25 + @Override
  26 + public boolean handleCommandResponse(MatrixResponse matrixResponse) {
  27 + log.info("------Qrb3000ClientIoHandler-----{}",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/qrb3000/Qrb3000ProtocolFactory.java 0 → 100644
@@ -0,0 +1,130 @@ @@ -0,0 +1,130 @@
  1 +package com.example.mina.client.box.qrb3000;
  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.server.util.CommandHelper;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.apache.mina.core.buffer.IoBuffer;
  9 +import org.apache.mina.core.session.IoSession;
  10 +import org.apache.mina.filter.codec.*;
  11 +
  12 +import java.nio.charset.CharacterCodingException;
  13 +import java.nio.charset.CharsetDecoder;
  14 +import java.nio.charset.CharsetEncoder;
  15 +import java.nio.charset.StandardCharsets;
  16 +
  17 +@Slf4j
  18 +public class Qrb3000ProtocolFactory implements ProtocolCodecFactory {
  19 +
  20 + private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder();
  21 +
  22 + private static final CharsetEncoder ENCODER = StandardCharsets.UTF_8.newEncoder();
  23 +
  24 + @Override
  25 + public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
  26 + return new Qrb3000ProtocolEncoder();
  27 + }
  28 +
  29 + @Override
  30 + public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
  31 + return new Qrb3000ProtocolDecoder();
  32 + }
  33 +
  34 + public static class Qrb3000ProtocolEncoder extends ProtocolEncoderAdapter {
  35 +
  36 + @Override
  37 + public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput)
  38 + throws CharacterCodingException {
  39 +
  40 + if (!(msg instanceof MatrixCommand)) {
  41 + log.error("error msg, msg is: {}", msg);
  42 + return;
  43 + }
  44 +
  45 + MatrixCommand mc = (MatrixCommand) msg;
  46 + log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {},type = {}",
  47 + mc.getAttn(),
  48 + mc.getCol(),
  49 + mc.getCommand(),
  50 + mc.getRow(),
  51 + mc.getMatrixId(),
  52 + mc.getType()
  53 + );
  54 + int cmd = mc.getCommand();
  55 +
  56 + IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
  57 +
  58 + if (cmd == MatrixConstants.COMMAND_SET_ATTN) {
  59 + buffer.put((byte)0x30);
  60 + buffer.put((byte)0xFE);
  61 + buffer.put((byte)0xFE);
  62 + buffer.put(mc.getRow().byteValue());
  63 + buffer.put(mc.getCol().byteValue());
  64 + buffer.put(mc.getAttn().byteValue());
  65 + buffer.put((byte)0xFE);
  66 + buffer.put((byte)0xFE);
  67 + }
  68 + if (cmd == MatrixConstants.COMMAND_SET_OFFSET) {
  69 + buffer.put((byte)0x68);
  70 + buffer.put((byte)0xFE);
  71 + buffer.put((byte)0xFE);
  72 + buffer.put(mc.getCol().byteValue());
  73 + buffer.put(mc.getOffset().byteValue());
  74 + buffer.put((byte)0xFE);
  75 + buffer.put((byte)0xFE);
  76 + buffer.put((byte)0xFE);
  77 +
  78 + }
  79 + if (cmd == MatrixConstants.COMMAND_GET_OFFSET) {
  80 + buffer.put((byte)0xFE);
  81 + buffer.put((byte)0xFE);
  82 + buffer.put((byte)0xFE);
  83 + buffer.put(mc.getCol().byteValue());
  84 + buffer.put((byte)0xFE);
  85 + buffer.put((byte)0xFE);
  86 + buffer.put((byte)0xFE);
  87 + }
  88 +
  89 + buffer.flip();
  90 + protocolEncoderOutput.write(buffer);
  91 + }
  92 + }
  93 +
  94 + public static class Qrb3000ProtocolDecoder extends ProtocolDecoderAdapter {
  95 +
  96 + @Override
  97 + public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput)
  98 + throws Exception {
  99 + log.info("--server返回给----client ------IoBuffer{}", ioBuffer);
  100 + MatrixResponse response = new MatrixResponse();
  101 + int len = ioBuffer.limit();
  102 + byte[] bytes = new byte[len];
  103 + ioBuffer.get(bytes);
  104 + int cmd = bytes[0];
  105 + if(cmd == 0x30 && len==8) {
  106 + int row = CommandHelper.BCD_REV[bytes[3]];
  107 + int col = CommandHelper.BCD_REV[bytes[4]];
  108 + int attn = bytes[5];
  109 +
  110 + response.setRow(row);
  111 + response.setCol(col);
  112 + response.setIsSetAttn(true);
  113 + response.setAttn(attn);
  114 +
  115 + } else if (cmd == 0x68 && len ==8) {
  116 + int offset = bytes[6];
  117 + int col = CommandHelper.BCD_REV[bytes[3]];
  118 + response.setIsSetOffset(true);
  119 + response.setCol(col);
  120 + response.setOffset(offset);
  121 + } else {
  122 + int offset = bytes[6];
  123 + response.setOffset(offset);
  124 + }
  125 + protocolDecoderOutput.write(response);
  126 + }
  127 +
  128 + }
  129 +
  130 +}
src/main/java/com/example/mina/client/box/rec6000/REC6000ClientIoHandler.java
@@ -2,6 +2,7 @@ package com.example.mina.client.box.rec6000; @@ -2,6 +2,7 @@ package com.example.mina.client.box.rec6000;
2 2
3 import com.example.mina.client.base.AbstractMatrixIoHandler; 3 import com.example.mina.client.base.AbstractMatrixIoHandler;
4 import com.example.mina.client.base.MatrixDataProxy; 4 import com.example.mina.client.base.MatrixDataProxy;
  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.session.IoSession; 7 import org.apache.mina.core.session.IoSession;
7 8
@@ -22,8 +23,8 @@ public class REC6000ClientIoHandler extends AbstractMatrixIoHandler { @@ -22,8 +23,8 @@ public class REC6000ClientIoHandler extends AbstractMatrixIoHandler {
22 23
23 24
24 @Override 25 @Override
25 - public boolean handleCommandResponse(byte[] response) {  
26 - log.info("------REC3000ClientIoHandler-----{}",response); 26 + public boolean handleCommandResponse(MatrixResponse matrixResponse) {
  27 + log.info("------REC3000ClientIoHandler-----{}",matrixResponse);
27 return false; 28 return false;
28 } 29 }
29 30
src/main/java/com/example/mina/server/box/qrb3000/Qrb3000VirtualBoxHandler.java
@@ -50,6 +50,7 @@ public class Qrb3000VirtualBoxHandler @@ -50,6 +50,7 @@ public class Qrb3000VirtualBoxHandler
50 } 50 }
51 51
52 if (len == message.LENGTH8) { 52 if (len == message.LENGTH8) {
  53 +// byte[] input = message.getInput();
53 switch (message.getInput()[0]) { 54 switch (message.getInput()[0]) {
54 case 0x30: 55 case 0x30:
55 LogUtils.println(String.format("setAttenuation: %d, %d, %d", 56 LogUtils.println(String.format("setAttenuation: %d, %d, %d",
@@ -59,14 +60,14 @@ public class Qrb3000VirtualBoxHandler @@ -59,14 +60,14 @@ public class Qrb3000VirtualBoxHandler
59 dataBuffer.setAttenuation(CommandHelper.BCD_REV[message.getInput()[3]], 60 dataBuffer.setAttenuation(CommandHelper.BCD_REV[message.getInput()[3]],
60 CommandHelper.BCD_REV[message.getInput()[4]], 61 CommandHelper.BCD_REV[message.getInput()[4]],
61 message.getInput()[5]); 62 message.getInput()[5]);
  63 +
62 buffer[5] = (byte) dataBuffer.getAttenuation(CommandHelper.BCD_REV[message.getInput()[3]], 64 buffer[5] = (byte) dataBuffer.getAttenuation(CommandHelper.BCD_REV[message.getInput()[3]],
63 CommandHelper.BCD_REV[message.getInput()[4]]); 65 CommandHelper.BCD_REV[message.getInput()[4]]);
64 return Qrb3000ResponseMessage.builder().result(buffer).build(); 66 return Qrb3000ResponseMessage.builder().result(buffer).build();
65 67
66 case 0x68: 68 case 0x68:
67 // dataBuffer.setOffset(Qrb3000CommandHelper.BCD_REV[message.getInput()[3]], random.nextInt(120) - 60 ); 69 // dataBuffer.setOffset(Qrb3000CommandHelper.BCD_REV[message.getInput()[3]], random.nextInt(120) - 60 );
68 -  
69 - buffer[6] = (byte) dataBuffer.getOffset(CommandHelper.BCD_REV[message.getInput()[3]]); 70 + buffer[6] = (byte)dataBuffer.getOffset(CommandHelper.BCD_REV[message.getInput()[3]]);
70 return Qrb3000ResponseMessage.builder().result(buffer).build(); 71 return Qrb3000ResponseMessage.builder().result(buffer).build();
71 72
72 case 0x62: 73 case 0x62: