WMS.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. }
  105. else if (tpType == "2")//2198 2192
  106. {
  107. var muTP = Device.Find("2192", "2198").Select(v => v.Device<IStation521, IStation520>());
  108. if (muTP.Any(v => v.Data.PH_STATUS && v.Data.PH_STATUS2))
  109. {
  110. WMS.GetProductInfo(barcode, dev, true);
  111. }
  112. }
  113. else if (tpType == "3")//2201
  114. {
  115. var station_2201 = Device.Find("2201").Device<IStation521, IStation520>();
  116. if (station_2201.Data.PH_STATUS && station_2201.Data.PH_STATUS2)
  117. WMS.GetProductInfo(barcode, dev, true);
  118. }
  119. }
  120. else
  121. {
  122. throw new Exception(res.ResMessage);
  123. }
  124. if (res.ChildContainerType <= 0 || res.ChildContainerType > 9)
  125. throw new Exception("托盘类型错误");
  126. if (res.LoadCount <= 0)
  127. throw new Exception("最大组盘数量错误");
  128. if (res.DocId == 0)
  129. throw new Exception("单据ID错误");
  130. if (res.ProLine < 1 || res.ProLine > 9)
  131. throw new Exception("产线编号" + res.ProLine + "错误");
  132. }
  133. catch (Exception ex)
  134. {
  135. TaskException(dev, ex.Message);
  136. throw;
  137. }
  138. return res;
  139. }
  140. public static I_WCS_GetOutTaskResponseSingle GetPalletOutTask(short palletType, string devCode)
  141. {
  142. var warehouse = "mwhouseputong";
  143. if (devCode.StartsWith("MW_"))
  144. warehouse = "mwhouseyuanzhi";
  145. var res = APICaller.CallApi<I_WCS_GetOutTaskResponseSingle>(Url + "/api/Task/I_WCS_GetOutTask", new I_WCS_GetOutTaskRequest
  146. {
  147. OutType = 2,
  148. Memo2 = palletType.ToString(),
  149. WareHouseId = warehouse,
  150. OutEndPostion = devCode
  151. });
  152. if (!res.ResType)
  153. {
  154. TaskException(devCode, res.ResMessage);
  155. throw new Exception(res.ResMessage);
  156. }
  157. var tcode = "TY" + res.TunnelNum;
  158. //
  159. var tunnel = Device.Find(tcode);
  160. var next = tunnel.GetNext(devCode);
  161. res.Memo1 = next.CODE;
  162. res.TunnelNum = tunnel.CODE;
  163. return res;
  164. }
  165. public static I_WCS_GetOutTaskResponseSingle GetOutTask(string position, string devCode)
  166. {
  167. var warehouse = "mwhouseputong";
  168. var ws = int.Parse(position.Split('_')[1]);
  169. //if (ws > 3)
  170. warehouse = "mwhouseyuanzhi";
  171. var res = APICaller.CallApi<I_WCS_GetOutTaskResponseSingle>(Url + "/api/Task/I_WCS_GetOutTask", new I_WCS_GetOutTaskRequest
  172. {
  173. OutType = 1,
  174. WareHouseId = warehouse,
  175. OutEndPostion = position
  176. });
  177. if (!res.ResType)
  178. {
  179. var msg = $"目标位置[{position}]获取出库任务失败,原因[{res.ResMessage}]";
  180. LogHelper.AddWCS_EXCEPTION(msg);
  181. TaskException(position, res.ResMessage);
  182. throw new Exception(res.ResMessage);
  183. }
  184. var tcode = "TY" + res.TunnelNum;
  185. var tunnel = Device.Find(tcode);
  186. if (devCode == "3001")
  187. devCode = "3045";
  188. var next = tunnel.GetNext(devCode);
  189. res.Memo1 = next.CODE;
  190. res.TunnelNum = tunnel.CODE;
  191. return res;
  192. }
  193. public static WcsContractApiResponse AutoBuildUpGroupStock(string containerCode, string barcode, string dev)
  194. {
  195. var res = APICaller.CallApi<WcsContractApiResponse>(Url + "/api/Task/AutoBuildUpGroupStock", new
  196. {
  197. ContainerCode = containerCode,
  198. BarCode = barcode
  199. });
  200. return res;
  201. }
  202. public static I_WCS_GetTunnelListResponse GetTunnelList(int wmsTaskId, string dev)
  203. {
  204. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/Task/I_WCS_GetTunnelList", new I_WCS_GetTunnelListRequest
  205. {
  206. WMSTaskNum = wmsTaskId.ToString()
  207. });
  208. if (!res.ResType)
  209. {
  210. TaskException(dev, res.ResMessage);
  211. throw new Exception(res.ResMessage);
  212. }
  213. return res;
  214. }
  215. /// <summary>
  216. /// 上一个成功上传重量的任务号
  217. /// </summary>
  218. public static int lastWMSTaskId = 0;
  219. /// <summary>
  220. /// 上传重量
  221. /// </summary>
  222. /// <param name="wmsTaskId">WMS任务号</param>
  223. /// <param name="weight">重量</param>
  224. /// <param name="dev">调用方法的设备</param>
  225. /// <returns></returns>
  226. /// <exception cref="Exception"></exception>
  227. public static void GetWeight(int wmsTaskId, int weight, string dev)
  228. {
  229. if (lastWMSTaskId == wmsTaskId) return;
  230. var res = APICaller.CallApi<I_WCS_GetWeightResponse>(Url + "/api/Task/I_WCS_GetWeight", new I_WCS_GetWeightRequest
  231. {
  232. WMSTaskNum = wmsTaskId.ToString(),
  233. Weight = weight
  234. });
  235. if (!res.ResType)
  236. {
  237. TaskException(dev, res.ResMessage);
  238. throw new Exception(res.ResMessage);
  239. }
  240. else
  241. {
  242. lastWMSTaskId = wmsTaskId;
  243. }
  244. }
  245. public static void TaskException(string device, string exMsg)
  246. {
  247. if (exMsg == "接口调用中")
  248. return;
  249. Task.Run(() =>
  250. {
  251. try
  252. {
  253. Console.WriteLine(device + ":" + exMsg);
  254. var res = APICaller.CallApi2<I_WCS_GetExcTaskResponse>(Url + "/api/Task/I_WCS_GetExcTask", new I_WCS_GetExcTaskRequest
  255. {
  256. ExcCode = exMsg,
  257. EquipmentNo = device,
  258. ExcMessage = exMsg,
  259. });
  260. if (!res.ResType)
  261. throw new Exception(res.ResMessage);
  262. }
  263. catch (Exception ex)
  264. {
  265. }
  266. });
  267. }
  268. private static List<I_WCS_PutDevInfoRequest> DevInfoList = new List<I_WCS_PutDevInfoRequest>();
  269. public static void DevInfo(string device, string exMsg)
  270. {
  271. DevInfoList.Add(new I_WCS_PutDevInfoRequest
  272. {
  273. STA_EQUIPMENTNO = device,
  274. STA_ALARMSMSG = exMsg
  275. });
  276. }
  277. public static void UploadDevInfo()
  278. {
  279. try
  280. {
  281. var res = APICaller.CallApi<I_WCS_PutDevInfoResponse>(Url + "/api/Task/I_WCS_PutDevInfo", DevInfoList);
  282. if (!res.ResType)
  283. throw new Exception(res.ResMessage);
  284. }
  285. catch (Exception ex)
  286. {
  287. Console.WriteLine("I_WCS_PutDevInfo" + "接口调用失败");
  288. }
  289. finally
  290. {
  291. DevInfoList.Clear();
  292. }
  293. }
  294. public static void UnBound(string barcode)
  295. {
  296. try
  297. {
  298. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/PDA/UnboundMwGroupStock", new
  299. {
  300. containerCode = barcode
  301. });
  302. }
  303. catch { }
  304. }
  305. }
  306. }