|
|
|
|
using System.Text;
|
|
|
|
|
using Entity.Constant;
|
|
|
|
|
using Entity.DbModel.Station;
|
|
|
|
|
using log4net;
|
|
|
|
|
using Microsoft.VisualBasic;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Repository.Station;
|
|
|
|
|
using Service.Execute.Api;
|
|
|
|
|
using Service.Execute.Enum;
|
|
|
|
|
using Service.Execute.StaticTools;
|
|
|
|
|
using Service.Execute.SwapException;
|
|
|
|
|
using Service.Init;
|
|
|
|
|
using Swapping.Business.Common;
|
|
|
|
|
using Swapping.Business.Rfid;
|
|
|
|
|
using Swapping.Business.Tech;
|
|
|
|
|
|
|
|
|
|
namespace Service.Execute.Step;
|
|
|
|
|
|
|
|
|
|
public class StationReadyState : IState
|
|
|
|
|
{
|
|
|
|
|
private readonly ILog _log = LogManager.GetLogger(typeof(StationReadyState));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SwapOrderRepository SwapOrderRepository { get; set; }
|
|
|
|
|
|
|
|
|
|
public StateResult Handle(SwappingStateMachine machine)
|
|
|
|
|
{
|
|
|
|
|
_log.Info($"'goto stationReady");
|
|
|
|
|
|
|
|
|
|
machine.Reset();
|
|
|
|
|
SwappingStateMachine.BusinessSwappingState = InfoEnum.BusinessSwappingState.Idle;
|
|
|
|
|
SwappingStateMachine.BusinessSwappingStateUpdateTime = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
//判断换电站是否具备换电条件
|
|
|
|
|
if (!IsAutoSwapping())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//plc是否是远程模式
|
|
|
|
|
var plcIsRemote = PlcIsRemote(machine);
|
|
|
|
|
if (InvokeStatus.Done != plcIsRemote)
|
|
|
|
|
{
|
|
|
|
|
return SwappingStateMachine.ReturnWithInvokeErr(plcIsRemote, ExceptionReason.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//监测雷达
|
|
|
|
|
var entranceRadar = EntranceRadar(machine);
|
|
|
|
|
|
|
|
|
|
if (InvokeStatus.Done != entranceRadar)
|
|
|
|
|
{
|
|
|
|
|
return SwappingStateMachine.ReturnWithInvokeErr(entranceRadar, ExceptionReason.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//开始读rifd
|
|
|
|
|
var beginRfid = BeginRead(machine);
|
|
|
|
|
if (InvokeStatus.Done != beginRfid)
|
|
|
|
|
{
|
|
|
|
|
return SwappingStateMachine.ReturnWithInvokeErr(beginRfid, ExceptionReason.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 读取rfid
|
|
|
|
|
var readRfid = ReadRfid(machine);
|
|
|
|
|
if (InvokeStatus.Done != readRfid)
|
|
|
|
|
{
|
|
|
|
|
return SwappingStateMachine.ReturnWithInvokeErr(readRfid, ExceptionReason.ReadRfidError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new StateResult()
|
|
|
|
|
{
|
|
|
|
|
SwappingState = SwappingState.CarPrepare,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public InvokeStatus PlcIsRemote(SwappingStateMachine machine)
|
|
|
|
|
{
|
|
|
|
|
return Invoker.Invoke("check plc remote", 1000, 5, machine.IsCanceled, PlcApi.IsRemote,
|
|
|
|
|
() =>
|
|
|
|
|
{
|
|
|
|
|
//LED显示-欢迎光临_换电站点_正在营业(三行展示)
|
|
|
|
|
string welcomeContent = "欢迎光临" + StaticStationInfo.StationName + "正在营业";
|
|
|
|
|
|
|
|
|
|
machine.LedTool?.WriteContent(welcomeContent);
|
|
|
|
|
},
|
|
|
|
|
() => { }, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InvokeStatus EntranceRadar(SwappingStateMachine machine)
|
|
|
|
|
{
|
|
|
|
|
return Invoker.Invoke("wait entrance radar", 1000, 1, machine.IsCanceled,
|
|
|
|
|
() => SwappingStateMachine.RadarInFlag, () =>
|
|
|
|
|
{
|
|
|
|
|
if (!PlcApi.EntranceRadar())
|
|
|
|
|
{
|
|
|
|
|
_log.Info("entrance radar false");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SwappingStateMachine.ExceptionReason = ExceptionReason.None;
|
|
|
|
|
_log.Info("entrance radar true");
|
|
|
|
|
SwappingStateMachine.RadarInFlag = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InvokeStatus BeginRead(SwappingStateMachine machine)
|
|
|
|
|
{
|
|
|
|
|
return Invoker.Invoke("read rfid", 1000, 20, machine.IsCanceled,
|
|
|
|
|
() => SwappingStateMachine.BeginRfidReadFlag, () =>
|
|
|
|
|
{
|
|
|
|
|
Task<bool> beginRead = RfidApi.BeginRead();
|
|
|
|
|
beginRead.Wait();
|
|
|
|
|
if (!beginRead.Result)
|
|
|
|
|
{
|
|
|
|
|
_log.Info("begin read rfid error");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SwappingStateMachine.ExceptionReason = ExceptionReason.None;
|
|
|
|
|
_log.Info("begin read done");
|
|
|
|
|
SwappingStateMachine.BeginRfidReadFlag = true;
|
|
|
|
|
}
|
|
|
|
|
}, () =>
|
|
|
|
|
{
|
|
|
|
|
machine.LedTool.WriteProgramContent(InfoEnum.SwapInfo.ErrorReadRfid.GetLed());
|
|
|
|
|
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.ErrorReadRfid);
|
|
|
|
|
}, false, () => { SwappingStateMachine.ExceptionReason = ExceptionReason.ReadRfidError; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断换电站是否具备换电的条件
|
|
|
|
|
/// 1、换电站状态:
|
|
|
|
|
/// 2、换电站方式:
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private bool IsAutoSwapping()
|
|
|
|
|
{
|
|
|
|
|
var statusDes = StationConstant.EnumExtensions.GetDescription(StaticStationInfo.StationStatus);
|
|
|
|
|
_log.Info($"换电站处于{statusDes}状态");
|
|
|
|
|
var wayDes = StationConstant.EnumExtensions.GetDescription(StaticStationInfo.StationWay);
|
|
|
|
|
_log.Info($"换电站处于{wayDes}模式");
|
|
|
|
|
|
|
|
|
|
if (StationConstant.StationStatus.Run == StaticStationInfo.StationStatus
|
|
|
|
|
&& StationConstant.StationWay.Auto == StaticStationInfo.StationWay)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private InvokeStatus ReadRfid(SwappingStateMachine machine)
|
|
|
|
|
{
|
|
|
|
|
//开始读rifd
|
|
|
|
|
return Invoker.Invoke("read rfid", 1000, 20, machine.IsCanceled,
|
|
|
|
|
() => SwappingStateMachine.RfidReadFlag, () =>
|
|
|
|
|
{
|
|
|
|
|
//TODO::开始读rfid RfidService.BeginRead();
|
|
|
|
|
Task<RfidReadModel?> rfidReadModel = RfidApi.ReadRifd();
|
|
|
|
|
rfidReadModel.Wait();
|
|
|
|
|
if (rfidReadModel.IsCompletedSuccessfully && rfidReadModel.Result != null)
|
|
|
|
|
{
|
|
|
|
|
var resultFVelMac = rfidReadModel.Result.VelMac;
|
|
|
|
|
StringBuilder sb = new StringBuilder(16);
|
|
|
|
|
sb.Append(resultFVelMac[0]).Append(resultFVelMac[1]).Append(" ")
|
|
|
|
|
.Append(resultFVelMac[2]).Append(resultFVelMac[3]).Append(" ")
|
|
|
|
|
.Append(resultFVelMac[4]).Append(resultFVelMac[5]).Append(" ")
|
|
|
|
|
.Append(resultFVelMac[6]).Append(resultFVelMac[7]).Append(" ")
|
|
|
|
|
.Append(resultFVelMac[8]).Append(resultFVelMac[9]).Append(" ")
|
|
|
|
|
.Append(resultFVelMac[10]).Append(resultFVelMac[11]);
|
|
|
|
|
rfidReadModel.Result.VelMac = sb.ToString();
|
|
|
|
|
machine.RfidReadModel = rfidReadModel.Result;
|
|
|
|
|
_log.Info($"read rfid={JsonConvert.SerializeObject(machine.RfidReadModel)}");
|
|
|
|
|
|
|
|
|
|
SwappingStateMachine.ExceptionReason = ExceptionReason.None;
|
|
|
|
|
SwappingStateMachine.BusinessSwappingState = InfoEnum.BusinessSwappingState.ScanRfid;
|
|
|
|
|
SwappingStateMachine.BusinessSwappingStateUpdateTime = DateTime.Now;
|
|
|
|
|
_log.Info($"BusinessSwappingState={SwappingStateMachine.BusinessSwappingState}");
|
|
|
|
|
|
|
|
|
|
machine.SwapOrder = SaveOrder(BuildOrder(machine.RfidReadModel));
|
|
|
|
|
|
|
|
|
|
RfidApi.StopRead();
|
|
|
|
|
_log.Info("stop read rfid");
|
|
|
|
|
|
|
|
|
|
SwappingStateMachine.RfidReadFlag = true;
|
|
|
|
|
}
|
|
|
|
|
}, () =>
|
|
|
|
|
{
|
|
|
|
|
machine.LedTool.WriteProgramContent(InfoEnum.SwapInfo.ErrorReadRfid.GetLed());
|
|
|
|
|
SoundTool.PlayOneSound((int)InfoEnum.SwapInfo.ErrorReadRfid);
|
|
|
|
|
}, true, () => { SwappingStateMachine.ExceptionReason = ExceptionReason.ReadRfidError; }, 10,
|
|
|
|
|
InvokeStatus.Exception);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private SwapOrder BuildOrder(RfidReadModel rfidReadModel)
|
|
|
|
|
{
|
|
|
|
|
SwapOrder swapOrder = new SwapOrder()
|
|
|
|
|
{
|
|
|
|
|
Sn = SwapOrderNoGenerator.GenerateOrderNo(StaticStationInfo.StationNo),
|
|
|
|
|
VehicleVin = rfidReadModel.VelVin,
|
|
|
|
|
VehicleMac = rfidReadModel.VelMac,
|
|
|
|
|
VehicleNo = rfidReadModel.VelNo,
|
|
|
|
|
VehicleEnterTime = DateTime.Now,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return swapOrder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SwapOrder SaveOrder(SwapOrder swapOrder)
|
|
|
|
|
{
|
|
|
|
|
SwapOrderRepository.Insert(swapOrder);
|
|
|
|
|
return swapOrder;
|
|
|
|
|
}
|
|
|
|
|
}
|