using HslCommunication;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEAWCS_Common.ShuttleCarEnum;
|
using WIDESEAWCS_Common.TaskEnum;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob.DTO;
|
using WIDESEAWCS_QuartzJob;
|
using WIDESEAWCS_Core;
|
using Newtonsoft.Json;
|
using WIDESEAWCS_Core.Helper;
|
using WIDESEAWCS_DTO.WMSInfo;
|
using WIDESEAWCS_Tasks.ShuttleCarJob;
|
|
namespace WIDESEAWCS_Tasks
|
{
|
partial class CommonShuttleCarJob
|
{
|
private object _CarTaskObject = new object();
|
#region 堆垛机移车完成
|
/// <summary>
|
/// 获取堆垛机移车/移库完成任务
|
/// </summary>
|
public void GetSC_CarFinish()
|
{
|
Dt_Task dt_Task = GetTask(TaskCarStatusEnum.SC_CarFinish.ObjToInt(), TaskOtherTypeEnum.RelocationCar.ObjToInt());//移车任务
|
if (dt_Task != null)
|
{
|
UpdateShuttleCar(dt_Task.ShuttleCarCode, dt_Task.CurrentAddress);
|
_taskService.AddTask_Hty(dt_Task, TaskOperateTypeEnum.AutoComplete);
|
}
|
dt_Task = GetTask(CarChargingTaskEnum.SC_CarFinish.ObjToInt(), TaskOtherTypeEnum.RelocationCarCharging.ObjToInt());//移车充电任务
|
if (dt_Task != null)
|
{
|
UpdateShuttleCar(dt_Task.ShuttleCarCode, dt_Task.CurrentAddress);
|
SendTask(dt_Task);//下发充电任务
|
}
|
dt_Task = GetTask(TaskRelocationStatusEnum.SC_RelocationFinish.ObjToInt(), TaskRelocationTypeEnum.Relocation.ObjToInt());//移库任务
|
if (dt_Task != null)
|
SendTask(dt_Task);
|
}
|
#endregion
|
/// <summary>
|
/// 修改穿梭车位置信号
|
/// </summary>
|
/// <param name="ShuttleCarCode">穿梭车号</param>
|
/// <param name="Address">位置</param>
|
public void UpdateShuttleCar(string ShuttleCarCode, string Address)
|
{
|
var ShuttleCar = QueryCode(ShuttleCarCode);
|
string[] targetCodes = Address.Split("-");
|
var Row = Convert.ToInt16(targetCodes[0]);
|
if (Row == 3)
|
{
|
ShuttleCar.ShuttleCarPosition = Address;
|
ShuttleCar.ShuttleCarPosition1 = Address;
|
}
|
else
|
{
|
var a = Row == 1 ? 2.ToString("000") : 1.ToString("000");
|
ShuttleCar.ShuttleCarPosition = Address;
|
ShuttleCar.ShuttleCarPosition1 = a + "-" + targetCodes[1] + "-" + targetCodes[2];
|
}
|
_shuttleCarService.UpdateData(ShuttleCar);
|
}
|
#region 获取移车任务
|
/// <summary>
|
/// 获取移车任务/出入库任务
|
/// </summary>
|
public void Get_CarTask(ShuttleCar shuttleCar, ShuttleCarTaskCommandR command)
|
{
|
lock (_CarTaskObject)
|
{
|
#region 获取新建移车任务
|
Dt_Task dt_Task = GetTask((int)TaskCarStatusEnum.CarNew, (int)TaskOtherTypeEnum.RelocationCar);
|
if (dt_Task != null)
|
{
|
if (string.IsNullOrEmpty(dt_Task.SourceAddress))
|
{
|
(bool, Dt_ShuttleCar) result = GetSourceTask(shuttleCar, command);
|
if (result.Item1)
|
UpdateSource(ref dt_Task, result.Item2);
|
else
|
GetShuttleCar(ref dt_Task, shuttleCar);//获取另外一个穿梭车信息
|
}
|
else if (string.IsNullOrEmpty(dt_Task.TargetAddress))
|
{
|
#region 向WMS申请
|
WebResponseContent content = new WebResponseContent();
|
var ResultData = HttpHelper.PostAsync(WMSInterfaceAddress.GetPosition + $"?position={dt_Task.SourceAddress}", "", headers: new Dictionary<string, string>());
|
if (ResultData.Result != null)
|
{
|
content = JsonConvert.DeserializeObject<WebResponseContent>(ResultData.Result);
|
if (content != null && content.Status)
|
{
|
dt_Task.TargetAddress = content.Message;
|
dt_Task.NextAddress = dt_Task.TargetAddress;
|
_taskService.UpdateData(dt_Task);
|
_taskExecuteDetailService.AddTaskExecuteDetail(dt_Task.TaskNum, "更新移车任务目标地址");
|
}
|
}
|
#endregion
|
}
|
//穿梭车移车任务下发
|
if (!string.IsNullOrEmpty(dt_Task.SourceAddress) && !string.IsNullOrEmpty(dt_Task.TargetAddress) && !string.IsNullOrEmpty(dt_Task.ShuttleCarCode))
|
{
|
SendTask(dt_Task);
|
}
|
}
|
#endregion
|
|
#region 穿梭车任务
|
else
|
{
|
var task = _taskService.ShuttleCarRelocationTask(shuttleCar.DeviceCode);//获取穿梭车移库任务
|
if (task != null)
|
{
|
if (!task.SourceIsPickPlace)
|
SendTask(task);
|
}
|
else
|
{
|
task = _taskService.QueryShuttleCarTask(shuttleCar.DeviceCode);
|
if (task != null)
|
{
|
if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound && !task.SourceIsPickPlace)
|
{
|
var ShuttleCar = _shuttleCarService.QueryShuttleCar(task.SourceAddress);//出库任务判断起点是否有穿梭车
|
if (ShuttleCar != null) SendTask(task);
|
else _taskService.AddRelocationCarTask("", task.SourceAddress);
|
}
|
else
|
SendTask(task);
|
}
|
}
|
}
|
#endregion
|
}
|
}
|
#endregion
|
|
#region 更新移车任务起始地址
|
/// <summary>
|
/// 更新移车任务起始地址
|
/// </summary>
|
/// <param name="dt_Task"></param>
|
/// <param name="shuttleCar"></param>
|
/// <param name="command"></param>
|
public void UpdateSource(ref Dt_Task dt_Task, Dt_ShuttleCar _ShuttleCar)
|
{
|
dt_Task.SourceAddress = _ShuttleCar.ShuttleCarPosition;
|
dt_Task.CurrentAddress = dt_Task.SourceAddress;
|
dt_Task.ShuttleCarCode = _ShuttleCar.ShuttleCarCode;
|
_taskService.UpdateData(dt_Task);
|
_taskExecuteDetailService.AddTaskExecuteDetail(dt_Task.TaskNum, "更新移车任务起始地址");
|
}
|
#endregion
|
/// <summary>
|
/// 判断当前穿梭车是否可以执行移库
|
/// </summary>
|
/// <param name="shuttleCar"></param>
|
/// <param name="command"></param>
|
/// <returns></returns>
|
public (bool, Dt_ShuttleCar) GetSourceTask(ShuttleCar shuttleCar, ShuttleCarTaskCommandR command)
|
{
|
Dt_ShuttleCar _ShuttleCar = QueryCode(shuttleCar.DeviceCode);
|
bool ok = false;
|
if (command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal)
|
{
|
#region 判断当前穿梭车是否存在任务
|
Dt_Task? _Task = _taskRepository.QueryFirst(x =>
|
x.SourceAddress == _ShuttleCar.ShuttleCarPosition ||
|
x.TargetAddress == _ShuttleCar.ShuttleCarPosition ||
|
x.SourceAddress == _ShuttleCar.ShuttleCarPosition1 ||
|
x.TargetAddress == _ShuttleCar.ShuttleCarPosition1);
|
#endregion
|
ok = _Task == null;
|
}
|
if (!ok)
|
{
|
if (command.Status == (short)ShuttleCarStatus.Charging && command.ElectricQuantity >= 50)//退出充电
|
{
|
ok = shuttleCar.SetValue(ShuttleCarDBName.TaskType, (short)ShuttleCarStatus.QuitCharging);
|
}
|
}
|
return (ok, _ShuttleCar);
|
}
|
|
/// <summary>
|
/// 获取另外一个穿梭车信息
|
/// </summary>
|
public void GetShuttleCar(ref Dt_Task dt_Task, ShuttleCar shuttleCar)
|
{
|
Dt_ShuttleCar _ShuttleCar = _shuttleCarService.QueryNoCode(shuttleCar.DeviceCode);
|
ShuttleCar shuttleCar1 = Storage.Devices.Where(x => x.DeviceCode.Equals(_ShuttleCar.ShuttleCarCode)).FirstOrDefault() as ShuttleCar;
|
ShuttleCarTaskCommandR command = ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar1, shuttleCar1.DeviceCode);//读取穿梭车信息
|
(bool, Dt_ShuttleCar) result = GetSourceTask(shuttleCar1, command);
|
if (result.Item1) UpdateSource(ref dt_Task, result.Item2);
|
}
|
|
#region 下发穿梭车任务
|
/// <summary>
|
/// 下发穿梭车任务
|
/// </summary>
|
/// <param name="task"></param>
|
private void SendTask(Dt_Task task)
|
{
|
ShuttleCar shuttleCar = Storage.Devices.FirstOrDefault(x => x.DeviceCode == task.ShuttleCarCode) as ShuttleCar;
|
if (shuttleCar != null)
|
{
|
ShuttleCarTaskCommandR command = ReadCustomer<ShuttleCarTaskCommandR>(shuttleCar, shuttleCar.DeviceCode);//读取穿梭车信息
|
if (command != null && command.Status == (short)ShuttleCarStatus.Standby && command.TaskTypeComplete == (short)ShuttleCarTaskComplete.Standby && command.Err_Status == (short)ShuttleCarErr.Normal)
|
{
|
#region 判断小车位置无AB面不下发任务
|
short Position = 0;
|
if (shuttleCar.GetValue<ShuttleCarDBName, bool>(ShuttleCarDBName.DirectionA)) Position = 1;
|
if (shuttleCar.GetValue<ShuttleCarDBName, bool>(ShuttleCarDBName.DirectionB)) Position = 2;
|
if (Position != 1 && Position != 2)
|
{
|
var Message = $"{shuttleCar.DeviceName}无AB面";
|
if (task.ExceptionMessage != Message)
|
_taskService.UpdateData(task);
|
return;
|
}
|
#endregion
|
//任务转换
|
shuttleCar.SetValue(ShuttleCarDBName.Direction, Position);//写入车体方向
|
ShuttleCarTaskCommandW shuttleCarTaskCommand = new ShuttleCarTaskCommandW();
|
shuttleCarTaskCommand.Direction = Position;
|
shuttleCarTaskCommand.TaskNum = task.TaskNum;
|
if (task.TaskType == (int)TaskOtherTypeEnum.RelocationCar)//移车
|
{
|
if (task.TaskState == (int)TaskCarStatusEnum.CarNew)
|
{
|
//if (task.Remark == ShuttleCarTaskType.ExitCharge.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark);
|
//else
|
shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
|
}
|
else if (task.TaskState == (int)TaskCarStatusEnum.SC_CarFinish)
|
{
|
if (task.Remark == ShuttleCarTaskType.Charging.ToString()) shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.Remark);
|
//else
|
// shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
|
}
|
}
|
else if (task.TaskType == (int)TaskOtherTypeEnum.RelocationCarCharging)
|
{
|
shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(ShuttleCarTaskType.Charging.ToString());
|
}
|
else
|
shuttleCarTaskCommand.TaskType = (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction);
|
//shuttleCarTaskCommand.TaskType = string.IsNullOrEmpty(task.Remark) ? (short)GetCarTaskType(task.TaskType, shuttleCarTaskCommand.Direction) : (short)GetCarTaskType(task.Remark);
|
//任务下发
|
if (shuttleCarTaskCommand.TaskType != null)//0
|
{
|
if (shuttleCar.SendCommand(shuttleCarTaskCommand, shuttleCar.DeviceCode))
|
{
|
if (!string.IsNullOrEmpty(task.Remark)) task.Remark = task.Remark + "已下发";
|
task.TaskState = GetTaskState(task.TaskType);
|
_taskService.UpdateData(task);
|
}
|
}
|
}
|
}
|
}
|
#endregion
|
|
|
/// <summary>
|
/// 根据任务状态、类型获取任务
|
/// </summary>
|
/// <param name="TaskState"></param>
|
/// <param name="TaskType"></param>
|
/// <returns></returns>
|
private Dt_Task GetTask(int TaskState, int TaskType)
|
{
|
return _taskService.GetTaskState(TaskState, TaskType);
|
}
|
/// <summary>
|
/// 根据穿梭车编号获取穿梭车信息
|
/// </summary>
|
/// <param name="ShuttleCarCode"></param>
|
/// <returns></returns>
|
public Dt_ShuttleCar QueryCode(string ShuttleCarCode)
|
{
|
return _shuttleCarService.QueryCode(ShuttleCarCode);
|
}
|
|
ShuttleCarTaskType GetCarTaskType(string remark) => remark switch
|
{
|
"Charging" => ShuttleCarTaskType.Charging,
|
"ExitCharge" => ShuttleCarTaskType.ExitCharge,
|
};
|
|
ShuttleCarTaskType GetCarTaskType(int TaskType, short Direction)
|
{
|
ShuttleCarTaskType taskType = new ShuttleCarTaskType();
|
switch (TaskType)
|
{
|
case (int)TaskInboundTypeEnum.Inbound:
|
taskType = ShuttleCarTaskType.In;
|
break;
|
case (int)TaskOutboundTypeEnum.Outbound:
|
taskType = ShuttleCarTaskType.Out;
|
break;
|
case (int)TaskOtherTypeEnum.RelocationCar:
|
case (int)TaskOtherTypeEnum.RelocationCarCharging:
|
taskType = Direction == 1 ? ShuttleCarTaskType.ZeroA : ShuttleCarTaskType.ZeroB;
|
break;
|
default:
|
break;
|
}
|
return taskType;
|
}
|
/// <summary>
|
/// 获取任务状态
|
/// </summary>
|
/// <param name="TaskType"></param>
|
/// <returns></returns>
|
int GetTaskState(int TaskType)
|
{
|
int state = 0;
|
switch (TaskType)
|
{
|
case (int)TaskInboundTypeEnum.Inbound:
|
case (int)TaskInboundTypeEnum.PalletInbound:
|
state = (int)TaskInStatusEnum.Car_InExecuting;
|
break;
|
case (int)TaskOutboundTypeEnum.Outbound:
|
case (int)TaskOutboundTypeEnum.PalletOutbound:
|
state = (int)TaskOutStatusEnum.Car_OutExecuting;
|
break;
|
case (int)TaskOtherTypeEnum.RelocationCar:
|
state = (int)TaskCarStatusEnum.ShuttleCar_Executing;
|
break;
|
case (int)TaskOtherTypeEnum.RelocationCarCharging:
|
state = (int)CarChargingTaskEnum.ShuttleCar_Charging;
|
break;
|
default:
|
break;
|
}
|
return state;
|
}
|
/// <summary>
|
/// 读取穿梭车信息
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="shuttleCar"></param>
|
/// <param name="deviceChildCode"></param>
|
/// <returns></returns>
|
/// <exception cref="Exception"></exception>
|
public T ReadCustomer<T>(ShuttleCar shuttleCar, string deviceChildCode) where T : IDataTransfer, new()
|
{
|
string deviceChildCode2 = deviceChildCode;
|
if (!shuttleCar.IsConnected)
|
{
|
throw new Exception("通讯连接错误,请检查网络");
|
}
|
|
DeviceProDTO? deviceProDTO = (from x in shuttleCar.DeviceProDTOs
|
where x.DeviceProParamType == "ReadDeviceCommand" && x.DeviceChildCode == deviceChildCode2
|
orderby x.DeviceProOffset
|
select x).FirstOrDefault();
|
if (deviceProDTO == null)
|
{
|
throw new Exception("未找到协议信息");
|
}
|
|
return shuttleCar.Communicator.ReadCustomer<T>(deviceProDTO.DeviceProAddress);
|
}
|
}
|
}
|