using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common.LocationEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core;
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.AGV.HIKROBOT;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.PortableExecutable;
using WIDESEAWCS_QuartzJob;
namespace WIDESEAWCS_TaskInfoService
{
public partial class TaskService
{
#region 海康AGV任务创建
///
/// 创建海康AGV入库任务
///
///
///
///
public WebResponseContent CreateHKNewInTask(TaskDTO taskDTO, int taskType)
{
WebResponseContent content = new WebResponseContent();
try
{
Dt_HKLocationInfo? hKLocationInfo = null;
if (taskType==(int)TaskTypeEnum.STURR)
{
var reslut=_stationInfo.Repository.QueryFirst(x=>x.StationCode==taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点货位【{taskDTO.fromLocationCode}】");
}
#region 点到点
if (!string.IsNullOrEmpty(taskDTO.toLocationCode))
{
hKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点货位【{taskDTO.toLocationCode}】");
if (hKLocationInfo.LocationStatus != (int)LocationStatusEnum.Free) throw new Exception($"终点货位【{taskDTO.toLocationCode}】货位状态不为空货位");
}
#endregion
//获取货位信息
else
hKLocationInfo = _hKLocationInfoService.GetFreeLocationInfo(taskDTO.toAreaCode) ?? throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!");
if (taskType == (int)TaskTypeEnum.Q1TSJ4)
{
var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
if (device.IsConnected)
{
var reslu = device.GetValue(HoistEnum.Emptycontainernumber);
var result = reslu?.TrimEnd('\0')?.Substring(2);
if (reslu == taskDTO.containerCode && result != null)
{
throw new Exception($"当前的料箱号为{reslu}与{taskDTO.containerCode}料箱号不匹配");
}
}
else
{
throw new Exception("提升机信息设备连接失败");
}
}
Dt_Task dt_Task = new Dt_Task()
{
TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
WMSTaskNum = taskDTO.taskCode,
//WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
Grade = taskDTO.taskPriority,
PalletCode = taskDTO.containerCode,
Roadway = hKLocationInfo.RoadwayNo,
TaskState = TaskStatusEnum.New.ObjToInt(),
TaskType = taskType,
SourceAddress = taskDTO.fromLocationCode,
CurrentAddress = taskDTO.fromLocationCode,
NextAddress = hKLocationInfo.LocationCode,
TargetAddress = hKLocationInfo.LocationCode,
Creater = "WMS",
};
hKLocationInfo.LocationStatus = LocationStatusEnum.InLock.ObjToInt();
#region 下发海康AGV任务
content = SendHIKROBOTTask(dt_Task);
if (!content.Status) throw new Exception(content.Message);
dt_Task.TaskState = (int)TaskStatusEnum.Execut;
dt_Task.Dispatchertime = DateTime.Now;
#endregion
try
{
Db.Ado.BeginTran();
BaseDal.AddData(dt_Task);
_hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
Db.Ado.CommitTran();
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
throw new Exception(ex.Message);
}
content.OK(data: new
{
taskCode = taskDTO.taskCode,
Message = "成功!"
});
}
catch (Exception ex)
{
content.Data = new
{
taskCode = taskDTO.taskCode,
Message = $"失败!{ex.Message}"
};
content.Error(ex.Message);
}
return content;
}
///
/// 创建海康AGV出库任务
///
///
///
///
public WebResponseContent CreateHKNewOutTask(TaskDTO taskDTO, int taskType)
{
WebResponseContent content = new WebResponseContent();
try
{
//4楼库内到提升机
Dt_HKLocationInfo? hKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
if (hKLocationInfo.LocationStatus != LocationStatusEnum.InStock.ObjToInt()) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】当前库位状态不可出库!");
if (hKLocationInfo.PalletCode != taskDTO.containerCode) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】绑定料箱号【{hKLocationInfo.PalletCode}】与任务料箱号【{taskDTO.containerCode}】不匹配!");
//库内到现边的点到点任务,是否需要判断货位状态?
if (taskType == (int)TaskTypeEnum.STU0003)
{
var reslut = _stationInfo.Repository.QueryFirst(x => x.StationCode == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点货位【{taskDTO.toLocationCode}】");
if (reslut.StationStatus != (int)LocationStatusEnum.Free)
{
throw new Exception($"终点站台【{taskDTO.toLocationCode}】状态不为空闲!");
}
}
Dt_Task dt_Task = new Dt_Task()
{
TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
WMSTaskNum = taskDTO.taskCode,
//WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
Grade = taskDTO.taskPriority,
PalletCode = taskDTO.containerCode,
Roadway = hKLocationInfo.RoadwayNo,
TaskState = TaskStatusEnum.New.ObjToInt(),
TaskType = taskType,
SourceAddress = taskDTO.fromLocationCode,
CurrentAddress = taskDTO.fromLocationCode,
NextAddress = taskDTO.toLocationCode,
TargetAddress = taskDTO.toLocationCode,
Creater = "WMS",
};
hKLocationInfo.LocationStatus = LocationStatusEnum.OutLock.ObjToInt();
#region 下发海康AGV任务
content = SendHIKROBOTTask(dt_Task);
if (!content.Status) throw new Exception(content.Message);
dt_Task.TaskState = (int)TaskStatusEnum.Execut;
dt_Task.Dispatchertime = DateTime.Now;
#endregion
try
{
Db.Ado.BeginTran();
BaseDal.AddData(dt_Task);
_hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
Db.Ado.CommitTran();
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
throw new Exception(ex.Message);
}
return content.OK(data: new
{
taskCode = taskDTO.taskCode,
Message = "成功!"
});
}
catch (Exception ex)
{
content.Data = new
{
taskCode = taskDTO.taskCode,
Message = $"失败!{ex.Message}"
};
return content.Error(ex.Message);
}
}
#endregion
#region 下发海康AGV任务
public WebResponseContent SendHIKROBOTTask([NotNull] Dt_Task task)
{
WebResponseContent content = new WebResponseContent();
HIKROBOTReturn hIKROBOTReturn = null;
HIKROBOTTaskSubmit hIKROBOTTaskSubmit = null;
try
{
Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(HIKROBOTTaskSubmit)) ?? throw new Exception("未找到海康AGV任务下发接口配置信息!请检查接口配置");
#region 实体类转换
hIKROBOTTaskSubmit = HIKROBOTTask(task);
if (hIKROBOTTaskSubmit == null) throw new Exception("海康AGV任务实体转换失败");
var json = hIKROBOTTaskSubmit.Serialize();
#endregion
// 创建Headers字典
var headers = new Dictionary
{
{ "X-lr-request-id", DateTimeOffset.Now.ToUnixTimeSeconds().ToString()+task.TaskNum }
// 如果需要其他Header,可以继续添加
// { "Content-Type", "application/json" }
};
// 传递Headers参数
string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskSubmit.Serialize(), headers: headers);
//string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskSubmit.Serialize());
hIKROBOTReturn = response.DeserializeObject();
if (hIKROBOTReturn.code == "SUCCESS")
{
var data = hIKROBOTReturn.data.ToString().DeserializeObject();
content.OK(data: data);
}
else
{
throw new Exception(hIKROBOTReturn.message);
}
}
catch (Exception ex)
{
content.Error(ex.Message);
}
finally
{
_trackloginfoService.AddTrackLog(hIKROBOTTaskSubmit, content, "下发海康AGV任务", "", hIKROBOTReturn.message);
}
return content;
}
#endregion
///
/// 海康任务实体类转换
///
///
///
public HIKROBOTTaskSubmit HIKROBOTTask(Dt_Task task)
{
HIKROBOTTaskSubmit hIKROBOTTaskSubmit = new HIKROBOTTaskSubmit();
try
{
var tasktype = (TaskTypeEnum)Enum.GetValues(typeof(TaskTypeEnum)).GetValue(task.TaskType - 1);
hIKROBOTTaskSubmit.initPriority = task.Grade;
hIKROBOTTaskSubmit.robotTaskCode = task.WMSTaskNum;
hIKROBOTTaskSubmit.taskType = tasktype.ToString(); ExtraDto extraDto = new ExtraDto();
if (task.TaskType != (int)TaskTypeEnum.MOVE && task.TaskType != (int)TaskTypeEnum.Q3RK && task.TaskType != (int)TaskTypeEnum.Q3CK && task.TaskType != (int)TaskTypeEnum.F01)
{
CarrierInfoDto carrierInfoDto = new CarrierInfoDto()
{
carrierCode = task.PalletCode,
carrierType = task.PalletCode.Contains("LXM") ? "DX" : "SX"
};
extraDto.carrierInfo.Add(carrierInfoDto);
hIKROBOTTaskSubmit.extra = extraDto;
}
switch (tasktype)
{
case TaskTypeEnum.CPInbound:
case TaskTypeEnum.CPOutbound:
case TaskTypeEnum.MLInbound:
case TaskTypeEnum.MLOutbound:
break;
case TaskTypeEnum.RK3F:
case TaskTypeEnum.CK3F:
case TaskTypeEnum.F02:
case TaskTypeEnum.F03:
case TaskTypeEnum.F04:
{
TargetRouteDto target = new TargetRouteDto()
{
code = task.CurrentAddress,
operation = "DELIVERY",//取货
seq = 0,
type = "SITE",
};
TargetRouteDto targetRoute = new TargetRouteDto()
{
code = task.NextAddress,
operation = "DELIVERY",//送货
seq = 1,
type = "SITE"
};
hIKROBOTTaskSubmit.targetRoute.Add(target);
hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
}
break;
case TaskTypeEnum.STURR:
case TaskTypeEnum.Q1TSJ4:
{
TargetRouteDto target = new TargetRouteDto()
{
code = task.CurrentAddress,
operation = "DELIVERY",//取货
seq = 0,
type = "SITE",
};
TargetRouteDto targetRoute = new TargetRouteDto()
{
code = task.NextAddress,
operation = "DELIVERY",//送货
seq = 1,
type = "STORAGE"
};
hIKROBOTTaskSubmit.targetRoute.Add(target);
hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
}
break;
case TaskTypeEnum.STU0003:
case TaskTypeEnum.CHUKU1:
{
TargetRouteDto target = new TargetRouteDto()
{
code = task.CurrentAddress,
operation = "DELIVERY",//取货
seq = 0,
type = "STORAGE",
};
TargetRouteDto targetRoute = new TargetRouteDto()
{
code = task.NextAddress,
operation = "DELIVERY",//送货
seq = 1,
type = "SITE",
};
hIKROBOTTaskSubmit.targetRoute.Add(target);
hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
}
break;
case TaskTypeEnum.Q3RK:
case TaskTypeEnum.Q3CK:
case TaskTypeEnum.MOVE:
case TaskTypeEnum.F01:
{
TargetRouteDto target = new TargetRouteDto()
{
code = task.CurrentAddress,
operation = "DELIVERY",//取货
seq = 0,
type = "SITE",
};
TargetRouteDto targetRoute = new TargetRouteDto()
{
code = task.NextAddress,
operation = "DELIVERY",//送货
seq = 1,
type = "SITE",
};
hIKROBOTTaskSubmit.targetRoute.Add(target);
hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
}
break;
case TaskTypeEnum.STUPTBY:
{
TargetRouteDto target = new TargetRouteDto()
{
code = task.CurrentAddress,
operation = "DELIVERY",//取货
seq = 0,
type = "STORAGE",
};
TargetRouteDto targetRoute = new TargetRouteDto()
{
code = task.NextAddress,
operation = "DELIVERY",//送货
seq = 1,
type = "STORAGE",
};
hIKROBOTTaskSubmit.targetRoute.Add(target);
hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
}
break;
case TaskTypeEnum.CPMoveInventory:
break;
default:
break;
}
}
catch (Exception ex)
{
}
return hIKROBOTTaskSubmit;
}
///
/// 海康AGV任务继续执行
///
/// 任务号
///
public WebResponseContent Hikvisiontaskscontinue(string TaskCode, string Address = null)
{
WebResponseContent content = new WebResponseContent();
HIKROBOTReturn hIKROBOTReturn = null;
HIKROBOTTaskContinue hIKROBOTTaskContinue = null;
try
{
// 1. 参数验证
if (string.IsNullOrWhiteSpace(TaskCode))
{
throw new Exception("任务编码不能为空");
}
Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(HIKROBOTTaskContinue)) ?? throw new Exception("未找到海康AGV继续执行接口配置信息!请检查接口配置");
hIKROBOTTaskContinue = new HIKROBOTTaskContinue()
{
triggerCode = TaskCode,
triggerType = "TASK",//固定值
targetRoute = string.IsNullOrEmpty(Address) ? null :
new TargetRouteDto()
{
code = Address,
type = "SITE",
}
};
var headers = new Dictionary
{
{ "X-lr-request-id", DateTimeOffset.Now.ToUnixTimeSeconds().ToString()+TaskCode }
};
string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskContinue.Serialize(), headers: headers);
hIKROBOTReturn = response.DeserializeObject();
if (hIKROBOTReturn.code == "SUCCESS")
{
var data = hIKROBOTReturn.data.ToString().DeserializeObject();
content.OK(data: data);
}
else
{
throw new Exception(hIKROBOTReturn.message);
}
}
catch (Exception ex)
{
content.Error(ex.Message);
}
finally
{
_trackloginfoService.AddTrackLog(hIKROBOTTaskContinue, content, "海康AGV继续执行任务", "", hIKROBOTReturn.message);
}
return content;
}
}
}