qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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)
            {
 
            }
        }
 
        /// <summary>
        /// 设备请求上料
        /// </summary>
        /// <param name="Devid"></param>    
        /// <param name="BatchNo"></param>
        /// <returns></returns>
        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<MESback>(jobj.ToString());
                string a = JsonConvert.SerializeObject(req);
                object jobj = HttpHelper.PostByPara(upperMaterIPAddresss, a);
                if (jobj == null) { throw new Exception(); }
                MESback reply = WebApiHelper.ParseFromJson<MESback>(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);
            }
        }
        /// <summary>
        /// 设备请求下料调用MES接口
        /// </summary>
        /// <param name="Devid"></param>
        /// <param name="BatchNo"></param>
        /// <returns></returns>
        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<MESback>(jobj.ToString());
                string a = JsonConvert.SerializeObject(req);
                object jobj = HttpHelper.PostByPara(lowerMaterIPAddresss, a);
                if (jobj == null) { throw new Exception(); }
                MESback reply = WebApiHelper.ParseFromJson<MESback>(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);
            }
        }
        /// <summary>
        /// 记录批号与缓存架绑定信息,物料绑定信息
        /// </summary>
        /// <param name="DevidGet"></param>
        /// <param name="DevidPut"></param>
        /// <param name="MaterialType"></param>
        /// <param name="BarCode"></param>
        /// <param name="MaterialStatus"></param>
        /// <returns></returns>
        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<MESback>(jobj.ToString());
                string a = JsonConvert.SerializeObject(req);
                object jobj = HttpHelper.PostByPara(BindMaterIPAddresss, a);
                if (jobj == null) { throw new Exception(); }
                MESback reply = WebApiHelper.ParseFromJson<MESback>(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);
            }
        }
        /// <summary>
        /// AGV库存异动登记
        /// </summary>
        /// <param name="CallSn"></param>
        /// <param name="DevidGet"></param>
        /// <param name="DevidPut"></param>
        /// <param name="MaterialType"></param>
        /// <param name="BarCode"></param>
        /// <param name="MaterialStatus"></param>
        /// <returns></returns>
        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<MESback>(jobj.ToString());
                string a = JsonConvert.SerializeObject(req);
                object jobj = HttpHelper.PostByPara(StockChangeIPAddresss, a);
                if (jobj == null) { throw new Exception(); }
                MESback reply = WebApiHelper.ParseFromJson<MESback>(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);
            }
        }
        /// <summary>
        /// 上传AGV运行状态,调用MES
        /// </summary>
        /// <param name="AgvCode"></param>
        /// <param name="RunningTime"></param>
        /// <param name="RealtimeSpeed"></param>
        /// <param name="HandState"></param>
        /// <param name="AbnormalState"></param>
        /// <returns></returns>
        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<MESback>(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);
            }
        }
        /// <summary>
        /// 入库预定
        /// </summary>
        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<MESback>(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);
            }
        }
        /// <summary>
        /// 解绑预定入库任务
        /// </summary>
        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<MESback>(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);
            }
        }
        /// <summary>
        /// 入库物料绑定
        /// </summary>
        /// <param name="Devid"></param>
        /// <param name="MaterialType"></param>
        /// <param name="BarCode"></param>
        /// <param name="MaterialStatus"></param>
        /// <returns></returns>
        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<MESback>(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);
            }
        }
        /// <summary> 
        /// 出库要料
        /// </summary>
        /// <param name="TaskID"></param>
        /// <param name="Devid"></param>
        /// <param name="OutType"></param>
        /// <param name="MaterialType"></param>
        /// <returns></returns>
        public static MESback OutNeedStosk(string TaskID, List<string> 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<MESback>(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);
            }
        }
        /// <summary>
        /// 出库物料确认搬走
        /// </summary>
        /// <param name="Devid"></param>
        /// <param name="MaterialType"></param>
        /// <returns></returns>
        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<MESback>(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);
            }
        }
        /// <summary>
        /// 烘箱物料绑定
        /// </summary>
        /// <param name="Devid"></param>
        /// <param name="Materials"></param>
        /// <returns></returns>
        public static MESback BakingFeedingBinding(string Devid, List<BakingClass> 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<BakingClass>();
 
                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<MESback>(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);
            }
        }
 
    }
}