#
whycq
2024-08-15 e54492fb532366faac8aa5d46ad8905ea3ff38d5
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
package com.example.agvcontroller.protocol;
 
import com.example.agvcontroller.socket.RadixTools;
 
import java.util.logging.Logger;
 
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
 
/**
 * 编码器
 * 此解码器将会生成校验码
 * 处理方式: 异或和
 * Created by vincent on 2019-04-02
 */
//@Component
@ChannelHandler.Sharable
public class ProtocolEncoder extends MessageToByteEncoder<Object> {
 
    //private static final Logger log = LoggerFactory.getLogger(ProtocolEncoder.class);
 
    //@Autowired
    private SystemProperties systemProperties;
 
    @Override
    protected void encode(ChannelHandlerContext ctx, Object obj, ByteBuf out) {
 
        if (obj instanceof ByteBuf){
            out.writeBytes((ByteBuf) obj);
 
        } else if (obj instanceof byte[]){
            out.writeBytes((byte[]) obj);
 
 
        } else if (obj instanceof AgvAction<?>){
 
 
            AgvAction action = (AgvAction)obj;
 
            String uniqueNo = action.getAgvNo();
 
            byte[] uniqueNoBytes = RadixTools.intToBytes(Integer.parseInt(uniqueNo));   // uniqueno
 
            byte[] bodyBytes = action.writeToBytes();
 
 
            int len = PackagePart.UNIQUENO.getLen()     // len
                    + PackagePart.TIMESTAMP.getLen()
                    + PackagePart.COMMAND_MARK.getLen()
                    + bodyBytes.length;
 
            out.writeByte((byte)0xEE)         // symbol
                    .writeShortLE(len)
                    .writeBytes(Utils.reverse(uniqueNoBytes))       // uniqueno
                    .writeIntLE((int) (System.currentTimeMillis() / 1000))   // timestamp
                    .writeByte(ProtocolPojoType.ACTION_COMMAND.protocolType.getCode()) // type
                    .writeBytes(bodyBytes)                          // body
                    .writeShort((short)0)                 // valid
            ;
 
            int validCode = ValidUtil.calculateValidByteFromBuff(out);
            out.resetReaderIndex();
 
            out.writerIndex(out.readableBytes() - 2);
            out.writeShortLE(validCode);
 
        } else if (obj instanceof AgvPackage){
 
            AgvPackage pac = (AgvPackage)obj;
 
 
 
            byte[] bodyBytes = pac.getBody().getMessageBody().writeToBytes();   // body
 
            //String uniqueNo = pac.getHeader().getUniqueNo();
 
            //byte[] uniquenoBytes = RadixTools.intToBytes(Integer.parseInt(pac.getHeader().getUniqueNo()));   // uniqueno
 
 
            int len = PackagePart.UNIQUENO.getLen()     // len
                    + PackagePart.TIMESTAMP.getLen()
                    + PackagePart.COMMAND_MARK.getLen()
                    + bodyBytes.length;
 
            out.writeByte(pac.getHeader().getStartSymbol())         // symbol
                    .writeShortLE(len)
                    //.writeBytes(Utils.reverse(uniquenoBytes))       // uniqueno
                    .writeIntLE((int) (System.currentTimeMillis() / 1000))   // timestamp
                    .writeByte(pac.getHeader().getProtocolType().getCode()) // type
                    .writeBytes(bodyBytes)                          // body
                    .writeShort(pac.getValidCode())                 // valid
            ;
 
            pac.setValidCode(ValidUtil.calculateValidByteFromBuff(out));
            out.resetReaderIndex();
 
            out.writerIndex(out.readableBytes() - 2);
            out.writeShortLE(pac.getValidCode());
 
 
            if (systemProperties.isPrintPacLog()){
                //log.info("Agv [{}] 下行 [{}] >>> {}", uniqueNo, pac.getHeader().getProtocolType().getDes(), ByteBufUtil.hexDump(out).toUpperCase());
            }
 
        }
 
 
    }
 
// EE | 11 00 | 01 00 00 00 | 0C AB 12 64 | F0  | 01 00 | 01 01 | 00 00 00 00 | 4C F7
// #  | len   | uniqueno    | timestamp   | cmd | content                     | crc
 
}