清空接口

master
smartwyy 6 months ago
parent 6cf78d77ef
commit 25ee45142e

@ -16,15 +16,8 @@ public class SessionMgr
public static IoSession? GetSession(string key) public static IoSession? GetSession(string key)
{ {
if (Dictionary.ContainsKey(key)) Dictionary.TryGetValue(key, out IoSession? value);
{ return value;
IoSession? value;
Dictionary.TryGetValue(key, out value);
return value;
}
return null;
} }
public static List<IoSession> GetSessionList() public static List<IoSession> GetSessionList()
@ -59,6 +52,18 @@ public class SessionMgr
Dictionary.AddOrUpdate(channel.Id.ToString(), ioSession, (k, oldSession) => ioSession); 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) public static void RegisterModbusSession(string key, ModbusSession ioSession)
{ {

@ -1,5 +1,6 @@
using DotNetty.Transport.Channels; using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.Session;
using Service.Car.Msg.Car.Req; using Service.Car.Msg.Car.Req;
using Service.Car.Msg.Host.Resp; using Service.Car.Msg.Host.Resp;
using Service.Car.Server; using Service.Car.Server;
@ -22,6 +23,13 @@ public class HeartBeatMsgHandler : SimpleChannelInboundHandler<HeartBeatMsg>, IB
protected override void ChannelRead0(IChannelHandlerContext ctx, HeartBeatMsg msg) protected override void ChannelRead0(IChannelHandlerContext ctx, HeartBeatMsg msg)
{ {
CarServerMgr.CarServer.Connected = true; 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.CarNo = msg.CarNo;
CarServerMgr.CarServer.HeartBeatMsg = msg; CarServerMgr.CarServer.HeartBeatMsg = msg;
HeartBeatMsgResp resp = new HeartBeatMsgResp( CarServerMgr.CarServer.StationStatus) HeartBeatMsgResp resp = new HeartBeatMsgResp( CarServerMgr.CarServer.StationStatus)

@ -36,15 +36,21 @@ public class CarServer : TcpServer<IBaseHandler, Decoder, Encoder>
public CarServer() : base() public CarServer() : base()
{ {
this.ChannelInActiveAction = () => this.ChannelInActiveAction = Clean;
{ }
Connected = false;
HeartBeatMsg = null; /// <summary>
ElecMsg = null; /// 清理数据
LockMsgResp = null; /// </summary>
UnLockMsgResp = null; public void Clean()
SetParamMsgResp = null; {
SettleConfirmMsgResp = null; Connected = false;
}; CarNo = null;
HeartBeatMsg = null;
ElecMsg = null;
LockMsgResp = null;
UnLockMsgResp = null;
SetParamMsgResp = null;
SettleConfirmMsgResp = null;
} }
} }

@ -125,4 +125,24 @@ public class CarController : ControllerBase
return true; 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;
}
} }
Loading…
Cancel
Save