using Quartz;
|
using System;
|
using System.Diagnostics.CodeAnalysis;
|
using System.IO;
|
using System.Threading.Tasks;
|
using WIDESEAWCS_Common.TaskEnum;
|
using WIDESEAWCS_ITaskInfoRepository;
|
using WIDESEAWCS_ITaskInfoService;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob;
|
using WIDESEAWCS_QuartzJob.StackerCrane;
|
using WIDESEAWCS_Tasks.StackerCraneJob;
|
using WIDESEA_Core;
|
using WIDESEAWCS_QuartzJob.Service;
|
|
namespace WIDESEAWCS_Tasks
|
{
|
[DisallowConcurrentExecution]
|
public class CommonStackerCraneJob : IJob
|
{
|
private readonly ITaskService _taskService;
|
private readonly ITaskExecuteDetailService _taskExecuteDetailService;
|
private readonly ITaskRepository _taskRepository;
|
|
private readonly StackerCraneCommandConfig _config;
|
private readonly StackerCraneTaskSelector _taskSelector;
|
private readonly StackerCraneCommandBuilder _commandBuilder;
|
|
public CommonStackerCraneJob(
|
ITaskService taskService,
|
ITaskExecuteDetailService taskExecuteDetailService,
|
ITaskRepository taskRepository,
|
IRouterService routerService,
|
HttpClientHelper httpClientHelper)
|
{
|
_taskService = taskService;
|
_taskExecuteDetailService = taskExecuteDetailService;
|
_taskRepository = taskRepository;
|
|
_config = LoadConfig();
|
_taskSelector = new StackerCraneTaskSelector(taskService, routerService, httpClientHelper);
|
_commandBuilder = new StackerCraneCommandBuilder(taskService, routerService, _config);
|
}
|
|
/// <summary>
|
/// 加载配置(优先级:配置文件 > 默认配置)
|
/// </summary>
|
private static StackerCraneCommandConfig LoadConfig()
|
{
|
try
|
{
|
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StackerCraneJob", "stackercrane-command-config.json");
|
if (File.Exists(configPath))
|
{
|
string json = File.ReadAllText(configPath);
|
return System.Text.Json.JsonSerializer.Deserialize<StackerCraneCommandConfig>(json) ?? new StackerCraneCommandConfig();
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"配置加载失败: {ex.Message},使用默认配置");
|
}
|
|
return new StackerCraneCommandConfig();
|
}
|
|
public Task Execute(IJobExecutionContext context)
|
{
|
try
|
{
|
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " CommonStackerCraneJob Start");
|
|
bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
|
if (!flag || value is not IStackerCrane commonStackerCrane)
|
{
|
return Task.CompletedTask;
|
}
|
|
// 订阅一次任务完成事件。
|
if (!commonStackerCrane.IsEventSubscribed)
|
{
|
commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;
|
}
|
|
commonStackerCrane.CheckStackerCraneTaskCompleted();
|
|
if (!commonStackerCrane.IsCanSendTask(commonStackerCrane.Communicator, commonStackerCrane.DeviceProDTOs, commonStackerCrane.DeviceProtocolDetailDTOs))
|
{
|
return Task.CompletedTask;
|
}
|
|
|
// 任务选择下沉到专用选择器。
|
Dt_Task? task = _taskSelector.SelectTask(commonStackerCrane);
|
if (task == null)
|
{
|
return Task.CompletedTask;
|
}
|
|
// 命令构建下沉到专用构建器。
|
object? stackerCraneTaskCommand = _commandBuilder.ConvertToStackerCraneTaskCommand(task);
|
if (stackerCraneTaskCommand == null)
|
{
|
return Task.CompletedTask;
|
}
|
|
bool sendFlag = SendStackerCraneCommand(commonStackerCrane, stackerCraneTaskCommand);
|
if (sendFlag)
|
{
|
commonStackerCrane.LastTaskType = task.TaskType;
|
_taskService.UpdateTaskStatusToNext(task.TaskNum);
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"CommonStackerCraneJob Error: {ex.Message}");
|
}
|
|
return Task.CompletedTask;
|
}
|
|
/// <summary>
|
/// 任务完成事件订阅的方法
|
/// </summary>
|
private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e)
|
{
|
CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
|
if (commonStackerCrane != null)
|
{
|
Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
|
_taskService.StackCraneTaskCompleted(e.TaskNum);
|
commonStackerCrane.SetValue(StackerCraneDBName.WorkAction, 2);
|
}
|
}
|
|
private static bool SendStackerCraneCommand(IStackerCrane commonStackerCrane, object command)
|
{
|
return command switch
|
{
|
FormationStackerCraneTaskCommand formationCommand => commonStackerCrane.SendCommand(formationCommand),
|
StackerCraneTaskCommand standardCommand => commonStackerCrane.SendCommand(standardCommand),
|
_ => false
|
};
|
}
|
}
|
}
|