清空接口

master
smartwyy 6 months ago
parent 6cf78d77ef
commit 25ee45142e

@ -16,17 +16,10 @@ public class SessionMgr
public static IoSession? GetSession(string key)
{
if (Dictionary.ContainsKey(key))
{
IoSession? value;
Dictionary.TryGetValue(key, out value);
Dictionary.TryGetValue(key, out IoSession? value);
return value;
}
return null;
}
public static List<IoSession> GetSessionList()
{
return Dictionary.Values.ToList();
@ -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)
{

@ -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<HeartBeatMsg>, 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)

@ -36,15 +36,21 @@ public class CarServer : TcpServer<IBaseHandler, Decoder, Encoder>
public CarServer() : base()
{
this.ChannelInActiveAction = () =>
this.ChannelInActiveAction = Clean;
}
/// <summary>
/// 清理数据
/// </summary>
public void Clean()
{
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;
}
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