You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
10 KiB

using log4net;
using Repository.Station;
using Service.Execute.Api;
using Service.Execute.Enum;
using Service.Execute.Model;
using Service.Execute.StaticTools;
using Service.Execute.SwapException;
using Swapping.Business.Tech;
namespace Service.Execute.Step;
public class DoSwappingState : IState
{
private readonly ILog _log = LogManager.GetLogger(typeof(DoSwappingState));
private SwapOrderRepository SwapOrderRepository { get; set; }
public StateResult Handle(SwappingStateMachine machine)
{
//上报云平台换电开始
SwappingStateMachine.BusinessSwappingForCloudState = InfoEnum.BusinessSwappingForCloudState.BeginSwap;
SwappingStateMachine.BusinessSwappingStateUpdateTime = DateTime.Now;
_log.Info($"BusinessSwappingForCloudState={SwappingStateMachine.BusinessSwappingForCloudState}");
CloudApi.SendStateLog(machine.SwapOrder, InfoEnum.BusinessSwappingForCloudState.BeginSwap);
//再次读锁止状态防止plc需要挪车
InvokeStatus carInPosition2 = CarInPosition2(machine);
if (carInPosition2 != InvokeStatus.Done)
{
return SwappingStateMachine.ReturnWithInvokeErr(carInPosition2, ExceptionReason.None);
}
//下发启动换电
InvokeStatus startSwapping = StartSwapping(machine);
if (startSwapping != InvokeStatus.Done)
{
return SwappingStateMachine.ReturnWithInvokeErr(startSwapping, ExceptionReason.None);
}
//查看通道状态
StateResult checkChannelStatus = CheckChannelStatus(machine);
if (checkChannelStatus != null)
{
return checkChannelStatus;
}
//旧电池拆卸
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
};
}
/// <summary>
/// 下发plc启动换电
/// </summary>
/// <returns></returns>
public InvokeStatus StartSwapping(SwappingStateMachine machine)
{
return Invoker.Invoke("UnLockCar", 1000, 5, machine.IsCanceled,
() => SwappingStateMachine.StartSwappingFlag, () =>
{
Task<bool> result = TBoxApi.UnLockCar();
bool unLock = result.Result;
if (unLock)
{
//查询车辆锁止状态
var startSwapping = PlcApi.StartSwapping();
if (startSwapping)
{
machine.SwapOrder.SwapBeginTime = DateTime.Now;
SwapOrderRepository.Update(machine.SwapOrder);
machine.SwapStatus = 0;
SwappingStateMachine.StartSwappingFlag = true;
}
}
}, () => { });
}
/// <summary>
/// 读取plc任务状态电池拆卸中
/// 读取plc任务状态电池入库搬运中
/// 读取plc任务状态电池出库搬运中
/// 读取plc任务状态电池安装中
/// 读取plc任务状态电池安装完成
/// </summary>
/// <returns></returns>
public InvokeStatus UnPack(SwappingStateMachine machine)
{
return Invoker.Invoke("plc UnPack", 500, 5, machine.IsCanceled,
() => SwappingStateMachine.UnOldBatteryFlag, () =>
{
machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.InfoUnPack.GetLed());
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.InfoUnPack);
SwappingStateMachine.UnOldBatteryFlag = PlcApi.ReadPlcTaskStatus() == 1002;
}, () => { });
}
public InvokeStatus OldBatteryCarryIn(SwappingStateMachine machine)
{
return Invoker.Invoke("plc OldBatteryCarryIn", 500, 5, machine.IsCanceled,
() => SwappingStateMachine.StorageOldBatteryFlag, () =>
{
machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.InfoOldBatteryCarryIn.GetLed());
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.InfoOldBatteryCarryIn);
SwappingStateMachine.StorageOldBatteryFlag = PlcApi.ReadPlcTaskStatus() == 1003;
}, () => { });
}
public InvokeStatus NewBatteryCarryOut(SwappingStateMachine machine)
{
return Invoker.Invoke("plc NewBatteryCarryOut", 500, 5, machine.IsCanceled,
() => SwappingStateMachine.OutNewBatteryFlag, () =>
{
machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.InfoNewBatteryCarryOut.GetLed());
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.InfoNewBatteryCarryOut);
SwappingStateMachine.OutNewBatteryFlag = PlcApi.ReadPlcTaskStatus() == 1004;
}, () => { });
}
public InvokeStatus Pack(SwappingStateMachine machine)
{
return Invoker.Invoke("plc Pack ing", 500, 5, machine.IsCanceled,
() => SwappingStateMachine.InstallNewBatteryFlag, () =>
{
machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.InfoPack.GetLed());
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.InfoPack);
SwappingStateMachine.InstallNewBatteryFlag = PlcApi.ReadPlcTaskStatus() == 1005;
}, () => { });
}
public InvokeStatus PackFinish(SwappingStateMachine machine)
{
return Invoker.Invoke("plc Pack Finish", 500, 5, machine.IsCanceled,
() => SwappingStateMachine.FinishNewBatteryFlag, () =>
{
machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.InfoPackFinish.GetLed());
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.InfoPackFinish);
if (PlcApi.ReadPlcTaskStatus() == 1006)
{
SwappingStateMachine.FinishNewBatteryFlag = true;
SwappingStateMachine.BusinessSwappingForCloudState =
InfoEnum.BusinessSwappingForCloudState.SwapFinish;
CloudApi.SendStateLog(machine.SwapOrder,SwappingStateMachine.BusinessSwappingForCloudState);
SwappingStateMachine.BusinessSwappingStateUpdateTime = DateTime.Now;
//上报云平台换电完成
machine.SwapStatus = 1;
}
}, () => { });
}
/// <summary>
/// 查看通道状态
/// :0无效值
/// 1000;拍照OK
///1010;拍照NG
///1020;拍照超限,请移车
/// </summary>
/// <returns></returns>
public StateResult CheckChannelStatus(SwappingStateMachine machine)
{
while (!SwappingStateMachine.ChannelStatusOkFlag)
{
_log.Info("begin plc CheckChannelStatus");
Thread.Sleep(1000);
var channelStatus = PlcApi.ChannelStatus();
if (channelStatus == 1000)
{
SwappingStateMachine.ChannelStatusOkFlag = true;
return null;
}
else if (channelStatus == 1020)
{
//需要移车 先解锁 提示移动车辆,等待3分钟
var lockCar = TBoxApi.LockCar();
if (lockCar.Result)
{
machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrChannelStatus.GetLed());
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.ErrChannelStatus);
//等待3分钟
Thread.Sleep(3000);
SwappingStateMachine.VehiclesInPlace2Flag = false;
SwappingStateMachine.StartSwappingFlag = false;
//回归到本阶段的读锁止状态
return new StateResult()
{
SwappingState = SwappingState.DoSwapping
};
}
}
else if (channelStatus == 1010)
{
//故障 不能继续换电,提示驶离,上报云平台,修改换电订单
machine.SwapStatus = (int)InfoEnum.SwapOrderResult.Fail;
//跳转到SwappDone
return new StateResult()
{
SwappingState = SwappingState.SwapDone
};
}
}
return null;
}
/// <summary>
/// 车辆到位
/// </summary>
/// <returns></returns>
public InvokeStatus CarInPosition2(SwappingStateMachine machine)
{
return Invoker.Invoke("check CarInPosition2", 500, 50, machine.IsCanceled,
() => SwappingStateMachine.VehiclesInPlace2Flag, () =>
{
var result = TBoxApi.GetCarInfo();
TboxCarInfoModel tboxCarInfoModel = result.Result;
if (tboxCarInfoModel.HeartBeatMsg.KeyStatus == 0)
{
SwappingStateMachine.VehiclesInPlace2Flag = true;
}
}, () =>
{
SwappingStateMachine.ExceptionReason = ExceptionReason.CarInPositionError;
machine.LedTool?.WriteProgramContent(InfoEnum.SwapInfo.ErrorCarInPositionTimeout.GetLed());
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.ErrorCarInPositionTimeout);
}, false, () => { }
, 20, InvokeStatus.None);
}
}