WMS.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. if (devCode == "3001" || devCode == "2122" || devCode == "2131" || devCode == "2143")
  150. {
  151. //TaskException(position, res.ResMessage);
  152. throw new Exception($"目标位置[{position}]获取出库任务失败,原因:[{res.ResMessage}]");
  153. }
  154. else
  155. {
  156. TaskException(position, res.ResMessage);
  157. throw new Exception(res.ResMessage);
  158. }
  159. }
  160. var tcode = "TY" + res.TunnelNum;
  161. var tunnel = Device.Find(tcode);
  162. if (devCode == "3001")
  163. devCode = "3045";
  164. var next = tunnel.GetNext(devCode);
  165. res.Memo1 = next.CODE;
  166. res.TunnelNum = tunnel.CODE;
  167. return res;
  168. }
  169. public static WcsContractApiResponse AutoBuildUpGroupStock(string containerCode, string barcode, string dev)
  170. {
  171. var res = APICaller.CallApi<WcsContractApiResponse>(Url + "/api/Task/AutoBuildUpGroupStock", new
  172. {
  173. ContainerCode = containerCode,
  174. BarCode = barcode
  175. });
  176. return res;
  177. }
  178. public static I_WCS_GetTunnelListResponse GetTunnelList(int wmsTaskId, string dev)
  179. {
  180. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/Task/I_WCS_GetTunnelList", new I_WCS_GetTunnelListRequest
  181. {
  182. WMSTaskNum = wmsTaskId.ToString()
  183. });
  184. if (!res.ResType)
  185. {
  186. TaskException(dev, res.ResMessage);
  187. throw new Exception(res.ResMessage);
  188. }
  189. return res;
  190. }
  191. /// <summary>
  192. /// 上一个成功上传重量的任务号
  193. /// </summary>
  194. public static int lastWMSTaskId = 0;
  195. /// <summary>
  196. /// 上传重量
  197. /// </summary>
  198. /// <param name="wmsTaskId">WMS任务号</param>
  199. /// <param name="weight">重量</param>
  200. /// <param name="dev">调用方法的设备</param>
  201. /// <returns></returns>
  202. /// <exception cref="Exception"></exception>
  203. public static void GetWeight(int wmsTaskId, int weight, string dev)
  204. {
  205. if (lastWMSTaskId == wmsTaskId) return;
  206. var res = APICaller.CallApi<I_WCS_GetWeightResponse>(Url + "/api/Task/I_WCS_GetWeight", new I_WCS_GetWeightRequest
  207. {
  208. WMSTaskNum = wmsTaskId.ToString(),
  209. Weight = weight
  210. });
  211. if (!res.ResType)
  212. {
  213. TaskException(dev, res.ResMessage);
  214. throw new Exception(res.ResMessage);
  215. }
  216. else
  217. {
  218. lastWMSTaskId = wmsTaskId;
  219. }
  220. }
  221. public static void TaskException(string device, string exMsg)
  222. {
  223. if (exMsg == "接口调用中")
  224. return;
  225. Task.Run(() =>
  226. {
  227. try
  228. {
  229. Console.WriteLine(device + ":" + exMsg);
  230. var res = APICaller.CallApi2<I_WCS_GetExcTaskResponse>(Url + "/api/Task/I_WCS_GetExcTask", new I_WCS_GetExcTaskRequest
  231. {
  232. ExcCode = exMsg,
  233. EquipmentNo = device,
  234. ExcMessage = exMsg,
  235. });
  236. if (!res.ResType)
  237. throw new Exception(res.ResMessage);
  238. }
  239. catch (Exception ex)
  240. {
  241. }
  242. });
  243. }
  244. private static List<I_WCS_PutDevInfoRequest> DevInfoList = new List<I_WCS_PutDevInfoRequest>();
  245. public static void DevInfo(string device, string exMsg)
  246. {
  247. DevInfoList.Add(new I_WCS_PutDevInfoRequest
  248. {
  249. STA_EQUIPMENTNO = device,
  250. STA_ALARMSMSG = exMsg
  251. });
  252. }
  253. public static void UploadDevInfo()
  254. {
  255. try
  256. {
  257. var res = APICaller.CallApi<I_WCS_PutDevInfoResponse>(Url + "/api/Task/I_WCS_PutDevInfo", DevInfoList);
  258. if (!res.ResType)
  259. throw new Exception(res.ResMessage);
  260. }
  261. catch (Exception ex)
  262. {
  263. Console.WriteLine("I_WCS_PutDevInfo" + "接口调用失败");
  264. }
  265. finally
  266. {
  267. DevInfoList.Clear();
  268. }
  269. }
  270. public static void UnBound(string barcode)
  271. {
  272. try
  273. {
  274. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/PDA/UnboundMwGroupStock", new
  275. {
  276. containerCode = barcode
  277. });
  278. }
  279. catch { }
  280. }
  281. }
  282. }