Lte3000ProtocolFactory.java
3.82 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
package com.example.mina.client.box.lte3000;
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 Lte3000ProtocolFactory 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 Lte3000ProtocolEncoder();
}
@Override
public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
return new Lte3000ProtocolDecoder();
}
public static class Lte3000ProtocolEncoder 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);
int row = mc.getRow();
byte[] command;
if (row == 0) {
command = Lte3000CommandHelper.genCommandSetAttn(mc.getCol(), mc.getAttn());
}else{
command = Lte3000CommandHelper.genCommandSetCross(row, mc.getCol());
}
buffer.put(command);
buffer.flip();
protocolEncoderOutput.write(buffer);
}
}
public static class Lte3000ProtocolDecoder 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 attn = bytes[5];
response.setRow(row);
response.setCol(col);
response.setIsSetAttn(true);
response.setAttn(attn);
} else if (cmd == 0x68 && len ==8) {
int offset = bytes[6];
int col = CommandHelper.BCD_REV[bytes[3]];
response.setIsSetOffset(true);
response.setCol(col);
response.setOffset(offset);
} else {
int offset = bytes[6];
response.setOffset(offset);
}
protocolDecoderOutput.write(response);
}
}
}