From 8e6d7c8275117ca2659e7f82051f8af19741aa9d Mon Sep 17 00:00:00 2001
From: whycq <913841844@qq.com>
Date: 星期五, 07 二月 2025 16:51:33 +0800
Subject: [PATCH] #

---
 app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java |   63 ++++++++++++++++++++-----------
 1 files changed, 40 insertions(+), 23 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 0c05544..bcc5bad 100644
--- a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
+++ b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
@@ -46,6 +46,7 @@
     private static final String TAG = "NettyServerHandler";
     private static ConcurrentHashMap<String, Channel> channelMap = new ConcurrentHashMap<>();
     private Map<String, Runnable> pendingRemovals = new HashMap<>();
+    AGVCar agvCar;
     int battery = 0;
     int status = 0;
     int agvStatus = 0;
@@ -58,6 +59,8 @@
     int forkExtend = 0;
     int forkAngle = 0;
     int agvError = 0;
+    String agvNo = "--";
+    String log;
     private Handler handler = new Handler(Looper.getMainLooper()) {
         @Override
         public void handleMessage(Message msg) {
@@ -75,14 +78,19 @@
         String ip = remoteAddress.getAddress().getHostAddress();
         int port = remoteAddress.getPort();
         channelMap.put(clientId, ctx.channel());
-        EventBus.getDefault().post(new AGVCar(clientId,ip,port,"--",0));
-        Log.d(TAG, "Client connected: " + clientId);
+        agvCar = new AGVCar(clientId, ip, port, agvNo, 1, battery,agvStatus,positionID,positionX,positionY,agvAngle,gyroAngle,forkHeight,forkExtend,forkAngle,agvError);
 
-        // 鍙栨秷寤惰繜鍒犻櫎鎿嶄綔
-        if (pendingRemovals.containsKey(clientId)) {
-            handler.removeCallbacks(pendingRemovals.get(clientId));
-            pendingRemovals.remove(clientId);
-        }
+        EventBus.getDefault().post(agvCar);
+//        Log.d(TAG, "Client connected: " + clientId);
+        log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " 涓婅: " + ip + "[tcp]>>>宸茶繛鎺�";
+        Log.d("updown", log);
+        AGVApplication.addLog(log);
+
+//        // 鍙栨秷寤惰繜鍒犻櫎鎿嶄綔
+//        if (pendingRemovals.containsKey(clientId)) {
+//            handler.removeCallbacks(pendingRemovals.get(clientId));
+//            pendingRemovals.remove(clientId);
+//        }
 
     }
 
@@ -92,24 +100,38 @@
         InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
         String ip = remoteAddress.getAddress().getHostAddress();
         int port = remoteAddress.getPort();
-        channelMap.remove(clientId);
+//        channelMap.remove(clientId);
         EventBus.getDefault().post(clientId);
-        Log.d(TAG, "Client disconnected: " + clientId);
+//        Log.d(TAG, "Client disconnected: " + clientId);
+        agvCar = new AGVCar(clientId, ip, port, agvNo, 0, battery,agvStatus,positionID,positionX,positionY,agvAngle,gyroAngle,forkHeight,forkExtend,forkAngle,agvError);
 
-        // 鍚姩寤惰繜鍒犻櫎鎿嶄綔
-        Runnable removalRunnable = new Runnable() {
-            @Override
-            public void run() {
-                removeItem(clientId);
-            }
-        };
-        pendingRemovals.put(clientId, removalRunnable);
-        handler.postDelayed(removalRunnable, 20000); // 20绉掑悗鎵ц鍒犻櫎鎿嶄綔
+        EventBus.getDefault().post(agvCar);
+//        Log.d(TAG, "Client connected: " + clientId);
+        log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " 涓婅: " + ip + "[tcp]>>>鏂紑杩炴帴";
+        Log.d("updown", log);
+        AGVApplication.addLog(log);
+//        // 鍚姩寤惰繜鍒犻櫎鎿嶄綔
+//        Runnable removalRunnable = new Runnable() {
+//            @Override
+//            public void run() {
+//                removeItem(clientId);
+//            }
+//        };
+//        pendingRemovals.put(clientId, removalRunnable);
+//        handler.postDelayed(removalRunnable, 20000); // 20绉掑悗鎵ц鍒犻櫎鎿嶄綔
 
     }
 
 
     private void removeItem(String clientId) {
+//        鍘熷厛鏄鍒犻櫎鍚庣画涓轰簡鑳藉姩鎬佽嚜鍔ㄨ繛鎺ラ渶瑕佹洿鏂�
+//        if (channelMap.remove(clientId) != null) {
+//            Log.d(TAG, "Client removed after 20 seconds: " + clientId);
+//            EventBus.getDefault().post(clientId);
+//        } else {
+//            Log.d(TAG, "Client already reconnected or not found: " + clientId);
+//        }
+
         if (channelMap.remove(clientId) != null) {
             Log.d(TAG, "Client removed after 20 seconds: " + clientId);
             EventBus.getDefault().post(clientId);
@@ -150,11 +172,6 @@
         // ack
         ProtocolType ackType = isNeedAck(pac);
         final String uniqueNo = pac.getHeader().getUniqueNo();
-        String agvNo;
-        AGVCar agvCar;
-
-        String log;
-
         label : switch (pac.getHeader().getProtocolType()){
             case ACTION_COMPLETE:   // 鍔ㄤ綔瀹屾垚鏁版嵁鍖�
                 AGV_11_UP agv_11_up = (AGV_11_UP) pac.getBody().getMessageBody();

--
Gitblit v1.9.1