Commit 6c853c2409fce9fc58624bb689de2902fa3a1046
Exists in
develop
Merge branch 'develop' of http://139.198.15.205/linbenlei/virtualbox into develop
Showing
8 changed files
with
352 additions
and
0 deletions
Show diff stats
src/main/java/com/example/mina/box/qrb3000/Qrb3000CodecFactory.java
0 → 100644
... | ... | @@ -0,0 +1,70 @@ |
1 | +package com.example.mina.box.qrb3000; | |
2 | + | |
3 | +import org.apache.mina.core.buffer.IoBuffer; | |
4 | +import org.apache.mina.core.session.IoSession; | |
5 | +import org.apache.mina.filter.codec.*; | |
6 | + | |
7 | +/** | |
8 | + * @author dy | |
9 | + * @date 20/07/07 | |
10 | + */ | |
11 | +public class Qrb3000CodecFactory implements ProtocolCodecFactory { | |
12 | + | |
13 | + @Override | |
14 | + public ProtocolEncoder getEncoder(IoSession session) { | |
15 | + return new Qrb3000MessageEncoder(); | |
16 | + } | |
17 | + | |
18 | + @Override | |
19 | + public ProtocolDecoder getDecoder(IoSession session) { | |
20 | + return new Qrb3000MessageDecoder(); | |
21 | + } | |
22 | + | |
23 | + static class Qrb3000MessageEncoder implements ProtocolEncoder { | |
24 | + | |
25 | + @Override | |
26 | + public void encode(IoSession session, Object message, ProtocolEncoderOutput out) { | |
27 | + | |
28 | + if (message instanceof Qrb3000ResponseMessage) { | |
29 | + | |
30 | + byte[] result = ((Qrb3000ResponseMessage) message).getResult(); | |
31 | + session.write(IoBuffer.wrap(result)); | |
32 | + } | |
33 | + } | |
34 | + | |
35 | + @Override | |
36 | + public void dispose(IoSession session) { | |
37 | + } | |
38 | + | |
39 | + } | |
40 | + | |
41 | + static class Qrb3000MessageDecoder implements ProtocolDecoder { | |
42 | + | |
43 | + | |
44 | + @Override | |
45 | + public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { | |
46 | + | |
47 | + byte[] bytes = new byte[in.limit()]; | |
48 | + in.get(bytes); | |
49 | + | |
50 | + Qrb3000RequestMessage requestMessage = Qrb3000RequestMessage.builder() | |
51 | + .input(bytes) | |
52 | + .readNum(bytes.length) | |
53 | + .build(); | |
54 | + | |
55 | + out.write(requestMessage); | |
56 | + } | |
57 | + | |
58 | + @Override | |
59 | + public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception { | |
60 | + | |
61 | + } | |
62 | + | |
63 | + @Override | |
64 | + public void dispose(IoSession session) throws Exception { | |
65 | + | |
66 | + } | |
67 | + | |
68 | + } | |
69 | + | |
70 | +} | ... | ... |
src/main/java/com/example/mina/box/qrb3000/Qrb3000RequestMessage.java
0 → 100644
... | ... | @@ -0,0 +1,29 @@ |
1 | +package com.example.mina.box.qrb3000; | |
2 | + | |
3 | +import com.example.mina.base.BaseRequestMessage; | |
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 Qrb3000RequestMessage extends BaseRequestMessage { | |
18 | + | |
19 | + /** | |
20 | + * 从输入流中读取到的字节数 | |
21 | + */ | |
22 | + protected int readNum; | |
23 | + | |
24 | + /** | |
25 | + * 传递给服务器的消息 | |
26 | + */ | |
27 | + protected byte[] input; | |
28 | + | |
29 | +} | ... | ... |
src/main/java/com/example/mina/box/qrb3000/Qrb3000ResponseMessage.java
0 → 100644
... | ... | @@ -0,0 +1,19 @@ |
1 | +package com.example.mina.box.qrb3000; | |
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 Qrb3000ResponseMessage extends BaseResponseMessage { | |
18 | + | |
19 | +} | ... | ... |
src/main/java/com/example/mina/box/qrb3000/Qrb3000VirtualBoxHandler.java
0 → 100644
... | ... | @@ -0,0 +1,151 @@ |
1 | +package com.example.mina.box.qrb3000; | |
2 | + | |
3 | +import com.example.mina.base.AbstractVirtualBoxHandler; | |
4 | +import com.example.mina.entity.Qrb3000DataBuffer; | |
5 | +import com.example.mina.util.CommandHelper; | |
6 | +import com.example.mina.util.LogUtils; | |
7 | +import lombok.extern.slf4j.Slf4j; | |
8 | +import org.apache.mina.core.session.IoSession; | |
9 | + | |
10 | +/** | |
11 | + * @author dy | |
12 | + * @date 21/03/05 | |
13 | + */ | |
14 | +@Slf4j | |
15 | +public class Qrb3000VirtualBoxHandler | |
16 | + extends AbstractVirtualBoxHandler<Qrb3000RequestMessage, Qrb3000ResponseMessage> { | |
17 | + | |
18 | + private Qrb3000DataBuffer dataBuffer; | |
19 | + | |
20 | + public Qrb3000VirtualBoxHandler() { | |
21 | + this.initMatrix(); | |
22 | + } | |
23 | + | |
24 | + @Override | |
25 | + protected void initMatrix() { | |
26 | + | |
27 | + int row = 100; | |
28 | + int col = 100; | |
29 | + int maxAttenuate = 63; | |
30 | + | |
31 | + dataBuffer = new Qrb3000DataBuffer(row, col, maxAttenuate); | |
32 | + } | |
33 | + | |
34 | + @Override | |
35 | + public void messageReceived(IoSession session, Object message) { | |
36 | + | |
37 | + if (message instanceof Qrb3000RequestMessage) { | |
38 | + | |
39 | + Qrb3000ResponseMessage responseMessage = handleMessage((Qrb3000RequestMessage) message); | |
40 | + session.write(responseMessage); | |
41 | + } | |
42 | + } | |
43 | + | |
44 | + @Override | |
45 | + protected Qrb3000ResponseMessage handleMessage(Qrb3000RequestMessage message) { | |
46 | + String cmd = new String(message.getInput()).trim(); | |
47 | + | |
48 | + log.info("qrb3000VirtualBoxHandler receive: {}", cmd); | |
49 | + | |
50 | + byte[] buffer; | |
51 | + | |
52 | + int len = message.getReadNum(); | |
53 | + | |
54 | + //F: Firmware Version / Unit ID Page 82 | |
55 | + //02 XX XX 46 03 XX | |
56 | + //response | |
57 | + //06 XX XX 46 76 37 2E 30 30 20 50 76 32 2E 31 35 20… | |
58 | + //ACK ADR ADR F v 7 . 0 0 P v 2 . 1 5 … | |
59 | + //…58 52 4D 32 32 35 30 2F 30 33 32 58 30 33 32 3 XX | |
60 | + //… X R M 2 2 5 0 / 0 3 2 X 0 3 2 ETX CHK | |
61 | + | |
62 | + if (len == 6) { | |
63 | + buffer = new byte[]{0x06, 0x30, 0x30, 0x46, 0x76, 0x37, 0x2E, 0x30, 0x30, 0x20, 0x50 | |
64 | + , 0x76, 0x32, 0x2E, 0x31, 0x35, 0x20, 0x58, 0x52, 0x4D, 0x32, 0x32, 0x35, 0x30 | |
65 | + , 0x2F, 0x30, 0x33, 0x32, 0x58, 0x30, 0x33, 0x32, 0x3, 0x00}; | |
66 | + return Qrb3000ResponseMessage.builder().result(buffer).build(); | |
67 | + } | |
68 | + | |
69 | + | |
70 | + if (len == 8) { | |
71 | + switch (message.getInput()[0]) { | |
72 | + case 0x30: | |
73 | + LogUtils.println(String.format("setAttenuation: %d, %d, %d", CommandHelper.BCD_REV[message.getInput()[3]], CommandHelper.BCD_REV[message.getInput()[4]], message.getInput()[5])); | |
74 | + dataBuffer.setAttenuation(CommandHelper.BCD_REV[message.getInput()[3]], CommandHelper.BCD_REV[message.getInput()[4]], message.getInput()[5]); | |
75 | + buffer = new byte[8]; | |
76 | + buffer[0] = message.getInput()[0]; | |
77 | + buffer[1] = message.getInput()[1]; | |
78 | + buffer[2] = message.getInput()[2]; | |
79 | + buffer[3] = message.getInput()[3]; | |
80 | + buffer[4] = message.getInput()[4]; | |
81 | + buffer[5] = (byte) dataBuffer.getAttenuation(CommandHelper.BCD_REV[message.getInput()[3]], CommandHelper.BCD_REV[message.getInput()[4]]); | |
82 | + buffer[6] = (byte) 0xAA; | |
83 | + buffer[7] = (byte) 0xAA; | |
84 | + return Qrb3000ResponseMessage.builder().result(buffer).build(); | |
85 | + | |
86 | + case 0x68: | |
87 | +// dataBuffer.setOffset(Qrb3000CommandHelper.BCD_REV[message.getInput()[3]], random.nextInt(120) - 60 ); | |
88 | + | |
89 | + buffer = new byte[8]; | |
90 | + buffer[0] = message.getInput()[0]; | |
91 | + buffer[1] = message.getInput()[1]; | |
92 | + buffer[2] = message.getInput()[2]; | |
93 | + buffer[3] = message.getInput()[3]; | |
94 | + buffer[4] = 0x00;//TODO | |
95 | + buffer[5] = 0x00;//TODO | |
96 | + buffer[6] = (byte) dataBuffer.getOffset(CommandHelper.BCD_REV[message.getInput()[3]]); | |
97 | + buffer[7] = (byte) 0xAA; | |
98 | + return Qrb3000ResponseMessage.builder().result(buffer).build(); | |
99 | + | |
100 | + case 0x62: | |
101 | + return Qrb3000ResponseMessage.builder().result(message.getInput()).build(); | |
102 | + default: | |
103 | + break; | |
104 | + } | |
105 | + } else { | |
106 | + buffer = new byte[8]; | |
107 | + buffer[0] = message.getInput()[0]; | |
108 | + buffer[1] = message.getInput()[1]; | |
109 | + buffer[2] = message.getInput()[2]; | |
110 | + buffer[3] = message.getInput()[3]; | |
111 | + buffer[4] = 0x00;//TODO | |
112 | + buffer[5] = 0x00;//TODO | |
113 | + buffer[6] = (byte) dataBuffer.getOffset(CommandHelper.BCD_REV[message.getInput()[3]]); | |
114 | + buffer[7] = (byte) 0xAA; | |
115 | + return Qrb3000ResponseMessage.builder().result(buffer).build(); | |
116 | + } | |
117 | + | |
118 | + buffer = new byte[len + 2]; | |
119 | + buffer[0] = (byte) 0xFE; | |
120 | + buffer[1] = (byte) 0xFE; | |
121 | + System.arraycopy(message.getInput(), 0, buffer, 2, len); | |
122 | + | |
123 | + return Qrb3000ResponseMessage.builder().result(buffer).build(); | |
124 | + | |
125 | + } | |
126 | + | |
127 | + @Override | |
128 | + public void sessionCreated(IoSession session) { | |
129 | + | |
130 | + log.info("--- abstractVirtual server session created"); | |
131 | + } | |
132 | + | |
133 | + @Override | |
134 | + public void sessionOpened(IoSession session) { | |
135 | + | |
136 | + log.info("--- abstractVirtual server session Opened"); | |
137 | + } | |
138 | + | |
139 | + @Override | |
140 | + public void sessionClosed(IoSession session) { | |
141 | + | |
142 | + log.info("--- abstractVirtual server session Closed"); | |
143 | + } | |
144 | + | |
145 | + @Override | |
146 | + public void messageSent(IoSession session, Object message) { | |
147 | + | |
148 | + log.info("--- abstractVirtual 发送数据成功!{}", message); | |
149 | + } | |
150 | + | |
151 | +} | ... | ... |
src/main/java/com/example/mina/box/qrb3000/Qrb3000VirtualBoxServer.java
0 → 100644
... | ... | @@ -0,0 +1,21 @@ |
1 | +package com.example.mina.box.qrb3000; | |
2 | + | |
3 | +import com.example.mina.base.AbstractVirtualBoxServer; | |
4 | +import com.example.mina.property.AeroflexVirtualBoxProperties; | |
5 | +import com.example.mina.property.Qrb3000VirtualBoxProperties; | |
6 | +import lombok.extern.slf4j.Slf4j; | |
7 | +import org.springframework.context.annotation.Configuration; | |
8 | + | |
9 | +/** | |
10 | + * @author dy | |
11 | + * @date 21/03/05 | |
12 | + */ | |
13 | +@Slf4j | |
14 | +@Configuration(proxyBeanMethods = false) | |
15 | +public class Qrb3000VirtualBoxServer extends AbstractVirtualBoxServer { | |
16 | + | |
17 | + public Qrb3000VirtualBoxServer(Qrb3000VirtualBoxProperties qrb3000VirtualBoxProperties) { | |
18 | + super(qrb3000VirtualBoxProperties, new Qrb3000VirtualBoxHandler(), new Qrb3000CodecFactory()); | |
19 | + } | |
20 | + | |
21 | +} | ... | ... |
src/main/java/com/example/mina/entity/Qrb3000DataBuffer.java
0 → 100644
... | ... | @@ -0,0 +1,36 @@ |
1 | +package com.example.mina.entity; | |
2 | + | |
3 | +import com.example.mina.base.AbstractHardwareDataBuffer; | |
4 | + | |
5 | +public class Qrb3000DataBuffer extends AbstractHardwareDataBuffer { | |
6 | + | |
7 | + public Qrb3000DataBuffer(int row, int col, int maxAtten) { | |
8 | + super(row, col, row, maxAtten); | |
9 | + | |
10 | + matrixData = new Entry[row][col]; | |
11 | + offsetData = new Entry[row]; | |
12 | + | |
13 | + int offset = -79; | |
14 | + int val = 0; | |
15 | + int ox = 0; | |
16 | + for (int i = 0; i < row; i++) { | |
17 | + val = Math.abs(offset); | |
18 | + ox = (val / 10) * 16 + (val % 10); | |
19 | + | |
20 | + if (offset < 0) { | |
21 | + ox += 0x80; | |
22 | + } | |
23 | + | |
24 | + offsetData[i] = new Entry(i, 0, "rr", ox, false); | |
25 | + offset += 5; | |
26 | + } | |
27 | + | |
28 | + for (int i = 0; i < row; i++) { | |
29 | + for (int k = 0; k < col; k++) { | |
30 | + matrixData[i][k] = new Entry(i, k, "kk", maxAtten, false); | |
31 | + } | |
32 | + } | |
33 | + } | |
34 | + | |
35 | + | |
36 | +} | ... | ... |
src/main/java/com/example/mina/property/Qrb3000VirtualBoxProperties.java
0 → 100644
... | ... | @@ -0,0 +1,22 @@ |
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 dy | |
12 | + * @date 2021/03/05 | |
13 | + */ | |
14 | +@Getter | |
15 | +@Setter | |
16 | +@ToString | |
17 | +@ConfigurationProperties(prefix = "qrb3000-virtual") | |
18 | +@Configuration | |
19 | +@EnableConfigurationProperties(Qrb3000VirtualBoxProperties.class) | |
20 | +public class Qrb3000VirtualBoxProperties extends AbstractVirtualBoxProperties { | |
21 | + | |
22 | +} | |
0 | 23 | \ No newline at end of file | ... | ... |