using Autofac.Core;
using Magicodes.IE.Core;
using NetTaste;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection.Metadata;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.LocationEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.AGV.HIKROBOT;
using WIDESEAWCS_DTO.RGV.FOURBOT;
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
namespace WIDESEAWCS_TaskInfoService
{
public partial class TaskService
{
#region 更新任务状态
///
/// 更新任务状态
///
///
///
///
///
public WebResponseContent UpdateTaskStatus(object TaskDTO, DeviceTypeEnum deviceTypeEnum)
{
WebResponseContent content = new WebResponseContent();
GALAXISUpdateTaskStatus gALAXIS = new();
RGVReturnInfo Rgv = new();//四向车任务状态通知
HIKROBOTTaskProcessReport hIKROBOT = new();//海康机器人
string deviceName = "";
try
{
if (deviceTypeEnum == DeviceTypeEnum.GALAXIS)
{
deviceName = "凯乐士";
gALAXIS = TaskDTO.Serialize().DeserializeObject();
if (gALAXIS == null) throw new Exception("参数转换失败!");
var task = BaseDal.QueryFirst(x => x.WMSTaskNum == gALAXIS.taskId);
if (task == null) throw new Exception($"未找到任务,任务编号【{gALAXIS.taskId}】");
switch (gALAXIS.taskStatus)
{
case 0:
TaskCompleted(task, deviceTypeEnum);
break;
case 4:
TaskFromCompleted(task, deviceTypeEnum);
break;
default:
task.TaskState = ReturnTaskStatus(gALAXIS.taskStatus, deviceTypeEnum);
if (gALAXIS.taskStatus == 10) TaskFeedback(task, 1);
else BaseDal.UpdateData(task);
break;
}
}
else if (deviceTypeEnum == DeviceTypeEnum.HIKROBOT)
{
deviceName = "海康";
hIKROBOT = TaskDTO.Serialize().DeserializeObject();
if (hIKROBOT == null) throw new Exception("参数转换失败!");
var task = BaseDal.QueryFirst(x => x.WMSTaskNum == hIKROBOT.robotTaskCode);
if (task == null) throw new Exception($"未找到任务,任务编号【{hIKROBOT.robotTaskCode}】");
switch (hIKROBOT.extra.values.method)
{
case "end":
TaskCompleted(task, deviceTypeEnum);
break;
case "outbin":
TaskFromCompleted(task, deviceTypeEnum);
break;
case "start":
task.TaskState = (int)TaskStatusEnum.Takeing;
TaskFeedback(task, 1);
break;
default:
task.TaskState = ReturnTaskStatus(hIKROBOT.extra.values.method, deviceTypeEnum);
BaseDal.UpdateData(task);
break;
}
}
else //四向车
{
deviceName = "四向车";
Rgv = TaskDTO.Serialize().DeserializeObject();
if (Rgv == null) throw new Exception("参数转换失败!");
//var task = BaseDal.QueryFirst(x => x.RGVTaskId == Rgv.content.taskID);
var task = BaseDal.QueryFirst(x => x.PalletCode == Rgv.content.podID);
if (task == null) throw new Exception($"未找到任务,任务编号【{Rgv.content.taskID}】");
if (Rgv.messageType == 72)//小车顶起货物
{
if (task.TaskType == (int)TaskTypeEnum.CPInbound && task.TaskState == (int)TaskStatusEnum.Execut)
{
task.TaskState = (int)TaskStatusEnum.WaiCheckShape;
//BaseDal.UpdateData(task);//给质检门写入启动信号
var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "F1") as OtherDevice;
if (device == null) task.ExceptionMessage = "未找到1楼质检门设备信息";
else
{
if (device.IsConnected)
device.SetValue(QualityInspectionCommandEnum.StartqualityInspection, true, task.SourceAddress);
else
task.ExceptionMessage = "1楼质检门设备连接失败";
}
TaskFeedback(task, 4);
}
else
{
TaskFromCompleted(task, deviceTypeEnum);
}
}
else if (Rgv.messageType == 10)
{
if (Rgv.content.status == 2) TaskFeedback(task, 1);
if (Rgv.content.status == 4)
{
if (task.TaskType == TaskTypeEnum.CPMoveInventory.ObjToInt())
{
deviceName += "移库";
WMSMoveLocationFeedback wMSMoveLocationFeedback = new WMSMoveLocationFeedback()
{
containerCode = task.PalletCode,
fromStationCode = task.SourceAddress,
toLocationCode = task.TargetAddress
};
Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSInOutBoundCompleteFeedback));
WMSReturn agvContent = null;
if (apiInfo == null) task.ExceptionMessage = "未找到出入库完成反馈WMS接口配置信息!请检查接口配置";
else
{
string response = HttpHelper.Post(apiInfo.ApiAddress, wMSMoveLocationFeedback.Serialize());
agvContent = response.DeserializeObject();
content.OK(data: agvContent);
if (agvContent.code != 200)
{
task.ExceptionMessage = agvContent.message;
content.Error(agvContent.message);
}
}
}
else if (task.TaskType == (int)TaskTypeEnum.CPInbound && task.TaskState == (int)TaskStatusEnum.TakeFinish)
{
if (!string.IsNullOrEmpty(task.ExceptionMessage)) task.TaskState = TaskStatusEnum.CheckShapeingNG.ObjToInt();
else task.TaskState = (int)TaskStatusEnum.CheckShapeing;
BaseDal.UpdateData(task);
}
else if (task.TaskType == (int)TaskTypeEnum.CPInbound && task.TaskState == (int)TaskStatusEnum.Puting)
{
if (!string.IsNullOrEmpty(task.ExceptionMessage))
ErrorTaskFeedback(task, true/*!task.ExceptionMessage.Contains("外检失败")*/);
else
TaskCompleted(task, deviceTypeEnum);
}
else if (task.TaskType == (int)TaskTypeEnum.CPOutbound || task.TaskType == (int)TaskTypeEnum.CPMoveInventory)//出库、移库完成
{
TaskCompleted(task, deviceTypeEnum);
}
}
}
}
content.OK();
}
catch (Exception ex)
{
content.Error(ex.Message);
}
finally
{
_trackloginfoService.AddTrackLog(TaskDTO, content, $"{deviceName}任务状态反馈", "", "");
}
return content;
}
#endregion
#region 任务状态转换
public int ReturnTaskStatus(object status, DeviceTypeEnum deviceTypeEnum)
{
try
{
if (deviceTypeEnum == DeviceTypeEnum.GALAXIS)
{
TaskStatusEnum taskStatus = status switch
{
1 => TaskStatusEnum.FromOutOfStock,
2 => TaskStatusEnum.ToaddInStock,
3 => TaskStatusEnum.TaskNumRepetition,
5 => TaskStatusEnum.RCSDataError,
6 => TaskStatusEnum.TaskTypeError,
7 => TaskStatusEnum.ParameterError,
8 => TaskStatusEnum.RCSError,
10 => TaskStatusEnum.Takeing,
13 => TaskStatusEnum.Cancel,
_ => throw new ArgumentOutOfRangeException(
nameof(status),
status,
$"未知的任务状态码: {status}"
)
};
return (int)taskStatus;
}
else if (deviceTypeEnum == DeviceTypeEnum.HIKROBOT)
{
return 0;
}
else
{
return 0;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 任务起点完成
///
/// 任务起点完成
///
///
///
///
public WebResponseContent TaskFromCompleted(Dt_Task dt_Task, DeviceTypeEnum deviceTypeEnum)
{
try
{
dt_Task.TaskState = TaskStatusEnum.TakeFinish.ObjToInt();
TaskFeedback(dt_Task, 4);
//Db.Ado.BeginTran();
UpdateLocationStatus(dt_Task.SourceAddress, dt_Task.PalletCode, deviceTypeEnum, LocationStatusEnum.Free);
//BaseDal.UpdateData(dt_Task);
//Db.Ado.CommitTran();
return WebResponseContent.Instance.OK();
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
throw new Exception(ex.Message);
}
}
#endregion
#region 任务完成
///
/// 任务完成
///
///
///
///
public WebResponseContent TaskCompleted(Dt_Task dt_Task, DeviceTypeEnum deviceTypeEnum)
{
try
{
dt_Task.TaskState = TaskStatusEnum.Finish.ObjToInt();
TaskFeedback(dt_Task, 2);
//Db.Ado.BeginTran();
UpdateLocationStatus(dt_Task.TargetAddress, dt_Task.PalletCode, deviceTypeEnum, LocationStatusEnum.InStock);
//Db.Ado.CommitTran();
return WebResponseContent.Instance.OK();
}
catch (Exception ex)
{
//Db.Ado.RollbackTran();
throw new Exception(ex.Message);
}
}
#endregion
#region 更新货位状态
///
/// 更新货位状态
///
///
///
///
///
public WebResponseContent UpdateLocationStatus(string locationCode, string palletCode, DeviceTypeEnum deviceTypeEnum, LocationStatusEnum locationStatusEnum)
{
try
{
if (deviceTypeEnum == DeviceTypeEnum.GALAXIS)
{
Dt_KLSLocationInfo? dt_KLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.LocationCode == locationCode);
if (dt_KLSLocationInfo != null)
{
if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
{
dt_KLSLocationInfo.PalletCode = "";
}
else
{
dt_KLSLocationInfo.PalletCode = palletCode;
}
dt_KLSLocationInfo.LocationStatus = locationStatusEnum.ObjToInt();
_kLSLocationInfoService.Repository.UpdateData(dt_KLSLocationInfo);
}
}
else if (deviceTypeEnum == DeviceTypeEnum.HIKROBOT)
{
Dt_HKLocationInfo? dt_HKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == locationCode);
if (dt_HKLocationInfo != null)
{
if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
{
dt_HKLocationInfo.PalletCode = "";
}
else
{
dt_HKLocationInfo.PalletCode = palletCode;
}
dt_HKLocationInfo.LocationStatus = locationStatusEnum.ObjToInt();
_hKLocationInfoService.Repository.UpdateData(dt_HKLocationInfo);
}
}
else if (deviceTypeEnum == DeviceTypeEnum.YuanLiJuHe)
{
Dt_RGVLocationInfo dt_RGVLocationInfo = _rGVLocationInfoService.Repository.QueryFirst(x => x.LocationCode == locationCode);
//这里将任务的托盘号给货位表中的PalletCode字段。
if (dt_RGVLocationInfo != null)
{
if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
{
dt_RGVLocationInfo.PalletCode = "";
}
else
{
dt_RGVLocationInfo.PalletCode = palletCode;
}
dt_RGVLocationInfo.LocationStatus = locationStatusEnum.ObjToInt();
_rGVLocationInfoService.Repository.UpdateData(dt_RGVLocationInfo);
}
}
else//平库库位
{
}
return WebResponseContent.Instance.OK();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 上报WMS
public WebResponseContent TaskFeedback(Dt_Task dt_Task, int Status)
{
WebResponseContent content = new WebResponseContent();
WMSInOutBoundCompleteFeedback boundCompleteFeedback = new WMSInOutBoundCompleteFeedback();
WMSReturn agvContent = null;
try
{
Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSInOutBoundCompleteFeedback));
if (apiInfo == null) dt_Task.ExceptionMessage = "未找到出入库完成反馈WMS接口配置信息!请检查接口配置";
else
{
boundCompleteFeedback.taskCode = dt_Task.WMSTaskNum;
boundCompleteFeedback.containerCode = dt_Task.PalletCode;
boundCompleteFeedback.fromStationCode = dt_Task.SourceAddress;
boundCompleteFeedback.toLocationCode = dt_Task.TargetAddress;
boundCompleteFeedback.status = Status;
boundCompleteFeedback.custStatus = "";
string response = HttpHelper.Post(apiInfo.ApiAddress, boundCompleteFeedback.Serialize());
agvContent = response.DeserializeObject();
content.OK(data: agvContent);
if (agvContent.code != 200)
{
dt_Task.ExceptionMessage = agvContent.message;
content.Error(agvContent.message);
}
}
}
catch (Exception ex)
{
dt_Task.ExceptionMessage = ex.Message;
}
finally
{
_trackloginfoService.AddTrackLog(boundCompleteFeedback, content, "出入库任务状态反馈WMS", "", "");
if (agvContent != null && agvContent.code == 200 && Status == 2)
BaseDal.DeleteAndMoveIntoHty(dt_Task, OperateTypeEnum.自动完成);
else
BaseDal.UpdateData(dt_Task);
}
return WebResponseContent.Instance.OK();
}
#endregion
#region 失败任务上报WMS并删除任务
public WebResponseContent ErrorTaskFeedback(Dt_Task dt_Task, bool Del)
{
WebResponseContent content = new WebResponseContent();
WMSInOutBoundCompleteFeedback boundCompleteFeedback = new WMSInOutBoundCompleteFeedback();
WMSReturn agvContent = null;
try
{
Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSInOutBoundCompleteFeedback));
if (apiInfo == null) dt_Task.ExceptionMessage = "未找到出入库完成反馈WMS接口配置信息!请检查接口配置";
else
{
boundCompleteFeedback.taskCode = dt_Task.WMSTaskNum;
boundCompleteFeedback.containerCode = dt_Task.PalletCode;
boundCompleteFeedback.fromStationCode = dt_Task.SourceAddress;
boundCompleteFeedback.toLocationCode = dt_Task.TargetAddress;
boundCompleteFeedback.status = 3;
boundCompleteFeedback.custStatus = "";
boundCompleteFeedback.memo = dt_Task.ExceptionMessage;
string response = HttpHelper.Post(apiInfo.ApiAddress, boundCompleteFeedback.Serialize());
agvContent = response.DeserializeObject();
content.OK(data: agvContent);
if (agvContent.code != 200)
{
dt_Task.ExceptionMessage = agvContent.message;
content.Error(agvContent.message);
}
}
}
catch (Exception ex)
{
dt_Task.ExceptionMessage = ex.Message;
content.Error(ex.Message);
}
finally
{
_trackloginfoService.AddTrackLog(boundCompleteFeedback, content, "出入库任务状态反馈WMS", "", "");
if (agvContent != null && agvContent.code == 200 && Del) BaseDal.DeleteAndMoveIntoHty(dt_Task, OperateTypeEnum.自动删除);
else BaseDal.UpdateData(dt_Task);
}
return content;
}
#endregion
}
}