WMS.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using DBHelper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using WCS.Core;
  7. using WCS.Entity.Protocol;
  8. using WCS.Service.Entity;
  9. using WCS.Service.Helpers;
  10. namespace WCS.Service
  11. {
  12. public class WMS
  13. {
  14. private static string Url = "http://192.168.249.120:8016";
  15. public static I_WCS_GetInTaskResponse InBound(string barcode, string devCode, bool getTunnel = false)
  16. {
  17. var warehouse = "mwhouseputong";
  18. if (devCode.StartsWith("MW_") || devCode == "2088" || devCode == "3001")
  19. warehouse = "mwhouseyuanzhi";
  20. var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(Url + "/api/Task/I_WCS_GetInTask", new I_WCS_GetInTaskRequest
  21. {
  22. ContainerBarCode = barcode,
  23. WareHouseId = warehouse,
  24. EquipmentNo = devCode,
  25. Memo1 = getTunnel ? "1" : "" //1:分巷道 2:分货位
  26. });
  27. if (!res.ResType)
  28. {
  29. TaskException(devCode, res.ResMessage);
  30. throw new Exception(res.ResMessage);
  31. }
  32. return res;
  33. }
  34. public static I_WCS_GetInTaskResponse PalletInbound(DB db, string barcode, string dev)
  35. {
  36. var res = APICaller.CallApi<Result>(Url + "/api/PDA/PVCSemiFinishedProductContainerReBack", new PVCSemiFinishedProductReBackRequest
  37. {
  38. BarCode = barcode,
  39. ContainerCode = barcode,
  40. Quantity = 1
  41. });
  42. if (res.StatusCode != Result.Success && res.StatusCode != 4004)
  43. {
  44. TaskException(dev, res.Message);
  45. throw new Exception(res.Message);
  46. }
  47. return InBound(barcode, dev);
  48. }
  49. public static short GetPalletType(string rollCode)
  50. {
  51. return 1;
  52. }
  53. public static I_WCS_GetWareCellResponse GetLocalIn(int wmstaskid, string tunnel, string device)
  54. {
  55. var res = APICaller.CallApi<I_WCS_GetWareCellResponse>(Url + "/api/Task/I_WCS_GetWareCell", new I_WCS_GetWareCellRequest
  56. {
  57. PickUpEquipmentNo = device,
  58. TunnelNum = tunnel.Last().ToString(),
  59. WMSTaskNum = wmstaskid.ToString()
  60. });
  61. if (!res.ResType)
  62. {
  63. TaskException(device, res.ResMessage);
  64. throw new Exception(res.ResMessage);
  65. }
  66. return res;
  67. }
  68. public static void UpdateTask(int wmstaskid, int status, string addnext, int size = -1)
  69. {
  70. var res = APICaller.CallApi<List<I_WCS_PutTaskStepResponse>>(Url + "/api/Task/I_WCS_PutTaskStep", new List<I_WCS_TASKRequest>
  71. {
  72. new I_WCS_TASKRequest
  73. {
  74. TASK_WKSTATUS = status,
  75. TASK_WMSNO = wmstaskid.ToString(),
  76. Size=size,
  77. TASK_POSIDNEXT=addnext
  78. }
  79. });
  80. if (res == null || res.Count == 0)
  81. {
  82. throw new Exception("I_WCS_PutTaskStep调用失败");
  83. }
  84. if (!res.First().ResType)
  85. throw new Exception(res.First().ResMessage);
  86. }
  87. public static GetProductInfoResponse GetProductInfo(string barcode, string dev,bool isok = false)
  88. {
  89. var res = APICaller.CallApi<GetProductInfoResponse>(Url + "/api/Task/GetProductInfo", new GetProductInfoRequest
  90. {
  91. BarCode = barcode
  92. });
  93. try
  94. {
  95. if (!res.ResType)
  96. if (res.ResMessage.Contains("请联系仓库"))
  97. {
  98. var tpType = res.Memo3;//
  99. if (tpType == "1")//2195
  100. {
  101. var station_2195 = Device.Find("2195").Device<IStation521, IStation520>();
  102. if (station_2195.Data.PH_STATUS && station_2195.Data.PH_STATUS2)
  103. WMS.GetProductInfo(barcode, dev, true);
  104. else
  105. {
  106. throw new Exception(res.ResMessage);
  107. }
  108. }
  109. else if (tpType == "2")//2198 2192
  110. {
  111. var muTP = Device.Find("2192", "2198").Select(v => v.Device<IStation521, IStation520>());
  112. if (muTP.Any(v => v.Data.PH_STATUS && v.Data.PH_STATUS2))
  113. {
  114. WMS.GetProductInfo(barcode, dev, true);
  115. }
  116. else
  117. {
  118. throw new Exception(res.ResMessage);
  119. }
  120. }
  121. else if (tpType == "3")//2201
  122. {
  123. var station_2201 = Device.Find("2201").Device<IStation521, IStation520>();
  124. if (station_2201.Data.PH_STATUS && station_2201.Data.PH_STATUS2)
  125. WMS.GetProductInfo(barcode, dev, true);
  126. }
  127. else
  128. {
  129. throw new Exception(res.ResMessage);
  130. }
  131. }
  132. else
  133. {
  134. throw new Exception(res.ResMessage);
  135. }
  136. if (res.ChildContainerType <= 0 || res.ChildContainerType > 9)
  137. throw new Exception("托盘类型错误");
  138. if (res.LoadCount <= 0)
  139. throw new Exception("最大组盘数量错误");
  140. if (res.DocId == 0)
  141. throw new Exception("单据ID错误");
  142. if (res.ProLine < 1 || res.ProLine > 9)
  143. throw new Exception("产线编号" + res.ProLine + "错误");
  144. }
  145. catch (Exception ex)
  146. {
  147. TaskException(dev, ex.Message);
  148. throw;
  149. }
  150. return res;
  151. }
  152. public static I_WCS_GetOutTaskResponseSingle GetPalletOutTask(short palletType, string devCode)
  153. {
  154. var warehouse = "mwhouseputong";
  155. if (devCode.StartsWith("MW_"))
  156. warehouse = "mwhouseyuanzhi";
  157. var res = APICaller.CallApi<I_WCS_GetOutTaskResponseSingle>(Url + "/api/Task/I_WCS_GetOutTask", new I_WCS_GetOutTaskRequest
  158. {
  159. OutType = 2,
  160. Memo2 = palletType.ToString(),
  161. WareHouseId = warehouse,
  162. OutEndPostion = devCode
  163. });
  164. if (!res.ResType)
  165. {
  166. TaskException(devCode, res.ResMessage);
  167. throw new Exception(res.ResMessage);
  168. }
  169. var tcode = "TY" + res.TunnelNum;
  170. //
  171. var tunnel = Device.Find(tcode);
  172. var next = tunnel.GetNext(devCode);
  173. res.Memo1 = next.CODE;
  174. res.TunnelNum = tunnel.CODE;
  175. return res;
  176. }
  177. public static I_WCS_GetOutTaskResponseSingle GetOutTask(string position, string devCode)
  178. {
  179. var warehouse = "mwhouseputong";
  180. var ws = int.Parse(position.Split('_')[1]);
  181. //if (ws > 3)
  182. warehouse = "mwhouseyuanzhi";
  183. var res = APICaller.CallApi<I_WCS_GetOutTaskResponseSingle>(Url + "/api/Task/I_WCS_GetOutTask", new I_WCS_GetOutTaskRequest
  184. {
  185. OutType = 1,
  186. WareHouseId = warehouse,
  187. OutEndPostion = position
  188. });
  189. if (!res.ResType)
  190. {
  191. var msg = $"目标位置[{position}]获取出库任务失败,原因[{res.ResMessage}]";
  192. LogHelper.AddWCS_EXCEPTION(msg);
  193. TaskException(position, res.ResMessage);
  194. throw new Exception(res.ResMessage);
  195. }
  196. var tcode = "TY" + res.TunnelNum;
  197. var tunnel = Device.Find(tcode);
  198. if (devCode == "3001")
  199. devCode = "3045";
  200. var next = tunnel.GetNext(devCode);
  201. res.Memo1 = next.CODE;
  202. res.TunnelNum = tunnel.CODE;
  203. return res;
  204. }
  205. public static WcsContractApiResponse AutoBuildUpGroupStock(string containerCode, string barcode, string dev)
  206. {
  207. var res = APICaller.CallApi<WcsContractApiResponse>(Url + "/api/Task/AutoBuildUpGroupStock", new
  208. {
  209. ContainerCode = containerCode,
  210. BarCode = barcode
  211. });
  212. return res;
  213. }
  214. public static I_WCS_GetTunnelListResponse GetTunnelList(int wmsTaskId, string dev)
  215. {
  216. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/Task/I_WCS_GetTunnelList", new I_WCS_GetTunnelListRequest
  217. {
  218. WMSTaskNum = wmsTaskId.ToString()
  219. });
  220. if (!res.ResType)
  221. {
  222. TaskException(dev, res.ResMessage);
  223. throw new Exception(res.ResMessage);
  224. }
  225. return res;
  226. }
  227. /// <summary>
  228. /// 上一个成功上传重量的任务号
  229. /// </summary>
  230. public static int lastWMSTaskId = 0;
  231. /// <summary>
  232. /// 上传重量
  233. /// </summary>
  234. /// <param name="wmsTaskId">WMS任务号</param>
  235. /// <param name="weight">重量</param>
  236. /// <param name="dev">调用方法的设备</param>
  237. /// <returns></returns>
  238. /// <exception cref="Exception"></exception>
  239. public static void GetWeight(int wmsTaskId, int weight, string dev)
  240. {
  241. if (lastWMSTaskId == wmsTaskId) return;
  242. var res = APICaller.CallApi<I_WCS_GetWeightResponse>(Url + "/api/Task/I_WCS_GetWeight", new I_WCS_GetWeightRequest
  243. {
  244. WMSTaskNum = wmsTaskId.ToString(),
  245. Weight = weight
  246. });
  247. if (!res.ResType)
  248. {
  249. TaskException(dev, res.ResMessage);
  250. throw new Exception(res.ResMessage);
  251. }
  252. else
  253. {
  254. lastWMSTaskId = wmsTaskId;
  255. }
  256. }
  257. public static void TaskException(string device, string exMsg)
  258. {
  259. if (exMsg == "接口调用中")
  260. return;
  261. Task.Run(() =>
  262. {
  263. try
  264. {
  265. Console.WriteLine(device + ":" + exMsg);
  266. var res = APICaller.CallApi2<I_WCS_GetExcTaskResponse>(Url + "/api/Task/I_WCS_GetExcTask", new I_WCS_GetExcTaskRequest
  267. {
  268. ExcCode = exMsg,
  269. EquipmentNo = device,
  270. ExcMessage = exMsg,
  271. });
  272. if (!res.ResType)
  273. throw new Exception(res.ResMessage);
  274. }
  275. catch (Exception ex)
  276. {
  277. }
  278. });
  279. }
  280. private static List<I_WCS_PutDevInfoRequest> DevInfoList = new List<I_WCS_PutDevInfoRequest>();
  281. public static void DevInfo(string device, string exMsg)
  282. {
  283. DevInfoList.Add(new I_WCS_PutDevInfoRequest
  284. {
  285. STA_EQUIPMENTNO = device,
  286. STA_ALARMSMSG = exMsg
  287. });
  288. }
  289. public static void UploadDevInfo()
  290. {
  291. try
  292. {
  293. var res = APICaller.CallApi<I_WCS_PutDevInfoResponse>(Url + "/api/Task/I_WCS_PutDevInfo", DevInfoList);
  294. if (!res.ResType)
  295. throw new Exception(res.ResMessage);
  296. }
  297. catch (Exception ex)
  298. {
  299. Console.WriteLine("I_WCS_PutDevInfo" + "接口调用失败");
  300. }
  301. finally
  302. {
  303. DevInfoList.Clear();
  304. }
  305. }
  306. public static void UnBound(string barcode)
  307. {
  308. try
  309. {
  310. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/PDA/UnboundMwGroupStock", new
  311. {
  312. containerCode = barcode
  313. });
  314. }
  315. catch { }
  316. }
  317. }
  318. }