Commit 40f3a57b17c0e62d8953c9f1080c6aedcce533fa

Authored by dy
1 parent fe6c067a
Exists in develop

feat: 新增Rbm3000VirtualBox

src/main/java/com/example/mina/base/CommandHelper.java 0 → 100644
@@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
  1 +package com.example.mina.base;
  2 +
  3 +public class CommandHelper {
  4 + public static final byte[] BCD = new byte[]{
  5 + 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
  6 + 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,
  7 + 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,
  8 + 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
  9 + 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
  10 + 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
  11 + 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
  12 + 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
  13 + (byte)0x80,(byte)0x81,(byte)0x82,(byte)0x83,(byte)0x84,(byte)0x85,(byte)0x86,(byte)0x87,(byte)0x88,(byte)0x89,
  14 + (byte)0x90,(byte)0x91,(byte)0x92,(byte)0x93,(byte)0x94,(byte)0x95,(byte)0x96,(byte)0x97,(byte)0x98,(byte)0x99
  15 + };
  16 +
  17 + public static final int[] BCD_REV = new int[]{
  18 + 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
  19 + 10,11,12,13,14,15,16,17,18,19,-1,-1,-1,-1,-1,-1,
  20 + 20,21,22,23,24,25,26,27,28,29,-1,-1,-1,-1,-1,-1,
  21 + 30,31,32,33,34,35,36,37,38,39,-1,-1,-1,-1,-1,-1,
  22 + 40,41,42,43,44,45,46,47,48,49,-1,-1,-1,-1,-1,-1,
  23 + 50,51,52,53,54,55,56,57,58,59,-1,-1,-1,-1,-1,-1,
  24 + 60,61,62,63,64,65,66,67,68,69,-1,-1,-1,-1,-1,-1,
  25 + 70,71,72,73,74,75,76,77,78,79,-1,-1,-1,-1,-1,-1,
  26 + 80,81,82,83,84,85,86,87,88,89,-1,-1,-1,-1,-1,-1,
  27 + 90,91,92,93,94,95,96,97,98,99,-1,-1,-1,-1,-1,-1,
  28 + };
  29 +
  30 +
  31 +
  32 +
  33 +}
