using Newtonsoft.Json;
using WIDESEA_Core;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.HttpEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.Stock;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks.SocketServer;
namespace WIDESEAWCS_Tasks
{
///
/// 机械手任务处理器 - 负责机械手任务执行和处理
///
public class RobotTaskProcessor
{
private readonly TcpSocketServer _tcpSocket;
private readonly RobotStateManager _stateManager;
private readonly IRobotTaskService _robotTaskService;
private readonly ITaskService _taskService;
private readonly HttpClientHelper _httpClientHelper;
public RobotTaskProcessor(
TcpSocketServer tcpSocket,
RobotStateManager stateManager,
IRobotTaskService robotTaskService,
ITaskService taskService,
HttpClientHelper httpClientHelper)
{
_tcpSocket = tcpSocket;
_stateManager = stateManager;
_robotTaskService = robotTaskService;
_taskService = taskService;
_httpClientHelper = httpClientHelper;
}
///
/// 获取机械手任务
///
public Dt_RobotTask? GetTask(RobotCraneDevice robotCrane)
{
return _robotTaskService.QueryRobotCraneTask(robotCrane.DeviceCode);
}
///
/// 获取机械手任务
///
public bool? DeleteTask(int ID)
{
return _robotTaskService.Repository.DeleteDataById(ID);
}
///
/// 发送机械手取货命令
///
public async Task SendSocketRobotPickAsync(Dt_RobotTask task, RobotSocketState state)
{
string taskString = $"Pickbattery,{task.RobotSourceAddress}";
// 发送任务指令
bool result = await _tcpSocket.SendToClientAsync(state.IPAddress, taskString);
if (result)
{
task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
state.CurrentTask = task;
// 更新缓存中的状态(使用安全更新防止并发覆盖)
if (_stateManager.TryUpdateStateSafely(state.IPAddress, state))
await _robotTaskService.UpdateRobotTaskAsync(task);
}
}
///
/// 处理入库任务
///
public async Task HandleInboundTaskAsync(RobotSocketState state, bool useSourceAddress)
{
var currentTask = state.CurrentTask;
if (currentTask == null)
{
return false;
}
string roadway = currentTask.RobotSourceAddressLineCode;
int warehouseId = currentTask.RobotRoadway == "ZYRB1" ? 1 : currentTask.RobotRoadway == "HPRB001" ? 2 : 3;
int taskType = 0;
string SourceAddress = currentTask.RobotTargetAddressLineCode;
string TargetAddress = currentTask.RobotSourceAddressLineCode;
string PalletCode = string.Empty;
// 直接转换为枚举类型进行比较
var robotTaskType = (RobotTaskTypeEnum)currentTask.RobotTaskType;
if (useSourceAddress)
{
switch (robotTaskType)
{
case RobotTaskTypeEnum.GroupPallet:
return false;
case RobotTaskTypeEnum.ChangePallet:
case RobotTaskTypeEnum.SplitPallet:
taskType = TaskTypeEnum.InEmpty.GetHashCode();
PalletCode = currentTask.RobotSourceAddressPalletCode;
break;
}
}
else
{
switch (robotTaskType)
{
case RobotTaskTypeEnum.ChangePallet:
case RobotTaskTypeEnum.GroupPallet:
taskType = TaskTypeEnum.Inbound.GetHashCode();
PalletCode = currentTask.RobotTargetAddressPalletCode;
break;
case RobotTaskTypeEnum.SplitPallet:
return true;
}
}
CreateTaskDto taskDto = new CreateTaskDto
{
PalletCode = PalletCode,
SourceAddress = SourceAddress ?? string.Empty,
TargetAddress = TargetAddress ?? string.Empty,
Roadway = roadway,
WarehouseId = warehouseId,
PalletType = 1,
TaskType = taskType
};
var result = _httpClientHelper.Post(nameof(ConfigKey.CreateTaskInboundAsync), taskDto.ToJson());
if (!result.Data.Status && result.IsSuccess)
{
return false;
}
WMSTaskDTO taskDTO = JsonConvert.DeserializeObject(result.Data.Data.ToJson() ?? string.Empty) ?? new WMSTaskDTO();
var content = _taskService.ReceiveWMSTask(new List { taskDTO });
if (!content.Status) return false;
var taskInfo = JsonConvert.DeserializeObject(content.Data.ToJson() ?? string.Empty) ?? new Dt_Task();
string sourceAddress = taskDTO.SourceAddress;
IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceProDTOs.Any(d => d.DeviceChildCode == sourceAddress));
if (device != null)
{
CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
conveyorLine.SetValue(ConveyorLineDBNameNew.Target, taskInfo.NextAddress, sourceAddress);
conveyorLine.SetValue(ConveyorLineDBNameNew.TaskNo, taskInfo.TaskNum, sourceAddress);
conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_STB, 1, sourceAddress);
if (_taskService.UpdateTaskStatusToNext(taskInfo).Status)
{
return true;
}
}
return false;
}
///
/// 构建库存DTO
///
public static StockDTO BuildStockDTO(RobotSocketState state, int[] positions)
{
return new StockDTO
{
SourceLineNo = state.CurrentTask.RobotSourceAddressLineCode,
SourcePalletNo = state.CurrentTask.RobotSourceAddressPalletCode,
TargetPalletNo = state.CurrentTask.RobotTargetAddressPalletCode,
TargetLineNo = state.CurrentTask.RobotTargetAddressLineCode,
Details = positions
.Where(x => x > 0)
.OrderBy(x => x)
.Select((x, idx) => new StockDetailDTO
{
Quantity = state.RobotTaskTotalNum > 0 ? state.RobotTaskTotalNum + positions.Length : positions.Length,
Channel = x,
CellBarcode = state.CellBarcode?.Count > 0 ? state.CellBarcode[x - 1] : ""
})
.ToList()
};
}
///
/// 调用拆盘API
///
public HttpResponseResult PostSplitPalletAsync(StockDTO stockDTO)
{
return _httpClientHelper.Post(nameof(ConfigKey.SplitPalletAsync), stockDTO.ToJson());
}
///
/// 调用组盘或换盘API
///
public HttpResponseResult PostGroupPalletAsync(string configKey, StockDTO stockDTO)
{
return _httpClientHelper.Post(configKey, stockDTO.ToJson());
}
}
}