using Autofac.Core;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Quartz;
using System;
using System.Collections.Generic;
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_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_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 IPlatformStationService _PlatformStationService;
public SpeStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IPlatformStationService platformStation)
{
_taskService = taskService;
_taskExecuteDetailService = taskExecuteDetailService;
_taskRepository = taskRepository;
_routerService = routerService;
_PlatformStationService = platformStation;
}
public Task Execute(IJobExecutionContext context)
{
try
{
SpeStackerCrane speStackerCrane = (SpeStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
if (speStackerCrane != null)
{
ConveyorRgvCommand command = RgvCommand(speStackerCrane);
if (command.Online && !command.Alarm) //判断是否联机与是否报警
{
if (command.STB && command.TaskNo !=0 && command.TaskFinish && !command.Free) //判断申请,任务不为0,搬货完成,繁忙状态
{
//写入完成流程
CommonStackerCrane_StackerCraneTaskCompletedEventHandler(speStackerCrane, command.TaskNo);
}
else
{
if(command.TaskNo == 0 && command.Free)
{
Dt_Task? task = GetTask(speStackerCrane);
if(task != null)
{
ConveyorRgvDB conveyorRgv = RgvCommandDB(speStackerCrane);
if (conveyorRgv != null)
{
bool sendFlag = SendCommand(conveyorRgv, speStackerCrane, task);
if (sendFlag)
{
_taskService.UpdateTaskStatusToNext(task.TaskNum);
}
}
}
else
{
wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"未找到需要下发的任务");
}
}
}
}
}
}
catch (Exception ex)
{
//Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
}
return Task.CompletedTask;
}
///
/// 任务完成事件订阅的方法
///
///
///
private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(SpeStackerCrane speStackerCrane,int TaskNum)
{
wcsWriteLog(speStackerCrane.DeviceCode,"任务完成",$"读取到{speStackerCrane.DeviceCode}号RGV任务完成,任务号:{TaskNum}");
DeviceProDTO? deviceProDTO6 = speStackerCrane.DeviceProDTOs.Find(x=> x.DeviceProParamName == "ACK" && x.DeviceProParamType == "DeviceCommand");
if (_taskService.StackCraneTaskCompleted(TaskNum).Status)
{
if (speStackerCrane.Communicator.Write(deviceProDTO6.DeviceProAddress, (bool)true))
{
wcsWriteLog(speStackerCrane.DeviceCode, "正常信息", $"写入{speStackerCrane.DeviceCode}任务完成交互信号成功,地址:{deviceProDTO6.DeviceProAddress}");
DeviceProDTO? deviceProDTO7 = speStackerCrane.DeviceProDTOs.Find(x => x.DeviceProParamName == "STB" && x.DeviceProParamType == "ReadDeviceCommand");
if (!speStackerCrane.Communicator.Read(deviceProDTO7.DeviceProAddress))
{
speStackerCrane.Communicator.Write(deviceProDTO6.DeviceProAddress, (bool)false);
wcsWriteLog(speStackerCrane.DeviceCode, "正常信息", $"清除{speStackerCrane.DeviceCode}任务完成交互信号成功,地址:{deviceProDTO6.DeviceProAddress}");
}
else
{
wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"未读取到{speStackerCrane.DeviceCode}反馈信息,地址:{deviceProDTO7.DeviceProAddress}");
}
}
else
{
wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"写入{speStackerCrane.DeviceCode}任务完成交互信号失败,{deviceProDTO6.DeviceProAddress}");
}
}
else
{
wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"任务号:{TaskNum},任务更新失败");
}
}
///
/// 获取任务
///
/// 堆垛机对象
///
private Dt_Task? GetTask(SpeStackerCrane speStackerCrane)
{
Dt_Task task;
List platformsList = _PlatformStationService.GetPlatform3(speStackerCrane.DeviceCode);
task = _taskService.QueryRgvCraneTask(platformsList);
return task;
}
public ConveyorRgvCommand RgvCommand(SpeStackerCrane conveyorRgv)
{
ConveyorRgvCommand conveyorRgvCommand = new ConveyorRgvCommand();
List deviceProDTO6 = conveyorRgv.DeviceProDTOs
.Where(x => x.DeviceChildCode == conveyorRgv.DeviceCode
&& DBLine.RgvDBstaticdata.Contains(x.DeviceProParamName)
&& x.DeviceProParamType == "ReadDeviceCommand")
.ToList();
foreach (var item in deviceProDTO6)
{
var paramName = item.DeviceProParamName;
var propertyInfo = typeof(ConveyorRgvCommand).GetProperty(paramName);
if (propertyInfo != null)
{
if (item.DeviceDataType == "bool")
{
var value = conveyorRgv.Communicator.Read(item.DeviceProAddress);
propertyInfo.SetValue(conveyorRgvCommand, value);
}
else if (item.DeviceDataType == "int")
{
var value = conveyorRgv.Communicator.Read(item.DeviceProAddress);
propertyInfo.SetValue(conveyorRgvCommand, value);
}
}
}
return conveyorRgvCommand;
}
public ConveyorRgvDB RgvCommandDB(SpeStackerCrane conveyorRgv)
{
ConveyorRgvDB rgvDB = new ConveyorRgvDB();
List deviceProDTO6 = conveyorRgv.DeviceProDTOs
.Where(x => x.DeviceChildCode == conveyorRgv.DeviceCode
&& DBLine.RgvDBdata.Contains(x.DeviceProParamName)
&& x.DeviceProParamType == "DeviceCommand")
.ToList();
foreach (var item in deviceProDTO6)
{
var paramName = item.DeviceProParamName;
var propertyInfo = typeof(ConveyorRgvDB).GetProperty(paramName);
if (propertyInfo != null)
{
propertyInfo.SetValue(rgvDB, item.DeviceProAddress);
}
}
return rgvDB;
}
public bool SendCommand(ConveyorRgvDB conveyorRgv, SpeStackerCrane speStackerCrane, [NotNull] Dt_Task task)
{
try
{
speStackerCrane.Communicator.Write(conveyorRgv.TaskNo, (int)task.TaskNum);
speStackerCrane.Communicator.Write(conveyorRgv.StartPos, (short)Convert.ToInt16(task.CurrentAddress));
speStackerCrane.Communicator.Write(conveyorRgv.EndPos, (short)Convert.ToInt16(task.NextAddress));
speStackerCrane.Communicator.Write(conveyorRgv.WorkType, (short)2);
speStackerCrane.Communicator.Write(conveyorRgv.GoodsType, (short)1);
if (speStackerCrane.Communicator.Write(conveyorRgv.STB, (bool)true))
{
DeviceProDTO? deviceProDTO6 = speStackerCrane.DeviceProDTOs.Find(x => x.DeviceChildCode == speStackerCrane.DeviceCode
&& x.DeviceProParamName == "ACK"
&& x.DeviceProParamType == "ReadDeviceCommand");
if (speStackerCrane.Communicator.Read(deviceProDTO6.DeviceProAddress))
{
/*speStackerCrane.Communicator.Write(conveyorRgv.TaskNo, (int)0);
speStackerCrane.Communicator.Write(conveyorRgv.WorkType, (int)0);
speStackerCrane.Communicator.Write(conveyorRgv.WorkType, (int)0);
speStackerCrane.Communicator.Write(conveyorRgv.StartPos, (int)0);
speStackerCrane.Communicator.Write(conveyorRgv.EndPos, (int)0);*/
speStackerCrane.Communicator.Write(conveyorRgv.STB, (bool)false);
wcsWriteLog(speStackerCrane.DeviceCode, "正常信息", $"读取到反馈信息,并清除STB,false,地址{deviceProDTO6.DeviceProAddress}");
return true;
}
else
{
wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"未读取到反馈信息ACK,地址{deviceProDTO6.DeviceProAddress}");
}
}
else
{
wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"下发RGV任务失败,写入STB交互失败,地址{conveyorRgv.STB}");
}
return false;
}
catch (Exception ex)
{
wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"下发RGV任务失败,错误信息:{ex.Message}");
return false;
}
}
public void wcsWriteLog(string SCLLinStack, string Logtype, string Magessadd)
{
WriteLog.Write_Log("RGV", SCLLinStack + "号RGV", Logtype, new { 信息 = Magessadd });
}
}
}