src/main/java/com/example/mina/box1/Rbm3000VirtualBoxHander.java 0 → 100644
@@ -0,0 +1,176 @@ @@ -0,0 +1,176 @@
  1 +package com.example.mina.box1;
  2 +
  3 +import com.example.mina.base.AbstractVirtualBoxHandler;
  4 +import com.example.mina.base.CommandHelper;
  5 +import com.example.mina.entity.AeroflexDataBuffer;
  6 +import com.example.mina.entity.Entry;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.apache.mina.core.buffer.IoBuffer;
  9 +import org.apache.mina.core.session.IoSession;
  10 +
  11 +/**
  12 + * @author dy
  13 + * @date 2021/3/5
  14 + */
  15 +@Slf4j
  16 +public class Rbm3000VirtualBoxHander extends AbstractVirtualBoxHandler {
  17 +
  18 + private AeroflexDataBuffer dataBuffer;
  19 +
  20 + public Rbm3000VirtualBoxHander() {
  21 + this.initMatrix();
  22 + }
  23 +
  24 + @Override
  25 + protected void initMatrix() {
  26 +
  27 + int row = 6;
  28 + int col = 8;
  29 + int maxAtten = 222;
  30 +
  31 + dataBuffer = new AeroflexDataBuffer(row, maxAtten);
  32 + }
  33 +
  34 + @Override
  35 + protected byte[] handleMessage(byte[] cmd, int len) {
  36 + return new byte[0];
  37 + }
  38 +
  39 + @Override
  40 + public void messageReceived(IoSession session, Object message) {
  41 +
  42 + IoBuffer ioBuffer = (IoBuffer) message;
  43 + byte[] bytes = new byte[ioBuffer.limit()];
  44 + ioBuffer.get(bytes, ioBuffer.position(), ioBuffer.limit());
  45 +
  46 + byte[] result = handleCommand(bytes, bytes.length);
  47 +
  48 + session.write(IoBuffer.wrap(result));
  49 + }
  50 +
  51 + @Override
  52 + public void sessionCreated(IoSession session) {
  53 +
  54 + log.info("---server session created");
  55 + }
  56 +
  57 + @Override
  58 + public void sessionOpened(IoSession session) {
  59 +
  60 + log.info("---server session Opened");
  61 + }
  62 +
  63 + @Override
  64 + public void sessionClosed(IoSession session) {
  65 +
  66 + log.info("---server session Closed");
  67 + }
  68 +
  69 + @Override
  70 + public void messageSent(IoSession session, Object message) {
  71 +
  72 + log.info("---发送数据成功了。。。{}", message);
  73 + }
  74 +
  75 + protected byte[] handleCommand(byte[] cmd, int len) {
  76 +
  77 + byte[] buffer;
  78 + Entry[][] entries = dataBuffer.getMatrixData();
  79 +
  80 + if (len == 8) {
  81 + switch (cmd[0]) {
  82 + case 0x30:
  83 + int row = CommandHelper.BCD_REV[cmd[3]];
  84 + int col = CommandHelper.BCD_REV[cmd[4]];
  85 +
  86 + int rowCol = 0;
  87 + int colRow = 0;
  88 +
  89 + for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
  90 + if (entries[row - 1][i].getValue() == 1) {
  91 + rowCol = i + 1;
  92 + break;
  93 + }
  94 + }
  95 + for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
  96 + if (entries[i][col - 1].getValue() == 1) {
  97 + colRow = i + 1;
  98 + }
  99 + }
  100 +
  101 + dataBuffer.setAttenuation(row, rowCol, 0);
  102 + dataBuffer.setAttenuation(row, col, 1);
  103 + dataBuffer.setAttenuation(colRow, col, 0);
  104 + dataBuffer.setAttenuation(colRow, rowCol, 1);
  105 +
  106 + buffer = new byte[8];
  107 + buffer[0] = cmd[0];
  108 + buffer[1] = cmd[1];
  109 + buffer[2] = cmd[2];
  110 + buffer[3] = cmd[3];
  111 + buffer[4] = cmd[4];
  112 + buffer[5] = CommandHelper.BCD[colRow];
  113 + buffer[6] = CommandHelper.BCD[rowCol];
  114 + buffer[7] = (byte) 0x03;
  115 +
  116 + return buffer;
  117 + case 0x68:
  118 + boolean isA = (cmd[3] == (byte) 0x0A);
  119 + int callPort = CommandHelper.BCD_REV[cmd[4]];
  120 + int returnPort = 0;
  121 +
  122 + if (isA) {
  123 + for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
  124 + if (entries[callPort - 1][i].getValue() == 1) {
  125 + returnPort = i + 1;
  126 + break;
  127 + }
  128 + }
  129 + } else {
  130 + for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
  131 + if (entries[i][callPort - 1].getValue() == 1) {
  132 + returnPort = i + 1;
  133 + }
  134 + }
  135 + }
  136 +
  137 + buffer = new byte[8];
  138 + buffer[0] = cmd[0];
  139 + buffer[1] = cmd[1];
  140 + buffer[2] = cmd[2];
  141 + if (isA) {
  142 + buffer[3] = 0x0B;
  143 + } else {
  144 + buffer[3] = 0x0A;
  145 + }
  146 + buffer[4] = CommandHelper.BCD[returnPort];
  147 + buffer[5] = 0x00;
  148 + buffer[6] = 0x06;
  149 + buffer[7] = (byte) 0x03;
  150 +
  151 + return buffer;
  152 + case 0x62:
  153 + buffer = new byte[8];
  154 + buffer[0] = cmd[0];
  155 + buffer[1] = cmd[1];
  156 + buffer[2] = cmd[2];
  157 + buffer[3] = cmd[3];
  158 + buffer[4] = cmd[4];
  159 + buffer[5] = cmd[5];
  160 + buffer[6] = cmd[6];
  161 + buffer[7] = (byte) 0x03;
  162 +
  163 + return buffer;
  164 + default:
  165 + break;
  166 + }
  167 + }
  168 +
  169 + buffer = new byte[len + 2];
  170 + buffer[0] = (byte) 0xFE;
  171 + buffer[1] = (byte) 0xFE;
  172 + System.arraycopy(cmd, 0, buffer, 2, len);
  173 +
  174 + return buffer;
  175 + }
  176 +}
