|
|
using System.Collections.Concurrent;
|
|
|
using Autofac;
|
|
|
using Common.Const;
|
|
|
using DotNetty.Transport.Channels;
|
|
|
using Entity.DbModel.Station;
|
|
|
using HybirdFrameworkCore.Autofac;
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
using HybirdFrameworkDriver.Session;
|
|
|
using log4net;
|
|
|
using Service.Charger.Server;
|
|
|
|
|
|
namespace Service.Charger.Client;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 示例程序
|
|
|
/// </summary>
|
|
|
[Scope]
|
|
|
public static class ClientMgr
|
|
|
{
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientMgr));
|
|
|
|
|
|
public static readonly ConcurrentDictionary<string, PlcClient> Dictionary = new();
|
|
|
|
|
|
public static PlcClient? GetBySn(string sn)
|
|
|
{
|
|
|
Dictionary.TryGetValue(sn, out var o);
|
|
|
return o;
|
|
|
}
|
|
|
|
|
|
/// <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 PlcClient? client)
|
|
|
{
|
|
|
string? snt = ChannelUtils.GetAttr(channel, PlcConst.ChargerSn);
|
|
|
|
|
|
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 void AddBySn(string sn, PlcClient client)
|
|
|
{
|
|
|
Dictionary[sn] = client;
|
|
|
}
|
|
|
|
|
|
//TODO 连接、鉴权,开始充电,结束充电,设置尖峰平谷,读取尖峰平谷,发送功率调节指令,发送辅助源控制指令,下发掉线停止充电,
|
|
|
public static void InitClient()
|
|
|
{
|
|
|
Task.Run(() =>
|
|
|
{
|
|
|
ConnClient();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private static void ConnClient()
|
|
|
{
|
|
|
EquipNetInfo netInfo = new EquipNetInfo()
|
|
|
{
|
|
|
NetAddr = "192.168.10.1",
|
|
|
//NetAddr = "127.0.0.1",
|
|
|
NetPort = "2000",
|
|
|
Code = PlcConst.ChargeEqpCode,
|
|
|
DestAddr = "132,208,208,224"
|
|
|
};
|
|
|
Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
|
|
|
PlcClient client = AppInfo.Container.Resolve<PlcClient>();
|
|
|
client.AutoReconnect = true;
|
|
|
//Plc编号
|
|
|
client.Sn = PlcConst.ChargeEqpCode;
|
|
|
//电池仓编号
|
|
|
//client.BinNo = binInfo?.No;
|
|
|
//client.BatteryNo = binInfo?.BatteryNo;
|
|
|
client.LogName = "Charger" + netInfo.Code;
|
|
|
client.ConnectedEventHandler += (sender, b) =>
|
|
|
{
|
|
|
client.SessionAttr(netInfo.Code, netInfo.DestAddr);
|
|
|
//鉴权
|
|
|
// client.SendAuth();
|
|
|
};
|
|
|
//ip
|
|
|
client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort));
|
|
|
|
|
|
/*Task.Run(() =>
|
|
|
{*/
|
|
|
try
|
|
|
{
|
|
|
|
|
|
client.Connect();
|
|
|
client.SessionAttr(netInfo.Code, netInfo.DestAddr);
|
|
|
Log.Info($"succeed to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
//});
|
|
|
|
|
|
AddBySn(netInfo.Code, client);
|
|
|
Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
|
|
|
}
|
|
|
}
|