NoInteractionSystems.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using PlcSiemens.Core.Extension;
  2. using ServiceCenter.Extensions;
  3. using ServiceCenter.Logs;
  4. using ServiceCenter.Redis;
  5. using ServiceCenter.SqlSugars;
  6. using System.ComponentModel;
  7. using WCS.Core;
  8. using WCS.Entity;
  9. using WCS.Entity.Protocol.Station;
  10. using WCS.WorkEngineering.Extensions;
  11. using WCS.WorkEngineering.Worlds;
  12. using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;
  13. using TaskStatus = WCS.Entity.TaskStatus;
  14. namespace WCS.WorkEngineering.Systems
  15. {
  16. /// <summary>
  17. /// 无交互系统
  18. /// </summary>
  19. [BelongTo(typeof(NoInteractionWorld))]
  20. [Description("无交互系统")]
  21. public class NoInteractionSystems : DeviceSystem<Device<IStation520>>
  22. {
  23. public NoInteractionSystems()
  24. {
  25. }
  26. protected override bool ParallelDo => true;
  27. protected override bool SaveLogsToFile => true;
  28. public override void Do(Device<IStation520> obj)
  29. {
  30. RedisHub.Do(obj.Entity.Code, redis =>
  31. {
  32. var taskInfos = new List<WCS_TaskInfo>();
  33. SqlSugarHelper.Do(db =>
  34. {
  35. //获取所有的新建任务,组盘任务不需要
  36. taskInfos = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.Status == Entity.TaskStatus.NewBuild && !(t.Type == TaskType.SetPlate && t.AddrFrom == "Robot")).ToList();
  37. });
  38. if (!taskInfos.Any()) return;
  39. foreach (var item in taskInfos)
  40. {
  41. try
  42. {
  43. SqlSugarHelper.Do(db =>
  44. {
  45. var task = db.Default.Queryable<WCS_TaskInfo>().Where(t => t.ID == item.ID).First() ?? throw new Exception($"未找到对应的WCS任务[{item.ID}]");
  46. switch (task.Type)
  47. {
  48. case TaskType.SetPlate:
  49. if (task.AddrFrom != "Robot")
  50. {
  51. //更新任务状态
  52. task.Status = TaskStatus.WaitingToExecute;
  53. db.Default.Updateable(task).ExecuteCommand();
  54. task.AddWCS_TASK_DTL(db.Default, task.Device, $"初始化入库任务信息");
  55. }
  56. break;
  57. case TaskType.EnterDepot:
  58. //更新任务状态
  59. task.Status = Entity.TaskStatus.WaitingToExecute;
  60. task.Device = task.WarehouseCode switch
  61. {
  62. "1N" => "SRM1",
  63. "1S" => "SRM2",
  64. "2N" => "SRM3",
  65. "2S" => "SRM4",
  66. "3N" => "SRM5",
  67. "3S" => "SRM6",
  68. _ => task.Device
  69. };
  70. task.Device = task.AddrFrom switch
  71. {
  72. "1666" or "1661" => "SRM1",
  73. "1681" or "1676" => "SRM2",
  74. "1696" or "1691" => "SRM3",
  75. "1711" or "1706" => "SRM4",
  76. "1726" or "1721" => "SRM5",
  77. "1741" or "1736" => "SRM6",
  78. _ => task.Device
  79. };
  80. //计算下一个地址
  81. var path1 = DevicePath.GetPath(task.AddrFrom, task.Device);
  82. task.AddrNext = path1.Points[1].Code;
  83. task.SrmStation = task.BarCode.Contains("TPA") || task.BarCode.Contains("TPB") ? task.AddrFrom : path1.Points[2].Code;
  84. db.Default.Updateable(task).ExecuteCommand();
  85. task.AddWCS_TASK_DTL(db.Default, task.Device, $"初始化入库任务信息");
  86. break;
  87. case TaskType.OutDepot:
  88. {
  89. if (task.Device.Contains("Robot"))
  90. {
  91. var pos = task.AddrFrom.Split("-");
  92. task.Status = Entity.TaskStatus.WaitingToExecute;
  93. task.Line = pos[0].ToShort();
  94. task.Col = pos[1].ToShort();
  95. task.Layer = pos[2].ToShort();
  96. task.Depth = pos[3].ToShort();
  97. db.Default.Updateable(task).ExecuteCommand();
  98. task.AddWCS_TASK_DTL(db.Default, task.Device, $"初始化出库任务信息,放货站台:{task.SrmStation}");
  99. }
  100. else
  101. {
  102. if (task.SrmStation == "1")
  103. {
  104. var srmStation = new string[2];
  105. switch (task.Device)
  106. {
  107. case "SRM1":
  108. srmStation[0] = "2534";
  109. srmStation[1] = "2533";
  110. break;
  111. case "SRM2":
  112. srmStation[0] = "2734";
  113. srmStation[1] = "2733";
  114. break;
  115. case "SRM3":
  116. srmStation[0] = "2934";
  117. srmStation[1] = "2933";
  118. break;
  119. case "SRM4":
  120. srmStation[0] = "3134";
  121. srmStation[1] = "3133";
  122. break;
  123. case "SRM5":
  124. srmStation[0] = "3334";
  125. srmStation[1] = "3333";
  126. break;
  127. case "SRM6":
  128. srmStation[0] = "3534";
  129. srmStation[1] = "3533";
  130. break;
  131. }
  132. //TODO:暂时不考虑入库卡控
  133. //开始计算当前这个任务要从哪个站台出
  134. //计算两个站台小于取货完成数量的AGV任务
  135. var agv1 = db.Default.Queryable<WCS_AgvTaskInfo>().SplitTable(x => x.Take(2)).Count(x => x.Status <= AGVTaskStatus.LeaveGet && x.Station == srmStation[0]);
  136. var agv2 = db.Default.Queryable<WCS_AgvTaskInfo>().SplitTable(x => x.Take(2)).Count(x => x.Status <= AGVTaskStatus.LeaveGet && x.Station == srmStation[1]);
  137. task.SrmStation = "";
  138. task.AddrTo = agv1 > agv2 ? srmStation[1] : srmStation[0];
  139. }
  140. if (task.SrmStation.IsNullOrEmpty()) //如果没有指定放货站台
  141. {
  142. if (task.Device.IsNullOrEmpty())
  143. {
  144. task.Device = "SRM" + task.Tunnel.GetLastDigit();
  145. }
  146. //获取堆垛机到目标地址的路径信息
  147. var path = DevicePath.GetPath(task.Device, task.AddrTo);
  148. task.SrmStation = path.Points[1].Code;
  149. }
  150. var devs = Device.All.Where(x => x.HasFlag(DeviceFlags.AGV取货站台口)).Select(x => x.Code);
  151. if (devs.Contains(task.SrmStation))
  152. {
  153. //开始处理车间叫料AGV任务任务
  154. db.Default.Insertable(new WCS_AgvTaskInfo()
  155. {
  156. ID = db.GetAgvTaskId(),
  157. TaskType = AGVTaskType.CallMaterial,
  158. Status = AGVTaskStatus.NewBuild,
  159. TaskId = task.ID,
  160. Position = task.WorkBench,
  161. Station = task.SrmStation,
  162. AddWho = "WCS",
  163. AddTime = DateTime.Now
  164. }).SplitTable().ExecuteCommand();
  165. db.Default.Insertable(new WCS_AgvTaskInfo()
  166. {
  167. ID = db.GetAgvTaskId(),
  168. TaskType = AGVTaskType.EnterDepot,
  169. Status = AGVTaskStatus.NewBuild,
  170. TaskId = task.ID,
  171. Position = task.WorkBench,
  172. Station = "2501",
  173. AddWho = "WCS",
  174. AddTime = DateTime.Now
  175. }).SplitTable().ExecuteCommand();
  176. }
  177. //更新任务状态
  178. task.Status = Entity.TaskStatus.WaitingToExecute;
  179. db.Default.Updateable(task).ExecuteCommand();
  180. task.AddWCS_TASK_DTL(db.Default, task.Device, $"初始化出库任务信息,放货站台:{task.SrmStation}");
  181. }
  182. break;
  183. }
  184. case TaskType.Delivery:
  185. break;
  186. case TaskType.EmptyInit:
  187. break;
  188. }
  189. });
  190. }
  191. catch (Exception ex)
  192. {
  193. World.Log(ex.Message, LogLevelEnum.Mid);
  194. }
  195. }
  196. });
  197. }
  198. public override bool Select(Device dev)
  199. {
  200. return dev.Code == nameof(NoInteractionSystems);
  201. }
  202. }
  203. }