WMS.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using Logs;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using WCS.Service.Entity;
  7. using WCS.Service.Extensions;
  8. namespace WCS.Service
  9. {
  10. public class WMS
  11. {
  12. private const string Url = "http://192.168.249.150:8026";
  13. //private static string Url = "http://127.0.0.1:8026";
  14. private const string WareHouseId = "opphouse";
  15. /// <summary>
  16. /// 向WMS获取入库任务 一次单卷
  17. /// </summary>
  18. /// <param name="barcode">产品条码</param>
  19. /// <param name="devCode">设备条码</param>
  20. /// <param name="getTunnel"></param>
  21. /// <returns></returns>
  22. /// <exception cref="Exception"></exception>
  23. public static List<I_WCS_GetInTaskResponseItem> I_WCS_GetInTask(string barcode, string devCode, bool getTunnel = false)
  24. {
  25. var data = JsonConvert.SerializeObject(new List<I_WCS_GetInTaskRequest>()
  26. {
  27. new(){
  28. ContainerBarCode = barcode,
  29. WareHouseId = WareHouseId,
  30. EquipmentNo = devCode,
  31. Memo1 = getTunnel ? "1" : "" //1:分巷道 2:分货位
  32. }
  33. });
  34. var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  35. {
  36. Name = "I_WCS_GetInTask",
  37. Parameters = data,
  38. ParametersMD5 = data.GetMD5()
  39. });
  40. if (res.ResType) return res.TaskList;
  41. TaskException(devCode, res.ResMessage);
  42. throw new WarnException(res.ResMessage);
  43. }
  44. /// <summary>
  45. /// 向WMS获取入库任务 一次双卷
  46. /// </summary>
  47. /// <param name="barcode1">产品1条码</param>
  48. /// <param name="devCode1">设备1编号</param>
  49. /// <param name="getTunnel1">产品1是否直接分配巷道</param>
  50. /// <param name="barcode2">产品2条码</param>
  51. /// <param name="devCode2">设备2编号</param>
  52. /// <param name="getTunnel2">产品2是否直接分配巷道</param>
  53. /// <exception cref="WarnException"></exception>
  54. /// <returns></returns>
  55. public static List<I_WCS_GetInTaskResponseItem> I_WCS_GetInTask(string barcode1, string devCode1, string barcode2, string devCode2, bool getTunnel1 = false, bool getTunnel2 = false)
  56. {
  57. var data = JsonConvert.SerializeObject(new List<I_WCS_GetInTaskRequest>()
  58. {
  59. new(){
  60. ContainerBarCode = barcode1,
  61. WareHouseId = WareHouseId,
  62. EquipmentNo = devCode1,
  63. Memo1 = getTunnel1 ? "1" : "" //1:分巷道 2:分货位
  64. },
  65. new(){
  66. ContainerBarCode = barcode2,
  67. WareHouseId = WareHouseId,
  68. EquipmentNo = devCode2,
  69. Memo1 = getTunnel2 ? "1" : "" //1:分巷道 2:分货位
  70. },
  71. });
  72. var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  73. {
  74. Name = "I_WCS_GetInTask",
  75. Parameters = data,
  76. ParametersMD5 = data.GetMD5()
  77. });
  78. if (res.ResType) return res.TaskList;
  79. TaskException($"G{devCode1}", res.ResMessage);
  80. throw new WarnException(res.ResMessage);
  81. }
  82. /// <summary>
  83. /// 分配货位
  84. /// </summary>
  85. /// <param name="wmstaskid">WMS任务ID</param>
  86. /// <param name="tunnel">巷道</param>
  87. /// <param name="device">设备号</param>
  88. /// <param name="ForkNum">申请任务对应的货叉</param>
  89. /// <exception cref="WarnException"></exception>
  90. /// <returns></returns>
  91. public static I_WCS_GetWareCellResponse GetLocalIn(int wmstaskid, string tunnel, string device, WareCellForkNum ForkNum)
  92. {
  93. var data = JsonConvert.SerializeObject(new I_WCS_GetWareCellRequest
  94. {
  95. PickUpEquipmentNo = device,
  96. TunnelNum = tunnel.Last().ToString(),
  97. WMSTaskNum = wmstaskid.ToString(),
  98. ForkNum = ForkNum
  99. });
  100. var res = APICaller.CallApi<I_WCS_GetWareCellResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  101. {
  102. Name = "I_WCS_GetWareCell",
  103. Parameters = data,
  104. ParametersMD5 = data.GetMD5()
  105. });
  106. if (res.ResType) return res;
  107. TaskException(device, res.ResMessage);
  108. throw new WarnException(res.ResMessage);
  109. }
  110. public static void UpdateTask(string POSIDNEXT, int wmstaskid, int status, int size = -1)
  111. {
  112. var data = JsonConvert.SerializeObject(new List<I_WCS_TASKRequest>
  113. {
  114. new()
  115. {
  116. TASK_WKSTATUS = status,
  117. TASK_WMSNO = wmstaskid.ToString(),
  118. TASK_POSIDNEXT=POSIDNEXT,
  119. Size=size
  120. }
  121. });
  122. var res = APICaller.CallApi<List<I_WCS_PutTaskStepResponse>>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  123. {
  124. Name = "I_WCS_PutTaskStep",
  125. Parameters = data,
  126. ParametersMD5 = data.GetMD5()
  127. });
  128. if (res == null || res.Count == 0)
  129. {
  130. throw new WarnException("I_WCS_PutTaskStep调用失败");
  131. }
  132. if (!res.First().ResType)
  133. throw new WarnException(res.First().ResMessage);
  134. }
  135. public static GetProductInfoResponse GetProductInfo(string barcode, string dev)
  136. {
  137. var data = JsonConvert.SerializeObject(new GetProductInfoRequest
  138. {
  139. BarCode = barcode
  140. });
  141. var res = APICaller.CallApi<GetProductInfoResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  142. {
  143. Name = "GetProductInfo",
  144. Parameters = data,
  145. ParametersMD5 = data.GetMD5()
  146. });
  147. try
  148. {
  149. if (!res.ResType)
  150. throw new WarnException(res.ResMessage);
  151. if (res.ChildContainerType is <= 0 or > 9)
  152. throw new WarnException("托盘类型错误");
  153. if (res.LoadCount <= 0)
  154. throw new WarnException("最大组盘数量错误");
  155. if (res.DocId == 0)
  156. throw new WarnException("单据ID错误");
  157. if (res.ProLine is < 1 or > 9)
  158. throw new WarnException("产线编号" + res.ProLine + "错误");
  159. }
  160. catch (Exception ex)
  161. {
  162. TaskException(dev, ex.Message);
  163. throw;
  164. }
  165. return res;
  166. }
  167. /// <summary>
  168. /// 获取出库任务
  169. /// </summary>
  170. /// <param name="position">出库位置</param>
  171. /// <param name="devCode">调用方法的设备号</param>
  172. /// <returns></returns>
  173. /// <exception cref="Exception"></exception>
  174. public static List<I_WCS_GetOutTaskResponseSingle> GetOutTask(string position, string devCode)
  175. {
  176. var data = JsonConvert.SerializeObject(new I_WCS_GetOutTaskRequest
  177. {
  178. OutType = 1,
  179. WareHouseId = WareHouseId,
  180. OutEndPostion = position
  181. });
  182. var res = APICaller.CallApi<I_WCS_GetOutTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  183. {
  184. Name = "I_WCS_GetOutTask",
  185. Parameters = data,
  186. ParametersMD5 = data.GetMD5()
  187. });
  188. if (!res.ResType)
  189. {
  190. TaskException(position, res.ResMessage);
  191. throw new WarnException(res.ResMessage);
  192. }
  193. res.WMSTasks.ForEach(v =>
  194. {
  195. var tcode = "TY" + v.TunnelNum;
  196. var tunnel = Device.Find(tcode);
  197. //var next = tunnel.GetPath(devCode);
  198. //v.Memo1 = next.CODE;
  199. v.TunnelNum = tunnel.CODE;
  200. });
  201. return res.WMSTasks;
  202. }
  203. public static WcsContractApiResponse AutoBuildUpGroupStock(string containerCode, string barcode, string dev)
  204. {
  205. var res = APICaller.CallApi<WcsContractApiResponse>(Url + "/api/Task/AutoBuildUpGroupStock", new
  206. {
  207. ContainerCode = containerCode,
  208. BarCode = barcode
  209. });
  210. if (res.ResType) return res;
  211. TaskException(dev, res.ResMessage);
  212. throw new WarnException(res.ResMessage);
  213. }
  214. /// <summary>
  215. /// 获取巷道
  216. /// </summary>
  217. /// <param name="WMSTaskId">WMS任务号集合</param>
  218. /// <param name="dev">请求设备号</param>
  219. /// <returns></returns>
  220. /// <exception cref="Exception"></exception>
  221. public static I_WCS_GetTunnelListResponse GetTunnelList(List<string> WMSTaskId, string dev)
  222. {
  223. var data = JsonConvert.SerializeObject(new I_WCS_GetTunnelListRequest
  224. {
  225. WMSTaskNum = WMSTaskId
  226. });
  227. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  228. {
  229. Name = "I_WCS_GetTunnelList",
  230. Parameters = data,
  231. ParametersMD5 = data.GetMD5()
  232. });
  233. if (res.ResType) return res;
  234. TaskException(dev, res.ResMessage);
  235. throw new WarnException(res.ResMessage);
  236. }
  237. public static void TaskException(string device, string exMsg)
  238. {
  239. if (exMsg == "接口调用中")
  240. return;
  241. System.Threading.Tasks.Task.Run(() =>
  242. {
  243. try
  244. {
  245. var data = JsonConvert.SerializeObject(new I_WCS_GetExcTaskRequest
  246. {
  247. ExcCode = exMsg,
  248. EquipmentNo = device,
  249. ExcMessage = exMsg,
  250. });
  251. Console.WriteLine(device + ":" + exMsg);
  252. var res = APICaller.CallApi2<I_WCS_GetExcTaskResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  253. {
  254. Name = "I_WCS_GetExcTask",
  255. Parameters = data,
  256. ParametersMD5 = data.GetMD5()
  257. });
  258. InfoLog.INFO_I_WCS_GetExcTask(device + ":" + exMsg);
  259. if (!res.ResType) throw new WarnException(res.ResMessage);
  260. }
  261. catch (Exception)
  262. {
  263. }
  264. });
  265. }
  266. private static List<I_WCS_PutDevInfoRequest> DevInfoList = new();
  267. public static void DevInfo(string device, string exMsg)
  268. {
  269. DevInfoList.Add(new I_WCS_PutDevInfoRequest
  270. {
  271. STA_EQUIPMENTNO = device,
  272. STA_ALARMSMSG = exMsg
  273. });
  274. }
  275. public static void UploadDevInfo()
  276. {
  277. return;
  278. try
  279. {
  280. var data = JsonConvert.SerializeObject(DevInfoList);
  281. var res = APICaller.CallApi<I_WCS_PutDevInfoResponse>(Url + "/api/Task/I_WCS_UnifiedInterface", new InterfaceParameters
  282. {
  283. Name = "I_WCS_GetExcTask",
  284. Parameters = data,
  285. ParametersMD5 = data.GetMD5()
  286. });
  287. if (!res.ResType)
  288. throw new WarnException(res.ResMessage);
  289. }
  290. catch (Exception ex)
  291. {
  292. InfoLog.INFO_WARN($"I_WCS_PutDevInfo接口调用失败:{ex.Message}");
  293. }
  294. finally
  295. {
  296. DevInfoList.Clear();
  297. }
  298. }
  299. public static void UnBound(string barcode)
  300. {
  301. try
  302. {
  303. var res = APICaller.CallApi<I_WCS_GetTunnelListResponse>(Url + "/api/PDA/UnboundMwGroupStock", new
  304. {
  305. containerCode = barcode
  306. });
  307. }
  308. catch { }
  309. }
  310. }
  311. /// <summary>
  312. /// 调用接口传入参数
  313. /// </summary>
  314. public class InterfaceParameters
  315. {
  316. /// <summary>
  317. /// 业务名 例:I_WCS_GetInTask
  318. /// </summary>
  319. public string Name { get; set; }
  320. /// <summary>
  321. /// 传入参数为Json
  322. /// </summary>
  323. public string Parameters { get; set; }
  324. /// <summary>
  325. /// 传入参数的唯一值
  326. /// </summary>
  327. public string ParametersMD5 { get; set; }
  328. }
  329. }