#
whycq
2025-02-07 8e6d7c8275117ca2659e7f82051f8af19741aa9d
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();