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