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 | 123 +++++++++++++++++++++++++++++++++------- 1 files changed, 101 insertions(+), 22 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 28297aa..bcc5bad 100644 --- a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java +++ b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java @@ -2,6 +2,8 @@ +import static com.example.agvcontroller.utils.DateUtils.formatDate; + import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; @@ -13,11 +15,13 @@ import android.os.Message; import android.util.Log; +import com.example.agvcontroller.AGVApplication; import com.example.agvcontroller.AGVCar; 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_03_UP; import com.example.agvcontroller.protocol.AGV_12_UP; import com.example.agvcontroller.protocol.AGV_13_UP; import com.example.agvcontroller.protocol.AGV_A1_DOWN; @@ -26,11 +30,13 @@ import com.example.agvcontroller.protocol.AgvAction; import com.example.agvcontroller.protocol.AgvPackage; import com.example.agvcontroller.protocol.ProtocolType; +import com.example.agvcontroller.utils.DateUtils; import org.greenrobot.eventbus.EventBus; import java.net.InetSocketAddress; +import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -40,6 +46,21 @@ 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; + String positionID = "--"; + int positionX = 0; + int positionY = 0; + float agvAngle = 0; + float gyroAngle = 0; + int forkHeight = 0; + 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) { @@ -57,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); +// } } @@ -74,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); @@ -132,7 +172,6 @@ // ack ProtocolType ackType = isNeedAck(pac); final String uniqueNo = pac.getHeader().getUniqueNo(); - String agvNo; label : switch (pac.getHeader().getProtocolType()){ case ACTION_COMPLETE: // 鍔ㄤ綔瀹屾垚鏁版嵁鍖� AGV_11_UP agv_11_up = (AGV_11_UP) pac.getBody().getMessageBody(); @@ -149,24 +188,63 @@ AGV_12_UP agv_12_up = (AGV_12_UP) pac.getBody().getMessageBody(); agvNo = pac.getHeader().getUniqueNo(); channelMap.put(clientId, ctx.channel()); - EventBus.getDefault().post(new AGVCar(clientId,ip,port,agvNo,1)); + agvStatus = agv_12_up.getStatus(); + positionID = agv_12_up.getQrCode(); + positionX = agv_12_up.getOffsetX(); + positionY = agv_12_up.getOffsetY(); + agvAngle = agv_12_up.getAGVCurrentAngle(); + gyroAngle = agv_12_up.getGyroAngle(); + forkHeight = agv_12_up.getCurrentAltitude(); + forkExtend = agv_12_up.getForkLength(); + forkAngle = agv_12_up.getLoaderTheta(); + agvCar = new AGVCar(clientId, ip, port, agvNo, 1, battery,agvStatus,positionID,positionX,positionY,agvAngle,gyroAngle,forkHeight,forkExtend,forkAngle,agvError); + EventBus.getDefault().post(agvCar); + log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " 涓婅: " + ip + "[鏈夌爜瀹炴椂鏁版嵁鍖匽>>>" + pac.getSourceHexStr(); + Log.d("updown", log); + AGVApplication.addLog(log); break label; case DATA_WITHOUT_CODE_REPORT: AGV_13_UP agv_13_up = (AGV_13_UP) pac.getBody().getMessageBody(); agvNo = pac.getHeader().getUniqueNo(); channelMap.put(clientId, ctx.channel()); - EventBus.getDefault().post(new AGVCar(clientId,ip,port,agvNo,1)); + agvCar = new AGVCar(clientId, ip, port, agvNo, 1, battery,agvStatus,positionID,positionX,positionY,agvAngle,gyroAngle,forkHeight,forkExtend,forkAngle,agvError); + EventBus.getDefault().post(agvCar); + log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss:SSS") + " 涓婅: " + ip + "[鏃犵爜瀹炴椂鏁版嵁鍖匽>>>" + pac.getSourceHexStr(); + Log.d("updown", log); + AGVApplication.addLog(log); + break label; + case HEARTBEAT_REPORT: + AGV_03_UP agv_03_up = (AGV_03_UP) pac.getBody().getMessageBody(); + battery = agv_03_up.getBattery(); + agvError = agv_03_up.getError(); +// pac.getBody().getMessageBody() + agvNo = pac.getHeader().getUniqueNo(); + channelMap.put(clientId, ctx.channel()); + agvCar = new AGVCar(clientId, ip, port, agvNo, 1, battery,agvStatus,positionID,positionX,positionY,agvAngle,gyroAngle,forkHeight,forkExtend,forkAngle,agvError); + EventBus.getDefault().post(agvCar); + log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " 涓婅: " + ip + "[蹇冭烦鍖匽>>>" + pac.getSourceHexStr(); + Log.d("updown", log); + AGVApplication.addLog(log); break label; case LOGIN_REPORT: AGV_F0_UP agv_f0_up = (AGV_F0_UP) pac.getBody().getMessageBody(); if (null != ackType) { AgvPackage ackPac = AckMsgBuilder.ofSuccess(pac, ackType); AGV_F0_DOWN agv_f0_down = (AGV_F0_DOWN) ackPac.getBody().getMessageBody(); + log = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + " 涓婅: " + ip + "[鐧诲綍鍖匽>>>" + pac.getSourceHexStr(); + Log.d("updown", log); + AGVApplication.addLog(log); + +// EventBus.getDefault().post(log); + ctx.writeAndFlush(ackPac); } + battery = agv_f0_up.getBattery(); +// pac.getBody().getMessageBody() agvNo = pac.getHeader().getUniqueNo(); channelMap.put(clientId, ctx.channel()); - EventBus.getDefault().post(new AGVCar(clientId,ip,port,agvNo,1)); + agvCar = new AGVCar(clientId, ip, port, agvNo, 1, battery,agvStatus,positionID,positionX,positionY,agvAngle,gyroAngle,forkHeight,forkExtend,forkAngle,agvError); + EventBus.getDefault().post(agvCar); break label; } @@ -180,6 +258,7 @@ } return sb.toString(); } + // 灏嗗崄鍏繘鍒跺瓧绗︿覆杞崲涓哄瓧鑺傛暟缁� private byte[] hexStringToByteArray(String s) { int len = s.length(); -- Gitblit v1.9.1