Rbm3000ProtocolFactory.java
4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.example.mina.client.box.rbm3000;
import com.example.mina.client.base.MatrixCommand;
import com.example.mina.client.base.MatrixConstants;
import com.example.mina.client.base.MatrixResponse;
import com.example.mina.processor.VbLogUtils;
import com.example.mina.server.util.CommandHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.*;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
@Slf4j
public class Rbm3000ProtocolFactory implements ProtocolCodecFactory {
private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder();
private static final CharsetEncoder ENCODER = StandardCharsets.UTF_8.newEncoder();
@Override
public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
return new Qrb3000ProtocolEncoder();
}
@Override
public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
return new Qrb3000ProtocolDecoder();
}
public static class Qrb3000ProtocolEncoder extends ProtocolEncoderAdapter {
@Override
public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput)
throws CharacterCodingException {
if (!(msg instanceof MatrixCommand)) {
log.error("error msg, msg is: {}", msg);
return;
}
MatrixCommand mc = (MatrixCommand) msg;
log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {},type = {}",
mc.getAttn(),
mc.getCol(),
mc.getCommand(),
mc.getRow(),
mc.getMatrixId()
);
int cmd = mc.getCommand();
VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg);
IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);
// Len==8 cmd【0】 0x30 set row/col attn=1
if (cmd == MatrixConstants.COMMAND_SET_ATTN) {
buffer.put((byte)0x30);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put(mc.getRow().byteValue());
buffer.put(mc.getCol().byteValue());
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
}else if (cmd == MatrixConstants.COMMAND_GET_ATTN) {
buffer.put((byte)0x68);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
if(mc.getRow() != null){
// row
buffer.put((byte)0x0A);
buffer.put(mc.getRow().byteValue());
} else {
// col
buffer.put((byte)0x0B);
buffer.put(mc.getCol().byteValue());
}
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
} else {
buffer.put((byte)0x62);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
buffer.put((byte)0xFE);
}
buffer.flip();
protocolEncoderOutput.write(buffer);
}
}
public static class Qrb3000ProtocolDecoder extends ProtocolDecoderAdapter {
@Override
public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput)
throws Exception {
log.info("--server返回给----client ------IoBuffer{}", ioBuffer);
MatrixResponse response = new MatrixResponse();
int len = ioBuffer.limit();
byte[] bytes = new byte[len];
ioBuffer.get(bytes);
int cmd = bytes[0];
if (cmd == 0x30 && len == 8) {
int row = CommandHelper.BCD_REV[bytes[3]];
int col = CommandHelper.BCD_REV[bytes[4]];
int rowCol = bytes[6];
int colRow = bytes[5];
response.setRow(row);
response.setCol(col);
response.setRowCol(rowCol);
response.setColRow(colRow);
response.setIsSetAttn(true);
}
if (cmd == 0x68 && len == 8) {
boolean isA = (bytes[3] == (byte) 0x0A);
int tmp = bytes[4];
if (isA) {
// col
response.setCol(tmp);
} else {
// row
response.setRow(tmp);
}
}
protocolDecoderOutput.write(response);
}
}
}