| 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 |