using System.Collections.Concurrent; using Autofac; using DotNetty.Transport.Channels; using HybirdFrameworkCore.Autofac; using HybirdFrameworkDriver.Session; using log4net; using Service.Fire.Common; namespace Service.Fire.Client; public class FireMgr { private static readonly ILog Log = LogManager.GetLogger(typeof(FireMgr)); public static readonly ConcurrentDictionary Dictionary = new(); public static void ConnClient() { Log.Info($"begin to connect fire"); FireClient client = AppInfo.Container.Resolve(); client.AutoReconnect = true; client.Sn = FireConst.Sn; client.LogName = "Fire" + FireConst.EqmCode; client.ConnectedEventHandler += (sender, b) => { client.SessionAttr(FireConst.Sn); }; client.InitBootstrap(FireConst.Ip, FireConst.Port); Task.Run(() => { client.Connect(); client.SessionAttr(FireConst.Sn); Log.Info($"succeed to connect loading..."); }); AddBySn(FireConst.Sn, client); Log.Info($"begin to connect fire"); } public static void AddBySn(string sn, FireClient client) { Dictionary[sn] = client; } /// /// 通过channel获取client /// /// /// /// 获取不到,client则为空 /// public static bool TryGetClient(IChannel channel, out string sn, out FireClient? client) { string? snt = ChannelUtils.GetAttr(channel, FireConst.EqmCode); if (!string.IsNullOrWhiteSpace(snt)) { var chargerClient = GetBySn(snt); if (chargerClient != null) { sn = snt; client = chargerClient; return true; } } sn = string.Empty; client = null; return false; } public static FireClient? GetBySn(string sn) { Dictionary.TryGetValue(sn, out var o); return o; } }