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() .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(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(responseData.taskStatus, out TaskStatusEnum status); isUpdate = true; tasks[i].TaskStatus = status; //取消任务时 货架数据需要更新 if (status == TaskStatusEnum.取消完成) { var shelf = DbHelp.db.Queryable() .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(); /// /// 产生AGV点到点搬运任务 /// 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 positionCodePathItems = new List(); positionCodePathItems.Add(startPositionCodePathItem); positionCodePathItems.Add((endPositionCodePathItem)); //任务只允许一个一个发送 lock (lockFlag) { //正在执行中的任务 var tasks = DbHelp.db.Queryable() .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(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 positionCodePathItems = new List(); positionCodePathItems.Add(startPositionCodePathItem); positionCodePathItems.Add((endPositionCodePathItem)); //任务只允许一个一个发送 lock (lockFlag) { //正在执行中的任务 var tasks = DbHelp.db.Queryable() .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(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; } } /// /// RCS取消任务 /// /// /// 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(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}" }; } } /// /// RCS货架绑定 解绑 /// /// /// 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(url, body, "POST", true); //if (response.code == "0" && response.message == "成功") //{ //} return response; } } catch (Exception ex) { return new AGVResponseModel() { code = "-1", message = $"发生异常:{ex.Message}" }; } } } /// /// 解绑或者绑定 /// public enum BindPodAndBerthMethod { 解绑 = 0, 绑定 = 1, } }