Commit 609ec093fd8023de9248342ee08ce084f5112c02

Authored by dy
1 parent ed3de4ae
Exists in develop

feat: Rbm3000Virtual完成

src/main/java/com/example/mina/box/qrb3000/Qrb3000RequestMessage.java
... ... @@ -27,11 +27,6 @@ public class Qrb3000RequestMessage extends BaseRequestMessage {
27 27 protected final int LENGTH8 = 8;
28 28  
29 29 /**
30   - * 从输入流中读取到的字节数
31   - */
32   - protected int readNum;
33   -
34   - /**
35 30 * 传递给服务器的消息
36 31 */
37 32 protected byte[] input;
... ... @@ -41,9 +36,4 @@ public class Qrb3000RequestMessage extends BaseRequestMessage {
41 36 */
42 37 protected byte[] analyticalMessage;
43 38  
44   - /**
45   - * 特定字节数据需要设置衰减值 (ox30)
46   - */
47   - protected int firstByte;
48   -
49 39 }
... ...
src/main/java/com/example/mina/box/qrb3000/Qrb3000VirtualBoxHandler.java
... ... @@ -35,7 +35,7 @@ public class Qrb3000VirtualBoxHandler
35 35  
36 36 byte[] buffer = message.getAnalyticalMessage();
37 37  
38   - int len = message.getReadNum();
  38 + int len = message.getInput().length;
39 39  
40 40 //F: Firmware Version / Unit ID Page 82
41 41 //02 XX XX 46 03 XX
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000CodecFactory.java
1   -/*
2 1 package com.example.mina.box.rbm3000;
3 2  
  3 +import com.example.mina.util.CommandHelper;
4 4 import org.apache.mina.core.buffer.IoBuffer;
5 5 import org.apache.mina.core.session.IoSession;
6 6 import org.apache.mina.filter.codec.*;
7 7  
8   -*/
9 8 /**
10   - * @author 杜云山
11   - * @date 20/07/07
12   - *//*
  9 + * @author dy
  10 + * @date 21/03/09
  11 + */
13 12  
14 13 public class Rbm3000CodecFactory implements ProtocolCodecFactory {
15 14  
16 15 @Override
17 16 public ProtocolEncoder getEncoder(IoSession session) {
18   - return new AeroflexMessageEncoder();
  17 + return new Rbm3000MessageEncoder();
19 18 }
20 19  
21 20 @Override
22 21 public ProtocolDecoder getDecoder(IoSession session) {
23   - return new AeroflexMessageDecoder();
  22 + return new Rbm3000MessageDecoder();
24 23 }
25 24  
26   - static class AeroflexMessageEncoder implements ProtocolEncoder {
  25 + static class Rbm3000MessageEncoder implements ProtocolEncoder {
27 26  
28 27 @Override
29 28 public void encode(IoSession session, Object message, ProtocolEncoderOutput out) {
30   - if (message instanceof IoBuffer) {
31   - session.write(message);
  29 + if (message instanceof Rbm3000ResponseMessage) {
  30 +
  31 + byte[] result = ((Rbm3000ResponseMessage) message).getResult();
  32 + session.write(IoBuffer.wrap(result));
32 33 }
33 34 }
34 35  
... ... @@ -38,29 +39,102 @@ public class Rbm3000CodecFactory implements ProtocolCodecFactory {
38 39  
39 40 }
40 41  
41   - static class AeroflexMessageDecoder implements ProtocolDecoder {
  42 + static class Rbm3000MessageDecoder implements ProtocolDecoder {
42 43  
43 44 // private final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
44 45  
45 46 @Override
46   - public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
47   -
48   -// String string = in.getString(decoder);
  47 + public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) {
  48 + byte[] bytes = new byte[in.limit()];
  49 + in.get(bytes);
  50 +
  51 + byte[] buffer;
  52 +
  53 + Rbm3000RequestMessage rbm3000RequestMessage = Rbm3000RequestMessage.builder()
  54 + .input(bytes)
  55 + .build();
  56 +
  57 + if (bytes.length == rbm3000RequestMessage.LENGTH8) {
  58 + switch (bytes[0]) {
  59 + case 0x30:
  60 + int row = CommandHelper.BCD_REV[bytes[3]];
  61 + int col = CommandHelper.BCD_REV[bytes[4]];
  62 +
  63 + int rowCol = 0;
  64 + int colRow = 0;
  65 +
  66 + rbm3000RequestMessage.setRow(row);
  67 + rbm3000RequestMessage.setRow(col);
  68 + rbm3000RequestMessage.setRow(rowCol);
  69 + rbm3000RequestMessage.setRow(colRow);
  70 +
  71 + buffer = new byte[8];
  72 + buffer[0] = bytes[0];
  73 + buffer[1] = bytes[1];
  74 + buffer[2] = bytes[2];
  75 + buffer[3] = bytes[3];
  76 + buffer[4] = bytes[4];
  77 +
  78 + buffer[5] = CommandHelper.BCD[colRow];
  79 + buffer[6] = CommandHelper.BCD[rowCol];
  80 +
  81 + buffer[7] = (byte) 0x03;
  82 +
  83 + rbm3000RequestMessage.setAnalyticalMessage(buffer);
  84 + break;
  85 + case 0x68:
  86 + boolean isA = (bytes[3] == (byte) 0x0A);
  87 + int callPort = CommandHelper.BCD_REV[bytes[4]];
  88 + int returnPort = 0;
  89 +
  90 + rbm3000RequestMessage.setCallPort(callPort);
  91 + rbm3000RequestMessage.setA(isA);
  92 + rbm3000RequestMessage.setReturnPort(returnPort);
  93 +
  94 + buffer = new byte[8];
  95 + buffer[0] = bytes[0];
  96 + buffer[1] = bytes[1];
  97 + buffer[2] = bytes[2];
  98 + if (isA) {
  99 + buffer[3] = 0x0B;
  100 + } else {
  101 + buffer[3] = 0x0A;
  102 + }
  103 +
  104 + buffer[5] = 0x00;
  105 + buffer[6] = 0x06;
  106 + buffer[7] = (byte) 0x03;
  107 +
  108 + rbm3000RequestMessage.setAnalyticalMessage(buffer);
  109 + break;
  110 + default:
  111 + buffer = new byte[8];
  112 + buffer[0] = bytes[0];
  113 + buffer[1] = bytes[1];
  114 + buffer[2] = bytes[2];
  115 + buffer[3] = bytes[3];
  116 + buffer[4] = bytes[4];
  117 + buffer[5] = bytes[5];
  118 + buffer[6] = bytes[6];
  119 + buffer[7] = (byte) 0x03;
  120 +
  121 + rbm3000RequestMessage.setAnalyticalMessage(buffer);
  122 + break;
  123 + }
  124 + }
  125 + out.write(rbm3000RequestMessage);
49 126  
50   -// out.write(string);
51 127 }
52 128  
53 129 @Override
54   - public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
  130 + public void finishDecode(IoSession session, ProtocolDecoderOutput out) {
55 131  
56 132 }
57 133  
58 134 @Override
59   - public void dispose(IoSession session) throws Exception {
  135 + public void dispose(IoSession session) {
60 136  
61 137 }
62 138  
63 139 }
64   -
65 140 }
66   -*/
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000RequestMessage.java 0 → 100644
... ... @@ -0,0 +1,59 @@
  1 +package com.example.mina.box.rbm3000;
  2 +
  3 +import com.example.mina.base.BaseRequestMessage;
  4 +import com.example.mina.util.CommandHelper;
  5 +import lombok.AllArgsConstructor;
  6 +import lombok.Data;
  7 +import lombok.EqualsAndHashCode;
  8 +import lombok.experimental.SuperBuilder;
  9 +
  10 +/**
  11 + * @author dy
  12 + * @date 21/03/08
  13 + */
  14 +@EqualsAndHashCode(callSuper = true)
  15 +@SuperBuilder
  16 +@Data
  17 +@AllArgsConstructor
  18 +public class Rbm3000RequestMessage extends BaseRequestMessage {
  19 +
  20 + /**
  21 + * 读取的字节数
  22 + */
  23 + protected final int LENGTH6 = 6;
  24 +
  25 + /**
  26 + * 读取的字节数
  27 + */
  28 + protected final int LENGTH8 = 8;
  29 +
  30 + /**
  31 + * 从输入流中读取到的字节数
  32 + */
  33 + protected int readNum;
  34 +
  35 + /**
  36 + * 传递给服务器的消息
  37 + */
  38 + protected byte[] input;
  39 +
  40 + /**
  41 + * 解析后的消息
  42 + */
  43 + protected byte[] analyticalMessage;
  44 +
  45 + private int row;
  46 +
  47 + private int col;
  48 +
  49 + private int rowCol;
  50 +
  51 + private int colRow;
  52 +
  53 + private boolean a;
  54 +
  55 + private int callPort;
  56 +
  57 + private int returnPort;
  58 +
  59 +}
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000ResponseMessage.java 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +package com.example.mina.box.rbm3000;
  2 +
  3 +import com.example.mina.base.BaseResponseMessage;
  4 +import lombok.AllArgsConstructor;
  5 +import lombok.Data;
  6 +import lombok.EqualsAndHashCode;
  7 +import lombok.experimental.SuperBuilder;
  8 +
  9 +/**
  10 + * @author dy
  11 + * @date 21/03/08
  12 + */
  13 +@EqualsAndHashCode(callSuper = true)
  14 +@SuperBuilder
  15 +@Data
  16 +@AllArgsConstructor
  17 +public class Rbm3000ResponseMessage extends BaseResponseMessage {
  18 +
  19 +}
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000VirtualBoxHander.java
1   -//package com.example.mina.box.rbm3000;
2   -//
3   -//import com.example.mina.base.AbstractVirtualBoxHandler;
4   -//import com.example.mina.util.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   -//}
  1 +package com.example.mina.box.rbm3000;
  2 +
  3 +import com.example.mina.base.AbstractVirtualBoxHandler;
  4 +import com.example.mina.entity.Entry;
  5 +import com.example.mina.entity.Rbm3000DataBuffer;
  6 +import com.example.mina.util.CommandHelper;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.apache.mina.core.session.IoSession;
  9 +
  10 +/**
  11 + * @author dy
  12 + * @date 2021/3/5
  13 + */
  14 +@Slf4j
  15 +public class Rbm3000VirtualBoxHander extends AbstractVirtualBoxHandler<Rbm3000RequestMessage, Rbm3000ResponseMessage> {
  16 +
  17 + private Rbm3000DataBuffer dataBuffer;
  18 +
  19 + public Rbm3000VirtualBoxHander() {
  20 + this.initMatrix();
  21 + }
  22 +
  23 + @Override
  24 + protected void initMatrix() {
  25 +
  26 + int row = 6;
  27 + int col = 8;
  28 + int maxAtten = 222;
  29 +
  30 + dataBuffer = new Rbm3000DataBuffer(row, col);
  31 + }
  32 +
  33 + @Override
  34 + protected Rbm3000ResponseMessage handleMessage(Rbm3000RequestMessage message) {
  35 +
  36 + byte[] buffer = message.getAnalyticalMessage();
  37 +
  38 + int len = message.getInput().length;
  39 +
  40 + Entry[][] entries = dataBuffer.getMatrixData();
  41 +
  42 + if (len == message.LENGTH8) {
  43 + switch (message.getInput()[0]) {
  44 + case 0x30:
  45 + int row = message.getRow();
  46 + int col = message.getCol();
  47 +
  48 + int rowCol = message.getRowCol();
  49 + int colRow = message.getColRow();
  50 +
  51 + for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
  52 + if (entries[row - 1][i].getValue() == 1) {
  53 + rowCol = i + 1;
  54 + break;
  55 + }
  56 + }
  57 + for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
  58 + if (entries[i][col - 1].getValue() == 1) {
  59 + colRow = i + 1;
  60 + }
  61 + }
  62 +
  63 + dataBuffer.setAttenuation(row, rowCol, 0);
  64 + dataBuffer.setAttenuation(row, col, 1);
  65 + dataBuffer.setAttenuation(colRow, col, 0);
  66 + dataBuffer.setAttenuation(colRow, rowCol, 1);
  67 +
  68 + buffer[5] = CommandHelper.BCD[colRow];
  69 + buffer[6] = CommandHelper.BCD[rowCol];
  70 +
  71 + return Rbm3000ResponseMessage.builder().result(buffer).build();
  72 +
  73 + case 0x68:
  74 + boolean isA = message.isA();
  75 + int callPort = message.getCallPort();
  76 + int returnPort = message.getReturnPort();
  77 +
  78 + if (isA) {
  79 + for (int i = 0; i < dataBuffer.getMaxCol(); i++) {
  80 + if (entries[callPort - 1][i].getValue() == 1) {
  81 + returnPort = i + 1;
  82 + break;
  83 + }
  84 + }
  85 + } else {
  86 + for (int i = 0; i < dataBuffer.getMaxRow(); i++) {
  87 + if (entries[i][callPort - 1].getValue() == 1) {
  88 + returnPort = i + 1;
  89 + }
  90 + }
  91 + }
  92 +
  93 + buffer[4] = CommandHelper.BCD[returnPort];
  94 +
  95 + return Rbm3000ResponseMessage.builder().result(buffer).build();
  96 + case 0x62:
  97 + return Rbm3000ResponseMessage.builder().result(buffer).build();
  98 + default:
  99 + break;
  100 + }
  101 + }
  102 +
  103 + buffer = new byte[len + 2];
  104 + buffer[0] = (byte) 0xFE;
  105 + buffer[1] = (byte) 0xFE;
  106 + System.arraycopy(message.getInput(), 0, buffer, 2, len);
  107 +
  108 + return Rbm3000ResponseMessage.builder().result(buffer).build();
  109 + }
  110 +
  111 + @Override
  112 + public void sessionCreated(IoSession session) {
  113 +
  114 + log.info("---server session created");
  115 + }
  116 +
  117 + @Override
  118 + public void sessionOpened(IoSession session) {
  119 +
  120 + log.info("---server session Opened");
  121 + }
  122 +
  123 + @Override
  124 + public void sessionClosed(IoSession session) {
  125 +
  126 + log.info("---server session Closed");
  127 + }
  128 +
  129 + @Override
  130 + public void messageSent(IoSession session, Object message) {
  131 +
  132 + log.info("---发送数据成功了。。。{}", message);
  133 + }
  134 +
  135 +}
... ...
src/main/java/com/example/mina/box/rbm3000/Rbm3000VirtualBoxServer.java
1   -//package com.example.mina.box.rbm3000;
2   -//
3   -//import com.example.mina.base.AbstractVirtualBoxServer;
4   -//import com.example.mina.property.Rbm3000VitualBoxProperties;
5   -//import lombok.extern.slf4j.Slf4j;
6   -//import org.springframework.context.annotation.Configuration;
7   -//
8   -///**
9   -// * @author 杜云山
10   -// * @date 21/03/05
11   -// */
12   -//@Slf4j
13   -//@Configuration(proxyBeanMethods = false)
14   -//public class Rbm3000VirtualBoxServer extends AbstractVirtualBoxServer {
15   -//
16   -// public Rbm3000VirtualBoxServer(Rbm3000VitualBoxProperties rbm3000VitualBoxProperties) {
17   -// super(rbm3000VitualBoxProperties, new Rbm3000VirtualBoxHander(), new Rbm3000CodecFactory());
18   -// }
19   -//
20   -//}
  1 +package com.example.mina.box.rbm3000;
  2 +
  3 +import com.example.mina.base.AbstractVirtualBoxServer;
  4 +import com.example.mina.property.Rbm3000VitualBoxProperties;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.springframework.context.annotation.Configuration;
  7 +
  8 +/**
  9 + * @author dy
  10 + * @date 21/03/09
  11 + */
  12 +@Slf4j
  13 +@Configuration(proxyBeanMethods = false)
  14 +public class Rbm3000VirtualBoxServer extends AbstractVirtualBoxServer {
  15 +
  16 + public Rbm3000VirtualBoxServer(Rbm3000VitualBoxProperties rbm3000VitualBoxProperties) {
  17 + super(rbm3000VitualBoxProperties, new Rbm3000VirtualBoxHander(), new Rbm3000CodecFactory());
  18 + }
  19 +
  20 +}
... ...
src/main/java/com/example/mina/entity/Rbm3000DataBuffer.java 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +package com.example.mina.entity;
  2 +
  3 +import com.example.mina.base.AbstractHardwareDataBuffer;
  4 +
  5 +/**
  6 + * @author dy
  7 + * @date 21/03/08
  8 + */
  9 +public class Rbm3000DataBuffer extends AbstractHardwareDataBuffer {
  10 + private static final int MAX_ATTEN = 1;
  11 +
  12 + public Rbm3000DataBuffer(int row, int col) {
  13 +
  14 + super(row, col, 0, MAX_ATTEN);
  15 +
  16 + matrixData = new Entry[row][col];
  17 + offsetData = new Entry[0];
  18 +
  19 + for (int i = 0; i < row; i++) {
  20 + for (int k = 0; k < col; k++) {
  21 + matrixData[i][k] = new Entry(i, k, "kk", i == k ? MAX_ATTEN : 0, false);
  22 + }
  23 + }
  24 + }
  25 +
  26 +
  27 +}
... ...