using DBHelper; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WCS.Service.Entity; using WCS.Service.Helpers; namespace WCS.Service { public class WMS { private static string Url = "http://192.168.249.120:8016"; public static I_WCS_GetInTaskResponse InBound(string barcode, string devCode, bool getTunnel = false) { var warehouse = "mwhouseputong"; if (devCode.StartsWith("MW_") || devCode == "2088" || devCode == "3001") warehouse = "mwhouseyuanzhi"; var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetInTask", new I_WCS_GetInTaskRequest { ContainerBarCode = barcode, WareHouseId = warehouse, EquipmentNo = devCode, Memo1 = getTunnel ? "1" : "" //1:分巷道 2:分货位 }); if (!res.ResType) { TaskException(devCode, res.ResMessage); throw new Exception(res.ResMessage); } return res; } public static I_WCS_GetInTaskResponse PalletInbound(DB db, string barcode, string dev) { var res = APICaller.CallApi(Url + "/api/PDA/PVCSemiFinishedProductContainerReBack", new PVCSemiFinishedProductReBackRequest { BarCode = barcode, ContainerCode = barcode, Quantity = 1 }); if (res.StatusCode != Result.Success && res.StatusCode != 4004) { TaskException(dev, res.Message); throw new Exception(res.Message); } return InBound(barcode, dev); } public static short GetPalletType(string rollCode) { return 1; } public static I_WCS_GetWareCellResponse GetLocalIn(int wmstaskid, string tunnel, string device) { var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetWareCell", new I_WCS_GetWareCellRequest { PickUpEquipmentNo = device, TunnelNum = tunnel.Last().ToString(), WMSTaskNum = wmstaskid.ToString() }); if (!res.ResType) { TaskException(device, res.ResMessage); throw new Exception(res.ResMessage); } return res; } public static void UpdateTask(int wmstaskid, int status, string addnext, int size = -1) { var res = APICaller.CallApi>(Url + "/api/Task/I_WCS_PutTaskStep", new List { new I_WCS_TASKRequest { TASK_WKSTATUS = status, TASK_WMSNO = wmstaskid.ToString(), Size=size, TASK_POSIDNEXT=addnext } }); if (res == null || res.Count == 0) { throw new Exception("I_WCS_PutTaskStep调用失败"); } if (!res.First().ResType) throw new Exception(res.First().ResMessage); } public static GetProductInfoResponse GetProductInfo(string barcode, string dev) { var res = APICaller.CallApi(Url + "/api/Task/GetProductInfo", new GetProductInfoRequest { BarCode = barcode }); try { if (!res.ResType) throw new Exception(res.ResMessage); if (res.ChildContainerType <= 0 || res.ChildContainerType > 9) throw new Exception("托盘类型错误"); if (res.LoadCount <= 0) throw new Exception("最大组盘数量错误"); if (res.DocId == 0) throw new Exception("单据ID错误"); if (res.ProLine < 1 || res.ProLine > 9) throw new Exception("产线编号" + res.ProLine + "错误"); } catch (Exception ex) { TaskException(dev, ex.Message); throw; } return res; } public static I_WCS_GetOutTaskResponseSingle GetPalletOutTask(short palletType, string devCode) { var warehouse = "mwhouseputong"; if (devCode.StartsWith("MW_")) warehouse = "mwhouseyuanzhi"; var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetOutTask", new I_WCS_GetOutTaskRequest { OutType = 2, Memo2 = palletType.ToString(), WareHouseId = warehouse, OutEndPostion = devCode }); if (!res.ResType) { TaskException(devCode, res.ResMessage); throw new Exception(res.ResMessage); } var tcode = "TY" + res.TunnelNum; // var tunnel = Device.Find(tcode); var next = tunnel.GetNext(devCode); res.Memo1 = next.CODE; res.TunnelNum = tunnel.CODE; return res; } public static I_WCS_GetOutTaskResponseSingle GetOutTask(string position, string devCode) { var warehouse = "mwhouseputong"; var ws = int.Parse(position.Split('_')[1]); //if (ws > 3) warehouse = "mwhouseyuanzhi"; var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetOutTask", new I_WCS_GetOutTaskRequest { OutType = 1, WareHouseId = warehouse, OutEndPostion = position }); if (!res.ResType) { var msg = $"目标位置[{position}]获取出库任务失败,原因[{res.ResMessage}]"; LogHelper.AddWCS_EXCEPTION(msg); TaskException(position, res.ResMessage); throw new Exception(res.ResMessage); } var tcode = "TY" + res.TunnelNum; var tunnel = Device.Find(tcode); if (devCode == "3001") devCode = "3045"; var next = tunnel.GetNext(devCode); res.Memo1 = next.CODE; res.TunnelNum = tunnel.CODE; return res; } public static WcsContractApiResponse AutoBuildUpGroupStock(string containerCode, string barcode, string dev) { var res = APICaller.CallApi(Url + "/api/Task/AutoBuildUpGroupStock", new { ContainerCode = containerCode, BarCode = barcode }); return res; } public static I_WCS_GetTunnelListResponse GetTunnelList(int wmsTaskId, string dev) { var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetTunnelList", new I_WCS_GetTunnelListRequest { WMSTaskNum = wmsTaskId.ToString() }); if (!res.ResType) { TaskException(dev, res.ResMessage); throw new Exception(res.ResMessage); } return res; } /// /// 上一个成功上传重量的任务号 /// public static int lastWMSTaskId = 0; /// /// 上传重量 /// /// WMS任务号 /// 重量 /// 调用方法的设备 /// /// public static void GetWeight(int wmsTaskId, int weight, string dev) { if (lastWMSTaskId == wmsTaskId) return; var res = APICaller.CallApi(Url + "/api/Task/I_WCS_GetWeight", new I_WCS_GetWeightRequest { WMSTaskNum = wmsTaskId.ToString(), Weight = weight }); if (!res.ResType) { TaskException(dev, res.ResMessage); throw new Exception(res.ResMessage); } else { lastWMSTaskId = wmsTaskId; } } public static void TaskException(string device, string exMsg) { if (exMsg == "接口调用中") return; Task.Run(() => { try { Console.WriteLine(device + ":" + exMsg); var res = APICaller.CallApi2(Url + "/api/Task/I_WCS_GetExcTask", new I_WCS_GetExcTaskRequest { ExcCode = exMsg, EquipmentNo = device, ExcMessage = exMsg, }); if (!res.ResType) throw new Exception(res.ResMessage); } catch (Exception ex) { } }); } private static List DevInfoList = new List(); public static void DevInfo(string device, string exMsg) { DevInfoList.Add(new I_WCS_PutDevInfoRequest { STA_EQUIPMENTNO = device, STA_ALARMSMSG = exMsg }); } public static void UploadDevInfo() { try { var res = APICaller.CallApi(Url + "/api/Task/I_WCS_PutDevInfo", DevInfoList); if (!res.ResType) throw new Exception(res.ResMessage); } catch (Exception ex) { Console.WriteLine("I_WCS_PutDevInfo" + "接口调用失败"); } finally { DevInfoList.Clear(); } } public static void UnBound(string barcode) { try { var res = APICaller.CallApi(Url + "/api/PDA/UnboundMwGroupStock", new { containerCode = barcode }); } catch { } } } }