using Mapster;
|
using Newtonsoft.Json;
|
using Quartz;
|
using System.Diagnostics.CodeAnalysis;
|
using System.Text;
|
using WIDESEAWCS_BasicInfoRepository;
|
using WIDESEAWCS_Common;
|
using WIDESEAWCS_Common.TaskEnum;
|
using WIDESEAWCS_Core.Caches;
|
using WIDESEAWCS_Core.Helper;
|
using WIDESEAWCS_Core.HttpContextUser;
|
using WIDESEAWCS_IBasicInfoRepository;
|
using WIDESEAWCS_ISystemServices;
|
using WIDESEAWCS_ITaskInfoRepository;
|
using WIDESEAWCS_ITaskInfoService;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob;
|
using WIDESEAWCS_QuartzJob.DeviceBase;
|
using WIDESEAWCS_QuartzJob.Models;
|
using WIDESEAWCS_QuartzJob.Service;
|
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
|
using WIDESEAWCS_SignalR;
|
using WIDESEAWCS_Tasks.StackerCraneJob;
|
|
namespace WIDESEAWCS_Tasks
|
{
|
[DisallowConcurrentExecution]
|
public class CommonStackerCraneJob : JobBase, IJob
|
{
|
private readonly ITaskService _taskService;
|
private readonly ITaskExecuteDetailService _taskExecuteDetailService;
|
private readonly ITaskRepository _taskRepository;
|
private readonly IRouterService _routerService;
|
private readonly ICacheService _cacheService;
|
private readonly INoticeService _noticeService;
|
private readonly IDt_StationManagerRepository _stationManagerRepository;
|
private readonly ITask_HtyRepository _htyRepository;
|
private readonly ISys_ConfigService _sys_ConfigService;
|
private static List<string>? userTokenIds;
|
private static List<int>? userIds;
|
|
public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, ICacheService cacheService, INoticeService noticeService, IDt_StationManagerRepository stationManagerRepository, ITask_HtyRepository htyRepository, ISys_ConfigService sys_ConfigService)
|
{
|
_taskService = taskService;
|
_taskExecuteDetailService = taskExecuteDetailService;
|
_taskRepository = taskRepository;
|
_routerService = routerService;
|
_cacheService = cacheService;
|
_noticeService = noticeService;
|
_stationManagerRepository = stationManagerRepository;
|
_htyRepository = htyRepository;
|
_sys_ConfigService = sys_ConfigService;
|
}
|
|
public Task Execute(IJobExecutionContext context)
|
{
|
try
|
{
|
|
CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
|
if (commonStackerCrane != null)
|
{
|
if (!commonStackerCrane.IsEventSubscribed)
|
{
|
commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件
|
}
|
|
if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.ComputerMode)
|
{
|
commonStackerCrane.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次
|
|
if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
|
{
|
Dt_Task? task = GetTask(commonStackerCrane);
|
if (task != null)
|
{
|
StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
|
if (stackerCraneTaskCommand != null)
|
{
|
bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
|
if (sendFlag)
|
{
|
commonStackerCrane.LastTaskType = task.TaskType;
|
_taskService.UpdateTaskStatusToNext(task.TaskNum);
|
Thread.Sleep(1000);
|
}
|
}
|
}
|
}
|
}
|
|
#region 调用事件总线通知前端
|
|
var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
|
if (tokenInfos != null && tokenInfos.Any())
|
{
|
var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
|
var userIds = tokenInfos?.Select(x => x.UserId).ToList();
|
|
object obj = new
|
{
|
commonStackerCrane.StackerCraneAutoStatusDes,
|
commonStackerCrane.StackerCraneWorkStatusDes,
|
commonStackerCrane.DeviceCode,
|
commonStackerCrane.DeviceName,
|
StackerAlarm = StackerCraneAlarm(commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.Alarm)),
|
commonStackerCrane.CurrentTaskNum,
|
commonStackerCrane.LastTaskNum,
|
};
|
_noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonStackerCrane.DeviceName, data = obj });
|
}
|
|
#endregion 调用事件总线通知前端
|
}
|
}
|
catch (Exception ex)
|
{
|
WriteError("CommonStackerCraneJob", "test", ex);
|
ConsoleHelper.WriteErrorLine($"{ex.Message}");
|
}
|
return Task.CompletedTask;
|
}
|
|
#region 报警代码转换成文字说明
|
public string StackerCraneAlarm(short alarm) => alarm switch
|
{
|
1 => "左超限",
|
2 => "右超限",
|
3 => "前超限",
|
4 => "后超限",
|
5 => "高超限",
|
6 => "取货载货台有货",
|
7 => "放货载货台无货",
|
8 => "取货左货格无货",
|
9 => "取货右货格无货",
|
10 => "放货左一有货",
|
11 => "放货右一有货",
|
12 => "取左二货格无货",
|
13 => "取右二货格无货",
|
14 => "放货左二货格有货",
|
15 => "放货右二货格有货",
|
16 => "左二取货左一有货",
|
17 => "右二取货右一有货",
|
18 => "左取货失败",
|
19 => "右取货失败",
|
20 => "左放货失败",
|
21 => "右放货失败",
|
22 => "货叉运行超时",
|
23 => "货叉断路器跳闸",
|
24 => "货叉变频器报警",
|
25 => "货叉编码器故障",
|
26 => "货叉伸叉故障",
|
27 => "层运行货叉不在中位",
|
28 => "列运行货叉不在中位",
|
29 => "货叉中位开关故障",
|
30 => "货叉变频器通讯故障",
|
31 => "货叉编码器通讯故障",
|
32 => "伸叉超出左极限",
|
33 => "伸又超出右极限",
|
34 => "入左二货格货时左一货格有货",
|
35 => "入右二货格货时右一货格有货",
|
36 => "货叉右侧回中停止位置错误",
|
37 => "货叉左侧回中停止位置错误",
|
38 => "货叉左侧极限停止位置错误",
|
39 => "货叉右侧极限停止位置错误",
|
40 => "面板急停",
|
41 => "远程急停",
|
42 => "主接触器控制回路故障",
|
43 => "超重保护",
|
44 => "松绳保护",
|
45 => "限速保护",
|
46 => "层停止位置错误",
|
47 => "列停止位置错误",
|
48 => "接收任务错误",
|
49 => "接收列超限",
|
50 => "接收层超限",
|
51 => "行走前进极限",
|
52 => "行走后退极限",
|
53 => "提升上极限",
|
54 => "提升下极限",
|
55 => "行走运行超时",
|
56 => "提升运行超时",
|
57 => "行走断路器跳闸",
|
58 => "提升断路器跳闸",
|
59 => "行走变频器故障",
|
60 => "提升变频器故障",
|
61 => "行走测距/条码故障",
|
62 => "提升测距/条码故障",
|
63 => "行走测距/条码被挡",
|
64 => "提升测距/条码被挡",
|
65 => "上升减速光电故障",
|
66 => "下降减速光电故障",
|
67 => "提升原点光电故障",
|
68 => "行走前减速光电故障",
|
69 => "行走后减速光电故障",
|
70 => "行走原点光电故障",
|
71 => "与输送机通讯故障",
|
72 => "升降超出最小层",
|
73 => "升降超出最大层",
|
74 => "行走超出最小列",
|
75 => "行走超出最大列",
|
76 => "接收排错误",
|
77 => "起始站货格被禁用",
|
78 => "目的站货格被禁用",
|
79 => "行走抱闸故障",
|
80 => "提升抱闸故障",
|
81 => "安全门打开故障",
|
_ => $"未知报警({alarm})"
|
};
|
#endregion
|
|
|
/// <summary>
|
/// 任务完成事件订阅的方法
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
|
{
|
CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
|
if (commonStackerCrane != null)
|
{
|
if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.FinishConfirm) != 1)
|
{
|
ConsoleHelper.WriteColorLine($"【{commonStackerCrane.DeviceName}】堆垛机作业状态:【{(int)commonStackerCrane.StackerCraneWorkStatusValue}】时间【{DateTime.Now}】", ConsoleColor.Magenta);
|
|
string str = $"【{commonStackerCrane.DeviceName}】任务完成,任务号:【{e.TaskNum}】时间【{DateTime.Now}】";
|
WriteInfo(commonStackerCrane.DeviceName, str);
|
ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue);
|
var task = _taskRepository.QueryFirst(x => x.TaskNum == e.TaskNum);
|
|
if (task == null) commonStackerCrane.SetValue(StackerCraneDBName.FinishConfirm, 1);
|
|
|
var content = _taskService.StackCraneTaskCompleted(e.TaskNum);
|
|
var isWorkType = commonStackerCrane.SetValue(StackerCraneDBName.FinishConfirm, 1);
|
|
|
str = $"{commonStackerCrane.DeviceName}】WMS|WCS任务完成:【{content.Status}{content.Message}】,堆垛机完成信号写入:【{isWorkType}】,任务号:【{e.TaskNum}】时间【{DateTime.Now}】";
|
WriteInfo(commonStackerCrane.DeviceName, str);
|
ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取任务
|
/// </summary>
|
/// <param name="commonStackerCrane">堆垛机对象</param>
|
/// <returns></returns>
|
private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
|
{
|
Dt_Task task;
|
task = _taskService.QueryRelocationTask(commonStackerCrane.DeviceCode);
|
if (task != null)
|
{
|
return task;
|
}
|
|
if (commonStackerCrane.LastTaskType == null)
|
{
|
task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
|
}
|
else
|
{
|
var lastTaskTypeGroup = commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup();
|
if (lastTaskTypeGroup == TaskTypeGroup.OutbondGroup)
|
{
|
task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
|
if (task == null)
|
{
|
task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
|
}
|
}
|
else
|
{
|
task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
|
}
|
}
|
|
if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
|
{
|
// 检查当前出库任务站台是否允许放货
|
var occupiedStation = OutTaskStationIsOccupied(task);
|
if (occupiedStation == null)
|
{
|
// 如果当前出库任务站台不允许放货,排除当前任务,查找其他出库任务
|
|
var log = $"任务号:【{task.TaskNum}】出库地址:【{task.NextAddress}】不允许放货";
|
ConsoleHelper.WriteErrorLine(log);
|
|
_noticeService.Logs(userTokenIds, new { commonStackerCrane.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
|
WriteInfo(commonStackerCrane.DeviceName, log);
|
|
task = FindAnotherOutboundTask(commonStackerCrane.DeviceCode, task.TaskId);
|
|
if (task == null)
|
{
|
task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
|
}
|
}
|
else
|
{
|
return task;
|
}
|
|
}
|
else if (task == null)
|
{
|
task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
|
}
|
return task;
|
}
|
|
/// <summary>
|
/// 出库任务判断出库站台是否被占用
|
/// </summary>
|
/// <param name="task">任务实体</param>
|
/// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
|
private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task)
|
{
|
Dt_Router? router = _routerService.QueryNextRoutes(task.Roadway, task.NextAddress).FirstOrDefault();
|
|
if (router != null)
|
{
|
CommonConveyorLine? conveyorLine = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode) as CommonConveyorLine;
|
if (conveyorLine != null)
|
{
|
if (conveyorLine.IsOccupiedx(router.ChildPosi))//出库站台未被占用
|
{
|
return task;
|
}
|
}
|
else
|
{
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{router.ChildPosiDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用");
|
}
|
}
|
else
|
{
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台");
|
}
|
|
return null;
|
}
|
|
/// <summary>
|
/// 查找其他出库任务的辅助方法(排除指定任务ID的任务)
|
/// </summary>
|
/// <param name="deviceCode">设备代码</param>
|
/// <param name="excludedTaskId">要排除的任务ID</param>
|
/// <returns></returns>
|
private Dt_Task? FindAnotherOutboundTask(string deviceCode, int excludedTaskId)
|
{
|
// 先获取所有符合条件(排除指定任务ID)的出库任务列表
|
var allOutboundTasks = _taskService.QueryAllOutboundTasks(deviceCode);
|
var availableTasks = allOutboundTasks?.Where(t => excludedTaskId != t.TaskId && t.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup).ToList();
|
|
if (availableTasks == null || availableTasks.Count == 0)
|
{
|
return null;
|
}
|
|
// 遍历可用任务列表,检查任务站台是否允许放货,找到第一个允许放货的任务就返回
|
foreach (var candidateTask in availableTasks)
|
{
|
var occupiedStation = OutTaskStationIsOccupied(candidateTask);
|
if (occupiedStation != null)
|
{
|
return candidateTask;
|
}
|
var log = $"任务号:【{candidateTask.TaskNum}】出库地址:【{candidateTask.NextAddress}】不允许放货";
|
ConsoleHelper.WriteErrorLine(log);
|
|
_noticeService.Logs(userTokenIds, new { deviceCode, log = log, time = DateTime.Now.ToString("G"), color = "red" });
|
WriteInfo(deviceCode, log);
|
}
|
|
return null;
|
}
|
|
/// <summary>
|
/// 任务实体转换成命令Model
|
/// </summary>
|
/// <param name="task">任务实体</param>
|
/// <returns></returns>
|
/// <exception cref="Exception"></exception>
|
public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
|
{
|
StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
|
|
stackerCraneTaskCommand.Barcode = task.PalletCode;
|
stackerCraneTaskCommand.TaskNum = task.TaskNum;
|
stackerCraneTaskCommand.WorkType = 1;
|
stackerCraneTaskCommand.StartCommand = 1;
|
if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务
|
{
|
List<Dt_Router> routers = _routerService.QueryNextRoutes(task.CurrentAddress, task.Roadway);
|
if (routers.Count > 0)
|
{
|
stackerCraneTaskCommand.StartRow = Convert.ToInt16(routers.FirstOrDefault().SrmRow);
|
stackerCraneTaskCommand.StartColumn = Convert.ToInt16(routers.FirstOrDefault().SrmColumn);
|
stackerCraneTaskCommand.StartLayer = Convert.ToInt16(routers.FirstOrDefault().SrmLayer);
|
|
string[] targetCodes = task.NextAddress.Split("-");
|
if (targetCodes.Length == 3)
|
{
|
stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]) % 2 != 0 ? (short)1 : (short)2;
|
stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
|
stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
|
}
|
else
|
{
|
//数据配置错误
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
|
return null;
|
}
|
}
|
else
|
{
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息");
|
return null;
|
}
|
}
|
else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
|
{
|
List<Dt_Router> routers = _routerService.QueryNextRoutes(task.Roadway, task.TargetAddress);
|
if (routers.Count > 0)
|
{
|
stackerCraneTaskCommand.EndRow = Convert.ToInt16(routers.FirstOrDefault().SrmRow);
|
stackerCraneTaskCommand.EndColumn = Convert.ToInt16(routers.FirstOrDefault().SrmColumn);
|
stackerCraneTaskCommand.EndLayer = Convert.ToInt16(routers.FirstOrDefault().SrmLayer);
|
|
string[] sourceCodes = task.CurrentAddress.Split("-");
|
if (sourceCodes.Length == 3)
|
{
|
stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]) % 2 != 0 ? (short)1 : (short)2;
|
stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
|
stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
|
}
|
else
|
{
|
//数据配置错误
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
|
return null;
|
}
|
}
|
else
|
{
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息");
|
return null;
|
}
|
}
|
else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
|
{
|
string[] targetCodes = task.NextAddress.Split("-");
|
if (targetCodes.Length == 3)
|
{
|
stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]) % 2 != 0 ? (short)1 : (short)2;
|
stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
|
stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
|
}
|
else
|
{
|
//数据配置错误
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,起点:【{task.NextAddress}】");
|
return null;
|
}
|
string[] sourceCodes = task.CurrentAddress.Split("-");
|
if (sourceCodes.Length == 3)
|
{
|
stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]) % 2 != 0 ? (short)1 : (short)2;
|
stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
|
stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
|
}
|
else
|
{
|
//数据配置错误
|
_taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】");
|
return null;
|
}
|
}
|
return stackerCraneTaskCommand;
|
}
|
}
|
}
|