using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using WIDESEA_Common.Tools; using WIDESEA_Core.EFDbContext; using WIDESEA_Core.Extensions; using WIDESEA_Core.Utilities; using WIDESEA_Entity.DomainModels; using WIDESEA_Services.IRepositories; using WIDESEA_Services.Repositories; namespace WIDESEA_Services { public class MESAPIInvoke { public static string upperMaterIPAddresss = ConfigUtil.GetConfiguration["upperMaterURL"]; public static string lowerMaterIPAddresss = ConfigUtil.GetConfiguration["lowerMaterURL"]; public static string BindMaterIPAddresss = ConfigUtil.GetConfiguration["BindMaterURL"]; public static string StockChangeIPAddresss = ConfigUtil.GetConfiguration["StockChangeURL"]; public static string AgvStatusIPAddresss = ConfigUtil.GetConfiguration["AgvStatusURL"]; public static string StockMaterBindsIPAddresss = ConfigUtil.GetConfiguration["StockMaterBindsURL"]; public static string NeedStoskIPAddresss = ConfigUtil.GetConfiguration["NeedStoskURL"]; public static string StockMaterMoveIPAddresss = ConfigUtil.GetConfiguration["StockMaterMoveURL"]; public static string BakingFeedingBindingIPAddresss = ConfigUtil.GetConfiguration["BakingFeedingBindingURL"]; public static string PreTaskIPAddresss = ConfigUtil.GetConfiguration["PreTaskURL"]; public static string RescheduleIPAddresss = ConfigUtil.GetConfiguration["RescheduleURL"]; public static void GetInterfaceInfo(string InterfaceName, string TaskID, string DeviceID, int Code, string Message) { VOLContext Context = new VOLContext(); IInterfaceInfoRepository InterfaceInfoRepository = new InterfaceInfoRepository(Context); try { var info = InterfaceInfoRepository.FindFirst(f => f.InterfaceName == InterfaceName); info.TaskID = TaskID; info.DeviceID = DeviceID; info.Code = Code; info.Message = Message; info.updatetime = DateTime.Now; InterfaceInfoRepository.Update(info, true); } catch (Exception ex) { } } /// /// 设备请求上料 /// /// /// /// public static MESback EquipFeeding(string Devid, string BatchNo) { try { Task.Delay(10000).Wait(); WriteLog.Info("设备请求上料").Write("EquipFeeding接口调用开始,设备编号:" + Devid + "工单号:" + BatchNo + DateTime.Now, "设备请求上料"); EquipONorUPrequest req = new EquipONorUPrequest(); req.Devid = Devid; req.BatchNo = BatchNo; //JArray ja = new JArray(); //ja.Add(WebApiHelper.GetJson(req)); //JObject jobj = WebApiHelper.SendInfoToWebAPI(upperMaterIPAddresss, "", "Post", ja); //MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(upperMaterIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); GetInterfaceInfo("EquipFeeding", "", Devid, reply.Code, reply.Message); WriteLog.Info("设备请求上料").Write("EquipFeeding接口调用结束" + jobj + DateTime.Now, "设备请求上料"); return reply; } catch (Exception ex) { WriteLog.Info("设备请求上料").Write("EquipFeeding接口调用异常:" + ex.Message, "设备请求上料"); throw new Exception(ex.Message); } } /// /// 设备请求下料调用MES接口 /// /// /// /// public static MESback EquipUnloading(string Devid, string BatchNo) { try { WriteLog.Info("设备请求下料").Write("EquipUnloading接口调用开始,设备编号:" + Devid + "工单号:" + BatchNo + DateTime.Now, "设备请求下料"); EquipONorUPrequest req = new EquipONorUPrequest(); req.Devid = Devid; req.BatchNo = BatchNo; //JArray ja = new JArray(); //ja.Add(WebApiHelper.GetJson(req)); //JObject jobj = WebApiHelper.SendInfoToWebAPI(lowerMaterIPAddresss, "", "Post", ja); //MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(lowerMaterIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); GetInterfaceInfo("EquipUnloading", "", Devid, reply.Code, reply.Message); WriteLog.Info("设备请求下料").Write("EquipUnloading接口调用结束" + jobj + DateTime.Now, "设备请求下料"); return reply; } catch (Exception ex) { WriteLog.Info("设备请求下料").Write("EquipUnloading接口调用异常:" + ex.Message, "设备请求下料"); throw new Exception(ex.Message); } } /// /// 记录批号与缓存架绑定信息,物料绑定信息 /// /// /// /// /// /// /// public static MESback BindMaterialDevid(string DevidGet, string DevidPut, string MaterialType, string BarCode, string MaterialStatus) { try { WriteLog.Info("BindMaterialDevid").Write("BindMaterialDevid接口调用开始,DevidGet:" + DevidGet + "DevidPut:" + DevidPut + "MaterialType:" + MaterialType + "BarCode:" + BarCode + "MaterialStatus:" + MaterialStatus + DateTime.Now, "BindMaterialDevid"); BindMaterrequest req = new BindMaterrequest(); req.DevidGet = DevidGet; req.DevidPut = DevidPut; req.MaterialType = MaterialType; req.BarCode = BarCode; req.MaterialStatus = MaterialStatus; //JArray ja = new JArray(); //ja.Add(WebApiHelper.GetJson(req)); //JObject jobj = WebApiHelper.SendInfoToWebAPI(BindMaterIPAddresss, "", "Post", ja); //MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(BindMaterIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); WriteLog.Info("BindMaterialDevid").Write("BindMaterialDevid接口调用结束,DevidGet:" + DevidGet + "DevidPut:" + DevidPut + "MaterialType:" + MaterialType + "BarCode:" + BarCode + "MaterialStatus:" + MaterialStatus + DateTime.Now, "BindMaterialDevid"); return reply; } catch (Exception ex) { WriteLog.Info("BindMaterialDevid").Write("BindMaterialDevid接口调用异常:" + ex.Message, "BindMaterialDevid"); throw new Exception(ex.Message); } } /// /// AGV库存异动登记 /// /// /// /// /// /// /// /// public static MESback AgvStockChange(string CallSn, string DevidGet, string DevidPut, string MaterialType, string BarCode, string MaterialStatus) { try { WriteLog.Info("AgvStockChange").Write("AgvStockChange接口调用开始", "AgvStockChange"); StockChangerequest req = new StockChangerequest(); req.CallSn = CallSn; req.DevidGet = DevidGet; req.DevidPut = DevidPut; req.MaterialType = MaterialType; req.BarCode = BarCode; req.MaterialStatus = MaterialStatus; //JArray ja = new JArray(); //ja.Add(WebApiHelper.GetJson(req)); //JObject jobj = WebApiHelper.SendInfoToWebAPI(StockChangeIPAddresss, "", "Post", ja); //MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(StockChangeIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); WriteLog.Info("AgvStockChange").Write("AgvStockChange接口调用结束", "AgvStockChange"); return reply; } catch (Exception ex) { WriteLog.Info("AgvStockChange").Write("AgvStockChange接口调用异常:" + ex.Message, "AgvStockChange"); throw new Exception(ex.Message); } } /// /// 上传AGV运行状态,调用MES /// /// /// /// /// /// /// public static MESback UploadAgvStatus(string AgvCode, string RunningTime, string RealtimeSpeed, string HandState, string AbnormalState) { try { WriteLog.Info("UploadAgvStatus").Write("UploadAgvStatus接口调用开始", "UploadAgvStatus"); AgvStatusrequest req = new AgvStatusrequest(); req.AgvCode = AgvCode; req.RunningTime = RunningTime; req.RealtimeSpeed = RealtimeSpeed; req.HandState = HandState; req.AbnormalState = AbnormalState; JArray ja = new JArray(); ja.Add(WebApiHelper.GetJson(req)); JObject jobj = WebApiHelper.SendInfoToWebAPI(AgvStatusIPAddresss, "", "Post", ja); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); WriteLog.Info("UploadAgvStatus").Write("UploadAgvStatus接口调用结束" + jobj, "UploadAgvStatus"); return reply; } catch (Exception ex) { WriteLog.Info("UploadAgvStatus").Write("UploadAgvStatus接口调用异常:" + ex.Message, "UploadAgvStatus"); throw new Exception(ex.Message); } } /// /// 入库预定 /// public static MESback PreTask(int InType) { try { WriteLog.Info("入库预定").Write("PreTask接口调用开始,入库地址:" + InType + DateTime.Now, "入库预定"); PreStoreClass req = new PreStoreClass(); req.InType = InType; //JArray ja = new JArray(); //ja.Add(WebApiHelper.GetJson_2(req));hjhjhj //JObject jobj = WebApiHelper.SendInfoToWebAPI(StockMaterBindsIPAddresss, "", "Post", ja); string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(PreTaskIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); WriteLog.Info("入库预定").Write("PreTask接口调用结束,返回结果:" + jobj + DateTime.Now, "入库预定"); if (reply.Code > 0) { return null; }; return reply; } catch (Exception ex) { WriteLog.Info("入库预定").Write("PreTask接口调用异常:" + ex.Message, "入库预定"); throw new Exception(ex.Message); } } /// /// 解绑预定入库任务 /// public static MESback Reschedule(int InType) { try { WriteLog.Info("解绑预定入库任务").Write("Reschedule接口调用开始,入库地址:" + InType + DateTime.Now, "解绑预定入库任务"); PreStoreClass req = new PreStoreClass(); req.InType = InType; //JArray ja = new JArray(); //ja.Add(WebApiHelper.GetJson_2(req));hjhjhj //JObject jobj = WebApiHelper.SendInfoToWebAPI(StockMaterBindsIPAddresss, "", "Post", ja); string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(RescheduleIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); WriteLog.Info("解绑预定入库任务").Write("Reschedule接口调用结束,返回结果:" + jobj + DateTime.Now, "解绑预定入库任务"); if (reply.Code > 0) { return null; }; return reply; } catch (Exception ex) { WriteLog.Info("解绑预定入库任务").Write("Reschedule接口调用异常:" + ex.Message, "解绑预定入库任务"); throw new Exception(ex.Message); } } /// /// 入库物料绑定 /// /// /// /// /// /// public static MESback InStockMaterBind(string Devid, string MaterialType, string BarCode, string MaterialStatus, int InType) { try { WriteLog.Info("入库物料绑定").Write("InStockMaterBind接口调用开始,当前设备:" + Devid + ",物料类型:" + MaterialType + ",物料条码:" + BarCode + ",物料状态:" + MaterialStatus + DateTime.Now, "入库物料绑定"); StockMaterBindsrequest req = new StockMaterBindsrequest(); req.Devid = Devid; req.MaterialType = MaterialType; req.BarCode = BarCode; req.MaterialStatus = MaterialStatus; req.InType = InType; //JArray ja = new JArray(); //ja.Add(WebApiHelper.GetJson_2(req));hjhjhj //JObject jobj = WebApiHelper.SendInfoToWebAPI(StockMaterBindsIPAddresss, "", "Post", ja); string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(StockMaterBindsIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); GetInterfaceInfo("InStockMaterBind", "", Devid, reply.Code, reply.Message); WriteLog.Info("入库物料绑定").Write("InStockMaterBind接口调用结束,返回结果:" + jobj + DateTime.Now, "入库物料绑定"); return reply; } catch (Exception ex) { WriteLog.Info("入库物料绑定").Write("InStockMaterBind接口调用异常:" + ex.Message, "入库物料绑定"); throw new Exception(ex.Message); } } /// /// 出库要料 /// /// /// /// /// /// public static MESback OutNeedStosk(string TaskID, List Devid, int OutType, string MaterialType, string ProcessName, string DevicelD) { try { NeedStoskrequest req = new NeedStoskrequest(); WriteLog.Info("出库要料").Write($"OutNeedStosk接口调用开始,:任务号{TaskID},设备编号" + DevicelD + "出库类型" + OutType + "物料类型" + MaterialType + DateTime.Now, "出库要料"); req.Devid = Devid; req.MaterialType = MaterialType; req.OutType = OutType; req.TaskID = TaskID; req.DeviceID = DevicelD; req.ProcessName = ProcessName; string a = JsonConvert.SerializeObject(req);//这个A就是他要的JSON字符串 object jobj = HttpHelper.PostByPara(NeedStoskIPAddresss, a); if (jobj == null) { WriteLog.Info("出库要料").Write("OutNeedStosk接口调用异常:返回值为空", "出库要料"); return null; } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); GetInterfaceInfo("OutNeedStosk", TaskID, DevicelD, reply.Code, reply.Message); WriteLog.Info("出库要料").Write("OutNeedStosk接口调用结束," + a + jobj + DateTime.Now, "出库要料"); if (reply.Code > 0) { Thread.Sleep(3000); return null; }; return reply; } catch (Exception ex) { WriteLog.Info("出库要料").Write("OutNeedStosk接口调用异常:" + ex.Message, "出库要料"); return null; //throw new Exception(ex.Message); } } /// /// 出库物料确认搬走 /// /// /// /// public static MESback OutStockMaterMove(string Devid, string MaterialType, int OutType, int sum, string taskId = "") { VOLContext Context = new VOLContext(); Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context); try { WriteLog.Info("出库物料确认搬走").Write(taskId + ":" + "OutStockMaterMove接口调用开始" + "缓存架编号:" + Devid + "\t" + "物料类型:" + MaterialType + "\t" + "出库类型:" + OutType + "\t" + DateTime.Now, "出库物料确认搬走"); StockMaterMoverequest req = new StockMaterMoverequest(); req.Devid = Devid; req.MaterialType = MaterialType; req.OutType = OutType; if (Devid.Contains("FBT")) { req.sum = sum; } string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(StockMaterMoveIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); GetInterfaceInfo("OutStockMaterMove", "", Devid, reply.Code, reply.Message); WriteLog.Info("出库物料确认搬走").Write("OutStockMaterMove接口调用结束" + a + jobj + DateTime.Now, "出库物料确认搬走"); return reply; } catch (Exception ex) { WriteLog.Info("出库物料确认搬走").Write("OutStockMaterMove接口调用异常:" + ex.Message, "出库物料确认搬走"); throw new Exception(ex.Message); } } /// /// 烘箱物料绑定 /// /// /// /// public static MESback BakingFeedingBinding(string Devid, List Materials) { try { WriteLog.Info("烘箱物料绑定").Write("BakingFeedingBinding接口调用开始,烘箱编码:" + Devid + "\t" + "条码1:" + Materials[0].BarCode + "\t" + "物料类型1:" + Materials[0].MaterialType + "\t" + "条码2" + Materials[1].BarCode + "\t" + "物料类型2:" + Materials[1].MaterialType + "\t" + DateTime.Now, "烘箱物料绑定"); BakingFeedingClass req = new BakingFeedingClass(); req.Materials = new List(); req.Devid = Devid; for (int i = 0; i < Materials.Count(); i++) { BakingClass bakingClass = new BakingClass(); bakingClass.MaterialType = Materials[i].MaterialType; bakingClass.BarCode = Materials[i].BarCode; req.Materials.Add(bakingClass); WriteLog.Info("烘箱物料绑定").Write("BakingFeedingBinding接口调用" + req.Materials[i].BarCode + "\t" + req.Materials[i].MaterialType + "\t" + bakingClass.BarCode + "\t" + bakingClass.MaterialType + DateTime.Now, "烘箱物料绑定"); } string a = JsonConvert.SerializeObject(req); object jobj = HttpHelper.PostByPara(BakingFeedingBindingIPAddresss, a); if (jobj == null) { throw new Exception(); } MESback reply = WebApiHelper.ParseFromJson(jobj.ToString()); GetInterfaceInfo("BakingFeedingBinding", "", Devid, reply.Code, reply.Message); WriteLog.Info("烘箱物料绑定").Write("BakingFeedingBinding接口调用结束" + a + jobj + DateTime.Now, "烘箱物料绑定"); return reply; } catch (Exception ex) { WriteLog.Info("烘箱物料绑定").Write("BakingFeedingBinding接口调用异常:" + ex.Message, "烘箱物料绑定"); throw new Exception(ex.Message); } } } }