src/main/java/com/example/mina/config/AeroflexVirtualBoxConfiguration.java
1 package com.example.mina.config; 1 package com.example.mina.config;
2 2
3 import com.example.mina.box1.AeroflexVirtualBoxHandler; 3 import com.example.mina.box1.AeroflexVirtualBoxHandler;
4 -import com.example.mina.property.AeroflexVirtualProperties; 4 +import com.example.mina.property.AeroflexVirtualBoxProperties;
5 import lombok.extern.slf4j.Slf4j; 5 import lombok.extern.slf4j.Slf4j;
6 import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 6 import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
7 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Configuration;
@@ -17,21 +17,21 @@ import java.net.InetSocketAddress; @@ -17,21 +17,21 @@ import java.net.InetSocketAddress;
17 @Configuration(proxyBeanMethods = false) 17 @Configuration(proxyBeanMethods = false)
18 public class AeroflexVirtualBoxConfiguration { 18 public class AeroflexVirtualBoxConfiguration {
19 19
20 - private final AeroflexVirtualProperties aeroflexVirtualProperties; 20 + private final AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties;
21 21
22 - public AeroflexVirtualBoxConfiguration(AeroflexVirtualProperties aeroflexVirtualProperties) {  
23 - this.aeroflexVirtualProperties = aeroflexVirtualProperties; 22 + public AeroflexVirtualBoxConfiguration(AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties) {
  23 + this.aeroflexVirtualBoxProperties = aeroflexVirtualBoxProperties;
24 } 24 }
25 25
26 @PostConstruct 26 @PostConstruct
27 public void init() { 27 public void init() {
28 28
29 - if (!aeroflexVirtualProperties.getEnable()) { 29 + if (!aeroflexVirtualBoxProperties.getEnable()) {
30 log.info("AeroflexVirtual服务端 配置未开启"); 30 log.info("AeroflexVirtual服务端 配置未开启");
31 return; 31 return;
32 } 32 }
33 33
34 - if (aeroflexVirtualProperties.getPort() == null) { 34 + if (aeroflexVirtualBoxProperties.getPort() == null) {
35 log.info("AeroflexVirtual服务端 端口未配置"); 35 log.info("AeroflexVirtual服务端 端口未配置");
36 return; 36 return;
37 } 37 }
@@ -40,8 +40,8 @@ public class AeroflexVirtualBoxConfiguration { @@ -40,8 +40,8 @@ public class AeroflexVirtualBoxConfiguration {
40 NioSocketAcceptor acceptor = new NioSocketAcceptor(); 40 NioSocketAcceptor acceptor = new NioSocketAcceptor();
41 acceptor.setHandler(new AeroflexVirtualBoxHandler()); 41 acceptor.setHandler(new AeroflexVirtualBoxHandler());
42 acceptor.setReuseAddress(true); 42 acceptor.setReuseAddress(true);
43 - acceptor.bind(new InetSocketAddress(aeroflexVirtualProperties.getPort()));  
44 - log.info("AeroflexVirtual服务端已经启动,监听端口: {}", aeroflexVirtualProperties.getPort()); 43 + acceptor.bind(new InetSocketAddress(aeroflexVirtualBoxProperties.getPort()));
  44 + log.info("AeroflexVirtual服务端已经启动,监听端口: {}", aeroflexVirtualBoxProperties.getPort());
45 } catch (Exception e) { 45 } catch (Exception e) {
46 log.error("无法启动AeroflexVirtual服务端, {}", e.getMessage(), e); 46 log.error("无法启动AeroflexVirtual服务端, {}", e.getMessage(), e);
47 } 47 }
src/main/java/com/example/mina/config/Rbm3000VirtualBoxConfiguration.java 0 → 100644
@@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
  1 +package com.example.mina.config;
  2 +
  3 +import com.example.mina.box1.Rbm3000VirtualBoxHander;
  4 +import com.example.mina.property.Rbm3000VitualBoxProperties;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
  7 +import org.springframework.context.annotation.Configuration;
  8 +
  9 +import javax.annotation.PostConstruct;
  10 +import java.net.InetSocketAddress;
  11 +
  12 +/**
  13 + * @author 杜云山
  14 + * @date 21/03/05
  15 + */
  16 +@Slf4j
  17 +@Configuration(proxyBeanMethods = false)
  18 +public class Rbm3000VirtualBoxConfiguration {
  19 +
  20 + private final Rbm3000VitualBoxProperties rbm3000VitualBoxProperties;
  21 +
  22 + public Rbm3000VirtualBoxConfiguration(Rbm3000VitualBoxProperties rbm3000VitualBoxProperties) {
  23 + this.rbm3000VitualBoxProperties = rbm3000VitualBoxProperties;
  24 + }
  25 +
  26 +
  27 + @PostConstruct
  28 + public void init() {
  29 +
  30 + if (!rbm3000VitualBoxProperties.getEnable()) {
  31 + log.info("Rbm3000Virtual服务端 配置未开启");
  32 + return;
  33 + }
  34 +
  35 + if (rbm3000VitualBoxProperties.getPort() == null) {
  36 + log.info("Rbm3000Virtual服务端 端口未配置");
  37 + return;
  38 + }
  39 +
  40 + try {
  41 + NioSocketAcceptor acceptor = new NioSocketAcceptor();
  42 + acceptor.setHandler(new Rbm3000VirtualBoxHander());
  43 + acceptor.setReuseAddress(true);
  44 + acceptor.bind(new InetSocketAddress(rbm3000VitualBoxProperties.getPort()));
  45 + log.info("Rbm3000Virtual服务端已经启动,监听端口: {}", rbm3000VitualBoxProperties.getPort());
  46 + } catch (Exception e) {
  47 + log.error("无法启动Rbm3000Virtual服务端, {}", e.getMessage(), e);
  48 + }
  49 + }
  50 +
  51 +}
src/main/java/com/example/mina/property/AeroflexVirtualBoxProperties.java 0 → 100644
@@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
  1 +package com.example.mina.property;
  2 +
  3 +import lombok.Getter;
  4 +import lombok.Setter;
  5 +import lombok.ToString;
  6 +import org.springframework.boot.context.properties.ConfigurationProperties;
  7 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
  8 +import org.springframework.context.annotation.Configuration;
  9 +
  10 +/**
  11 + * @author 杜云山
  12 + * @date 2021/03/05
  13 + */
  14 +@Getter
  15 +@Setter
  16 +@ToString
  17 +@ConfigurationProperties(prefix = "aeroflex-virtual")
  18 +@Configuration
  19 +@EnableConfigurationProperties(AeroflexVirtualBoxProperties.class)
  20 +public class AeroflexVirtualBoxProperties {
  21 +
  22 + private Boolean enable = false;
  23 +
  24 + private Integer port = null;
  25 +
  26 +}
0 \ No newline at end of file 27 \ No newline at end of file
src/main/java/com/example/mina/property/AeroflexVirtualProperties.java
@@ -1,26 +0,0 @@ @@ -1,26 +0,0 @@
1 -package com.example.mina.property;  
2 -  
3 -import lombok.Getter;  
4 -import lombok.Setter;  
5 -import lombok.ToString;  
6 -import org.springframework.boot.context.properties.ConfigurationProperties;  
7 -import org.springframework.boot.context.properties.EnableConfigurationProperties;  
8 -import org.springframework.context.annotation.Configuration;  
9 -  
10 -/**  
11 - * @author 杜云山  
12 - * @date 2021/03/05  
13 - */  
14 -@Getter  
15 -@Setter  
16 -@ToString  
17 -@ConfigurationProperties(prefix = "aeroflex-virtual")  
18 -@Configuration  
19 -@EnableConfigurationProperties(AeroflexVirtualProperties.class)  
20 -public class AeroflexVirtualProperties {  
21 -  
22 - private Boolean enable = false;  
23 -  
24 - private Integer port = null;  
25 -  
26 -}  
27 \ No newline at end of file 0 \ No newline at end of file
src/main/java/com/example/mina/property/Rbm3000VitualBoxProperties.java 0 → 100644
@@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
  1 +package com.example.mina.property;
  2 +
  3 +import lombok.Getter;
  4 +import lombok.Setter;
  5 +import lombok.ToString;
  6 +import org.springframework.boot.context.properties.ConfigurationProperties;
  7 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
  8 +import org.springframework.context.annotation.Configuration;
  9 +
  10 +/**
  11 + * @author 杜云山
  12 + * @date 2021/03/05
  13 + */
  14 +@Getter
  15 +@Setter
  16 +@ToString
  17 +@ConfigurationProperties(prefix = "rbm3000-vitualbox-virtual")
  18 +@Configuration
  19 +@EnableConfigurationProperties(Rbm3000VitualBoxProperties.class)
  20 +public class Rbm3000VitualBoxProperties {
  21 +
  22 + private Boolean enable = false;
  23 +
  24 + private Integer port = null;
  25 +
  26 +}
0 \ No newline at end of file 27 \ No newline at end of file
src/main/resources/application.yml
1 aeroflex-virtual: 1 aeroflex-virtual:
2 enable: true 2 enable: true
3 - port: 9100  
4 \ No newline at end of file 3 \ No newline at end of file
  4 + port: 9100
  5 +rbm3000-vitualbox-virtual:
  6 + enable: true
  7 + port: 9101