#
whycq
2025-02-06 318bd727e2fe02e4f541dfe943f77606af41d509
app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java
@@ -1,8 +1,17 @@
package com.example.agvcontroller.protocol;
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;
@@ -28,6 +37,8 @@
    @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);
@@ -69,7 +80,16 @@
            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;
@@ -102,12 +122,40 @@
            out.writerIndex(out.readableBytes() - 2);
            out.writeShortLE(pac.getValidCode());
            // 生成整个报文的字节数组
            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 + "[" + 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