using Autofac;
using DotNetty.Transport.Channels;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkDriver.Session;
using log4net;
using Repository.Station;
using Service.Charger.Common;
using Service.Equipment;
using Service.Init;
namespace Service.Charger.Client;
///
/// 示例程序
///
[Scope("SingleInstance")]
public static class ClientMgr
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientMgr));
private static readonly Dictionary Dictionary = new();
private static bool AutoChargeWorking { get; set; }
public static ChargerClient? GetBySn(string sn)
{
Dictionary.TryGetValue(sn, out var o);
return o;
}
///
/// 通过channel获取client
///
///
///
/// 获取不到,client则为空
///
public static bool TryGetClient(IChannel channel, out string sn, out ChargerClient? client)
{
string? snt = ChannelUtils.GetAttr(channel, ChargerConst.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, ChargerClient client)
{
Dictionary[sn] = client;
}
//TODO 连接、鉴权,开始充电,结束充电,设置尖峰平谷,读取尖峰平谷,发送功率调节指令,发送辅助源控制指令,下发掉线停止充电,
public static void InitClient()
{
EquipInfoRepository equipInfoRepository = AppInfo.Container.Resolve();
EquipNetInfoRepository netInfoRepository = AppInfo.Container.Resolve();
List equipInfos = equipInfoRepository.QueryListByClause(it => it.TypeCode == (int)EquipmentType.Charger);
if (equipInfos.Count > 0)
{
Dictionary set = equipInfos.ToDictionary(it => it.Code, it => it);
List equipNetInfos = netInfoRepository.QueryListByClause(it => set.Keys.Contains(it.Code));
foreach (EquipNetInfo netInfo in equipNetInfos)
{
Task.Run(() =>
{
ConnClient(netInfo);
});
}
StartAutoChargeThread();
StartQueryBatteryInfoThread();
}
}
private static void StartQueryBatteryInfoThread()
{
Thread thread = new Thread(QueryBatteryInfo)
{
Name = @"QueryBatteryInfoThread"
};
thread.Start();
}
private static void QueryBatteryInfo()
{
while (true)
{
try
{
Thread.Sleep(1000);
foreach (var (key, client) in Dictionary)
{
client.SendQueryBattery();
}
}
catch (Exception e)
{
Log.Error("QueryBatteryInfo error", e);
}
}
}
///
///
///
private static void StartAutoChargeThread()
{
if (!AutoChargeWorking)
{
Thread thread = new Thread(AutoChargeThread)
{
Name = @"AutoChargeThread"
};
thread.Start();
AutoChargeWorking = true;
}
}
///
///
///
private static void AutoChargeThread()
{
ElecPriceModelVersionRepository elecPriceModelVersionRepository =
AppInfo.Container.Resolve();
ElecPriceModelVersionDetailRepository elecPriceModelVersionDetailRepository =
AppInfo.Container.Resolve();
BatteryOpModelRepository batteryOpModelRepository = AppInfo.Container.Resolve();
BatteryOpModelDetailRepository batteryOpModelDetailRepository =
AppInfo.Container.Resolve();
BinInfoRepository binInfoRepository = AppInfo.Container.Resolve();
while (true)
{
try
{
Thread.Sleep(1000 * 30);
DateTime now = DateTime.Now;
List binInfos = binInfoRepository.Query();
if (binInfos.Count < 0)
{
Log.Info("lack of binInfos");
continue;
}
var chargeSoc = StaticStationInfo.ChargeSoc;
List socNeedChargeList = binInfos.Where(t => t.Soc < chargeSoc).ToList();
if (socNeedChargeList.Count < 0)
{
Log.Info($"lack of binInfos.soc < {chargeSoc}");
continue;
}
#region 电价模型
int ceid = StaticStationInfo.Ceid;
ElecPriceModelVersion elecPriceModelVersion = elecPriceModelVersionRepository.QueryByClause(i => i.Version == ceid);
if (elecPriceModelVersion == null)
{
Log.Info("lack of effective elecPriceModelVersion");
continue;
}
List elecPriceModelVersionDetails = elecPriceModelVersionDetailRepository.QueryListByClause(it => it.Version == elecPriceModelVersion.Version);
ElecPriceModelVersionDetail? elecPriceModelVersionDetail = elecPriceModelVersionDetails.Where(i => i.StartHour <= now.Hour && i.StartMinute <= now.Minute
&& i.EndHour > now.Hour &&
i.EndMinute > now.Minute).FirstOrDefault();
if (elecPriceModelVersionDetail == null)
{
Log.Info("lack of effective elecPriceModelVersionDetail");
continue;
}
#endregion
#region 运营模型
int oid = int.Parse(StaticStationInfo.Oid);
BatteryOpModel batteryOpModel = batteryOpModelRepository.QueryByClause(d => d.ModelId == oid);
if (batteryOpModel == null)
{
Log.Info("lack of effective batteryOpModel");
continue;
}
List batteryOpModelDetails = batteryOpModelDetailRepository.QueryListByClause(d => d.ModelId == oid);
List opModelDetails = batteryOpModelDetails.Where(t =>
{
List start = t.StartTime.Split(":").Select(int.Parse).ToList();
List end = t.EndTime.Split(":").Select(int.Parse).ToList();
return now.Hour >= start[0] && now.Hour < end[0] && now.Minute >= start[1] && now.Minute < end[1] && now.Second >= start[2] && now.Second < end[2] ;
}).ToList();
if (opModelDetails.Count == 0)
{
Log.Info("lack of effective batteryOpModelDetails");
continue;
}
int needBatteryCount = opModelDetails[0].BatteryCount ?? 8;
if ((binInfos.Count - socNeedChargeList.Count) >= needBatteryCount)
{
Log.Info($"lack of needBatteryCount {needBatteryCount}");
continue;
}
#endregion
foreach (BinInfo binInfo in socNeedChargeList)
{
if (Dictionary.TryGetValue(binInfo.ChargerNo, out var client))
{
Result result = client.StartCharge();
Log.Info($"start {binInfo.ChargerNo} charge {result.IsSuccess}:{result.Msg}");
}
else
{
Log.Info($"can not find {binInfo.ChargerNo} in dict");
}
}
}
catch (Exception e)
{
Log.Error("AutoChargeThread error", e);
}
}
}
private static void ConnClient(EquipNetInfo netInfo)
{
Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
ChargerClient client = AppInfo.Container.Resolve();
client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort));
client.Connect();
client.SessionAttr(netInfo.Code, netInfo.DestAddr);
AddBySn(netInfo.Code, client);
Log.Info($"connected {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
}
}