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_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_Tasks.StackerCraneJob;
namespace WIDESEAWCS_Tasks
{
[DisallowConcurrentExecution]
public class CommonStackerCraneJob : IJob
{
private readonly ITaskService _taskService;
private readonly ITaskExecuteDetailService _taskExecuteDetailService;
private readonly ITaskRepository _taskRepository;
private readonly IRouterService _routerService;
public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
{
_taskService = taskService;
_taskExecuteDetailService = taskExecuteDetailService;
_taskRepository = taskRepository;
_routerService = routerService;
}
public Task Execute(IJobExecutionContext context)
{
try
{
CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
if (commonStackerCrane != null)
{
//添加判断agv状态是否可下发
if (ReadAGVstatus(commonStackerCrane) == 1 && ReadAGVworkingmode(commonStackerCrane) == 1)
{
GetTask(commonStackerCrane);
}
//处理agv是否完成任务
}
}
catch (Exception ex)
{
//Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
}
return Task.CompletedTask;
}
///
/// 任务完成事件订阅的方法
///
///
///
private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
{
CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
if (commonStackerCrane != null)
{
if (commonStackerCrane.GetValue(StackerCraneDBName.WorkType) != 5)
{
Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
_taskService.StackCraneTaskCompleted(e.TaskNum);
commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
}
}
}
///
/// 获取任务
///
/// 堆垛机对象
///
private void GetTask(CommonStackerCrane commonStackerCrane)
{
Dt_Task task;
task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
if (task != null)
{
StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
if (stackerCraneTaskCommand != null)
{
bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
if (sendFlag)
{
_taskService.UpdateTaskStatusToNext(task.TaskNum);
}
}
}
}
//读取AGV状态
public int ReadAGVstatus(CommonStackerCrane commonStackerCrane)
{
var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVCraneStatus");
return deviceProDTO != null ? commonStackerCrane.Communicator.Read(deviceProDTO.DeviceProAddress) : 99;
}
//读取AGV工作模式
public int ReadAGVworkingmode(CommonStackerCrane commonStackerCrane)
{
var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVWorkingmode");
return deviceProDTO != null ? commonStackerCrane.Communicator.Read(deviceProDTO.DeviceProAddress) : 99;
}
//读取任务号
public int Readtasknumber(CommonStackerCrane commonStackerCrane)
{
var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVWorkingmode");
return deviceProDTO != null ? commonStackerCrane.Communicator.Read(deviceProDTO.DeviceProAddress) : 99;
}
///
/// 任务实体转换成命令Model
///
/// 任务实体
///
///
public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
{
StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
stackerCraneTaskCommand.TaskNum = task.TaskNum;
stackerCraneTaskCommand.WorkType = 1;
stackerCraneTaskCommand.TrayType = 0;
stackerCraneTaskCommand.StartAdder = Convert.ToInt16(task.SourceAddress);
stackerCraneTaskCommand.EndAdder = Convert.ToInt16(task.TargetAddress);
return stackerCraneTaskCommand;
}
}
}