123
wanshenmean
2025-04-12 7d8f716f4bead12d27b54949846fc38ba993335b
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
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Tasks
{
    public class MESAPIInvoke
    {
        // 使用静态HttpClient实例
        private static readonly HttpClient _client = new HttpClient
        {
            BaseAddress = new Uri("http://192.168.12.212:9003/api/WMS/"),
            DefaultRequestHeaders =
            {
                Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
                UserAgent = { new ProductInfoHeaderValue("MESClient", "1.0") }
            }
        };
 
        /// <summary>
        /// 烘箱物料绑定
        /// </summary>
        /// <param name="Devid"></param>
        /// <param name="Materials"></param>
        /// <returns></returns>
        public static MESback BakingFeedingBinding(string devId, List<BakingClass> materials)
        {
            var request = new BakingFeedingClass
            {
                Devid = devId,
                Materials = materials
            };
 
            var response =  _client.PostAsJsonAsync("BakingFeedingBinding", request).Result;
            response.EnsureSuccessStatusCode();
            return response.Content.ReadFromJsonAsync<MESback>().Result;
 
            //using (HttpClient client = new HttpClient())
 
            //{
            //    client.BaseAddress = new Uri("http://192.168.12.212:9003/api/WMS/BakingFeedingBinding");
            //    client.DefaultRequestHeaders.Accept.Clear();
            //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //    client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
 
            //    HttpResponseMessage response = client.PostAsJsonAsync("BakingFeedingBinding", req).Result;
            //    if (response.IsSuccessStatusCode)
            //    {
            //        MESback back = response.Content.ReadAsStringAsync().Result.FromJson<MESback>();
            //        return back;
            //    }
            //    else
            //    {
            //        throw new Exception("调用MESAPI失败!");
            //    }
            //}
        }
    }
 
    public class MESback
    {
        /// <summary>
        /// 0:成功,大于0:失败
        /// </summary>
        public int Code { get; set; }
 
        /// <summary>
        /// 反馈信息
        /// </summary>
        public string Message { get; set; }
 
        /// <summary>
        /// 物料类型
        /// </summary>
        public string MaterialType { get; set; }
 
        /// <summary>
        /// 物料批号,写入PLC
        /// </summary>
        public string BarCode { get; set; }
 
        public string ProcessName { get; set; }
    }
}