Files
wcs/WCS.BLL/Manager/AGVManager.cs
2025-02-23 15:59:26 +08:00

363 lines
16 KiB
C#

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.BLL.DbModels;
using WCS.BLL.Tool;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.AGV;
using WCS.Model.ApiModel.InOutRecord;
using WCS.Model.ApiModel.MXBackgroundThread;
namespace WCS.BLL.Manager
{
//AGV动作相关
public static class AGVManager
{
public static void InitBackgroundThread()
{
#region :
Task.Run(() =>
{
while (true)
{
//每5秒同步一次
Thread.Sleep(3000);
try
{
var url = @"http://192.168.18.150:8181/rcms/services/rest/hikRpcService/queryTaskStatus";
var tasks = DbHelp.db.Queryable<AgvTask>()
.Where(t => t.CreateTime > DateTime.Now.AddDays(-7)) //只查询7天内agv任务数据
.Where(t =>t.TaskStatus != TaskStatusEnum. && t.TaskStatus!= TaskStatusEnum.)
.OrderBy(t => t.Id)
.ToList();
if (tasks != null && tasks.Count > 0)
{
for (int i = 0; i < tasks.Count; i++)
{
try
{
//请求RCS获取任务状态
var data = new AGVQueryTaskStatusRequest();
data.taskCodes.Add(tasks[i].TaskCode);
var result = ApiHelp.GetDataFromHttp<AGVQueryTaskStatusResponse>(url, data, "POST", true);
if (result != null && result.code == "0" && result.data !=null && result.data.Count > 0)
{
var isUpdate = false;
//获取成功 是否与当前任务状态一致 如果不一致 进行更新
var responseData = result.data.First();
if (responseData.agvCode != tasks[i].AgvCode)
{
isUpdate = true;
tasks[i].AgvCode = responseData.agvCode;
}
if (responseData.taskStatus != tasks[i].TaskStatus.ToString())
{
Enum.TryParse<TaskStatusEnum>(responseData.taskStatus, out TaskStatusEnum status);
isUpdate = true;
tasks[i].TaskStatus = status;
//取消任务时 货架数据需要更新
if (status == TaskStatusEnum.)
{
var shelf = DbHelp.db.Queryable<ShelfInfo>()
.Where(t => t.ShelfCode == tasks[i].ShelfCode)
.Where(t => t.CurrentLocationId == tasks[i].StratLocationId && t.DestinationLocationId == tasks[i].EndLocationId)
.First();
if (shelf != null)
{
shelf.DestinationLocationId = 0;
shelf.DestinationLocaiotnCode = string.Empty;
shelf.TransStatus = TransStatusEnum.;
DbHelp.db.Updateable(shelf).ExecuteCommand();
}
}
}
if (isUpdate)
{
tasks[i].ModifyTime = DateTime.Now;
DbHelp.db.Updateable(tasks[i]).ExecuteCommand();
}
}
}
catch (Exception ex)
{
Logs.Write("【定时任务】获取任务状态异常:" + ex.Message);
}
Thread.Sleep(50);
}
}
}
catch (Exception ex)
{
Logs.Write("【定时任务】获取任务状态异常:" + ex.Message);
}
}
});
#endregion
}
public static object lockFlag = new object();
/// <summary>
/// 产生AGV点到点搬运任务
/// </summary>
public static AGVResponseModel GenAgvSchedulingTask(LocationInfo startLocation, LocationInfo endLocation, string shelfCode, string createUser)
{
var url = @"http://192.168.18.150:8181/rcms/services/rest/hikRpcService/genAgvSchedulingTask";
var startPositionCodePathItem = new PositionCodePathItem()
{
positionCode = startLocation.RcsStoreCode,
};
var endPositionCodePathItem = new PositionCodePathItem()
{
positionCode = endLocation.RcsStoreCode,
};
List<PositionCodePathItem> positionCodePathItems = new List<PositionCodePathItem>();
positionCodePathItems.Add(startPositionCodePathItem);
positionCodePathItems.Add((endPositionCodePathItem));
//任务只允许一个一个发送
lock (lockFlag)
{
//正在执行中的任务
var tasks = DbHelp.db.Queryable<AgvTask>()
.Where(t => t.TaskStatus == TaskStatusEnum. || t.TaskStatus == TaskStatusEnum.)
.ToList();
//校验货架
if (tasks.Where(t => t.ShelfCode == shelfCode).Any())
{
return new AGVResponseModel()
{
code = "-999",
message = $"货架[{shelfCode}]已有执行中的任务!",
};
}
//校验起点
if (tasks.Where(t => t.StratLocationId == startLocation.Id).Any())
{
return new AGVResponseModel()
{
code = "-999",
message = $"工位[{startLocation.LocationCode}]作为起点已有执行中的任务!",
};
}
//校验终点
if (tasks.Where(t => t.EndLocationId == endLocation.Id).Any())
{
return new AGVResponseModel()
{
code = "-999",
message = $"工位[{endLocation.LocationCode}]作为终点已有执行中的任务!",
};
}
var body = new GenAgvSchedulingTaskRequest()
{
positionCodePath = positionCodePathItems,
};
var response = ApiHelp.GetDataFromHttp<AGVResponseModel>(url, body, "POST", true);
if (response.code == "0" && response.message == "成功")
{
//生成任务数据
var task = new AgvTask()
{
ShelfCode = shelfCode,
RequestCode = body.reqCode,
TaskCode = body.taskCode,
TaskType = "GenAgvSchedulingTask",
StratLocationId = startLocation.Id,
StartLocationCode = startLocation.LocationCode,
EndLocationId = endLocation.Id,
EndLocationCode = endLocation.LocationCode,
CreateUser = createUser
};
DbHelp.db.Insertable(task).ExecuteCommand();
}
return response;
}
}
public static AGVResponseModel GenAgvSchedulingTaskForResend(LocationInfo startLocation, LocationInfo endLocation, AgvTask agvTask,string shelfCode, string createUser)
{
var url = @"http://192.168.18.150:8181/rcms/services/rest/hikRpcService/genAgvSchedulingTask";
var startPositionCodePathItem = new PositionCodePathItem()
{
positionCode = startLocation.RcsStoreCode,
};
var endPositionCodePathItem = new PositionCodePathItem()
{
positionCode = endLocation.RcsStoreCode,
};
List<PositionCodePathItem> positionCodePathItems = new List<PositionCodePathItem>();
positionCodePathItems.Add(startPositionCodePathItem);
positionCodePathItems.Add((endPositionCodePathItem));
//任务只允许一个一个发送
lock (lockFlag)
{
//正在执行中的任务
var tasks = DbHelp.db.Queryable<AgvTask>()
.Where(t => t.TaskStatus == TaskStatusEnum. || t.TaskStatus == TaskStatusEnum.)
.ToList();
//校验货架
if (tasks.Where(t => t.ShelfCode == shelfCode).Any())
{
return new AGVResponseModel()
{
code = "-999",
message = $"货架[{shelfCode}]已有执行中的任务!",
};
}
//校验起点
if (tasks.Where(t => t.StratLocationId == startLocation.Id).Any())
{
return new AGVResponseModel()
{
code = "-999",
message = $"工位[{startLocation.LocationCode}]作为起点已有执行中的任务!",
};
}
//校验终点
if (tasks.Where(t => t.EndLocationId == endLocation.Id).Any())
{
return new AGVResponseModel()
{
code = "-999",
message = $"工位[{endLocation.LocationCode}]作为终点已有执行中的任务!",
};
}
agvTask.TaskCode = agvTask.TaskCode + "_resend";
agvTask.CreateTime = DateTime.Now;
agvTask.CreateUser = createUser;
var body = new GenAgvSchedulingTaskRequest()
{
taskCode = agvTask.TaskCode,
positionCodePath = positionCodePathItems,
};
var response = ApiHelp.GetDataFromHttp<AGVResponseModel>(url, body, "POST", true);
if (response.code == "0" && response.message == "成功")
{
//DbHelp.db.Updateable(agvTask).ExecuteCommand();
//生成任务数据
var task = new AgvTask()
{
ShelfCode = shelfCode,
RequestCode = body.reqCode,
TaskCode = agvTask.TaskCode,
TaskType = "GenAgvSchedulingTask",
StratLocationId = startLocation.Id,
StartLocationCode = startLocation.LocationCode,
EndLocationId = endLocation.Id,
EndLocationCode = endLocation.LocationCode,
CreateUser = createUser
};
DbHelp.db.Insertable(task).ExecuteCommand();
}
return response;
}
}
/// <summary>
/// RCS取消任务
/// </summary>
/// <param name="agvTask"></param>
/// <returns></returns>
public static AGVResponseModel CancelTask(AgvTask agvTask)
{
try
{
var url = @"http://192.168.18.150:8181/rcms/services/rest/hikRpcService/cancelTask";
//任务只允许一个一个发送
lock (lockFlag)
{
var body = new AGVCancelTaskRequest()
{
taskCode = agvTask.TaskCode,
};
var response = ApiHelp.GetDataFromHttp<AGVResponseModel>(url, body, "POST", true);
if (response.code == "0" && response.message == "成功")
{
//取消会统一在后台线程更新
//agvTask.TaskStatus = Model.ApiModel.AGV.TaskStatusEnum.取消完成;
//DbHelp.db.Updateable(agvTask).ExecuteCommand();
}
return response;
}
}
catch (Exception ex)
{
return new AGVResponseModel()
{
code = "-1",
message = $"发生异常:{ex.Message}"
};
}
}
/// <summary>
/// RCS货架绑定 解绑
/// </summary>
/// <param name="agvTask"></param>
/// <returns></returns>
public static AGVResponseModel BindPodAndBerth(string shelfCode,string locationCode , BindPodAndBerthMethod bindPodAndBerthMethod)
{
try
{
//货架绑定解绑地址
var url = @"http://192.168.18.150:8181/rcms/services/rest/hikRpcService/bindPodAndBerth";
//任务只允许一个一个发送
lock (lockFlag)
{
var body = new AGVBindPodAndBerthRequest()
{
podCode = shelfCode,
positionCode = locationCode,
pointCode = locationCode,
indBind = ((int)bindPodAndBerthMethod).ToString(),
};
var response = ApiHelp.GetDataFromHttp<AGVResponseModel>(url, body, "POST", true);
//if (response.code == "0" && response.message == "成功")
//{
//}
return response;
}
}
catch (Exception ex)
{
return new AGVResponseModel()
{
code = "-1",
message = $"发生异常:{ex.Message}"
};
}
}
}
/// <summary>
/// 解绑或者绑定
/// </summary>
public enum BindPodAndBerthMethod
{
= 0,
= 1,
}
}