From 25ee45142e035f2279150edbfc5320f5da496bb3 Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Thu, 23 May 2024 15:20:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=A9=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkDriver/Session/SessionMgr.cs | 23 +++++++++++------- Service/Car/Handler/HeartBeatMsgHandler.cs | 8 +++++++ Service/Car/Server/CarServer.cs | 26 +++++++++++++-------- WebStarter/Controllers/CarController.cs | 20 ++++++++++++++++ 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/HybirdFrameworkDriver/Session/SessionMgr.cs b/HybirdFrameworkDriver/Session/SessionMgr.cs index a09b507..48bded0 100644 --- a/HybirdFrameworkDriver/Session/SessionMgr.cs +++ b/HybirdFrameworkDriver/Session/SessionMgr.cs @@ -16,15 +16,8 @@ public class SessionMgr public static IoSession? GetSession(string key) { - if (Dictionary.ContainsKey(key)) - { - IoSession? value; - Dictionary.TryGetValue(key, out value); - - return value; - } - - return null; + Dictionary.TryGetValue(key, out IoSession? value); + return value; } public static List GetSessionList() @@ -59,6 +52,18 @@ public class SessionMgr Dictionary.AddOrUpdate(channel.Id.ToString(), ioSession, (k, oldSession) => ioSession); } + public static void ChangeSessionKey(IoSession ioSession, string newKey) + { + var oldKey = ioSession.Key; + if (oldKey != null) + { + Dictionary.Remove(oldKey, out IoSession? session); + } + + ioSession.Key = newKey; + Dictionary.AddOrUpdate(newKey, ioSession, (k, oldSession) => ioSession); + } + public static void RegisterModbusSession(string key, ModbusSession ioSession) { diff --git a/Service/Car/Handler/HeartBeatMsgHandler.cs b/Service/Car/Handler/HeartBeatMsgHandler.cs index 3ea7c56..dc9212a 100644 --- a/Service/Car/Handler/HeartBeatMsgHandler.cs +++ b/Service/Car/Handler/HeartBeatMsgHandler.cs @@ -1,5 +1,6 @@ using DotNetty.Transport.Channels; using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkDriver.Session; using Service.Car.Msg.Car.Req; using Service.Car.Msg.Host.Resp; using Service.Car.Server; @@ -22,6 +23,13 @@ public class HeartBeatMsgHandler : SimpleChannelInboundHandler, IB protected override void ChannelRead0(IChannelHandlerContext ctx, HeartBeatMsg msg) { CarServerMgr.CarServer.Connected = true; + + IoSession? ioSession = SessionMgr.GetSession(ctx.Channel.Id.ToString()); + if (ioSession != null && ioSession.Key != msg.CarNo) + { + SessionMgr.ChangeSessionKey(ioSession, msg.CarNo); + } + CarServerMgr.CarServer.CarNo = msg.CarNo; CarServerMgr.CarServer.HeartBeatMsg = msg; HeartBeatMsgResp resp = new HeartBeatMsgResp( CarServerMgr.CarServer.StationStatus) diff --git a/Service/Car/Server/CarServer.cs b/Service/Car/Server/CarServer.cs index 79c3c8a..15ac747 100644 --- a/Service/Car/Server/CarServer.cs +++ b/Service/Car/Server/CarServer.cs @@ -36,15 +36,21 @@ public class CarServer : TcpServer public CarServer() : base() { - this.ChannelInActiveAction = () => - { - Connected = false; - HeartBeatMsg = null; - ElecMsg = null; - LockMsgResp = null; - UnLockMsgResp = null; - SetParamMsgResp = null; - SettleConfirmMsgResp = null; - }; + this.ChannelInActiveAction = Clean; + } + + /// + /// 清理数据 + /// + public void Clean() + { + Connected = false; + CarNo = null; + HeartBeatMsg = null; + ElecMsg = null; + LockMsgResp = null; + UnLockMsgResp = null; + SetParamMsgResp = null; + SettleConfirmMsgResp = null; } } \ No newline at end of file diff --git a/WebStarter/Controllers/CarController.cs b/WebStarter/Controllers/CarController.cs index 5ddd6dd..7017e15 100644 --- a/WebStarter/Controllers/CarController.cs +++ b/WebStarter/Controllers/CarController.cs @@ -125,4 +125,24 @@ public class CarController : ControllerBase return true; } + + public bool Clear() + { + if (CarServerMgr.CarServer?.CarNo == null) + { + CarServerMgr.CarServer.Clean(); + return true; + } + + IoSession? session = SessionMgr.GetSession(CarServerMgr.CarServer.CarNo); + if (session == null) + { + CarServerMgr.CarServer.Clean(); + return true; + } + + session.Close(); + CarServerMgr.CarServer.Clean(); + return true; + } } \ No newline at end of file