using AutoMapper;
using HslCommunication;
using Newtonsoft.Json;
using Quartz;
using SqlSugar;
using System.Reflection;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_DTO.MOM;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IProcessRepository;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
namespace WIDESEAWCS_Tasks
{
[DisallowConcurrentExecution]
public partial class CommonConveyorLineJob : JobBase, IJob
{
public readonly ITaskService _taskService;
private readonly ITaskRepository _taskRepository;
private readonly ITaskExecuteDetailService _taskExecuteDetailService;
private readonly IRouterService _routerService;
private readonly IPlatFormRepository _platFormRepository;
private readonly ISys_ConfigService _sys_ConfigService;
private readonly IMapper _mapper;
private readonly IDt_StationManagerService _stationManagerService;
private readonly IDt_StationManagerRepository _stationManagerRepository;
private readonly ICacheService _cacheService;
private readonly INoticeService _noticeService;
public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, ITaskRepository taskRepository, IPlatFormRepository platFormRepository, ISys_ConfigService sys_ConfigService, IDt_StationManagerService stationManagerService, IDt_StationManagerRepository stationManagerRepository, ICacheService cacheService, INoticeService noticeService)
{
_taskService = taskService;
_taskExecuteDetailService = taskExecuteDetailService;
_routerService = routerService;
_mapper = mapper;
_taskRepository = taskRepository;
_platFormRepository = platFormRepository;
_sys_ConfigService = sys_ConfigService;
_stationManagerService = stationManagerService;
_stationManagerRepository = stationManagerRepository;
_cacheService = cacheService;
_noticeService = noticeService;
}
public async Task Execute(IJobExecutionContext context)
{
string jobName = context.JobDetail.Key.Name;
//if (MemoryLockManager.TryAcquireLock(jobName))
//{
try
{
// 从JobDataMapä¸èŽ·å–ä¼ é€’çš„å‚æ•°
CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
if (conveyorLine != null)
{
// 查询所有å设备的ä½ç½®
List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
// å¹¶è¡Œå¤„ç†æ¯ä¸ªå设备
var tasks = childDeviceCodes.Select(childDeviceCode => ProcessDeviceAsync(conveyorLine, childDeviceCode)).ToList();
await Task.WhenAll(tasks);
// èŽ·å–æ‰€æœ‰ç«™ç‚¹ç®¡ç†å™¨
List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
foreach (var station in stationManagers)
{
if (station.stationType == 11)
{
// 读å–任务命令和设备命令
ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
if (command != null && commandWrite != null)
{
// 将交互信å·è½¬æ¢ä¸ºå¸ƒå°”数组
var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
if (structs[0] == true)
{
// 输出è¦å‘Šä¿¡æ¯
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine.DeviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{station.stationChildCode}】空托盘请求扫ç 入库");
// 处ç†å…¥åº“请求
NGRequestTaskInbound(conveyorLine, command, station.stationChildCode, 0, station.stationLocation);
}
else
{
// å‘é€å®Œæˆä¿¡å·
ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
}
}
}
else if (station.stationType == 5)
{
// 读å–任务命令和设备命令
ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
if (command != null && commandWrite != null)
{
// 将交互信å·è½¬æ¢ä¸ºå¸ƒå°”数组
var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
if (structs[0] == true)
{
// 检查是å¦å˜åœ¨ä»»åŠ¡
if (_taskRepository.QueryData(x => x.SourceAddress == station.stationChildCode).Count() > 0)
{
return;
}
// 输出è¦å‘Šä¿¡æ¯
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine.DeviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{station.stationChildCode}】异常å£è¯·æ±‚扫ç 入库");
// å¤„ç†æ–°ä»»åŠ¡
await HandleNewTaskAsync(conveyorLine, command, station.stationChildCode, 0);
}
else
{
// å‘é€å®Œæˆä¿¡å·
ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
}
}
}
}
}
}
catch (Exception ex)
{
// 输出异常信æ¯
Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
}
// finally
// {
// MemoryLockManager.ReleaseLock(jobName);
// }
// }
// else
// {
// ConsoleHelper.WriteErrorLine($"[CommonConveyorLineJob]ã€{jobName}】任务已被é”å®šï¼Œæ— æ³•å¤„ç†");
// }
return;
}
private Task ProcessDeviceAsync(CommonConveyorLine conveyorLine, string childDeviceCode)
{
// 读å–任务命令和设备命令
ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(childDeviceCode);
ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(childDeviceCode, "DeviceCommand");
if (command != null && commandWrite != null)
{
#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
{
command,
commandWrite
};
// å‘é€é€šçŸ¥
_noticeService.LineData(userIds.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, childDeviceCode, data = obj });
}
#endregion 调用事件总线通知å‰ç«¯
// 将交互信å·è½¬æ¢ä¸ºå¸ƒå°”数组
var writeInteractiveSignal = commandWrite.WriteInteractiveSignal;
var structs = BitConverter.GetBytes(writeInteractiveSignal).Reverse().ToArray().ToBoolArray();
// 获å–设备å议详情
List<DeviceProtocolDetailDTO>? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.Where(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand.InteractiveSignal)).ToList();
if (deviceProtocolDetails != null)
{
foreach (var item in deviceProtocolDetails)
{
int itemValue = item.ProtocalDetailValue.ObjToInt();
if (structs[itemValue] == true)
{
var numRead = itemValue;
var numWrite = itemValue + 1;
// 输出è¦å‘Šä¿¡æ¯
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine.DeviceName}】ã€{childDeviceCode}】ã€{numRead.ToString()}】ä½è¾“é€çº¿è¯»å–ä¿¡å·ï¼šã€{structs[itemValue]}】 ã€{numWrite}】ä½WCS写入信å·ï¼šã€{structs[itemValue + 1]}】");
if (structs[itemValue + 1] != structs[itemValue])
{
// 获å–å¤„ç†æ–¹æ³•
MethodInfo? method = GetType().GetMethod(item.ProtocolDetailType);
if (method != null)
{
command.InteractiveSignal = writeInteractiveSignal;
// è°ƒç”¨å¤„ç†æ–¹æ³•
method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, itemValue });
}
}
}
else
{
// å‘é€å®Œæˆä¿¡å·
ConveyorLineSendFinish(conveyorLine, childDeviceCode, itemValue, false);
}
}
}
// 查询平å°ä¿¡æ¯
Platform platform = _platFormRepository.QueryFirst(x => x.PLCCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
if (platform != null && !string.IsNullOrEmpty(platform.Location))
{
var strings = platform.Location.Split(',').ToList();
foreach (var ite in strings)
{
// 读å–任务命令
ConveyorLineTaskCommand command1 = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(ite);
if (command1.InteractiveSignal != 2)
{
command.InteractiveSignal = writeInteractiveSignal;
int count = strings.Count;
// 获å–å¤„ç†æ–¹æ³•
MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
if (method != null)
{
command.InteractiveSignal = commandWrite.WriteInteractiveSignal;
// è°ƒç”¨å¤„ç†æ–¹æ³•
method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
};
}
}
}
}
return Task.CompletedTask;
}
#region 入库
/// <summary>
/// 输é€çº¿è¯·æ±‚入库
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="command">读å–的请求信æ¯</param>
/// <param name="childDeviceCode">å设备编å·</param>
/// <param name="ProtocalDetailValue">线体当å‰bool读å–å移地å€</param>
public async Task RequestInboundAsync(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
{
try
{
// 输出è¦å‘Šä¿¡æ¯ï¼Œè¡¨ç¤ºä»»åŠ¡å·²åˆ°è¾¾å设备并请求扫ç 入库
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】请求扫ç 入库");
// 查询æ¡ç 对应的任务
var task = _taskService.QueryBarCodeConveyorLineTask(command.Barcode, childDeviceCode);
if (task != null)
{
// 如果任务类型是出库或出托盘,则处ç†å‡ºåº“任务
if (task.TaskType == (int)TaskOutboundTypeEnum.OutTray || task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
{
HandleTaskOut(conveyorLine, command, childDeviceCode, ProtocalDetailValue, task);
}
else
{
// 获å–ä»»åŠ¡çš„ä¸‹ä¸€ç›®æ ‡åœ°å€
var next = task.NextAddress;
// å°†ä»»åŠ¡æ˜ å°„ä¸ºå‘½ä»¤
var taskCommand = MapTaskCommand(task, command);
// æ¢å¤ä»»åŠ¡çš„ä¸‹ä¸€ç›®æ ‡åœ°å€
task.NextAddress = next;
// å‘é€å‘½ä»¤åˆ°å设备
conveyorLine.SendCommand(taskCommand, childDeviceCode);
// 输出è¦å‘Šä¿¡æ¯ï¼Œè¡¨ç¤ºä»»åŠ¡å·²åˆ°è¾¾å设备并请求扫ç å…¥åº“ï¼Œä¸‹ä¸€ç›®æ ‡åœ°å€
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】请求扫ç 入库,ä¸‹ä¸€ç›®æ ‡åœ°å€ã€{taskCommand.TargetAddress}】");
// å‘é€ä»»åŠ¡å®Œæˆé€šçŸ¥
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
// 更新任务状æ€ä¸ºä¸‹ä¸€çжæ€
_taskService.UpdateTaskStatusToNext(task);
}
}
else
{
// 如果任务为空且æ¡ç ä¸ä¸º"NoRead"且æ¡ç ä¸ä¸ºç©ºï¼Œåˆ™å¤„ç†æ–°ä»»åŠ¡
if (task == null && command.Barcode != "NoRead" && command.Barcode.IsNotEmptyOrNull())
{
// 查询æ¡ç 对应的任务
task = _taskService.QueryBarcodeTask(command.Barcode, childDeviceCode);
if (task == null)
// 异æ¥å¤„ç†æ–°ä»»åŠ¡
await HandleNewTaskAsync(conveyorLine, command, childDeviceCode, ProtocalDetailValue);
}
}
}
catch (Exception ex)
{
// æ•获并输出异常信æ¯
Console.Out.WriteLine(ex.ToString());
}
}
#region 输é€çº¿è¯·æ±‚入库下一地å€
/// <summary>
/// 输é€çº¿è¯·æ±‚入库下一地å€
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="command">读å–的请求信æ¯</param>
/// <param name="childDeviceCode">å设备编å·</param>
public void RequestInNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
{
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】请求入库下一地å€");
Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
if (task != null)
{
if (command.Barcode == task.PalletCode)
{
Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
if (newTask != null)
{
//ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(newTask);
//taskCommand.InteractiveSignal = command.InteractiveSignal;
var next = newTask.NextAddress;
var taskCommand = MapTaskCommand(newTask, command);
newTask.NextAddress = next;
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】请求入库下一地å€,ä¸‹ä¸€ç›®æ ‡åœ°å€ã€{taskCommand.TargetAddress}】");
conveyorLine.SendCommand(taskCommand, childDeviceCode);
}
}
}
}
#endregion 输é€çº¿è¯·æ±‚入库下一地å€
/// <summary>
/// 输é€çº¿å…¥åº“完æˆ
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="command">读å–的请求信æ¯</param>
/// <param name="childDeviceCode">å设备编å·</param>
/// <param name="ProtocalDetailValue">线体当å‰bool读å–å移地å€</param>
public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
{
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿å…¥åº“完æˆ");
var task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
if (task != null && task.TaskState != (int)TaskInStatusEnum.Line_InFinish)
{
if (command.Barcode == task.PalletCode && childDeviceCode == task.NextAddress)
{
//conveyorLine.SetValue(ConveyorLineDBName.ConveyorLineTargetAddress, "1000", childDeviceCode);
ConsoleHelper.WriteWarningLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿å…¥åº“完æˆ,ä¸‹ä¸€ç›®æ ‡åœ°å€ã€ç‰å¾…分é…è´§ä½,并写入1000】");
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
if (task.TaskType == (int)TaskInboundTypeEnum.InNG)
{
int nextStatus = task.TaskState.GetNextNotCompletedStatus<TaskInStatusEnum>();
var station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.SourceAddress);
task.CurrentAddress = station.stationLocation;
task.NextAddress = station.stationNGLocation;
task.TargetAddress = task.NextAddress;
task.TaskState = nextStatus;
task.ModifyDate = DateTime.Now;
task.Modifier = "System";
_taskRepository.UpdateData(task);
}
else
{
WebResponseContent content = _taskService.UpdateTaskStatusToNext(task);
Console.Out.WriteLine(content.ToJsonString());
}
}
}
}
#endregion 入库
#region 出库
/// <summary>
/// 输é€çº¿è¯·æ±‚出信æ¯
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="command">读å–的请求信æ¯</param>
/// <param name="childDeviceCode">å设备编å·</param>
/// <param name="ProtocalDetailValue">线体当å‰bool读å–å移地å€</param>
public void RequestOutbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
{
// 查询输é€çº¿ä»»åŠ¡ï¼Œæ ¹æ®è¾“é€çº¿è®¾å¤‡å’Œå设备代ç 获å–任务信æ¯
var task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
// 输出æˆåŠŸä¿¡æ¯ï¼ŒåŒ…括输é€çº¿åç§°ã€ä»»åŠ¡å·ã€æ‰˜ç›˜æ¡ç å’Œå设备代ç ,以åŠä»»åŠ¡ä¿¡æ¯
ConsoleHelper.WriteSuccessLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿è¯·æ±‚出库,task{task.ToJsonString()}");
// 如果任务ä¸ä¸ºç©ºï¼Œåˆ™æ‰§è¡Œä»¥ä¸‹æ“作
if (task != null)
{
// 获å–ä»»åŠ¡çš„ä¸‹ä¸€ç›®æ ‡åœ°å€
var next = task.NextAddress;
// å°†ä»»åŠ¡å‘½ä»¤æ˜ å°„åˆ°å½“å‰ä»»åŠ¡
var taskCommand = MapTaskCommand(task, command);
// æ¢å¤ä»»åŠ¡çš„ä¸‹ä¸€ç›®æ ‡åœ°å€
task.NextAddress = next;
// 输出æˆåŠŸä¿¡æ¯ï¼ŒåŒ…括输é€çº¿åç§°ã€ä»»åŠ¡å·ã€æ‰˜ç›˜æ¡ç ã€å设备代ç å’Œä¸‹ä¸€ç›®æ ‡åœ°å€
ConsoleHelper.WriteSuccessLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿è¯·æ±‚出库,ä¸‹ä¸€ç›®æ ‡åœ°å€ã€{taskCommand.TargetAddress}】");
// å‘输é€çº¿å‘é€å‘½ä»¤
conveyorLine.SendCommand(taskCommand, childDeviceCode);
// 完æˆè¾“é€çº¿å‘é€ä»»åŠ¡ï¼Œå¹¶æ›´æ–°ä»»åŠ¡çŠ¶æ€
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
// 更新任务状æ€åˆ°ä¸‹ä¸€ä¸ªçжæ€
_taskService.UpdateTaskStatusToNext(task);
// å¦‚æžœä»»åŠ¡çš„ç›®æ ‡åœ°å€æ˜¯"1020-1"ï¼Œåˆ™å†æ¬¡æ›´æ–°ä»»åŠ¡çŠ¶æ€åˆ°ä¸‹ä¸€ä¸ªçжæ€
if (task.TargetAddress == "1020-1")
{
_taskService.UpdateTaskStatusToNext(task);
}
}
}
/// <summary>
/// 输é€çº¿è¯·æ±‚出库下一地å€
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="command">读å–的请求信æ¯</param>
/// <param name="childDeviceCode">å设备编å·</param>
public void RequestOutNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
{
// æ‰“å°æˆåŠŸæ—¥å¿—ï¼Œæ˜¾ç¤ºå½“å‰è®¾å¤‡åç§°ã€ä»»åŠ¡å·ã€æ‰˜ç›˜æ¡ç 以åŠè¯·æ±‚出库下一地å€çš„å设备代ç
ConsoleHelper.WriteSuccessLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿è¯·æ±‚出库下一地å€");
// 查询æ£åœ¨æ‰§è¡Œçš„输é€çº¿ä»»åŠ¡ï¼Œæ ¹æ®ä»»åŠ¡å·å’Œå设备代ç 获å–任务信æ¯
Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
// 如果任务å˜åœ¨
if (task != null)
{
// 检查任务ä¸çš„æ‰˜ç›˜æ¡ç 是å¦ä¸Žå‘½ä»¤ä¸çš„æ‰˜ç›˜æ¡ç 一致
if (command.Barcode == task.PalletCode)
{
// 更新任务的ä½ç½®ä¿¡æ¯ï¼Œå¹¶èŽ·å–æ›´æ–°åŽçš„任务对象
Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
// 如果更新åŽçš„任务对象ä¸ä¸ºç©º
if (newTask != null)
{
// 获å–ä¸‹ä¸€ç›®æ ‡åœ°å€
var next = newTask.NextAddress;
// å°†æ–°ä»»åŠ¡å¯¹è±¡æ˜ å°„ä¸ºä»»åŠ¡å‘½ä»¤å¯¹è±¡
var taskCommand = MapTaskCommand(newTask, command);
// æ¢å¤æ–°ä»»åŠ¡å¯¹è±¡çš„ä¸‹ä¸€ç›®æ ‡åœ°å€
newTask.NextAddress = next;
// æ‰“å°æˆåŠŸæ—¥å¿—ï¼Œæ˜¾ç¤ºå½“å‰è®¾å¤‡åç§°ã€ä»»åŠ¡å·ã€æ‰˜ç›˜æ¡ç ã€å设备代ç 以åŠä¸‹ä¸€ç›®æ ‡åœ°å€
ConsoleHelper.WriteSuccessLine($"ã€{conveyorLine._deviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿è¯·æ±‚出库下一地å€,ä¸‹ä¸€ç›®æ ‡åœ°å€ã€{taskCommand.TargetAddress}】");
// å‘é€ä»»åŠ¡å‘½ä»¤åˆ°å设备
conveyorLine.SendCommand(taskCommand, childDeviceCode);
// æ ‡è®°è¾“é€çº¿å‘é€ä»»åŠ¡å®Œæˆ
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
}
}
}
}
/// <summary>
/// 输é€çº¿å‡ºåº“完æˆ
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="command">读å–的请求信æ¯</param>
/// <param name="childDeviceCode">å设备编å·</param>
public void ConveyorLineOutFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
{
// æ‰“å°æˆåŠŸä¿¡æ¯ï¼Œè¡¨ç¤ºæ‰˜ç›˜å·²åˆ°è¾¾æŒ‡å®šè¾“é€çº¿å¹¶å®Œæˆå‡ºåº“
ConsoleHelper.WriteSuccessLine($"ã€{conveyorLine.DeviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿å‡ºåº“完æˆ");
// 查询æ£åœ¨æ‰§è¡Œçš„输é€çº¿ä»»åŠ¡
var task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
// 如果任务å˜åœ¨
if (task != null)
{
// 如果任务ä¸çš„æ‰˜ç›˜æ¡ç 与命令ä¸çš„æ‰˜ç›˜æ¡ç 一致
if (command.Barcode == task.PalletCode)
{
// 创建一个空的WebResponseContent对象
WebResponseContent content = new WebResponseContent();
// ä¿å˜ä»»åŠ¡çš„ä¸‹ä¸€ç›®æ ‡åœ°å€
var next = task.NextAddress;
// å°†ä»»åŠ¡æ˜ å°„ä¸ºå‘½ä»¤
var taskCommand = MapTaskCommand(task, command);
// æ¢å¤ä»»åŠ¡çš„ä¸‹ä¸€ç›®æ ‡åœ°å€
task.NextAddress = next;
// 如果任务的托盘æ¡ç 与命令ä¸çš„æ‰˜ç›˜æ¡ç ä¸ä¸€è‡´æˆ–者任务备注为"NG"
if (task.PalletCode != command.Barcode || task.Remark == "NG")
{
// æŸ¥è¯¢ç›®æ ‡åœ°å€çš„容é‡
var NGAddress = _platFormRepository.QueryFirst(x => x.PlatCode == task.TargetAddress).Capacity;
// è®¾ç½®å‘½ä»¤çš„ç›®æ ‡åœ°å€ä¸ºNG地å€
taskCommand.TargetAddress = NGAddress;
}
else
{
// è®¾ç½®å‘½ä»¤çš„ç›®æ ‡åœ°å€ä¸º1000
taskCommand.TargetAddress = 1000;
}
// æ‰“å°æˆåŠŸä¿¡æ¯ï¼Œè¡¨ç¤ºæ‰˜ç›˜å·²åˆ°è¾¾æŒ‡å®šè¾“é€çº¿å¹¶å®Œæˆå‡ºåº“ï¼Œä¸‹ä¸€ç›®æ ‡åœ°å€å·²ç¡®å®š
ConsoleHelper.WriteSuccessLine($"ã€{conveyorLine.DeviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿å‡ºåº“完æˆ,ä¸‹ä¸€ç›®æ ‡åœ°å€ã€{taskCommand.TargetAddress}】");
// å‘é€å‘½ä»¤åˆ°è¾“é€çº¿
conveyorLine.SendCommand(taskCommand, childDeviceCode);
// 完æˆè¾“é€çº¿å‘é€
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
// 更新任务状æ€åˆ°ä¸‹ä¸€ä¸ªçжæ€
content = _taskService.UpdateTaskStatusToNext(task);
}
else
{
// æŸ¥è¯¢ç›®æ ‡åœ°å€çš„容é‡
var NGAddress = _platFormRepository.QueryFirst(x => x.PlatCode == task.TargetAddress).Capacity;
// æ‰“å°æˆåŠŸä¿¡æ¯ï¼Œè¡¨ç¤ºæ‰˜ç›˜å·²åˆ°è¾¾æŒ‡å®šè¾“é€çº¿å¹¶å®Œæˆå‡ºåº“ï¼Œä¸‹ä¸€ç›®æ ‡åœ°å€å·²ç¡®å®š
ConsoleHelper.WriteSuccessLine($"ã€{conveyorLine.DeviceName}】任务å·ï¼šã€{command.TaskNum}】,托盘æ¡ç :ã€{command.Barcode}】已到达ã€{childDeviceCode}】输é€çº¿å‡ºåº“完æˆ,ä¸‹ä¸€ç›®æ ‡åœ°å€ã€{NGAddress}】");
// 设置输é€çº¿çš„ç›®æ ‡åœ°å€ä¸ºNG地å€
conveyorLine.SetValue(ConveyorLineDBName.ConveyorLineTargetAddress, NGAddress, childDeviceCode);
// 完æˆè¾“é€çº¿å‘é€
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
// 更新任务状æ€åˆ°ä¸‹ä¸€ä¸ªçжæ€
_taskService.UpdateTaskStatusToNext(task);
}
}
}
#endregion 出库
/// <summary>
/// 输é€çº¿äº¤äº’完æˆ
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="childDeviceCode">å设备编å·</param>
/// <param name="ProtocalDetailValue">线体当å‰bool读å–å移地å€</param>
/// <param name="value">值</param>
public void ConveyorLineSendFinish(CommonConveyorLine conveyorLine, string childDeviceCode, int ProtocalDetailValue, bool value)
{
// 从conveyorLineä¸çš„DeviceProDTOsåˆ—è¡¨ä¸æŸ¥æ‰¾ç¬¬ä¸€ä¸ªç¬¦åˆæ¡ä»¶çš„DeviceProDTO对象
// æ¡ä»¶æ˜¯DeviceProParamTypeç‰äºŽDeviceCommand,且DeviceChildCodeç‰äºŽchildDeviceCode
// 查找结果按DeviceProOffsetå‡åºæŽ’列
DeviceProDTO? devicePro = conveyorLine.DeviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == childDeviceCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
// å°†deviceProçš„DeviceProAddress按'.'分割æˆå—符串数组x
string[] x = devicePro.DeviceProAddress.Split('.');
// 将数组x的最åŽä¸€ä¸ªå…ƒç´ 替æ¢ä¸ºProtocalDetailValueåŠ 1åŽçš„å—符串形å¼
x[x.Length - 1] = (ProtocalDetailValue + 1).ToString();
// 将修改åŽçš„æ•°ç»„x釿–°æ‹¼æŽ¥æˆå—符串,作为新的DeviceProAddress
string DeviceProAddress = string.Join(".", x);
// 使用conveyorLineçš„Communicator对象的Write方法,将value写入新的DeviceProAddress地å€
conveyorLine.Communicator.Write(DeviceProAddress, value);
}
#region 检测空盘实盘任务
/// <summary>
/// 监测空托盘实盘出库
/// </summary>
/// <param name="conveyorLine">输é€çº¿å®žä¾‹å¯¹è±¡</param>
/// <param name="command">读å–的请求信æ¯</param>
/// <param name="childDeviceCode">å设备编å·</param>
/// <param name="index">线体当å‰bool读å–å移地å€</param>
// å®šä¹‰ä¸€ä¸ªå¼‚æ¥æ–¹æ³•,用于处ç†ç©ºæ‰˜ç›˜è¿”回任务
public async void EmptyTrayReturn(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int index, Platform platform)
{
try
{
// 定义任务类型枚举å˜é‡
TaskOutboundTypeEnum taskOutboundTypeEnum;
// æ ¹æ®å¹³å°ç±»åž‹è®¾ç½®ä»»åŠ¡ç±»åž‹æžšä¸¾
if (platform.PlatformType.Contains("OutTray"))
taskOutboundTypeEnum = TaskOutboundTypeEnum.OutTray;
else
taskOutboundTypeEnum = TaskOutboundTypeEnum.Outbound;
// 调用检查并创建任务的方法
await CheckAndCreateTask(taskOutboundTypeEnum, childDeviceCode, index, platform.Stacker, platform);
}
catch (Exception)
{
// æ•获并忽略异常
}
}
/// <summary>
/// 检查任务并创建新任务
/// </summary>
private async Task CheckAndCreateTask(TaskOutboundTypeEnum taskType, string childDeviceCode, int index, string roadWay, Platform platform)
{
// 查询数æ®åº“ä¸å·²æœ‰çš„任务
var tasks = _taskRepository.QueryData(x => x.TaskType == (int)taskType && x.TargetAddress == childDeviceCode);
// 如果已有任务数é‡å°äºŽæŒ‡å®šç´¢å¼•
if (tasks.Count < index)
{
// 调用WMS获å–出库任务
#region 调用WMS获å–出库任务
// 定义WMS任务数æ®ä¼ 输对象
WMSTaskDTO taskDTO = new WMSTaskDTO();
// 获å–WMS IP地å€é…ç½®
var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
var requestTrayOutTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestTrayOutTask)?.ConfigValue;
// 如果WMS IPåœ°å€æœªé…置,抛出异常
if (wmsBase == null || requestTrayOutTask == null)
{
throw new InvalidOperationException("WMS IP 未é…ç½®");
}
var wmsIpAddress = wmsBase + requestTrayOutTask;
// å‘é€HTTP POST请求获å–任务数æ®
var result = await HttpHelper.PostAsync(wmsIpAddress, new { position = childDeviceCode, tag = (int)taskType, areaCdoe = roadWay, platform.ProductionLine }.ToJsonString());
// è§£æžè¿”回的JSONæ•°æ®
WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(result);
// 检查返回状æ€ï¼Œå¦‚果失败则返回
if (!content.Status)
return;
// è§£æžä»»åŠ¡æ•°æ®
taskDTO = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
#endregion 调用WMS获å–出库任务
// 创建并å‘é€ä»»åŠ¡
CreateAndSendTask(taskDTO);
}
}
/// <summary>
/// 创建任务
/// </summary>
public WebResponseContent CreateAndSendTask(WMSTaskDTO taskDTO)
{
var content = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
if (content.Status)
{
Console.WriteLine($"{taskDTO.TaskType}呼嫿ˆåŠŸ");
}
return content;
}
#endregion 检测空盘实盘任务
#region 化æˆNGå£å…¥åº“
public void NGRequestTaskInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue, string TargetAddress)
{
var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.MOMIP_BASE)?.ConfigValue;
var ipAddress = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.TrayCellsStatus)?.ConfigValue;
if (wmsBase == null || ipAddress == null)
{
throw new InvalidOperationException("MOM IP 未é…ç½®");
}
Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode);
TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
{
Software = "WMS",
TrayBarcode = command.Barcode,
EquipmentCode = stationManager.stationEquipMOM,
SessionId = Guid.NewGuid().ToString(),
EmployeeNo = "MITest",
SceneType = "1",
RequestTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
};
var MOMIpAddress = wmsBase + ipAddress;
var result = HttpHelper.PostAsync(MOMIpAddress, trayCells.ToJsonString()).Result;
WriteInfo("å…¥ç«™æ ¡éªŒ", $"ã€{childDeviceCode}ã€‘å…¥ç«™æ ¡éªŒè¯·æ±‚å‚æ•°ã€{trayCells.ToJsonString()}】");
WriteInfo("å…¥ç«™æ ¡éªŒ", "");
WriteInfo("å…¥ç«™æ ¡éªŒ", $"ã€{childDeviceCode}ã€‘å…¥ç«™æ ¡éªŒè¿”å›žå‚æ•°ã€{result}】");
ResultTrayCellsStatus result1 = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(result);
var serialNosError = result1.SerialNos.Where(x => x.SerialNoStatus != 1).ToList();
if (serialNosError.Count > 0)
{
return;
}
if (result1.SerialNos.Count <= 0)
{
// 空托盘入库逻辑
Dt_Task dt_Task = new Dt_Task()
{
TargetAddress = TargetAddress,
PalletCode = command.Barcode,
NextAddress = TargetAddress,
TaskNum = 0
};
var next = dt_Task.NextAddress;
var taskCommand = MapTaskCommand(dt_Task, command);
dt_Task.NextAddress = next;
conveyorLine.SendCommand(taskCommand, childDeviceCode);
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
}
else
{
conveyorLine.SetValue(ConveyorLineDBName.ConveyorLineTargetAddress, 1000, childDeviceCode);
ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
}
}
#endregion 化æˆNGå£å…¥åº“
}
}