| | |
| | | package com.zy.acs.gateway.handler.coder; |
| | | package com.example.agvcontroller.protocol; |
| | | |
| | | import com.core.common.RadixTools; |
| | | import com.zy.acs.common.utils.Utils; |
| | | import com.zy.acs.gateway.config.SystemProperties; |
| | | import com.zy.acs.gateway.constant.PackagePart; |
| | | import com.zy.acs.gateway.domain.AgvPackage; |
| | | import com.zy.acs.gateway.utils.ValidUtil; |
| | | 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; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * 编码器 |
| | |
| | | * 处理方式: 异或和 |
| | | * Created by vincent on 2019-04-02 |
| | | */ |
| | | @Component |
| | | //@Component |
| | | @ChannelHandler.Sharable |
| | | public class ProtocolEncoder extends MessageToByteEncoder<Object> { |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(ProtocolEncoder.class); |
| | | //private static final Logger log = LoggerFactory.getLogger(ProtocolEncoder.class); |
| | | |
| | | @Autowired |
| | | //@Autowired |
| | | private SystemProperties systemProperties; |
| | | |
| | | @Override |
| | |
| | | } 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(); |
| | | //String uniqueNo = pac.getHeader().getUniqueNo(); |
| | | |
| | | byte[] uniquenoBytes = RadixTools.intToBytes(Integer.parseInt(pac.getHeader().getUniqueNo())); // uniqueno |
| | | //byte[] uniquenoBytes = RadixTools.intToBytes(Integer.parseInt(pac.getHeader().getUniqueNo())); // uniqueno |
| | | |
| | | |
| | | int len = PackagePart.UNIQUENO.getLen() // len |
| | |
| | | |
| | | out.writeByte(pac.getHeader().getStartSymbol()) // symbol |
| | | .writeShortLE(len) |
| | | .writeBytes(Utils.reverse(uniquenoBytes)) // uniqueno |
| | | //.writeBytes(Utils.reverse(uniquenoBytes)) // uniqueno |
| | | .writeIntLE((int) (System.currentTimeMillis() / 1000)) // timestamp |
| | | .writeByte(pac.getHeader().getProtocolType().getCode()) // type |
| | | .writeBytes(bodyBytes) // body |
| | |
| | | |
| | | |
| | | if (systemProperties.isPrintPacLog()){ |
| | | log.info("Agv [{}] 下行 [{}] >>> {}", uniqueNo, pac.getHeader().getProtocolType().getDes(), ByteBufUtil.hexDump(out).toUpperCase()); |
| | | //log.info("Agv [{}] 下行 [{}] >>> {}", uniqueNo, pac.getHeader().getProtocolType().getDes(), ByteBufUtil.hexDump(out).toUpperCase()); |
| | | } |
| | | |
| | | } |