WMS.cs 10 KB

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