#
whycq
2025-02-06 17bb52d9337328323f5f8d2a806cf4f445673b4a
app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java
@@ -1,20 +1,24 @@
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 static com.example.agvcontroller.utils.DateUtils.formatDate;
import android.util.Log;
import com.example.agvcontroller.AGVApplication;
import com.example.agvcontroller.socket.RadixTools;
import org.greenrobot.eventbus.EventBus;
import java.net.InetSocketAddress;
import java.util.Date;
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;
/**
 * 编码器
@@ -22,24 +26,70 @@
 * 处理方式: 异或和
 * 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
    protected void encode(ChannelHandlerContext ctx, Object obj, ByteBuf out) {
        InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String ip = remoteAddress.getAddress().getHostAddress();
        if (obj instanceof ByteBuf){
            out.writeBytes((ByteBuf) obj);
        } else if (obj instanceof byte[]){
            out.writeBytes((byte[]) obj);
//            out.writeBytes((byte[]) obj);
            out.writeBytes((byte[]) new byte[]{
                    0x0D, 0x0A
            });
        } 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);
            // 生成整个报文的字节数组
            byte[] buffer = new byte[out.readableBytes()];
            out.getBytes(out.readerIndex(), buffer);
            // 将字节数组转换为 16 进制字符串
            String hexMessage = bytesToHex(buffer);
            String log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss") + " 下行: " + ip + "[" + action.getHandleCmdType().getDesc() + "]>>>" + hexMessage;
            Log.d("updown", log);
            AGVApplication.addLog(log);
//            EventBus.getDefault().post(log);
        } else if (obj instanceof AgvPackage){
            AgvPackage pac = (AgvPackage)obj;
@@ -48,9 +98,9 @@
            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
@@ -72,17 +122,40 @@
            out.writerIndex(out.readableBytes() - 2);
            out.writeShortLE(pac.getValidCode());
            // 生成整个报文的字节数组
            byte[] buffer = new byte[out.readableBytes()];
            out.getBytes(out.readerIndex(), buffer);
            if (systemProperties.isPrintPacLog()){
                log.info("Agv [{}] 下行 [{}] >>> {}", uniqueNo, pac.getHeader().getProtocolType().getDes(), ByteBufUtil.hexDump(out).toUpperCase());
            }
            // 将字节数组转换为 16 进制字符串
            String hexMessage = bytesToHex(buffer);
            String log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss") + " 下行: " + ip + "[" + pac.getHeader().getProtocolType().getDes() + "]>>>" + hexMessage;
            Log.d("updown", log);
            AGVApplication.addLog(log);
//            EventBus.getDefault().post(log);
        }
    }
    private String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
    // 将十六进制字符串转换为字节数组
    private byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
// 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