using AutoMapper; using Common.Util; using Entity.Api.Resp; using Entity.Constant; using Entity.DbModel.Station; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using Repository.Station; using Service.Cloud.Client; using Service.Execute; using Service.Execute.Api; using Service.Execute.Model; using Service.Init; using Service.Plc.Client; namespace Service.Station; [Scope("SingleInstance")] public class MonitorService { public BinInfoRepository BinInfoRepository { get; set; } public SwapOrderRepository SwapOrderRepository { get; set; } public ChargeOrderRepository ChargeOrderRepository { get; set; } public Result GetSwapMonitorData() { var configBinInfo = new MapperConfiguration(cfg => cfg.CreateMap().ReverseMap()); IMapper mapperBinInfo = configBinInfo.CreateMapper(); List stateInfoList = mapperBinInfo.Map>(StationSoftMgr.SwappingStateMachine.StepModel); Task carInfo = TBoxApi.GetCarInfo(); List binInfos = BinInfoRepository.QueryListByClause(i => i.Exists == 1 && i.Status == 1); SwapMonitorScreenResp monitorScreenResp = new() { PlcSwapModel = new PlcSwapModelResp() { ModelState = PlcMgr.PlcToHostData.ModeControl.Value, StationInLampSts = PlcMgr.PlcToHostData.LightIn.Value, ControlModel = PlcMgr.PlcToHostData.RemoteLocalControlState.Value, }, StateInfo = stateInfoList, VehicleInfo = new SwapVehicleResp() { OrderNo = StationSoftMgr.SwappingStateMachine.SwapOrder != null ? StationSoftMgr.SwappingStateMachine.SwapOrder.Sn : null, VelMac = StationSoftMgr.SwappingStateMachine.RfidReadModel != null ? StationSoftMgr.SwappingStateMachine.RfidReadModel.VelMac : null, LockStatus = carInfo.Result != null ? carInfo.Result.HeartBeatMsg.LockStatus : null, KeyStatus = carInfo.Result != null ? carInfo.Result.HeartBeatMsg.KeyStatus : null, VelNo = StationSoftMgr.SwappingStateMachine.RfidReadModel != null ? StationSoftMgr.SwappingStateMachine.RfidReadModel.VelNo : null, VelVin = StationSoftMgr.SwappingStateMachine.RfidReadModel != null ? StationSoftMgr.SwappingStateMachine.RfidReadModel.VelVin : null, }, BatteryInfo = new() { BatteryTotalCount = binInfos.Count, UsingSwapBatteryCount = binInfos.Select(i => i.ChargeStatus == 2 && i.AmtLock == (int)InfoEnum.AmtBatLockStatus.UnLock && i.Soc > int.Parse(StaticStationInfo.SwapSoc) && new TimeSpan(DateTime.Now.Ticks - i.LastChargeFinishTime.ToDateTime().Ticks) .TotalMinutes > int.Parse(StaticStationInfo.SwapFinishChargeTime)).Count() } }; return Result.Success(monitorScreenResp); } /// /// 模式類 /// /// public Result GetModel() { SwapModelResp resp = new() { StationStatus = StaticStationInfo.StationStatus, StationWay = StaticStationInfo.StationWay, StationModel = StaticStationInfo.StationModel }; return Result.Success(resp); } /// /// 连接状态类 /// /// public Result GetDeviceState() { var isConnected = TBoxApi.IsConnected(); DeviceStateResp resp = new() { BoxConnectFlag = isConnected.Result != null ? isConnected.Result : false, CloudConnectFlag = CloudClientMgr.CloudClient.Connected, PlcConnectFlag = PlcMgr.PlcClient.Connected }; return Result.Success(resp); } public Result SwapAndChargingCount() { SwapAndChargingCountResp chargingCountResp = new() { ChargeTodayCount = ChargeOrderRepository.GetCount(i => DateUtils.IsDateTimeToday(i.EndTime)), ChargeTotalCount = ChargeOrderRepository.GetCount(i => i.EndTime != null), SwapTodayCount = SwapOrderRepository.GetCount(i => i.SwapResult != 0 && DateUtils.IsDateTimeToday(i.SwapEndTime)), SwapTotalCount = SwapOrderRepository.GetCount(i => i.SwapResult != 0), }; return Result.Success(chargingCountResp); } /// /// 电池移仓 /// /// /// /// public Result BatteryRelocation(ushort removeBinNo, ushort putBinNo) { //校验:出仓位 BinInfo? removeBin = BinInfoRepository.QueryByClause(i => i.No.Equals(removeBinNo) && i.ChargeStatus == 2 && i.Exists == 1 && i.AmtLock == 0); if (removeBin == null) { return Result.Fail("出仓位状态有误"); } BinInfo? putBin = BinInfoRepository.QueryByClause(i => i.No.Equals(putBinNo) && i.ChargeStatus == 2 && i.Exists == 0 && i.AmtLock == 0 && i.Status == 1); if (putBin == null) { return Result.Fail("入仓位状态有误"); } var result = PlcMgr.DistributeTask(putBinNo, removeBinNo, 2) ? Result.Success() : Result.Fail(); if (!result.IsSuccess) { return Result.Fail(); } //如果是缓存仓 更新缓存仓的数据 if (putBinNo == 1) { putBin.Exists = 1; putBin.Soc = removeBin.Soc; putBin.Soe = removeBin.Soe; putBin.Soh = removeBin.Soh; BinInfoRepository.Update(putBin); } return Result.Success(); } public Result> GetChargeBinOption() { List queryListByClause = BinInfoRepository.Query(); var configBinInfo = new MapperConfiguration(cfg => cfg.CreateMap().ReverseMap()); IMapper mapperBinInfo = configBinInfo.CreateMapper(); return Result>.Success(mapperBinInfo.Map>(queryListByClause)); } }