AgvApi.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using ServiceCenter.Extensions;
  2. using ServiceCenter.Logs;
  3. using ServiceCenter.Redis;
  4. using ServiceCenter.WebApi;
  5. using WCS.WorkEngineering.WebApi.Models.AGV;
  6. using WCS.WorkEngineering.WebApi.Models.AGV.Request;
  7. using WCS.WorkEngineering.WebApi.Models.AGV.Response;
  8. using WCS.WorkEngineering.WebApi.Models.WCS.Response;
  9. using WCS.WorkEngineering.WebApi.Models.WMS.Response;
  10. namespace WCS.WorkEngineering.WebApi.Controllers
  11. {
  12. /// <summary>
  13. /// AGV接口
  14. /// </summary>
  15. public static class AgvApi
  16. {
  17. private static string _AgvUrl = null!;
  18. /// <summary>
  19. /// AGV地址
  20. /// </summary>
  21. public static string AgvUrl
  22. {
  23. get
  24. {
  25. _AgvUrl ??= RedisHub.Default.Check("AgvUrl")!;
  26. if (string.IsNullOrEmpty(_AgvUrl))
  27. {
  28. throw new KnownException($"请在Redis配置AgvUrl", LogLevelEnum.High);
  29. }
  30. return _AgvUrl;
  31. }
  32. }
  33. #region 任务单生成
  34. /// <summary>
  35. /// 测试路径
  36. /// </summary>
  37. /// <param name="position">取货机台</param>
  38. /// <param name="taskCode">WMS任务号</param>
  39. /// <returns></returns>
  40. public static GenAgvSchedulingTaskResponse 测试路径(string position, string taskCode)
  41. {
  42. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  43. {
  44. new positionCodeClass(){ //取货机台
  45. positionCode=position,
  46. type="00"
  47. },
  48. new positionCodeClass(){ //巷道分配点
  49. positionCode="LX002",
  50. type="00"
  51. },
  52. new positionCodeClass(){ //预分配放货点
  53. positionCode="2501",
  54. type="00"
  55. }
  56. }, "1", taskCode, "LX03", "1");
  57. }
  58. /// <summary>
  59. /// 机台叫料
  60. /// </summary>
  61. /// <param name="SPosition">起始点</param>
  62. /// <param name="EPosition">目标点</param>
  63. /// <param name="taskCode">WMS任务号</param>
  64. /// <returns></returns>
  65. public static GenAgvSchedulingTaskResponse 机台叫料(string SPosition, string EPosition, string taskCode)
  66. {
  67. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  68. {
  69. new positionCodeClass(){ //取货机台
  70. positionCode=SPosition,
  71. type="00"
  72. },
  73. new positionCodeClass(){ //巷道分配点
  74. positionCode=EPosition,
  75. type="00"
  76. }
  77. }, "1", taskCode, "ZTGT35", "1");
  78. }
  79. /// <summary>
  80. /// 托盘回库
  81. /// </summary>
  82. /// <param name="position">取货机台</param>
  83. /// <param name="taskCode">WMS任务号</param>
  84. /// <param name="station">站台</param>
  85. /// <returns></returns>
  86. public static GenAgvSchedulingTaskResponse 托盘回库(string position, string taskCode, string station)
  87. {
  88. var position1 = "";
  89. var position2 = "";
  90. switch (station)
  91. {
  92. case "2501": //分拣一北
  93. position1 = "LX002";
  94. position2 = "2501";
  95. break;
  96. case "2701": //分拣一南
  97. position1 = "LX004";
  98. position2 = "2701";
  99. break;
  100. case "2901": //分拣二北
  101. position1 = "LX006";
  102. position2 = "2901";
  103. break;
  104. case "3101": //分拣二南
  105. position1 = "LX008";
  106. position2 = "3101";
  107. break;
  108. case "3301": //分拣三北
  109. position1 = "LX010";
  110. position2 = "3301";
  111. break;
  112. case "3501": //分拣三南
  113. position1 = "LX012";
  114. position2 = "3501";
  115. break;
  116. }
  117. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  118. {
  119. new positionCodeClass(){ //取货机台
  120. positionCode=position,
  121. type="00"
  122. },
  123. new positionCodeClass(){ //预分配放货点
  124. positionCode=position1,
  125. type="00"
  126. },
  127. new positionCodeClass(){ //放货点
  128. positionCode=position2,
  129. type="00"
  130. }
  131. }, "1", taskCode, "ZTGT31", "1");
  132. }
  133. /// <summary>
  134. /// 组盘入库
  135. /// </summary>
  136. /// <param name="SPosition">起始点</param>
  137. /// <param name="EPosition">目标点</param>
  138. /// <param name="taskCode">WMS任务号</param>
  139. /// <returns></returns>
  140. public static GenAgvSchedulingTaskResponse 组盘入库(string SPosition, string EPosition, string taskCode)
  141. {
  142. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  143. {
  144. new positionCodeClass(){ //取货位置
  145. positionCode=SPosition,
  146. type="00"
  147. },
  148. new positionCodeClass(){ //放货位置
  149. positionCode=EPosition,
  150. type="00"
  151. }
  152. }, "1", taskCode, "ZTGT67", "1");
  153. }
  154. /// <summary>
  155. /// 重绕叫料
  156. /// </summary>
  157. /// <param name="SPosition">起始点</param>
  158. /// <param name="EPosition">目标点</param>
  159. /// <param name="taskCode">WMS任务号</param>
  160. /// <returns></returns>
  161. public static GenAgvSchedulingTaskResponse 重绕叫料(string SPosition, string EPosition, string taskCode)
  162. {
  163. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  164. {
  165. new positionCodeClass(){ //取货位置
  166. positionCode=SPosition,
  167. type="00"
  168. },
  169. new positionCodeClass(){ //放货位置
  170. positionCode=EPosition,
  171. type="00"
  172. }
  173. }, "1", taskCode, "ZTGT67", "1");
  174. }
  175. /// <summary>
  176. /// 退料搬运
  177. /// </summary>
  178. /// <param name="SPosition">起始点</param>
  179. /// <param name="EPosition">目标点</param>
  180. /// <param name="taskCode">WMS任务号</param>
  181. /// <returns></returns>
  182. public static GenAgvSchedulingTaskResponse 退料搬运(string SPosition, string EPosition, string taskCode)
  183. {
  184. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  185. {
  186. new positionCodeClass(){ //取货位置
  187. positionCode=SPosition,
  188. type="00"
  189. },
  190. new positionCodeClass(){ //放货位置
  191. positionCode=EPosition,
  192. type="00"
  193. }
  194. }, "1", taskCode, "ZTGT67", "1");
  195. }
  196. /// <summary>
  197. /// 退料重绕
  198. /// </summary>
  199. /// <param name="position">取货机台</param>
  200. /// <param name="taskCode">WMS任务号</param>
  201. /// <param name="station">站台</param>
  202. /// <returns></returns>
  203. public static GenAgvSchedulingTaskResponse 退料重绕(string position, string taskCode, string station)
  204. {
  205. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  206. {
  207. new positionCodeClass(){ //取货机台
  208. positionCode=position,
  209. type="00"
  210. },
  211. new positionCodeClass(){ //巷道分配点
  212. positionCode="9999",
  213. type="00"
  214. },
  215. new positionCodeClass(){ //预分配放货点
  216. positionCode=station,
  217. type="00"
  218. }
  219. }, "1", taskCode, "ZTGT70", "1");
  220. }
  221. /// <summary>
  222. /// 满托入库
  223. /// </summary>
  224. /// <param name="startPosition">取货站台</param>
  225. /// <param name="taskCode">WMS任务号</param>
  226. /// <param name="station">放货站台</param>
  227. /// <returns></returns>
  228. public static GenAgvSchedulingTaskResponse 满托入库(string startPosition, string taskCode, string station)
  229. {
  230. var position = "";
  231. var position1 = "";
  232. position = startPosition;
  233. position1 = station;
  234. return GenAgvSchedulingTask("iwms_third", "", "4", new List<positionCodeClass>()
  235. {
  236. new positionCodeClass(){ //取货机台
  237. positionCode=position,
  238. type="00"
  239. },
  240. new positionCodeClass(){ //预分配放货点
  241. positionCode=position1,
  242. type="00"
  243. }
  244. }, "1", taskCode, "ZTGT69", "1");
  245. }
  246. /// <summary>
  247. /// Agv任务单生成接口
  248. /// </summary>
  249. /// <param name="clienCode">客户端编号</param>
  250. /// <param name="ctnrCode">容器编号</param>
  251. /// <param name="ctnrTyp">容器类型</param>
  252. /// <param name="positionCodePath">路径</param>
  253. /// <param name="priority">优先级</param>
  254. /// <param name="taskCode">任务单号</param>
  255. /// <param name="taskType">任务类型</param>
  256. /// <param name="hjTaskTy">合金任务模板</param>
  257. /// <returns></returns>
  258. /// <exception cref="KnownException"></exception>
  259. public static GenAgvSchedulingTaskResponse GenAgvSchedulingTask(string clienCode, string ctnrCode, string ctnrTyp, List<positionCodeClass> positionCodePath, string priority, string taskCode, string taskType, string hjTaskTy)
  260. {
  261. var res = APICaller.CallApi2<GenAgvSchedulingTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/genAgvSchedulingTask", new GenAgvSchedulingTaskRequest
  262. {
  263. clientCode = clienCode,
  264. ctnrCode = ctnrCode,
  265. ctnrTyp = ctnrTyp,
  266. interfaceName = "genAgvSchedulingTask",
  267. positionCodePath = positionCodePath,
  268. priority = priority,
  269. reqCode = Guid.NewGuid().ToString().Replace("-", ""),
  270. taskCode = taskCode,
  271. taskTyp = taskType,
  272. hjTaskTy = hjTaskTy,
  273. tokenCode = "56898661ea976b748f328cefa6960434",
  274. });
  275. if (res.code != AgvResponseCode.Success)
  276. {
  277. if (res.message.Contains("未知的呼叫站点"))
  278. {
  279. if (taskType == "ZTGT35")
  280. {
  281. res.message = res.message + positionCodePath[1].positionCode;
  282. }
  283. else if (taskType == "ZTGT31")
  284. {
  285. res.message = res.message + positionCodePath[0].positionCode;
  286. }
  287. }
  288. throw new KnownException($"AGV返回错误:{res.message}", LogLevelEnum.High);
  289. }
  290. return res;
  291. }
  292. #endregion 任务单生成
  293. #region 继续执行任务
  294. /// <summary>
  295. /// 继续执行任务
  296. /// </summary>
  297. /// <param name="taskCode">AGV任务号</param>
  298. /// <param name="nextPositionCode">下一个地址</param>
  299. /// <returns>接口返回结果</returns>
  300. /// <exception cref="KnownException"></exception>
  301. public static ContinueTaskResponse ContinueTask(string taskCode, string nextPositionCode)
  302. {
  303. var res = APICaller.CallApi2<ContinueTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/continueTask", new ContinueTaskRequest
  304. {
  305. reqCode = Guid.NewGuid().ToString().Replace("-", ""),
  306. taskCode = taskCode,
  307. nextPositionCode = new positionCodeClass()
  308. {
  309. positionCode = nextPositionCode,
  310. type = "00"
  311. }
  312. });
  313. if (res.message.Contains("状态出错"))
  314. {
  315. res.message = "任务" + taskCode + res.message + "请检查对应是否有误";
  316. }
  317. if (res.code != AgvResponseCode.Success)
  318. {
  319. throw new KnownException(res.message, LogLevelEnum.High);
  320. }
  321. return res;
  322. }
  323. #endregion 继续执行任务
  324. /// <summary>
  325. /// 取消任务
  326. /// </summary>
  327. /// <param name="AGVtaskCode">AGV任务号</param>
  328. /// <returns></returns>
  329. /// <exception cref="KnownException"></exception>
  330. public static CancelTaskResponse CancelAgvTask(string AGVtaskCode)
  331. {
  332. var res = APICaller.CallApi2<CancelTaskResponse>(AgvUrl + "/rcms/services/rest/hikRpcService/cancelTask", new CancelTaskRequest
  333. {
  334. reqCode = Guid.NewGuid().ToString().Replace("-", ""),
  335. taskCode = AGVtaskCode,
  336. forceCancel = "0",
  337. });
  338. if (res.code != AgvResponseCode.Success)
  339. {
  340. throw new KnownException(res.message, LogLevelEnum.High);
  341. }
  342. return res;
  343. }
  344. /// <summary>
  345. /// AGV取消任务验证
  346. /// </summary>
  347. /// <param name="sRes"></param>
  348. /// <param name="id"></param>
  349. /// <param name="agvTask">需要取消任务的AGV任务号</param>
  350. /// <returns></returns>
  351. public static CancelTaskResponse? CancelAgvTask(SRes<HandleTaskResponse> sRes, int id, string agvTask)
  352. {
  353. try
  354. {
  355. var res = AgvApi.CancelAgvTask(agvTask);
  356. return res;
  357. }
  358. catch (Exception ex)
  359. {
  360. sRes.ResDataList.Add(new HandleTaskResponse()
  361. {
  362. IsSuccess = false,
  363. TaskNo = id,
  364. Message = $"AGV错误:{ex.Message}",
  365. });
  366. return null;
  367. }
  368. }
  369. }
  370. }