WMS.cs 10 KB

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