You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<string, FireClient> Dictionary = new();
public static void ConnClient()
{
Log.Info($"begin to connect fire");
FireClient client = AppInfo.Container.Resolve<FireClient>();
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;
}
/// <summary>
/// 通过channel获取client
/// </summary>
/// <param name="channel"></param>
/// <param name="sn"></param>
/// <param name="client">获取不到client则为空</param>
/// <returns></returns>
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;
}
}