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
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using WIDESEA_Common.Tools;
using WIDESEA_Core.Configuration;
using WIDESEA_Core.Extensions;
using WIDESEA_Entity.DomainModels;
 
namespace WIDESEA_WCS.SchedulerExecute
{
    public class SendMESTask
    {
        public static async void SendMesTask(dt_agvtask agvtask, int type)
        {
            try
            {
                string lot = "";
                if (!string.IsNullOrEmpty(agvtask.agv_materbarcode))
                {
                    string[] lots = agvtask.agv_materbarcode.Split("*");
                    lot = lots[0];
                }
                
                dynamic body = new
                {
                    CmdID = 10101,
                    TaskNo = agvtask.agv_tasknum,
                    Type = type,
                    EID = "",
                    UploadTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Data = new
                    {
                        DevidGet = agvtask.agv_fromaddress,
                        DevidPut = agvtask.agv_toaddress,
                        LotNo = lot,
                        MaterialType= "1",
                        StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        EndTime = type > 0 ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : ""
                    }
                };
                HttpClient client = new HttpClient();
                var url = AppSetting.Configuration["MESURL"];
                var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                var response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
                WriteLog.Info("MES接口数据").Write($"调用接口成功,发送数据:{JsonConvert.SerializeObject(body)}返回数据:{response}  时间:{DateTime.Now}", "MES接口数据");
                //if (response.IsNullOrEmpty())
                //{
                //    for (int i = 0; i < 2; i++)
                //    {
                //        response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
                //        if (!response.IsNullOrEmpty())
                //        {
                //            var resp = JsonConvert.DeserializeObject<Response>(response);
                //            if ( resp.Data.Status != 1)//resp.Code != 200 ||
                //            {
                //                for (int j = 0; j < 2; j++)
                //                {
                //                    response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
                //                    resp = JsonConvert.DeserializeObject<Response>(response);
 
                //                    if (resp.Code == 200 || resp.Data.Status == 1) break;
                //                }
                //            }
                //        }
                //    }
                //}
                //else
                //{
                //    var resp = JsonConvert.DeserializeObject<Response>(response);
                //    if (resp.Data.Status != 1)//resp.Code != 200 || 
                //    {
                //        for (int i = 0; i < 2; i++)
                //        {
                //            response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
                //            resp = JsonConvert.DeserializeObject<Response>(response);
 
                //            if (resp.Code == 200 && resp.Data.Status == 1) break;
                //        }
                //    }
                //}
 
                var result = JsonConvert.DeserializeObject<Data>(response);
                WriteLog.Info("MES接口数据").Write($"调用接口1:{DateTime.Now}", "MES接口数据");
                if (result.Status != 1)
                {
                    WriteLog.Info("MES接口数据").Write($"调用接口失败:重新发送 时间:{DateTime.Now}", "MES接口数据");
                    for (int i = 0; i < 2; i++)
                    {
                        response = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
                        result = JsonConvert.DeserializeObject<Data>(response);
 
                        if (result.Status == 1) break;
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog.Info("MES接口数据").Write($"调用接口失败,返回数据:{ex.Message}  时间:{DateTime.Now}", "MES接口数据");
            }
 
        }
    }
 
    public class Data
    {
        public int Status { get; set; }
        public string Msg { get; set; }
    }
 
    public class Response
    {
        public int Code { get; set; }
        public string Msg { get; set; }
        public Data Data { get; set; }
    }
}