schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
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
using iWareCommon.Utils;
using iWareModel.Entity.AGV;
using iWareModel.Entity.MES;
using iWareModel.Entity.WCS;
using iWareModel.Entity.WMS;
using Newtonsoft.Json;
using System;
using System.Dynamic;
using System.Net.Http;
using System.Windows.Forms;
 
namespace iWareCC.Common.Helper
{
    /// <summary>
    /// 跟WMS交互的帮助类
    /// </summary>
    public class WMSRequestHelper
    {
        /// <summary>
        /// WMS的主机host
        /// </summary>
        private static string wmsApiHost = ConfigHelper.GetConfigString("WmsApiHost");
        private static HttpClient _httpClient = null;
        private static string base_Path = wmsApiHost;
        /// <summary>
        /// 3巷道寻找空库位,用途:移库任务、入库任务
        /// </summary>
        /// <param name="high">货物高度</param>
        /// <param name="containerCode">要处理的托盘号</param>
        /// <param name="flag">标记,0:正常入库 1:移库</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static CriterionContainerOutput FindEmptyLocationForLane3(int high, string containerCode, int flag)
        {
            var flagName = flag == 1 ? "移库任务" : "入库任务";
            var logTitle = $"[{flagName}],容器号{containerCode},高度{high}。";
            var utl = string.Format(@"task/FindEmptyLocationForLane3?high={0}&containerCode={1}&flag={2}", high, containerCode, flag);
            var result = new HTTPService(wmsApiHost).postContentForString(utl, "", "");
 
            if (result == null)
            {
                throw new Exception(logTitle + "创建失败!");
            }
            Log4NetHelper.WriteErrorLog(LogType.CCWCFService, logTitle + "FindEmptyLocationForLane3 返回的值:" + result + ",参数:high:" + high
                + ",参数:containerCode:" + containerCode + ",参数:flag:" + flag + ",地址:" + wmsApiHost + "/task/FindEmptyLocationForLane3");
 
            var wmsSysReturn = JsonConvert.DeserializeObject<WmsSysReturn>(result);
            if (wmsSysReturn.success == false)
            {
                throw new Exception(logTitle + "请求WMS失败!" + wmsSysReturn.message);
            }
            if (wmsSysReturn.data == null)
            {
                return null;
            }
            var data = JsonConvert.DeserializeObject<CriterionContainerOutput>(wmsSysReturn.data.ToString());
            return data;
        }
 
        /// <summary>
        /// Post请求
        /// </summary>
        /// <typeparam name="TResult">返回参数的数据类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="data">传入的数据</param>
        /// <returns></returns>
        public static TResult Post<TResult>(string url, object data)
        {
            try
            {
                _httpClient = new HttpClient();
                _httpClient.BaseAddress = new Uri(base_Path);
 
                var jsonData = JsonConvert.SerializeObject(data);
                HttpContent content = new StringContent(jsonData);
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpResponseMessage res = _httpClient.PostAsync(url, content).Result;
                if (res.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    string resMsgStr = res.Content.ReadAsStringAsync().Result;
                    var result = JsonConvert.DeserializeObject<TResult>(resMsgStr);
                    return result;
                }
                else
                {
                    MessageBox.Show(res.StatusCode.ToString());
                    return default;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return default;
            }
        }
    }
}