using Autofac.Core; using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Hosting; using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using Quartz; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.Design; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Eventing.Reader; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using WIDESEA_Common.Log; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Enums; using WIDESEAWCS_DTO.Enum; using WIDESEAWCS_ISystemServices; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_Model.Models.System; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DeviceBase; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_QuartzJob.StackerCrane; using WIDESEAWCS_QuartzJob.StackerCrane.Enum; using WIDESEAWCS_TaskInfoService; using WIDESEAWCS_Tasks.ConveyorLineJob; using WIDESEAWCS_Tasks.StackerCraneJob; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class SpeStackerCraneJob : IJob { private readonly ITaskService _taskService; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly ITaskRepository _taskRepository; private readonly IRouterService _routerService; private readonly IAgvStationService _agvStationService; public SpeStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IAgvStationService agvStationService) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _taskRepository = taskRepository; _routerService = routerService; _agvStationService = agvStationService; } public string ziche001 = "RGV104"; public string ziche002 = "RGV107"; public Task Execute(IJobExecutionContext context) { try { SpeStackerCrane speStackerCrane = (SpeStackerCrane)context.JobDetail.JobDataMap.Get("JobParams"); if (speStackerCrane != null) { GetStackerObject getStackerObject = new GetStackerObject(speStackerCrane); //读取设备完成信息 if (getStackerObject.StaclerkJobJobStatusValue == RgvJobCraneStatus.Completed && getStackerObject.CurrentRgvtaskid!=0) { _taskService.UpdateTaskStatus(getStackerObject.CurrentRgvtaskid); } if (getStackerObject.RgvCraneAutoStatusValue == RgvCraneAutoStatus.Automatic && getStackerObject.StaclerkJobJobStatusValue == RgvJobCraneStatus.Ready && getStackerObject.RgvCraneWorkStatusValue == RGVStepprocess.NoAction) { //下发RGV任务之前,先读取一下安全门状态 //查找RGV进行下发任务 Dt_Task task = GetTask(speStackerCrane.DeviceCode); if (task != null) { //调取写入RGV小车任务 RgvCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); bool sendFlag = getStackerObject.SendCommand(stackerCraneTaskCommand); if (sendFlag) { //修改任务状态 _taskService.UpdateTaskStatusToNext(task); } } } } } catch (Exception ex) { //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString()); } return Task.CompletedTask; } /// /// 获取任务(暂时方法if判断) /// /// /// /// private Dt_Task? GetTask(string DeviceCode) { Dt_Task task; task = _taskService.QueryStackerCraneTask(DeviceCode); if (task != null) { //判断入库任务 if(task.TaskType== (int)TaskInboundTypeEnum.Inbound) { if(task.TaskState== (int)TaskInStatusEnum.RGV_InNew) { return task; }else if(task.TaskState== (int)TaskInStatusEnum.RGV_InPickupFinish) //取货完成后,进行放货的流程 { //判断该HCJ是否可放货 bool Currebool = QueryCurrentAdder(task); //判断HCJ是否可以放货 if (Currebool) { return task; } else { return null; } }else if (task.TaskState == (int)TaskInStatusEnum.RGV_InReleaseFinish) //放货完成后,进行子车取货流程 { //默认子车在取货的地方 return task; } else if (task.TaskState == (int)TaskInStatusEnum.RGV_InZicheFinish) //子车取货完成后,进行放货流程 { //获取需要放货的站台 AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress)); //获取到过道站台放货信息 if (aGVStation.Station_Area == 1) //表示在外测 { bool muchebool=MotherCarMoves(task, aGVStation); if (muchebool) { return task; }else { return null; } } else if(aGVStation.Station_Area == 2) //表示在内侧 { bool muchebool = MotherCarMovesinside(task, aGVStation); if (muchebool) { return task; } else { return null; } }else if (aGVStation.Station_Area == 3) //放在过道HCJ { bool muchebool = HCJMotherCarMovesinside(task, aGVStation); if (muchebool) { return task; } else { return null; } } }else if (task.TaskState == (int)TaskInStatusEnum.RGV_InZicheReleaseFinish) //子车放货完成 { AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress)); if(aGVStation.Station_Area == 1) { return task; /*RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(task, "1021"); bool sendFlag = SendCommand2(standardCommands, ziche001); if (sendFlag) { //记录下发过母车移动任务 _taskService.UpdateTaskStatusToNext(task); }*/ } else if (aGVStation.Station_Area == 2) { RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(task, "1021"); //注意::::该子车需要待定去向 bool sendFlag = SendCommand2(standardCommands, ziche002); if (sendFlag) { //记录下发过母车移动任务 _taskService.UpdateTaskStatusToNext(task); } } //进行移动子车 } else if(task.TaskState == (int)TaskInStatusEnum.RGV_InZichemoveFinish) //子车移动完成后,进行 { //获取需要放货的站台 AGVStation aGVStation = _agvStationService.GetAllStationByDeviceCode(int.Parse(task.CurrentAddress)); //获取入库站台信息 if (aGVStation.Station_Area == 1) //入库口区域的1表示写入固定的1号子车104 { //判断子车在哪个位置 int zicheadder = GetZichelocation(task, ziche001); if(zicheadder!= int.Parse(aGVStation.ZicheMotherinlaw)) { return task; } } else if(aGVStation.Station_Area == 2) //入库口区域的1表示写入固定的1号子车107 { int zicheadder = GetZichelocation(task, ziche002); if (zicheadder != int.Parse(aGVStation.ZicheMotherinlaw)) { return task; } } } } else if(task.TaskType == (int)TaskOutboundTypeEnum.Outbound) { if (task.TaskState == (int)TaskOutStatusEnum.OutNew) { return task; }else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutMothercarFinish) { return task; } else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutZicheFinish) { return task; }else if(task.TaskState == (int)TaskOutStatusEnum.RGV_OutZicheReleaseFinish) { return task; }else if(task.TaskState == (int)TaskOutStatusEnum.RGV_OutPickupFinish && task.CurrentAddress != "") { return task; } } } return null; } //判断外侧母车位置 private bool MotherCarMoves(Dt_Task _Task, AGVStation aGVStation) { int mcadder = GetMothercarlocation(_Task, aGVStation.Motherinlaw); if(mcadder != 0) { if (mcadder == int.Parse(_Task.CurrentAddress)) //判断子车要去的位置,母车是否在该位置 { return true; } else { //下发母车移动出来接货任务 RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, _Task.CurrentAddress); bool sendFlag = SendCommand2(standardCommands, aGVStation.ChildPosiDeviceCode); if (sendFlag) { //记录下发过母车移动任务 _taskService.UpdateTaskStatusToNext(_Task); } } } return false; } //判断内侧母车位置 private bool MotherCarMovesinside(Dt_Task _Task, AGVStation aGVStation) { //获取外侧母车位置 int mcadder = GetMothercarlocation(_Task, aGVStation.MotherCarDeviceCode); //获取内侧母车位置 int ncadder = GetMothercarlocation(_Task, _Task.CurrentAddress); //传入过道的地址 if (mcadder != int.Parse(aGVStation.MotherCarDeviceCode) && ncadder == aGVStation.Station_code) //判断外侧母车位置 { return true; } else { if (mcadder == int.Parse(aGVStation.MotherCarDeviceCode)) //外侧母车需要移动 { //下发母车往里面走的任务 AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCarDeviceCode); //堆垛机入库口 RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVSta.Station_code.ToString()); //下发母车移动至入库站台位 bool sendFlag = SendCommand2(standardCommands, aGVSta.ChildPosiDeviceCode); if (sendFlag) { //记录下发过母车移动任务 _taskService.UpdateTaskStatusToNext(_Task); } } if (ncadder != aGVStation.Station_code) //下发母车 { RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVStation.Station_code.ToString()); bool sendFlag = SendCommand2(standardCommands, aGVStation.ChildPosiDeviceCode); if (sendFlag) { //记录下发过母车移动任务 _taskService.UpdateTaskStatusToNext(_Task); } } } return false; } private bool HCJMotherCarMovesinside(Dt_Task _Task, AGVStation aGVStation) //传进来的站台为HCJ { //获取外侧母车位置 int mcadder = GetMothercarlocation(_Task, aGVStation.MotherCarDeviceCode); //获取内侧母车位置 int ncadder = GetMothercarlocation(_Task, aGVStation.MotherCardeputy); if (mcadder != int.Parse(aGVStation.MotherCarDeviceCode) && ncadder != int.Parse(aGVStation.MotherCardeputy)) //判断外侧母车位置 { return true; } else { if (mcadder == int.Parse(aGVStation.MotherCarDeviceCode)) //外侧母车需要移动 { //下发母车往里面走的任务 AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCarDeviceCode); //堆垛机入库口 RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVSta.Station_code.ToString()); //下发母车移动至入库站台位 bool sendFlag = SendCommand2(standardCommands, aGVSta.ChildPosiDeviceCode); if (sendFlag) { //记录下发过母车移动任务 _taskService.UpdateTaskStatusToNext(_Task); } } if (ncadder == int.Parse(aGVStation.MotherCardeputy)) //下发母车 { //下发母车往里面走的任务 AGVStation aGVSta = _agvStationService.GetMothercarCode(aGVStation.MotherCardeputy); //堆垛机入库口 RgvCraneTaskCommand standardCommands = ConvertMotherCarTaskCommand(_Task, aGVSta.Station_code.ToString()); //下发母车移动至入库站台位 bool sendFlag = SendCommand2(standardCommands, aGVSta.ChildPosiDeviceCode); if (sendFlag) { //记录下发过母车移动任务 _taskService.UpdateTaskStatusToNext(_Task); } } } return false; } public bool QueryCurrentAdder(Dt_Task task) { try { //获取需要区分是否去入库与去不合格载货台任务 IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "HCJ2000"); CommonStackerCrane Commonstacker = (CommonStackerCrane)device; DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, task.CurrentAddress, "HCJ_GoodsStatus"); int HCJGStatus = GetLine(Commonstacker, deviceProDTO.DeviceProAddress); if (HCJGStatus == 0) //为无货 { return true; } } catch (Exception ex) { //记录异常情况 _taskService.UpdateTaskExceptionMessage(task.TaskNum,$"判断HCJ站台是否有货信息出错,出错信息:{ex.Message}"); throw; } return false; } //判断是否 public bool GetMotherCarCurrentAdder(Dt_Task task) { try { IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "HCJ2000"); CommonStackerCrane Commonstacker = (CommonStackerCrane)device; DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, task.CurrentAddress, "HCJ_GoodsStatus"); int HCJGStatus = GetLine(Commonstacker, deviceProDTO.DeviceProAddress); if (HCJGStatus == 0) //为无货 { return true; } } catch (Exception ex) { //记录异常情况 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"判断HCJ站台是否有货信息出错,出错信息:{ex.Message}"); throw; } return false; } /// /// 传入过道地址,获取母车位置 /// /// /// 过道地址 /// public int GetMothercarlocation(Dt_Task task,string MotherCarDeviceCode) { try { //利用入库站台地址获取母车plc AGVStation aGVStation =_agvStationService.GetMothercarCode(MotherCarDeviceCode); IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == aGVStation.ChildPosiDeviceCode); CommonStackerCrane Commonstacker = (CommonStackerCrane)device; DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, aGVStation.ChildPosiDeviceCode, "RGVCurrentlocation"); int HCJGStatus = GetLine(Commonstacker, deviceProDTO.DeviceProAddress); return HCJGStatus; } catch (Exception ex) { //记录异常情况 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"判断母车位置信息出错,出错信息:{ex.Message}"); throw; } } /// /// 传入过道地址,获取子车车位置 /// /// /// 入库站台地址 /// public int GetZichelocation(Dt_Task task, string ChildPosiDeviceCode) { try { IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == ChildPosiDeviceCode); CommonStackerCrane Commonstacker = (CommonStackerCrane)device; DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, ChildPosiDeviceCode, "RGVCurrentlocation"); int HCJGStatus = GetLine(Commonstacker, deviceProDTO.DeviceProAddress); return HCJGStatus; } catch (Exception ex) { //记录异常情况 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"判断子车位置信息出错,出错信息:{ex.Message}"); throw; } } /// /// 任务实体转换成命令Model /// /// 任务实体 /// /// public RgvCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task) { RgvCraneTaskCommand stackerCraneTaskCommand = new RgvCraneTaskCommand(); stackerCraneTaskCommand.RGV_RGVTasklocation = short.Parse(task.CurrentAddress); stackerCraneTaskCommand.RGV_RGVtasktype = 2; stackerCraneTaskCommand.RGV_Rgvtaskid = 1; //rgv任务号 stackerCraneTaskCommand.RGV_Lanjiantaskid = 0; //兰剑任务id return stackerCraneTaskCommand; } /// /// 下发移动任务 /// /// 任务实体 /// /// public RgvCraneTaskCommand? ConvertMotherCarTaskCommand([NotNull] Dt_Task task,string CurrentAddress) { RgvCraneTaskCommand stackerCraneTaskCommand = new RgvCraneTaskCommand(); stackerCraneTaskCommand.RGV_RGVTasklocation = short.Parse(CurrentAddress); stackerCraneTaskCommand.RGV_RGVtasktype = 3; stackerCraneTaskCommand.RGV_Rgvtaskid = 999; //rgv任务号 stackerCraneTaskCommand.RGV_Lanjiantaskid = 999; //兰剑任务id return stackerCraneTaskCommand; } public void wcsWriteLog(string SCLLinStack, string Logtype, string Magessadd) { WriteLog.Write_Log("RGV", SCLLinStack + "号RGV", Logtype, new { 信息 = Magessadd }); } //===========================================================HCJ协议读写查============================================================================= /// /// 查询具体地址 /// /// /// /// /// public DeviceProDTO? GetDeviceProDTO(CommonStackerCrane Commonstacker, string SCAddress, string Interactivet) { return Commonstacker.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SCAddress && x.DeviceProParamName == Interactivet && x.DeviceProParamType== "ReadDeviceCommand"); } /// /// 根地址读取输送线信息 /// /// /// /// public int GetLine(CommonStackerCrane Commonstacker, string DeviceProDataBlock) { return Commonstacker.Communicator.Read(DeviceProDataBlock); } //===========================================================HCJ协议读写查=============================================================================== public bool SendCommand2(RgvCraneTaskCommand command, string DeviceCode) { try { IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode); CommonStackerCrane Commonstacker = (CommonStackerCrane)device; DeviceProDTO? deviceProDTO = GetDeviceProDTO(Commonstacker, DeviceCode, "RGV_Rgvtaskstutas"); int MCGStatus = GetLine(Commonstacker, deviceProDTO.DeviceProAddress); if (Commonstacker.IsConnected) { if (MCGStatus == (int)RgvJobCraneStatus.Ready) { Commonstacker.SetValue(RgvCraneDBName.RGV_RGVTasklocation, command.RGV_RGVTasklocation); Commonstacker.SetValue(RgvCraneDBName.RGV_RGVtasktype, command.RGV_RGVtasktype); Commonstacker.SetValue(RgvCraneDBName.RGV_Rgvtaskid, command.RGV_Rgvtaskid); Commonstacker.SetValue(RgvCraneDBName.RGV_Lanjiantaskid, command.RGV_Lanjiantaskid); return true; } } return false; } catch (Exception ex) { return false; } } } }