using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using AutoMapper;
|
using Quartz;
|
using WIDESEAWCS_Common;
|
using WIDESEAWCS_Core.Helper;
|
using WIDESEAWCS_DTO.BasicInfo;
|
using WIDESEAWCS_IBasicInfoRepository;
|
using WIDESEAWCS_IBasicInfoService;
|
using WIDESEAWCS_ITaskInfoRepository;
|
using WIDESEAWCS_ITaskInfoService;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob;
|
using WIDESEAWCS_QuartzJob.DTO;
|
using WIDESEAWCS_Tasks.ConveyorLineJob;
|
|
namespace WIDESEAWCS_Tasks
|
{
|
/// <summary>
|
/// 输送线
|
/// </summary>
|
/// <remarks>
|
/// 1. 实现IJob接口,作为Quartz.NET作业 <br/>
|
/// 2. 使用[DisallowConcurrentExecution]特性防止并发执行 <br/>
|
/// 3. 主要功能: <br/>
|
/// - 处理PLC请求信号 <br/>
|
/// - 根据条码查询或生成任务 <br/>
|
/// - 设置WCS响应参数 <br/>
|
/// - 异常处理
|
/// </remarks>
|
[DisallowConcurrentExecution]
|
public class ConveyorLineOutJob : JobBase, IJob
|
{
|
/// <summary>
|
/// 任务服务接口实例,用于处理与任务相关的操作
|
/// </summary>
|
private readonly ITaskService _taskService;
|
|
/// <summary>
|
/// AutoMapper 对象映射器实例
|
/// </summary>
|
private readonly IMapper _mapper;
|
|
/// <summary>
|
/// 订单明细服务接口
|
/// </summary>
|
private readonly IOrderDetailsService _orderDetailsService;
|
|
/// <summary>
|
/// 任务仓储接口实例,用于操作任务数据
|
/// </summary>
|
private readonly ITaskRepository _taskRepository;
|
|
/// <summary>
|
/// 容器仓储接口实例,用于容器相关数据操作
|
/// </summary>
|
private readonly IContainerRepository _containerRepository;
|
|
/// <summary>
|
/// 静态容器实例,用于存放排出容器信息
|
/// </summary>
|
private static Dt_Container DischargeContainer;
|
|
/// <summary>
|
/// 排出站编号
|
/// </summary>
|
private static int dischargeStation = 8;
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="taskService">任务服务</param>
|
/// <param name="mapper">对象映射器</param>
|
/// <param name="orderDetailsService">订单详情服务</param>
|
/// <param name="taskRepository">任务仓储</param>
|
/// <param name="containerRepository">容器仓储</param>
|
public ConveyorLineOutJob(ITaskService taskService, IMapper mapper, IOrderDetailsService orderDetailsService, ITaskRepository taskRepository, IContainerRepository containerRepository)
|
{
|
_taskService = taskService;
|
_mapper = mapper;
|
_orderDetailsService = orderDetailsService;
|
_taskRepository = taskRepository;
|
_containerRepository = containerRepository;
|
}
|
|
public Task Execute(IJobExecutionContext context)
|
{
|
if (DischargeContainer == null)
|
{
|
DischargeContainer = _containerRepository.QueryFirst(x => x.ContainerEnable && x.ContainerType == ContainerTypeEnum.DischargeContainer.ObjToInt());
|
if (DischargeContainer != null)
|
{
|
dischargeStation = DischargeContainer.ContainerNo;
|
}
|
}
|
|
bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
|
if (flag && value != null && value is OtherDevice otherDevice)
|
{
|
try
|
{
|
bool request = otherDevice.GetValue<ConveyorLineStationDBName, bool>(ConveyorLineStationDBName.PLCStationRequest); //申请
|
bool response = otherDevice.GetValue<ConveyorLineStationDBName, bool>(ConveyorLineStationDBName.PLCStationResponse); //应答
|
bool wcsResponse = otherDevice.GetValue<ConveyorLineStationDBName, bool>(ConveyorLineStationDBName.WCSStationResponse); //应答
|
|
if (request && !response && !wcsResponse)
|
{
|
DeviceProDTO? devicePro = otherDevice.DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == ConveyorLineStationDBName.PLCStationBarcode.ToString());
|
if (devicePro == null)
|
{
|
WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", $"通讯协议数据未找到{ConveyorLineStationDBName.PLCStationBarcode.ToString()}信息");
|
return Task.CompletedTask;
|
}
|
|
string barcode = otherDevice.GetValue<ConveyorLineStationDBName, string>(ConveyorLineStationDBName.PLCStationBarcode);
|
if (string.IsNullOrEmpty(barcode))
|
{
|
return Task.CompletedTask;
|
}
|
Dt_Task task = _taskRepository.QueryFirst(x => x.PalletCode == barcode);
|
|
if (task == null)
|
{
|
OrderInfo orderInfo = _orderDetailsService.GetOrderInfoByBarcode(barcode);
|
|
Task.Run(() =>
|
{
|
_orderDetailsService.ToMes(barcode, 3);
|
});
|
|
int minWidth = AppSettings.Get("MinWidth").ObjToInt();
|
int maxWidth = AppSettings.Get("MaxWidth").ObjToInt();
|
int minLength = AppSettings.Get("MinLength").ObjToInt();
|
int maxLength = AppSettings.Get("MaxLength").ObjToInt();
|
bool isNormalOrientation = orderInfo.Width >= minWidth && orderInfo.Width <= maxWidth && orderInfo.Length >= minLength && orderInfo.Length <= maxLength;
|
bool isSwappedOrientation = orderInfo.Length >= minWidth && orderInfo.Length <= maxWidth && orderInfo.Width >= minLength && orderInfo.Width <= maxLength;
|
if (isNormalOrientation || isSwappedOrientation)
|
{
|
var (taskFlag, gTask, message) = _taskService.GenerateTask(orderInfo);
|
if (taskFlag && gTask != null)
|
{
|
task = gTask;
|
}
|
else
|
{
|
WriteDebug($"码垛任务生成", $"生成任务失败: 【{barcode}】{message}");
|
|
(taskFlag, gTask, message) = _taskService.GenerateExceptionTask(orderInfo);
|
if (taskFlag && gTask != null)
|
{
|
task = gTask;
|
}
|
}
|
}
|
else
|
{
|
var (taskFlag, gTask, message) = _taskService.GenerateExceptionTask(orderInfo);
|
if (taskFlag && gTask != null)
|
{
|
task = gTask;
|
}
|
}
|
}
|
|
if (task != null)
|
{
|
if (!string.IsNullOrEmpty(task.ItemInfo))
|
{
|
string[] itemInfos = task.ItemInfo.Split("*");
|
if (itemInfos.Length == 3)
|
{
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationLength, Convert.ToInt32(itemInfos[0]));
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationWidth, Convert.ToInt32(itemInfos[1]));
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationHeight, Convert.ToInt32(itemInfos[2]));
|
}
|
}
|
|
|
if (task.TargetAddress == dischargeStation.ToString())
|
{
|
List<Dt_Container> containers = _containerRepository.Db.Queryable<Dt_Container>().Where(x => x.ContainerType == ContainerTypeEnum.PutContainer.ObjToInt() && x.ContainerEnable).Includes(x => x.Items).ToList();
|
List<Dt_OrderContainer> orderContainers = _taskRepository.Db.Queryable<Dt_OrderContainer>().ToList();
|
List<Dt_Task> tasks = _taskRepository.Db.Queryable<Dt_Task>().ToList();
|
|
WriteDebug($"异常工位任务生成", $"【{barcode}】生成任务成功: 去向【{task.TargetAddress}】{Environment.NewLine}{Environment.NewLine}{containers.Serialize()}{Environment.NewLine}{Environment.NewLine}{orderContainers.Serialize()}{Environment.NewLine}{Environment.NewLine}{tasks.Serialize()}");
|
}
|
else
|
{
|
WriteDebug($"码垛任务生成", $"【{barcode}】生成任务成功: 去向【{task.TargetAddress}】");
|
}
|
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, Convert.ToInt32(task.TargetAddress));
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTaskNum, task.TaskNum);
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true);
|
}
|
else
|
{
|
WriteDebug($"码垛任务生成", $"【{barcode}】生成任务失败: 排到8号工位");
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, dischargeStation);
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTaskNum, 998);
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true);
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, dischargeStation);
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTaskNum, 999);
|
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true);
|
|
WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", ex.Message);
|
}
|
}
|
else
|
{
|
WriteError(nameof(CommonConveyorLineOutJob), "参数错误,未传递设备参数或设备类型错误");
|
}
|
return Task.CompletedTask;
|
}
|
}
|
}
|