using Autofac; using Entity.Attr; using Entity.Constant; using HybirdFrameworkCore.Autofac; using log4net; using Repository.Station; 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.Init; using Service.MyTask; using Service.Station; namespace Service.Execute.Step; public class DoSwappingState : IState { private readonly ILog _log = LogManager.GetLogger(typeof(DoSwappingState)); private readonly CommonMgr _CommonMgr = AppInfo.Container.Resolve(); public StateResult Handle(SwappingStateMachine machine) { //上报云平台换电开始 machine.BusinessSwappingForCloudState = InfoEnum.BusinessSwappingForCloudState.BeginSwap; machine.BusinessSwappingStateUpdateTime = DateTime.Now; _log.Info($"BusinessSwappingForCloudState={machine.BusinessSwappingForCloudState}"); //下发启动换电 InvokeStatus startSwapping = StartSwapping(machine); if (startSwapping != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(startSwapping, ExceptionReason.None); } //读plc任务,上使能 InvokeStatus holdOn = HoldOn(machine); if (holdOn != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(holdOn, ExceptionReason.None); } //旧电池拆卸 InvokeStatus unPack = UnPack(machine); if (unPack != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(unPack, ExceptionReason.None); } //旧电池搬运 InvokeStatus oldBatteryCarryIn = OldBatteryCarryIn(machine); if (oldBatteryCarryIn != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(oldBatteryCarryIn, ExceptionReason.None); } //新电池搬运 InvokeStatus newBatteryCarryOut = NewBatteryCarryOut(machine); if (newBatteryCarryOut != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(newBatteryCarryOut, ExceptionReason.None); } //安装 InvokeStatus pack = Pack(machine); if (pack != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(pack, ExceptionReason.None); } //安装完成 InvokeStatus packFinish = PackFinish(machine); if (packFinish != InvokeStatus.Done) { return SwappingStateMachine.ReturnWithInvokeErr(packFinish, ExceptionReason.None); } return new StateResult() { SwappingState = SwappingState.SwapDone }; } /// /// 下发plc启动换电 /// /// public InvokeStatus StartSwapping(SwappingStateMachine machine) { return Invoker.Invoke("UnLockCar", 1000, 5, machine.IsCanceled, () => machine.StartSwappingFlag, () => { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.StartSwapping); //下发启动换电 var startSwapping = PlcApi.StartSwapping(machine.SwapOrderBatteryInfo.InBinInfo[0].No, machine.SwapOrderBatteryInfo.UpBinInfo[0].No); //查詢當前任務狀態是否被更改 var readPlcTaskStatus = PlcApi.ReadTaskStatus(2); if (startSwapping && readPlcTaskStatus) { machine.SwapOrder.SwapBeginTime = DateTime.Now; _CommonMgr.UpdateSwapOrder(machine); machine.SwapStatus = 0; machine.StartSwappingFlag = true; _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.StartSwappingFlag, machine); } }, () => { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.ErrStartSwap); }, false, () => { }, 10, InvokeStatus.None); } /// /// plc读任务状态 上使能 /// /// /// public InvokeStatus HoldOn(SwappingStateMachine machine) { return Invoker.Invoke("plc HoldOn", 1000, 5, machine.IsCanceled, () => machine.PlcHoldFlag, () => { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.HoldOn); //读取任务状态 /*var taskPrepare = PlcApi.ReadTaskStatus(2); if (taskPrepare)*/ { bool holdOn = PlcApi.HoldOn(); if (holdOn) { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.StartSwapInfo); machine.PlcHoldFlag = true; } } }, () => { }); } /// /// 读取plc任务状态电池拆卸中 /// 读取plc任务状态电池入库搬运中 /// 读取plc任务状态电池出库搬运中 /// 读取plc任务状态电池安装中 /// 读取plc任务状态电池安装完成 /// /// public InvokeStatus UnPack(SwappingStateMachine machine) { return Invoker.Invoke("plc UnPack", 500, 5, machine.IsCanceled, ReadTaskErr, () => machine.UnOldBatteryFlag, () => { int status = PlcApi.ReadPlcTaskStatus(); //处理程序太快,plc还没有初始值的状态 if (status == 1002 || status != 1003) { } else { machine.UnOldBatteryFlag = true; } if (machine.UnOldBatteryFlag) { _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.UnOldBatteryFlag, machine); } SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoUnPack); }, () => { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoUnPack); }, false, 10, InvokeStatus.None); } public InvokeStatus OldBatteryCarryIn(SwappingStateMachine machine) { return Invoker.Invoke("plc OldBatteryCarryIn", 500, 5, machine.IsCanceled, ReadTaskErr, () => machine.StorageOldBatteryFlag, () => { var readPlcTaskStatus = PlcApi.ReadPlcTaskStatus(); if (readPlcTaskStatus == 1003 || readPlcTaskStatus != 1004) { } else { machine.StorageOldBatteryFlag = true; } machine.StorageOldBatteryFlag = readPlcTaskStatus == 1004; if (machine.StorageOldBatteryFlag) { _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.StorageOldBatteryFlag, machine); } SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoOldBatteryCarryIn); }, () => { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoOldBatteryCarryIn); }, false, 10, InvokeStatus.None); } public InvokeStatus NewBatteryCarryOut(SwappingStateMachine machine) { return Invoker.Invoke("plc NewBatteryCarryOut", 500, 5, machine.IsCanceled, ReadTaskErr, () => machine.OutNewBatteryFlag, () => { if (PlcApi.ReadPlcTaskStatus() == 1004) { } else { machine.OutNewBatteryFlag = true; } if (machine.OutNewBatteryFlag) { _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.OutNewBatteryFlag, machine); } SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoNewBatteryCarryOut); }, () => { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoNewBatteryCarryOut); }, false, 10, InvokeStatus.None); } public InvokeStatus Pack(SwappingStateMachine machine) { return Invoker.Invoke("plc Pack ing", 500, 5, machine.IsCanceled, ReadTaskErr, () => machine.InstallNewBatteryFlag, () => { // machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.InfoPack.GetLed()); if (PlcApi.ReadPlcTaskStatus() == 1005) { } else { machine.InstallNewBatteryFlag = true; } if (machine.InstallNewBatteryFlag) { _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.InstallNewBatteryFlag, machine); } SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoPack); }, () => { SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoPack); }, false, 10, InvokeStatus.None); } public InvokeStatus PackFinish(SwappingStateMachine machine) { return Invoker.Invoke("plc Pack Finish", 500, 5, machine.IsCanceled, ReadTaskErr, () => machine.FinishNewBatteryFlag, () => { var readPlcTaskStatus = PlcApi.ReadPlcTaskStatus(); if (readPlcTaskStatus == 1006 || readPlcTaskStatus == 1007) { machine.FinishNewBatteryFlag = true; machine.BusinessSwappingForCloudState = InfoEnum.BusinessSwappingForCloudState.SwapFinish; // CloudApi.SendStateLog(machine.SwapOrder, machine.BusinessSwappingForCloudState); machine.BusinessSwappingStateUpdateTime = DateTime.Now; SoundApi.PlayOneSound((int)InfoEnum.SwapInfo.InfoPackFinish); _CommonMgr.InsertStep(InfoEnum.BusinessSwappingStep.FinishNewBatteryFlag, machine); } }, () => { }, false, 10, InvokeStatus.None); } public bool ReadTaskErr() { if (PlcApi.ReadTaskStatus(1)) { StationSoftMgr.SwappingStateMachine.RfidReadModel = null; TBoxApi.SetHeart("1", 1);//心跳状态清空 StationSoftMgr.SwappingStateMachine.PLCAbnormalFlag = true; StationSoftMgr.SwappingStateMachine.SwapStatus = (int)InfoEnum.SwapOrderResult.Fail; StationSoftMgr.SwappingStateMachine.SwapFailReason = "PLC任务异常结束"; return true; } return false; } }