#
vincentlu
2025-05-13 ebd2f4397a92c6a5096de1b86d59154363344720
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
package com.zy.acs.gateway.mock;
 
import com.alibaba.fastjson.JSON;
import com.zy.acs.common.domain.protocol.AGV_12_UP;
import com.zy.acs.common.domain.protocol.IMessageBody;
import com.zy.acs.common.utils.Utils;
import com.zy.acs.framework.common.RadixTools;
import com.zy.acs.gateway.constant.ProtocolType;
import com.zy.acs.gateway.domain.AgvPackage;
import com.zy.acs.gateway.handler.MessageBodyHandler;
import com.zy.acs.gateway.handler.coder.ProtocolDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
 
public class ParseProtocolUtils {
 
    public static void main(String[] args) {
        String protocolMsg = "AA30000F0000003DF06C67124B000000DCFFDDFF358EB043000029BAC0358EB043000000000000000000000000A6FF40020C031909";
 
        byte[] bytes = RadixTools.hexStringToBytes(protocolMsg);
 
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(256);
 
        buffer.writeBytes(bytes);
 
 
        ProtocolDecoder protocolDecoder = new ProtocolDecoder(1024);
 
        AgvPackage pac = AgvPackage.valueOfEmpty();
        pac.setSourceBuff(buffer);
 
        protocolDecoder.analyzeProtocol(pac);
 
 
        Boolean b = msgChannelRead(pac);
 
 
        System.out.println(JSON.toJSONString(pac));
 
        IMessageBody messageBody = pac.getBody().getMessageBody();
        if (messageBody instanceof AGV_12_UP) {
            AGV_12_UP agv_12_up = (AGV_12_UP) messageBody;
            System.out.println(agv_12_up.getQrCode());
        }
    }
 
 
    private static Boolean msgChannelRead(AgvPackage pac) {
        String namespace = IMessageBody.class.getPackage().getName();
 
        ProtocolType protocolType = pac.getHeader().getProtocolType();
 
 
        if (null == protocolType) {
 
            // 未知包
            return Boolean.FALSE;
        }
 
        String className =  namespace +  ".AGV_" + Utils.zeroFill(Integer.toHexString(protocolType.getCode()).toUpperCase(), 2)
                +"_"
 
                +  pac.getHeader().getProtocolType().getDirection().toString().toUpperCase();
 
        Class<?> cls = null;
        try {
 
 
            cls  =   Class.forName(className);;
 
        } catch (ClassNotFoundException e) {
 
 
            return Boolean.FALSE;
        }
 
        Object obj = null;
 
        if (null != cls) {
 
 
            try {
                obj = cls.newInstance();
 
            } catch (ReflectiveOperationException reflectiveOperationException) {
 
                // 。。。
                return Boolean.FALSE;
            }
 
 
        }
 
        ByteBuf contentBuf = pac.getBody().getContent();           contentBuf.markReaderIndex();   // sign
        // body.buf 属于切片获取, slice 与 channel中的缓冲区 为同一 refenec
 
 
 
        contentBuf.resetReaderIndex();
 
        int len= contentBuf.readableBytes();
 
        byte[] bytes = new byte[len];
 
 
 
        pac.getBody().getContent().readBytes(bytes);
 
 
 
        // 读取字节数组 ===>>     实际报文类型
        IMessageBody iMessageBody = null;
 
        if (null != obj) {
 
            if ( obj  instanceof IMessageBody) {
 
                iMessageBody = (IMessageBody) obj;
 
 
                iMessageBody.readFromBytes(bytes);
            }
 
        }
 
 
 
        pac.getBody().setMessageBody(iMessageBody);     contentBuf.resetReaderIndex();
 
        return true;
    }
 
}
 
// AA 30000F0000001DEE6C67120B0000001200EDFF8340B5420100E76FC28340B542000000000000000000000000A6FF40020C030674