using Autofac; using Entity.Attr; using Entity.Constant; using HybirdFrameworkCore.Autofac; using log4net; using Service.Execute.Api; using Service.Execute.Model; using Service.Execute.Model.Tbox; using Service.Execute.StaticTools; using Service.Execute.SwapException; using Service.Execute.Utils; using Service.Station; namespace Service.Execute.Step; public class CarCtrlState : IState { private readonly ILog _log = LogManager.GetLogger(typeof(CarCtrlState)); private readonly CommonMgr _CommonMgr = AppInfo.Container.Resolve(); public StateResult Handle(SwappingStateMachine machine) { //拍照定位 //查看通道状态 StateResult checkChannelStatus = CheckChannelStatus(machine); if (checkChannelStatus != null) { return checkChannelStatus; } //解锁车辆 InvokeStatus unLockCar = UnLockCar(machine); if (unLockCar != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(unLockCar, ExceptionReason.None); } //站控下发换电启动命令到VCU和TBOX //vcu反馈换电就绪 InvokeStatus startSwapToTBox = StartSwapToTBox(machine); if (startSwapToTBox != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(startSwapToTBox, ExceptionReason.None); } //TBOX收到抬起到位信号 InvokeStatus tBoxHandUp = TBoxHandUp(machine); if (tBoxHandUp != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(tBoxHandUp, ExceptionReason.None); } //TBOX收到车辆下电OK信号 InvokeStatus tBoxEnergyDown = TBoxEnergyDown(machine); if (tBoxEnergyDown != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(tBoxEnergyDown, ExceptionReason.None); } //请操作换电启动按钮 SoundApi.PlayOneSound((int) InfoEnum.SwapInfo.InfoManualStartSwap); Thread.Sleep(2000); /*//下发选包 InvokeStatus distributeSelectPack = DistributeSelectPack(machine); if (distributeSelectPack != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(distributeSelectPack, ExceptionReason.None); }*/ return new StateResult() { SwappingState = SwappingState.DoSwapping }; } /// /// 车辆解锁 /// /// public InvokeStatus UnLockCar(SwappingStateMachine machine) { return Invoker.Invoke("UnLockCar", 500, 10, machine.IsCanceled, () => machine.VelUnlockFlag, () => { Task result = TBoxApi.UnLockCarManyTimes(machine.RfidReadModel.VelVin); bool unLock = result.Result; if (unLock) { //查询车辆锁止状态 Task carInfo = TBoxApi.GetCarInfo(machine.RfidReadModel.VelVin); var resultHeartBeatMsg = carInfo.Result.CarStatus; if (resultHeartBeatMsg?.LockStatus == 1 || machine.ManualConfirmCarUnlockFlag) { machine.ManualConfirmCarUnlockFlag = false; machine.BoxCarInfoModel = carInfo.Result; machine.VelUnlockFlag = true; _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.VelUnlockFlag, machine); } } }, () => { // machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrUnLockCar.GetLed()); SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.ErrUnLockCar); }, false, () => { machine.ExceptionReason = ExceptionReason.UnLockCarError; } , 10, InvokeStatus.None); } /// ///收到抬起到位信号 /// /// public InvokeStatus StartSwapToTBox(SwappingStateMachine machine) { return Invoker.Invoke("StartSwapToTBox", 500, 10, machine.IsCanceled, () => machine.TboxHandUpFlag, () => { Task result = TBoxApi.StartSwap(machine.RfidReadModel.VelVin); byte res = result.Result; //换电就绪 if (res == 1) { //查询车辆锁止状态 machine.TboxHandUpFlag = true; _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.TBoxSwapReadyFlag, machine); } }, () => { // machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrUnLockCar.GetLed()); SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.ErrStartSwapToTBox); }, false, () => { machine.ExceptionReason = ExceptionReason.StartSwapToTBoxError; } , 10, InvokeStatus.None); } /// ///收到抬起到位信号 /// /// public InvokeStatus TBoxHandUp(SwappingStateMachine machine) { return Invoker.Invoke("TBoxHandUp", 500, 5, machine.IsCanceled, () => machine.TboxHandUpFlag, () => { Task result = TBoxApi.StartSwap(machine.RfidReadModel.VelVin); byte res = result.Result; //换电就绪 if (res == 2) { //查询车辆锁止状态 machine.TboxHandUpFlag = true; // _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.TBoxSwapReadyFlag, // machine); } }, () => { // machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrUnLockCar.GetLed()); SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoTboxHandUp); }, false, () => { } , 10, InvokeStatus.None); } /// ///收到下电成功 /// /// public InvokeStatus TBoxEnergyDown(SwappingStateMachine machine) { return Invoker.Invoke("TBoxEnergyDown", 500, 5, machine.IsCanceled, () => machine.TboxEnergyDownFlag, () => { Task result = TBoxApi.StartSwap(machine.RfidReadModel.VelVin); byte res = result.Result; //换电就绪 if (res == 3) { //查询车辆锁止状态 machine.TboxEnergyDownFlag = true; // _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.TBoxSwapReadyFlag, // machine); } }, () => { // machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrUnLockCar.GetLed()); SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoTboxEnergyDown); }, false, () => { } , 10, InvokeStatus.None); } /// /// 下发选包 /// /// public InvokeStatus DistributeSelectPack(SwappingStateMachine machine) { return Invoker.Invoke("DistributeSelectPack", 500, 10, machine.IsCanceled, () => machine.DistributeSelectPackFlag, () => { var swapOrderBatteryInfo = machine.SwapOrderBatteryInfo; machine.DistributeSelectPackFlag = PlcApi.DistributeSelectPack(swapOrderBatteryInfo.InBinInfo.No, swapOrderBatteryInfo.UpBinInfo.No); if (machine.DistributeSelectPackFlag) { _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.DistributeSelectPackFlag, machine); } }, () => { // machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrDistributeSelectPack.GetLed()); }); } /// /// 查看通道状态 /// :0:无效值 /// 1000;拍照OK ///1010;拍照NG ///1020;拍照超限,请移车 /// /// public StateResult CheckChannelStatus(SwappingStateMachine machine) { int count = 0; while (!machine.ChannelStatusOkFlag) { if (machine.CancelFlag) { _log.Info($" CheckChannelStatus canceled"); return StateResult.Cancel; } _log.Info("begin plc CheckChannelStatus"); Thread.Sleep(2000); var channelStatus = PlcApi.ChannelStatus(); if (count % 10 == 0) { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.CarTakePhotoInfo); } count++; if (channelStatus == 1000) { machine.ChannelStatusOkFlag = true; return null; } else { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.CarTakePhotoErrRepeat); //故障 不能继续换电,提示驶离,上报云平台,修改换电订单 machine.SwapStatus = (int)InfoEnum.SwapOrderResult.Fail; machine.SwapFailReason = "拍照故障 plc故障码1010"; //跳转到SwappDone return new StateResult() { SwappingState = SwappingState.SwapDone }; } } return null; } }