WMS.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using Log;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using WCS.BaseExtensions;
  7. using WCS.Core;
  8. using WCS.Service.Entity;
  9. namespace WCS.Service
  10. {
  11. public class WMS
  12. {
  13. private const string Url = "http://192.168.249.150:8026";
  14. //private static string Url = "http://127.0.0.1:8026";
  15. private const string WareHouseId = "opphouse";
  16. /// <summary>
  17. /// 向WMS获取入库任务 一次单卷
  18. /// </summary>
  19. /// <param name="barcode">产品条码</param>
  20. /// <param name="devCode">设备条码</param>
  21. /// <param name="getTunnel"></param>
  22. /// <returns></returns>
  23. /// <exception cref="Exception"></exception>
  24. public static List<I_WCS_GetInTaskResponseItem> I_WCS_GetInTask(string barcode, string devCode, bool getTunnel = false)
  25. {
  26. var data = JsonConvert.SerializeObject(new List<I_WCS_GetInTaskRequest>()
  27. {
  28. new I_WCS_GetInTaskRequest(){
  29. ContainerBarCode = barcode,
  30. WareHouseId = WareHouseId,
  31. EquipmentNo = devCode,
  32. Memo1 = getTunnel ? "1" : "" //1:分巷道 2:分货位
  33. }
  34. });
  35. var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  36. {
  37. Name = "I_WCS_GetInTask",
  38. Parameters = data,
  39. ParametersMD5 = data.GetMD5()
  40. });
  41. if (res.ResType) return res.TaskList;
  42. TaskException(devCode, res.ResMessage);
  43. throw new WarnException(res.ResMessage);
  44. }
  45. /// <summary>
  46. /// 向WMS获取入库任务 一次双卷
  47. /// </summary>
  48. /// <param name="barcode1">产品1条码</param>
  49. /// <param name="devCode1">设备1编号</param>
  50. /// <param name="getTunnel1">产品1是否直接分配巷道</param>
  51. /// <param name="barcode2">产品2条码</param>
  52. /// <param name="devCode2">设备2编号</param>
  53. /// <param name="getTunnel2">产品2是否直接分配巷道</param>
  54. /// <exception cref="WarnException"></exception>
  55. /// <returns></returns>
  56. public static List<I_WCS_GetInTaskResponseItem> I_WCS_GetInTask(string barcode1, string devCode1, string barcode2, string devCode2, bool getTunnel1 = false, bool getTunnel2 = false)
  57. {
  58. if (!Configs.Any(SystemMode.虚拟PLC))
  59. {
  60. var data = JsonConvert.SerializeObject(new List<I_WCS_GetInTaskRequest>()
  61. {
  62. new(){
  63. ContainerBarCode = barcode1,
  64. WareHouseId = WareHouseId,
  65. EquipmentNo = devCode1,
  66. Memo1 = getTunnel1 ? "1" : "" //1:分巷道 2:分货位
  67. },
  68. new(){
  69. ContainerBarCode = barcode2,
  70. WareHouseId = WareHouseId,
  71. EquipmentNo = devCode2,
  72. Memo1 = getTunnel2 ? "1" : "" //1:分巷道 2:分货位
  73. },
  74. });
  75. var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  76. {
  77. Name = "I_WCS_GetInTask",
  78. Parameters = data,
  79. ParametersMD5 = data.GetMD5()
  80. });
  81. if (res.ResType) return res.TaskList;
  82. TaskException($"G{devCode1}", res.ResMessage);
  83. throw new WarnException(res.ResMessage);
  84. }
  85. return new List<I_WCS_GetInTaskResponseItem>()
  86. {
  87. new I_WCS_GetInTaskResponseItem(){
  88. WMSTaskNum="315199",
  89. TaskType=1,
  90. WareHouseName="OPP立库",
  91. TunnelNum="",
  92. EndPostion="srm",
  93. ContainerCode=barcode1,
  94. ContainerType=0
  95. },
  96. new I_WCS_GetInTaskResponseItem() {
  97. WMSTaskNum="315200",
  98. TaskType=1,
  99. WareHouseName="OPP立库",
  100. TunnelNum="",
  101. EndPostion="srm",
  102. ContainerCode=barcode2,
  103. ContainerType=0
  104. },
  105. };
  106. }
  107. /// <summary>
  108. /// 分配货位
  109. /// </summary>
  110. /// <param name="wmstaskid">WMS任务ID</param>
  111. /// <param name="tunnel">巷道</param>
  112. /// <param name="device">设备号</param>
  113. /// <param name="ForkNum">申请任务对应的货叉</param>
  114. /// <exception cref="WarnException"></exception>
  115. /// <returns></returns>
  116. public static I_WCS_GetWareCellResponse GetLocalIn(int wmstaskid, string tunnel, string device, WareCellForkNum ForkNum)
  117. {
  118. var data = JsonConvert.SerializeObject(new I_WCS_GetWareCellRequest
  119. {
  120. PickUpEquipmentNo = device,
  121. TunnelNum = tunnel.Last().ToString(),
  122. WMSTaskNum = wmstaskid.ToString(),
  123. ForkNum = ForkNum
  124. });
  125. var res = APICaller.CallApi<I_WCS_GetWareCellResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  126. {
  127. Name = "I_WCS_GetWareCell",
  128. Parameters = data,
  129. ParametersMD5 = data.GetMD5()
  130. });
  131. if (res.ResType) return res;
  132. TaskException(device, res.ResMessage);
  133. throw new WarnException(res.ResMessage);
  134. }
  135. public static void UpdateTask(string POSIDNEXT, int wmstaskid, int status, int size = -1)
  136. {
  137. if (Configs.Any(SystemMode.虚拟PLC)) return;
  138. var data = JsonConvert.SerializeObject(new List<I_WCS_TASKRequest>
  139. {
  140. new()
  141. {
  142. TASK_WKSTATUS = status,
  143. TASK_WMSNO = wmstaskid.ToString(),
  144. TASK_POSIDNEXT=POSIDNEXT,
  145. Size=size
  146. }
  147. });
  148. var res = APICaller.CallApi<List<I_WCS_PutTaskStepResponse>>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  149. {
  150. Name = "I_WCS_PutTaskStep",
  151. Parameters = data,
  152. ParametersMD5 = data.GetMD5()
  153. });
  154. if (res == null || res.Count == 0)
  155. {
  156. throw new WarnException("I_WCS_PutTaskStep调用失败");
  157. }
  158. if (!res.First().ResType)
  159. throw new WarnException(res.First().ResMessage);
  160. }
  161. public static GetProductInfoResponse GetProductInfo(string barcode, string dev)
  162. {
  163. var data = JsonConvert.SerializeObject(new GetProductInfoRequest
  164. {
  165. BarCode = barcode
  166. });
  167. var res = APICaller.CallApi<GetProductInfoResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  168. {
  169. Name = "GetProductInfo",
  170. Parameters = data,
  171. ParametersMD5 = data.GetMD5()
  172. });
  173. try
  174. {
  175. if (!res.ResType)
  176. throw new WarnException(res.ResMessage);
  177. if (res.ChildContainerType is <= 0 or > 9)
  178. throw new WarnException("托盘类型错误");
  179. if (res.LoadCount <= 0)
  180. throw new WarnException("最大组盘数量错误");
  181. if (res.DocId == 0)
  182. throw new WarnException("单据ID错误");
  183. if (res.ProLine is < 1 or > 9)
  184. throw new WarnException("产线编号" + res.ProLine + "错误");
  185. }
  186. catch (Exception ex)
  187. {
  188. TaskException(dev, ex.Message);
  189. throw;
  190. }
  191. return res;
  192. }
  193. /// <summary>
  194. /// 获取出库任务
  195. /// </summary>
  196. /// <param name="position">出库位置</param>
  197. /// <param name="devCode">调用方法的设备号</param>
  198. /// <returns></returns>
  199. /// <exception cref="Exception"></exception>
  200. public static List<I_WCS_GetOutTaskResponseSingle> GetOutTask(string position, string devCode)
  201. {
  202. var data = JsonConvert.SerializeObject(new I_WCS_GetOutTaskRequest
  203. {
  204. OutType = 1,
  205. WareHouseId = WareHouseId,
  206. OutEndPostion = position
  207. });
  208. var res = APICaller.CallApi<I_WCS_GetOutTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  209. {
  210. Name = "I_WCS_GetOutTask",
  211. Parameters = data,
  212. ParametersMD5 = data.GetMD5()
  213. });
  214. if (!res.ResType)
  215. {
  216. TaskException(position, res.ResMessage);
  217. throw new WarnException(res.ResMessage);
  218. }
  219. res.WMSTasks.ForEach(v =>
  220. {
  221. var tcode = "TY" + v.TunnelNum;
  222. var tunnel = Device.Find(tcode);
  223. //var next = tunnel.GetPath(devCode);
  224. //v.Memo1 = next.CODE;
  225. v.TunnelNum = tunnel.CODE;
  226. });
  227. return res.WMSTasks;
  228. }
  229. public static WcsContractApiResponse AutoBuildUpGroupStock(string containerCode, string barcode, string dev)
  230. {
  231. var res = APICaller.CallApi<WcsContractApiResponse>(Url + "/api/Task/AutoBuildUpGroupStock", new
  232. {
  233. ContainerCode = containerCode,
  234. BarCode = barcode
  235. });
  236. if (res.ResType) return res;
  237. TaskException(dev, res.ResMessage);
  238. throw new WarnException(res.ResMessage);
  239. }
  240. /// <summary>
  241. /// 获取巷道
  242. /// </summary>
  243. /// <param name="WMSTaskId">WMS任务号集合</param>
  244. /// <param name="dev">请求设备号</param>
  245. /// <returns></returns>
  246. /// <exception cref="Exception"></exception>
  247. public static I_WCS_GetTunnelListResponse GetTunnelList(List<string> WMSTaskId, string dev)
  248. {
  249. var data = JsonConvert.SerializeObject(new I_WCS_GetTunnelListRequest
  250. {
  251. WMSTaskNum = WMSTaskId
  252. });
  253. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  254. {
  255. Name = "I_WCS_GetTunnelList",
  256. Parameters = data,
  257. ParametersMD5 = data.GetMD5()
  258. });
  259. if (res.ResType) return res;
  260. TaskException(dev, res.ResMessage);
  261. throw new WarnException(res.ResMessage);
  262. }
  263. public static void TaskException(string device, string exMsg)
  264. {
  265. if (exMsg == "接口调用中")
  266. return;
  267. System.Threading.Tasks.Task.Run(() =>
  268. {
  269. try
  270. {
  271. var data = JsonConvert.SerializeObject(new I_WCS_GetExcTaskRequest
  272. {
  273. ExcCode = exMsg,
  274. EquipmentNo = device,
  275. ExcMessage = exMsg,
  276. });
  277. Console.WriteLine(device + ":" + exMsg);
  278. var res = APICaller.CallApi2<I_WCS_GetExcTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  279. {
  280. Name = "I_WCS_GetExcTask",
  281. Parameters = data,
  282. ParametersMD5 = data.GetMD5()
  283. });
  284. InfoLog.INFO_I_WCS_GetExcTask(device + ":" + exMsg);
  285. if (!res.ResType) throw new WarnException(res.ResMessage);
  286. }
  287. catch (Exception)
  288. {
  289. }
  290. });
  291. }
  292. private static List<I_WCS_PutDevInfoRequest> DevInfoList = new();
  293. public static void DevInfo(string device, string exMsg)
  294. {
  295. DevInfoList.Add(new I_WCS_PutDevInfoRequest
  296. {
  297. STA_EQUIPMENTNO = device,
  298. STA_ALARMSMSG = exMsg
  299. });
  300. }
  301. public static void UploadDevInfo()
  302. {
  303. return;
  304. try
  305. {
  306. var data = JsonConvert.SerializeObject(DevInfoList);
  307. var res = APICaller.CallApi<I_WCS_PutDevInfoResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  308. {
  309. Name = "I_WCS_GetExcTask",
  310. Parameters = data,
  311. ParametersMD5 = data.GetMD5()
  312. });
  313. if (!res.ResType)
  314. throw new WarnException(res.ResMessage);
  315. }
  316. catch (Exception ex)
  317. {
  318. InfoLog.INFO_WARN($"I_WCS_PutDevInfo接口调用失败:{ex.Message}");
  319. }
  320. finally
  321. {
  322. DevInfoList.Clear();
  323. }
  324. }
  325. public static void UnBound(string barcode)
  326. {
  327. try
  328. {
  329. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/PDA/UnboundMwGroupStock", new
  330. {
  331. containerCode = barcode
  332. });
  333. }
  334. catch { }
  335. }
  336. }
  337. /// <summary>
  338. /// 调用接口传入参数
  339. /// </summary>
  340. public class InterfaceParameters
  341. {
  342. /// <summary>
  343. /// 业务名 例:I_WCS_GetInTask
  344. /// </summary>
  345. public string Name { get; set; }
  346. /// <summary>
  347. /// 传入参数为Json
  348. /// </summary>
  349. public string Parameters { get; set; }
  350. /// <summary>
  351. /// 传入参数的唯一值
  352. /// </summary>
  353. public string ParametersMD5 { get; set; }
  354. }
  355. }