using Autofac;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using log4net;
using Service.Charger.Server;
using Service.Sound.SoundClient;
namespace Service.Charger.Client;
///
/// 示例程序
///
[Scope]
public static class ClientMgr
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientMgr));
public static PlcClient? PlcClient { get; set; }
//TODO 连接、鉴权,开始充电,结束充电,设置尖峰平谷,读取尖峰平谷,发送功率调节指令,发送辅助源控制指令,下发掉线停止充电,
public static void InitClient()
{
Task.Run(ConnClient);
}
private static void ConnClient()
{
EquipNetInfo netInfo = new EquipNetInfo()
{
//NetAddr = "127.0.0.1",
NetAddr = "192.168.3.20",
NetPort = "2000",
//NetPort = "10000",
Code = PlcConst.PlcEqpCode,
DestAddr = "132,208,208,224"
};
Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
PlcClient = AppInfo.Container.Resolve();
Log.Info($"plc client Id={PlcClient.GetHashCode()}");
PlcClient.AutoReconnect = true;
PlcClient.Sn = PlcConst.PlcEqpCode;
PlcClient.ConnectedEventHandler += (sender, b) =>
{
PlcClient.SessionAttr(netInfo.Code, netInfo.DestAddr);
//鉴权
// client.SendAuth();
};
//ip
PlcClient.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort), 30, 0, 0);
/*Task.Run(() =>
{*/
try
{
PlcClient.Connect();
PlcClient.SessionAttr(netInfo.Code, netInfo.DestAddr);
PlcConst.EquipAlarmDefines = PlcClient.GetAlarmDeinneList();
Log.Info($"succeed to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
}
catch (Exception ex)
{
Log.Error($"conn plc error ={ex.StackTrace}");
}
//});
}
}