using System.Collections.Concurrent;
using Common.Util;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.AutoTask;
using log4net;
using Repository.Station;
using Service.Charger.Client;
using Service.Charger.Common;
using Service.Charger.Msg.Charger.OutCharger.Req;
using Service.Charger.Msg.Http.Req;
using Service.Init;
namespace Service.Charger.MyTask;
///
/// 9.2.1.5 站控上报充电枪实时数据上报
///
[Scope]
public class PileRealtimeTask : ITask
{
private static readonly ILog Log = LogManager.GetLogger(typeof(PileRealtimeTask));
private volatile bool _stop;
private readonly BinGunInfoRepository _binGunInfoRepository;
public PileRealtimeTask(BinGunInfoRepository binGunInfoRepository)
{
_binGunInfoRepository = binGunInfoRepository;
}
public string Name()
{
return "PileRealtimeTask";
}
public int Interval()
{
return 1000 * 5;
}
public void Handle()
{
if (ChargerConst._dictionary.Count <= 0)
{
var binGunInfo = _binGunInfoRepository.QueryAsync();
foreach (var binGun in binGunInfo.Result)
{
ChargerConst._dictionary.Add(new Tuple(binGun.ChargerNo,binGun.GunNo),binGun.ChargerGunNo);
}
} if (ChargerConst._dictionary.Count <= 0)return;
ConcurrentDictionary chargerClients = ClientMgr.Dictionary;
if (chargerClients.Values.Count <= 0)
{
return;
}
foreach (var kvp in chargerClients)
{
ChargerClient client = kvp.Value;
HandleChargerPile(client, 1);
HandleChargerPile(client, 2);
}
}
public bool Stoped()
{
return _stop;
}
public void Stop()
{
_stop = true;
}
public void ResetStop()
{
_stop = false;
}
private void HandleChargerPile(ChargerClient client, byte pileIndex)
{
if (client.Connected)
{
ChargerPile chargerPile = pileIndex == 1 ? client.ChargerPile1 : client.ChargerPile2;
PileUploadTelemetry pileUploadTelemetry = client.PileUploadTelemetry[pileIndex];
PileUploadRemoteSignal pileUploadRemoteSignal = client.PileUploadRemoteSignal[pileIndex];
PileRealtimeReq req = new PileRealtimeReq
{
sn = StaticStationInfo.StationNo,
pn = ChargerConst._dictionary[new Tuple(client.Sn,pileIndex)],//chargerPile.pn,
ps = GetPileStatus(client.WorkStatus[pileIndex]),
pov = pileUploadTelemetry.BmsChargingVoltage,
poe = pileUploadTelemetry.BmsChargingCurrent,
con = pileUploadRemoteSignal.ChargeStationGunHolderStatus?0:1,
ec = 0
};
HttpUtil.SendPostRequest(req, "http://127.0.0.1:5034/api/OutCharger/SendPileRealtime");
}
}
private int GetPileStatus(int workStatus)
{
return workStatus switch
{
0 => 1,
1 => 3,
2 => 4,
3 => 5,
_ => 1
};
}
}