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.
141 lines
4.9 KiB
141 lines
4.9 KiB
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;
|
|
|
|
namespace Service.Charger.Client;
|
|
|
|
/// <summary>
|
|
/// 示例程序
|
|
/// </summary>
|
|
[Scope("SingleInstance")]
|
|
public static class ClientMgr
|
|
{
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientMgr));
|
|
|
|
private static readonly Dictionary<string, ChargerClient> Dictionary = new();
|
|
|
|
public static ChargerClient? GetBySn(string sn)
|
|
{
|
|
Dictionary.TryGetValue(sn, out var o);
|
|
return o;
|
|
}
|
|
|
|
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<EquipInfoRepository>();
|
|
EquipNetInfoRepository netInfoRepository = AppInfo.Container.Resolve<EquipNetInfoRepository>();
|
|
List<EquipInfo> equipInfos = equipInfoRepository.QueryListByClause(it => it.TypeCode == (int)EquipmentType.Charger);
|
|
if (equipInfos.Count > 0)
|
|
{
|
|
Dictionary<string,EquipInfo> set = equipInfos.ToDictionary(it => it.Code, it => it);
|
|
List<EquipNetInfo> equipNetInfos = netInfoRepository.QueryListByClause(it => set.ContainsKey(it.Code));
|
|
foreach (EquipNetInfo netInfo in equipNetInfos)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
ConnClient(netInfo);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static void StartAutoChargeThread()
|
|
{
|
|
Thread thread = new Thread(AutoChargeThread)
|
|
{
|
|
Name = @"auto-charge"
|
|
};
|
|
thread.Start();
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private static void AutoChargeThread()
|
|
{
|
|
ElecPriceModelVersionRepository elecPriceModelVersionRepository =
|
|
AppInfo.Container.Resolve<ElecPriceModelVersionRepository>();
|
|
|
|
ElecPriceModelVersionDetailRepository elecPriceModelVersionDetailRepository =
|
|
AppInfo.Container.Resolve<ElecPriceModelVersionDetailRepository>();
|
|
|
|
while (true)
|
|
{
|
|
Thread.Sleep(1000 * 30);
|
|
DateTime now = DateTime.Now;
|
|
ElecPriceModelVersion elecPriceModelVersion = elecPriceModelVersionRepository.QueryByClause(i => i.StartTime >= now && i.EndTime < now);
|
|
if (elecPriceModelVersion == null)
|
|
{
|
|
Log.Info("lack of effective elec price model");
|
|
continue;
|
|
}
|
|
|
|
List<ElecPriceModelVersionDetail> 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 elec price model detail");
|
|
continue;
|
|
}
|
|
|
|
//int batteryCount = elecPriceModelVersionDetail.BatteryCount;
|
|
|
|
|
|
foreach (KeyValuePair<string,ChargerClient> pair in Dictionary)
|
|
{
|
|
Result<bool> result = pair.Value.StartCharge();
|
|
Log.Info($"start {pair.Key} charge {result.IsSuccess}:{result.Msg}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ConnClient(EquipNetInfo netInfo)
|
|
{
|
|
Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
|
|
ChargerClient client = AppInfo.Container.Resolve<ChargerClient>();
|
|
client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort));
|
|
client.Connect();
|
|
client.SessionAttr(netInfo.Code, netInfo.NetAddr);
|
|
AddBySn(netInfo.Code, client);
|
|
Log.Info($"connected {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
|
|
}
|
|
} |