From 5bdad72f5d5077ca875dd03cfdaafb3d7aba93da Mon Sep 17 00:00:00 2001
From: whycq <913841844@qq.com>
Date: 星期五, 16 八月 2024 14:36:58 +0800
Subject: [PATCH] #

---
 app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java |  120 ++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 85 insertions(+), 35 deletions(-)

diff --git a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
index 2d2c049..f27d329 100644
--- a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
+++ b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
@@ -1,28 +1,34 @@
 package com.example.agvcontroller.socket;
 
-import java.nio.charset.StandardCharsets;
+
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelInboundHandlerAdapter;
 import android.util.Log;
 
 import com.example.agvcontroller.Item;
+import com.example.agvcontroller.MainActivity;
+import com.example.agvcontroller.action.AGV_11_UP;
+import com.example.agvcontroller.action.AckMsgBuilder;
+import com.example.agvcontroller.met.AbstractInboundHandler;
+import com.example.agvcontroller.protocol.AGV_A1_DOWN;
+import com.example.agvcontroller.protocol.AgvAction;
+import com.example.agvcontroller.protocol.AgvPackage;
+import com.example.agvcontroller.protocol.ProtocolType;
 
 
 import org.greenrobot.eventbus.EventBus;
-import org.greenrobot.eventbus.Subscribe;
-import org.greenrobot.eventbus.ThreadMode;
 
-import java.util.Arrays;
 import java.util.concurrent.ConcurrentHashMap;
 
-public class NettyServerHandler extends ChannelInboundHandlerAdapter {
+public class NettyServerHandler  extends AbstractInboundHandler<AgvPackage> {
 
     private static final String TAG = "NettyServerHandler";
     private static ConcurrentHashMap<String, Channel> channelMap = new ConcurrentHashMap<>();
+
 
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
@@ -40,35 +46,53 @@
         Log.d(TAG, "Client disconnected: " + clientId);
     }
 
-    @Override
-    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
-        // 澶勭悊鎺ユ敹鍒扮殑娑堟伅
-        ByteBuf byteBuf = (ByteBuf) msg;
-        //byte[] data = new byte[byteBuf.readableBytes()];
-        //byteBuf.readBytes(data);
-        //String received = new String(data, StandardCharsets.UTF_8);
-        //System.out.println("Received from client: " + received);
-        //
-        //// 鍥炲娑堟伅
-        ////ByteBuf response = Unpooled.copiedBuffer("Response from server", StandardCharsets.UTF_8);
-        ////ctx.writeAndFlush(response);
-        //
-        //byte[] responseHex = hexStringToByteArray("48656c6c6f20576f726c64"); // "Hello World" in hex
-        //ByteBuf response = Unpooled.wrappedBuffer(responseHex);
-        //ctx.writeAndFlush(response);
+//    @Override
+//    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+//        // 澶勭悊鎺ユ敹鍒扮殑娑堟伅
+//        ByteBuf byteBuf = (ByteBuf) msg;
+//        try {
+//            while (byteBuf.isReadable()) {
+//                byte[] bytes = new byte[byteBuf.readableBytes()];
+//                byteBuf.readBytes(bytes);
+//                String hexString = bytesToHex(bytes);
+//                // 鑾峰彇agv淇℃伅 娣诲姞鍒發ist涓�
+//                Log.d(TAG, "ctx: " + ctx.channel().remoteAddress().toString() );
+//                Log.d(TAG, "Received: " + hexString);
+//            }
+//        } finally {
+//            byteBuf.release();
+//        }
+//    }
 
-        try {
-            while (byteBuf.isReadable()) {
-                byte[] bytes = new byte[byteBuf.readableBytes()];
-                byteBuf.readBytes(bytes);
-                String hexString = bytesToHex(bytes);
-                // 鑾峰彇agv淇℃伅 娣诲姞鍒發ist涓�
-                Log.d(TAG, "ctx: " + ctx.channel().remoteAddress().toString() );
-                Log.d(TAG, "Received: " + hexString);
-            }
-        } finally {
-            byteBuf.release();
+    @Override
+    protected boolean channelRead0(ChannelHandlerContext ctx, AgvPackage pac) throws Exception {
+
+        String serialNum = pac.getHeader().getSerialNum();
+        // ack
+        ProtocolType ackType = isNeedAck(pac);
+        final String uniqueNo = pac.getHeader().getUniqueNo();
+        label : switch (pac.getHeader().getProtocolType()){
+            case ACTION_COMPLETE:   // 鍔ㄤ綔瀹屾垚鏁版嵁鍖�
+
+
+                AGV_11_UP agv_11_up = (AGV_11_UP) pac.getBody().getMessageBody();
+//                redis.push(RedisConstant.AGV_COMPLETE_FLAG, AgvProtocol.build(uniqueNo).setMessageBody(agv_11_up));
+
+                // 鍔ㄤ綔瀹屾垚搴旂瓟
+                if (null != ackType) {
+                    AgvPackage ackPac = AckMsgBuilder.ofSuccess(pac, ackType);
+
+                    AGV_A1_DOWN agv_a1_down = (AGV_A1_DOWN) ackPac.getBody().getMessageBody();
+                    agv_a1_down.setAckSign((byte) agv_11_up.getCompleteCode());
+
+                    ctx.writeAndFlush(ackPac);
+                }
+
+                MainActivity.map.put(serialNum, Boolean.TRUE);
+
+                break label;
         }
+        return false;
     }
 
     private String bytesToHex(byte[] bytes) {
@@ -90,16 +114,28 @@
     }
 
     public static void sendMessageToClient(String clientId, byte[] message) {
-
         Channel channel = channelMap.get(clientId);
         if (channel != null && channel.isActive()) {
             ByteBuf buf = Unpooled.wrappedBuffer(message);
+            String upperCase = ByteBufUtil.hexDump(buf).toUpperCase();
+            Log.d(TAG, "upperCase " + upperCase);
             channel.writeAndFlush(buf);
         } else {
             Log.d(TAG, "Client " + clientId + " is not connected");
         }
+    }
 
-        Log.d(TAG, "Client " + clientId + " is not connected");
+    public static void sendMessageToClient(String clientId, AgvAction<?> action) {
+
+
+
+        Channel channel = channelMap.get(clientId);
+        if (channel != null && channel.isActive()) {
+
+            channel.writeAndFlush(action);
+        } else {
+            Log.d(TAG, "Client " + clientId + " is not connected");
+        }
     }
 
     @Override
@@ -108,4 +144,18 @@
         ctx.close();
     }
 
+    /**
+     * 鏈嶅姟鍣ㄦ槸鍚﹂渶瑕佸簲绛�
+     */
+    public static ProtocolType isNeedAck(AgvPackage pac) {
+        switch (pac.getHeader().getProtocolType()) {
+            case ACTION_COMPLETE:
+                return ProtocolType.ACTION_SUCCESS_ACK;
+            case LOGIN_REPORT:
+                return ProtocolType.LOGIN_ACK;
+            default:
+                return null;
+        }
+    }
+
 }

--
Gitblit v1.9.1