#region << 版 本 注 释 >>
/*----------------------------------------------------------------
* 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob
* 创建者:胡童庆
* 创建时间:2024/8/2 16:13:36
* 版本:V1.0.0
* 描述:
*
* ----------------------------------------------------------------
* 修改人:
* 修改时间:
* 版本:V1.0.1
* 修改说明:
*
*----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using AutoMapper;
using HslCommunication;
using Quartz;
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.BasicInfo;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Model.Models.System;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.ConveyorLineJob;
namespace WIDESEAWCS_Tasks
{
///
/// 通用输送线站台任务类,用于处理输送线站台的PLC信号交互和任务管理
///
///
/// 1. 实现IJob接口,作为Quartz.NET的定时任务
/// 2. 通过DisallowConcurrentExecution特性防止并发执行
/// 3. 主要功能:
/// - 监控PLC站台请求信号
/// - 处理任务重新生成逻辑
/// - 更新任务状态
/// - 设置PLC响应信号
///
[DisallowConcurrentExecution]
public class CommonConveyorLineStationJob : JobBase, IJob
{
private readonly IMapper _mapper;
private readonly ITaskRepository _taskRepository;
private readonly ITaskService _taskService;
public CommonConveyorLineStationJob(IMapper mapper, ITaskRepository taskRepository, ITaskService taskService)
{
_mapper = mapper;
_taskRepository = taskRepository;
_taskService = taskService;
}
public Task Execute(IJobExecutionContext context)
{
bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
if (flag && value != null && value is OtherDevice otherDevice)
{
try
{
List deviceChildCodes = otherDevice.DeviceProDTOs.GroupBy(x => x.DeviceChildCode).Select(x => x.Key).ToList();
for (int i = 0; i < deviceChildCodes.Count; i++)
{
bool request = otherDevice.GetValue(ConveyorLineStationDBName.PLCStationRequest, deviceChildCodes[i]); //申请
bool response = otherDevice.GetValue(ConveyorLineStationDBName.PLCStationResponse, deviceChildCodes[i]); //应答
bool wcsResponse = otherDevice.GetValue(ConveyorLineStationDBName.WCSStationResponse, deviceChildCodes[i]); //应答
if (request && !response && !wcsResponse)
{
int taskNum = otherDevice.GetValue(ConveyorLineStationDBName.PLCStationTaskNum, deviceChildCodes[i]);
if (taskNum > 0)
{
Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == taskNum);
if (task != null)
{
if (task.TaskState == TaskStatusEnum.Gantry_BeReassign.ObjToInt())
{
var (taskFlag, gTask, message) = _taskService.RegenerateTask(task, deviceChildCodes[i]);
if (!taskFlag || gTask == null)
{
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, 8, deviceChildCodes[i]);
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true, deviceChildCodes[i]);
continue;
}
}
else
{
task.TaskState = TaskStatusEnum.Gantry_New.ObjToInt();
_taskRepository.UpdateData(task);
}
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, 0, deviceChildCodes[i]);
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true, deviceChildCodes[i]);
}
else
{
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationTarget, 8, deviceChildCodes[i]);
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, true, deviceChildCodes[i]);
}
}
}
else
{
if (wcsResponse && !request)
otherDevice.SetValue(ConveyorLineStationDBName.WCSStationResponse, false, deviceChildCodes[i]); //清除响应
}
}
}
catch (Exception ex)
{
WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", ex.Message, ex);
}
}
else
{
WriteError(nameof(CommonConveyorLineStationJob), "参数错误,未传递设备参数或设备类型错误");
}
return Task.CompletedTask;
}
public int[] GetIndexArray(T[] values, T value)
{
List result = new List();
for (int i = 0; i < values.Length; i++)
{
if (value.Equals(values[i]))
{
result.Add(i);
}
}
return result.ToArray();
}
}
}