Product_Wms/WcsMain/ApiServe/Service/AGVService/AGVService.cs
2024-11-25 10:46:38 +08:00

247 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using WcsMain.ApiClient.AGV.Dto;
using WcsMain.ApiServe.Dto.AGV;
using WcsMain.Business.CommonAction;
using WcsMain.Constant.Enum.Stacker;
using WcsMain.DataBase.Dao;
using WcsMain.DataBase.TableEntity;
using WcsMain.Constant.WcsAttribute.AutoFacAttribute;
using WcsMain.EquipOperation.StackerConvey;
using WcsMain.ApiClient.AGV;
namespace WcsMain.ApiServe.Service.AGVService;
[Service]
public class AGVService(AppWmsTaskDao wmsTaskDao, WCSTaskExecuteEvent taskExecuteEvent, StackerConveyOperation stackerConveyOperation, AGVWebApiAction webApiAction)
{
/// <summary>
/// AGV 任务回告
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public AGVResponseLayout<AGVResponseLayout<object>> TaskCallBack(Dto.AGV.AGVRequestLayout<AGVRequestBodyLayout<AGVNoVeBuckMoveRequest>> request)
{
var bodyData = request.Body;
if(bodyData == default)
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = "Body 未能正确解析" }
};
}
string? wmsTaskId = bodyData.Event?.RobotJobId; // 任务号
string? status = bodyData.Event?.State; // 状态
if(status == default || wmsTaskId == default)
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = "任务号和状态未能正确解析" }
};
}
/* 实际业务处理 */
// 找到此任务
List<AppWmsTask>? wmsTasks = wmsTaskDao.Select(new() { TaskId = wmsTaskId });
if(wmsTasks == default)
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = "数据连接异常" }
};
}
if (wmsTasks.Count < 1)
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = "该任务号不存在任务" }
};
}
AppWmsTask wmsTask = wmsTasks.First();
// 若是入库任务或者移库任务则在开始移动时上报WMS货物离开起始位置
if (status == "MOVE_BEGIN" && (wmsTask.TaskType == (int)WmsTaskTypeEnum.inTask || wmsTask.TaskType == (int)WmsTaskTypeEnum.agv))
{
// 上报WMS任务已经开始
string? errText = taskExecuteEvent.LeaveStart(wmsTask);
if (string.IsNullOrEmpty(errText))
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "SUCCESS", Success = true, Message = "操作成功" }
};
}
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = $"操作异常:{errText}" }
};
}
// 若是出库位置则在上报任务完成时上报WMS任务完成
if (status == "DONE" || status == "ABNORMAL_COMPLETED")
{
Task.Factory.StartNew(() =>
{
// 如果是完成并且是入库任务或者agv任务则给放货位置写放货完成
if (status == "DONE" && (wmsTask.TaskType == (int)WmsTaskTypeEnum.inTask || wmsTask.TaskType == (int)WmsTaskTypeEnum.agv))
{
string? setLocation = wmsTask.TaskType == (int)WmsTaskTypeEnum.inTask ? wmsTask.MidPoint : wmsTask.Destination;
string plcLocation = setLocation == null ? "" : setLocation.Trim().Split('_')[0];
string? plcErrtext = stackerConveyOperation.SetVehicelComplete(plcLocation ?? "");
ConsoleLog.Info($"AGV 放货完成通知PLC结果{(string.IsNullOrEmpty(plcErrtext) ? "" : plcErrtext)},站台:{plcLocation},箱号:{wmsTask.VehicleNo}");
}
});
if(wmsTask.TaskType == (int)WmsTaskTypeEnum.outTask || wmsTask.TaskType == (int)WmsTaskTypeEnum.agv) // 出库或移库
{
string? errText = taskExecuteEvent.CompleteTaskEvent(wmsTask, "AGV上报完成", wmsTask.Destination);
if (string.IsNullOrEmpty(errText))
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "SUCCESS", Success = true, Message = "操作成功" }
};
}
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = $"操作异常:{errText}" }
};
}
if(wmsTask.TaskType == (int)WmsTaskTypeEnum.inTask) // 入库
{
var updateResult = wmsTaskDao.Update(new() { TaskId = wmsTask.TaskId, TaskStatus = (int)WmsTaskStatusEnum.arriveMid, ModifyTime = DateTime.Now });
if(updateResult > 0)
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "SUCCESS", Success = true, Message = "操作成功" }
};
}
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = $"操作异常,数据更新失败" }
};
}
}
if(status == "CANCEL" || status == "ABNORMAL_CANCEL")
{
var updateResult = wmsTaskDao.Update(new() { TaskId = wmsTask.TaskId, TaskStatus = (int)WmsTaskStatusEnum.err, TaskMsg = status, ModifyTime = DateTime.Now });
if (updateResult > 0)
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "SUCCESS", Success = true, Message = "操作成功" }
};
}
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = $"操作异常数据更新失败" }
};
}
var updateAllResult = wmsTaskDao.Update(new() { TaskId = wmsTask.TaskId, TaskMsg = status, ModifyTime = DateTime.Now });
if (updateAllResult > 0)
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "SUCCESS", Success = true, Message = "操作成功" }
};
}
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = $"操作异常数据更新失败" }
};
}
/// <summary>
/// AGV请求动作
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public AGVResponseLayout<AGVResponseLayout<object>> RequestAction(Dto.AGV.AGVRequestLayout<AGVRequestBodyBaseLayout<AGVRequestActionRequest>> request)
{
string? agvCode = request.Body?.Event?.AgvCode;
string? location = request.Body?.Event?.Command;
if(string.IsNullOrWhiteSpace(agvCode)|| string.IsNullOrWhiteSpace(location))
{
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "ERROR", Success = false, Message = $"数据无法解析" }
};
}
// [TODO]
webApiAction.Send_AGV_Action(agvCode, "TRUE");
return new()
{
Header = new() { RequestId = request.Header?.RequestId, Version = request.Header?.version, TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
Body = new() { Code = "SUCCESS", Success = true, Message = "操作成功" }
};
}
public void test()
{
Dictionary<string, int> locationIndex = [];
/*--------------- 拣选站台 ----------------*/
locationIndex.TryAdd("WWYPYA", 0); // 1-1
locationIndex.TryAdd("wDj7k3", 1); // 1-2
locationIndex.TryAdd("5sNjZj", 2); // 1-3
locationIndex.TryAdd("x3hmTx", 3); // 1-4
locationIndex.TryAdd("TrkieR", 4); // 2-1
locationIndex.TryAdd("fED33G", 5); // 2-2
locationIndex.TryAdd("SAwJWb", 6); // 2-3
locationIndex.TryAdd("kSyBN2", 7); // 2-4
locationIndex.TryAdd("tZQJmY", 8); // 3-1
locationIndex.TryAdd("aHB3A4", 9); // 3-2
locationIndex.TryAdd("WAdrMA", 10);// 3-3
locationIndex.TryAdd("EdW8JH", 11);// 3-4
locationIndex.TryAdd("rK5RbX", 12);// 4-1
locationIndex.TryAdd("rThaME", 13);// 4-2
locationIndex.TryAdd("28PW7p", 14);// 4-3
locationIndex.TryAdd("rNH2Bb", 15);// 4-4
locationIndex.TryAdd("ZbkKHC", 16);// 5-1
locationIndex.TryAdd("YeSNEd", 17);// 5-2
locationIndex.TryAdd("QCECwK", 18);// 5-3
locationIndex.TryAdd("heYrie", 19);// 5-4
locationIndex.TryAdd("byBDnX", 20);// 6
/*----------------- 翻包区 -----------------*/
locationIndex.TryAdd("TQzNDN", 21);
locationIndex.TryAdd("SsNakz", 22);
locationIndex.TryAdd("TjtsJf", 23);
locationIndex.TryAdd("dM28J8", 24);
locationIndex.TryAdd("jAiFNS", 25);
locationIndex.TryAdd("AHQGKW", 26);
locationIndex.TryAdd("ACEMTS", 27);
locationIndex.TryAdd("6HbTKm", 28);
/*----------------- 出入库口 -----------------*/
locationIndex.TryAdd("FaaZHG", 29);
locationIndex.TryAdd("kzxhes", 30);
locationIndex.TryAdd("yDN8ZZ", 31);
locationIndex.TryAdd("6cDXzi", 32);
locationIndex.TryAdd("ranQyi", 33);
locationIndex.TryAdd("SxjX8e", 34);
locationIndex.TryAdd("jC6x3C", 35);
locationIndex.TryAdd("MSP3te", 36);
locationIndex.TryAdd("611", 29);
locationIndex.TryAdd("511", 30);
locationIndex.TryAdd("513", 31);
locationIndex.TryAdd("411", 32);
locationIndex.TryAdd("311", 33);
locationIndex.TryAdd("213", 34);
locationIndex.TryAdd("211", 35);
locationIndex.TryAdd("111", 36);
}
}