using System.Collections.Concurrent; using Entity.DbModel.Station; using Entity.Dto.Resp; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using Repository.Station; using Service.Charger.Client; using Service.Charger.Msg.Host.Req; using Service.Init; using SqlSugar; namespace Service.Charger; /// /// 充电机服务 /// [Scope] public class ChargerService { public BinInfoRepository BinInfoRepository { get; set; } public EquipInfoRepository EquipInfoRepository { get; set; } public ElecPriceModelVersionDetailRepository ElecPriceModelVersionDetailRepository { get; set; } /// /// 启动充电 /// /// /// public Result StartChargeByBinNo(string binNo) { BinInfo? binInfo = BinInfoRepository.QueryByBinNo(binNo); if (binInfo == null) { return Result.Fail(@"充电仓不存在"); } if (string.IsNullOrWhiteSpace(binInfo.ChargerNo)) { return Result.Fail(@"充电仓未配置充电机编号"); } ChargerClient? chargerClient = ClientMgr.GetBySn(binInfo.ChargerNo); if (chargerClient == null || !chargerClient.Connected) { return Result.Fail(@"充电机未连接"); } byte chargeSoc = StaticStationInfo.ChargeSoc; float? chargePower = EquipInfoRepository.QueryPowerByCode(binNo); float? power = chargePower == null ? StaticStationInfo.ChargePower : chargePower; return chargerClient.StartCharge(chargeSoc,(float)power); } /// /// 停止充电 /// /// /// public Result StopChargeByBinNo(string binNo) { BinInfo? binInfo = BinInfoRepository.QueryByBinNo(binNo); if (binInfo == null) { return Result.Fail(@"充电仓不存在"); } if (string.IsNullOrWhiteSpace(binInfo.ChargerNo)) { return Result.Fail(@"充电仓未配置充电机编号"); } ChargerClient? chargerClient = ClientMgr.GetBySn(binInfo.ChargerNo); if (chargerClient == null || !chargerClient.Connected) { return Result.Fail(@"充电机未连接"); } chargerClient.SendRemoteStopCharging(); return Result.Success(true, "发送停止命令成功"); } /// /// 下发尖峰平谷 /// /// /// public Result DistributeElecPriceForCharge(int version) { ConcurrentDictionary chargerClients = ClientMgr.Dictionary; if (chargerClients.Values.Count <= 0) { return Result.Fail(); } foreach (var chargerClientsValue in chargerClients.Values) { if (chargerClientsValue.Connected) { chargerClientsValue.SendSetPeakValleyTime(BulidSetPeakValleyTimeObj(version)); } } return Result.Success(); } public SetPeakValleyTime BulidSetPeakValleyTimeObj(int version) { List elecPriceModelVersionDetails = ElecPriceModelVersionDetailRepository.QueryListByClause(u => u.Version == version, u => u.StartHour, OrderByType.Asc); SetPeakValleyTime setPeakValleyTime = new SetPeakValleyTime() { NumberTime = Convert.ToByte(elecPriceModelVersionDetails.Count), StartHH1 = Convert.ToByte(elecPriceModelVersionDetails[0].StartHour), StartHH2 = Convert.ToByte(elecPriceModelVersionDetails[1].StartHour), StartHH3 = Convert.ToByte(elecPriceModelVersionDetails[2].StartHour), StartHH4 = Convert.ToByte(elecPriceModelVersionDetails[3].StartHour), StartHH5 = Convert.ToByte(elecPriceModelVersionDetails[4].StartHour), StartHH6 = Convert.ToByte(elecPriceModelVersionDetails[5].StartHour), StartHH7 = Convert.ToByte(elecPriceModelVersionDetails[6].StartHour), StartHH8 = Convert.ToByte(elecPriceModelVersionDetails[7].StartHour), StartMM1 = Convert.ToByte(elecPriceModelVersionDetails[0].StartMinute), StartMM2 = Convert.ToByte(elecPriceModelVersionDetails[1].StartMinute), StartMM3 = Convert.ToByte(elecPriceModelVersionDetails[2].StartMinute), StartMM4 = Convert.ToByte(elecPriceModelVersionDetails[3].StartMinute), StartMM5 = Convert.ToByte(elecPriceModelVersionDetails[4].StartMinute), StartMM6 = Convert.ToByte(elecPriceModelVersionDetails[5].StartMinute), StartMM7 = Convert.ToByte(elecPriceModelVersionDetails[6].StartMinute), StartMM8 = Convert.ToByte(elecPriceModelVersionDetails[7].StartMinute), TimePeak1 = Convert.ToByte(elecPriceModelVersionDetails[0].Type), TimePeak2 = Convert.ToByte(elecPriceModelVersionDetails[1].Type), TimePeak3 = Convert.ToByte(elecPriceModelVersionDetails[2].Type), TimePeak4 = Convert.ToByte(elecPriceModelVersionDetails[3].Type), TimePeak5 = Convert.ToByte(elecPriceModelVersionDetails[4].Type), TimePeak6 = Convert.ToByte(elecPriceModelVersionDetails[5].Type), TimePeak7 = Convert.ToByte(elecPriceModelVersionDetails[6].Type), TimePeak8 = Convert.ToByte(elecPriceModelVersionDetails[7].Type) }; return setPeakValleyTime; } /// /// 电池状态信息:电池总数 满电数量、充电中、故障电池、维护中电池 /// /// public Result BatteryStatusInfo() { BatteryStatusInfoResp batteryStatusInfoResp = new BatteryStatusInfoResp(); List binInfos = BinInfoRepository.QueryListByClause(i => i.Exists == 1 && i.Status == 1); if (binInfos.Count > 0) batteryStatusInfoResp.btyTotalCount = binInfos.Count(); List canSwapCounts = BinInfoRepository.QueryListByClause(i => i.Exists == 1 && i.Status == 1 && i.Soc > Convert.ToDecimal(StaticStationInfo.SwapSoc) && i.ChargeStatus!=1); if (canSwapCounts.Count > 0) batteryStatusInfoResp.canSwapCount = canSwapCounts.Count(); List chargingCounts = BinInfoRepository.QueryListByClause(i => i.Exists == 1 && i.Status == 1 && i.ChargeStatus == 1); if (chargingCounts.Count > 0) batteryStatusInfoResp.chargingCount = chargingCounts.Count(); return Result.Success(batteryStatusInfoResp); } }