111
cjs
2025-06-20 c248264e3bc150d145fd38f76f468f938270782a
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
using Hanhe.iWCS.Business;
using Hanhe.iWCS.Common;
using Hanhe.iWCS.DeviceDriver;
using Hanhe.iWCS.MData;
using Hanhe.iWCS.Model;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Hanhe.iWCS.TaizhouGEMTwoProtocol
{
    public class AMSHelper
    {
 
        public static HHAmsExecuteResult CreateTask(string taskNo, string locationFrom, string locationTo, string type, int priorityNo, string batchNo = "",string timeStamp = "") {
            var result = new HHAmsExecuteResult();
            List<HHAmsTaskEntity> entities = new List<HHAmsTaskEntity>();
            HHAmsTaskEntity entity = new HHAmsTaskEntity();
            entity.batchNo = batchNo;
            entity.taskNo = taskNo;
            entity.businessType = type;
            entity.locationFrom = locationFrom;
            entity.locationTo = locationTo;
            entity.priority = priorityNo;
            entity.ext2 = timeStamp;
            entity.deviceName = "AMS";
            entity.sysName = "AMS";
            entities.Add(entity);
 
            result = AddTask(entities);
 
            return result;
        }
        public static HHAmsExecuteResult AddTask(List<HHAmsTaskEntity> data) {
            HardwareAccessObject hao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_AMS);
            //string url = "http://10.15.73.28:9435/api/AmsInf/"; //正式环境需要改回去
            string msg = "";
            string feedback = "";
 
            var param = JsonConvert.SerializeObject(data);
            HHAmsExecuteResult amsResponse = new HHAmsExecuteResult() ;
            if (hao != null) {
            //if (string.IsNullOrEmpty(url)) {
                try {
                    string webAPIUrl = hao._WebApiClientInstance.WebApiUrl.Replace("AmsInf", "HHAms") + "AddTask";
                    //string webAPIUrl = url.Replace("AmsInf", "HHAms") + "AddTask";
                    Console.WriteLine(webAPIUrl);
                    feedback = new WebApiHelper().WebPost(webAPIUrl, param);
                    if (!string.IsNullOrEmpty(feedback)) {
                        amsResponse = JsonConvert.DeserializeObject<HHAmsExecuteResult>(feedback);
                        //var res = feedback.Replace("{", "").Replace("}", "").Replace(":", "=").Replace(",", ";").Replace('"', ' ');
                        if (amsResponse.success == true) {
                            msg = "【ams create task】success!;WebUrl=" + hao.WebUrl + "AmsInf/AddTask;param=" + param;
                        }
                        else {
                            string errMsg = amsResponse.errMsg;
                            msg = "【ams create task】fail!err=" + errMsg + ";WebUrl=" + webAPIUrl + ";param=" + param;
                        }
                    }
                    else {
                        string errMsg = "can't find the address";
                        msg = "【ams create task】fail!err=" + errMsg + ";WebUrl=" + webAPIUrl + ";param=" + param;
                    }
                }
                catch (Exception e) {
                    CMMLog.Error(string.Format("【ams create task】fail!err={0}", e.Message), e);
                }
            }
            else {
                msg = string.Format("ams create task fail,ams workcenter not set!");
            }
            if (msg != "") {
                CMMLog.Info(msg);
            }
            return amsResponse;
 
            //{"Success":false,"ErrorCode":"1","FailList":[{"Agv_TaskNo":"sample string 5","Error_Code":"1","Err_Msg":"未配置相关设备的事件解释!"},{"Agv_TaskNo":"sample string 5","Error_Code":"1","Err_Msg":"未配置相关设备的事件解释!"},{"Agv_TaskNo":"sample string 5","Error_Code":"1","Err_Msg":"未配置相关设备的事件解释!"}]}
        }
 
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="jasonStr"></param>
        /// <returns></returns>
        private static bool CreateAMSTask(string jasonStr) {
            bool bResult = false;
            HardwareAccessObject AMSCreateHao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_AMS);
            if (AMSCreateHao != null) {
                try {
                    string webAPIUrl = AMSCreateHao._WebApiClientInstance.WebApiUrl.Replace("AmsInf", "HHAms");
                    string amsReturnValue = AMSCreateHao._WebApiClientInstance.GetWebRequest(webAPIUrl + "AddTask", jasonStr, "application/json");
                    var dynamicObject = JsonConvert.DeserializeObject<dynamic>(amsReturnValue);
                    if (dynamicObject.success == "True" || dynamicObject.success == "true") {
                        bResult = true;
                        CMMLog.Info("AMS任务创建成功;请求参数=" + jasonStr + "");
                    }
                    else {
                        CMMLog.Info("AMS任务创建失败;请求参数=" + jasonStr + "");
                    }
                }
                catch (Exception ex) {
                    CMMLog.Info("AMS任务创建异常;请求参数=" + jasonStr + ";Exception=" + ex.Message);
                }
            }
            return bResult;
        }
 
        /// <summary>
        /// 变更任务目的地
        /// </summary>
        /// <param name="jasonStr"></param>
        /// <returns></returns>
        public static bool ChangeTaskTargetBit(string jasonStr) {
            bool bResult = false;
            HardwareAccessObject AMSCreateHao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_AMS);
            if (AMSCreateHao != null) {
                try {
                    string amsReturnValue = AMSCreateHao._WebApiClientInstance.GetWebRequest(AMSCreateHao._WebApiClientInstance.WebApiUrl + "ChangeTaskTargetBit", jasonStr, "application/json");
                    var dynamicObject = JsonConvert.DeserializeObject<dynamic>(amsReturnValue);
                    if (dynamicObject.Success == "True" || dynamicObject.Success == "true") {
                        bResult = true;
                        CMMLog.Info("AMS变更任务目的地成功;请求参数=" + jasonStr + "");
                    }
                    else {
                        CMMLog.Info("AMS变更任务目的地失败;请求参数=" + jasonStr + "");
                    }
                }
                catch (Exception ex) {
                    CMMLog.Info("AMS变更任务目的地异常;请求参数=" + jasonStr + ";Exception=" + ex.Message);
                }
            }
            return bResult;
        }
 
        /// <summary>
        /// 反馈AMS的状态
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="actionCode"></param>
        public static void SetStatus(string taskNo, int orderNo, string actionCode, string forkliftNo, bool success = false) {
            HardwareAccessObject AMSHao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_AMS);
            if (AMSHao != null)
            {
                //           if (actionCode == "2")
                //           {
                //actionCode = "完成";
                //           }
                string data = "{\"TaskNo\":\"" + taskNo + "\",\"OrderIndex\":\"" + orderNo + "\",\"TaskStatus\":\"" + actionCode + "\",\"ForkliftNo\":\"" + forkliftNo + "\",\"Success\":\"" + success + "\",\"ErrorMsg\":\"\"}";
                CMMLog.Debug(data);
                //string resultCode = AMSHao._WebApiClientInstance.GetWebRequest(AMSHao._WebApiClientInstance.WebApiUrl + "SetStatus?jsonData=" + data);
                WebApiHelper api = new WebApiHelper();
                api.WebPost(AMSHao._WebApiClientInstance.WebApiUrl + "SetStatus?jsonData=" + data, "");
            }
        }
 
        /// <summary>
        /// 消息交互日志
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="cmd"></param>
        /// <param name="statusText"></param>
        /// <param name="success"></param>
        /// <param name="errMsg"></param>
        /// <param name="createUser"></param>
        /// <param name="createUserName"></param>
        public static void TaskTrackLogMessage(string taskNo, string orderNo, string cmd, string statusText, string remark = "") {
            HardwareAccessObject AMSHao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_AMS);
            if (AMSHao != null) {
                string data = "{\"CN_N_ORDER\":\"" + orderNo + "\",\"CN_S_CMD\":\"" + cmd + "\",\"CN_S_CREATOR\":\"wcs\",\"CN_S_CREATOR_NAME\":\"wcs\",\"CN_S_SUCCESS\":\"true\",\"CN_S_TASK_NO\":\"" + taskNo + "\",\"CN_S_TASK_STATUS\":\"" + statusText + "\",\"CN_S_ERROR\":\"" + remark + "\"}";
                string resultCode = AMSHao._WebApiClientInstance.GetWebRequest(AMSHao._WebApiClientInstance.WebApiUrl + "TaskTrackLogMessage?jsonData=" + data);
            }
        }
 
 
 
        public static void ChangeEndBitNew(TN_I_TASK_MST mstTask, out int agvBit) {
            string newEndBitCode = mstTask.CN_S_END_BIT;
            agvBit = StockInstance.Instance.GetAGVCodeForBitCode(newEndBitCode);
            UpdateEndBit(mstTask, newEndBitCode, agvBit);
        }
        private static void UpdateEndBit(TN_I_TASK_MST mstTask, string newEndBitCode, int agvBit) {
            var updateBuider = Update.Set("CN_S_END_BIT", newEndBitCode);//新目的地
            updateBuider = updateBuider.Set("CN_N_END_AGV_BIT", agvBit);
            MongoDBSingleton.Instance.Update<TN_I_TASK_DTL>(Query.EQ("CN_S_TASK_NO", mstTask.CN_S_TASK_NO), updateBuider, UpdateFlags.None);
            var query = Query.EQ("_id", mstTask._id);
            updateBuider = Update.Set("CN_S_END_BIT", newEndBitCode).Set("Ext3", "J2SSHB199");
            updateBuider = updateBuider.Set("CN_S_END_AGVBIT", agvBit.ToString());
            MongoDBSingleton.Instance.Update<TN_I_TASK_MST>(query, updateBuider, UpdateFlags.None);
        }
 
 
        public class HHAmsTaskEntity
        {
            //public List<TransportInfo> transportInfo { get; set; }
            public string batchNo { get; set; }
            public string businessType { get; set; }
            public string sysName { get; set; }
            public string deviceName { get; set; }
            public string taskNo { get; set; }
            public string locationFrom { get; set; }
            public string locationTo { get; set; }
            public int priority { get; set; }
            public string ext1 { get; set; }
            public string ext2 { get; set; }
            public string ext3 { get; set; }
            //public class TransportInfo {
            //    /// <summary>
            //    /// 物料编码
            //    /// </summary>
            //    public string itemCode { get; set; }
 
            //    /// <summary>
            //    /// 物料扩展编码
            //    /// </summary>
            //    public string itemPCode { get; set; }
 
            //    /// <summary>
            //    /// 物料名称
            //    /// </summary>
            //    public string itemName { get; set; }
 
            //    /// <summary>
            //    /// 托盘码
            //    /// </summary>
            //    public string trayCode { get; set; }
 
            //    /// <summary>
            //    /// 物料重量
            //    /// </summary>
            //    public string itemWeight { get; set; }
 
            //    /// <summary>
            //    /// 物料特征
            //    /// </summary>
            //    public string itemFeatures { get; set; }
            //}
 
        }
        public class HHAmsExecuteResult
        {
            public bool success { get; set; }
            /// <summary>
            /// 0:成功 1:数据异常 2:已存在任务 3:程序异常
            /// </summary>
            public string errCode { get; set; }
            public string errMsg { get; set; }
 
            private List<HHAmsAddTaskResult> _failList = new List<HHAmsAddTaskResult>();
            public List<HHAmsAddTaskResult> failList {
                get { return _failList; }
                set { _failList = value; }
            }
        }
        public class HHAmsAddTaskResult
        {
            public string taskNo { get; set; }
            public string errCode { get; set; }
            public string errMsg { get; set; }
        }
        public class WMSResponseModel
        {
            //{"success":false,"errCode":null,"errMsg":"算法返回结果库存不足","okList":[],"failList":[{"index":0,"errCode":null,"errMsg":"算法返回结果库存不足"}],"tasks":[]}
            public bool success { get; set; }
            public string errMsg { get; set; }
            public string errCode { get; set; }
            //public OKInfo[] tasks { get; set; }
            //public FailInfo[] failList { get; set; }
            public OKInfo[] okList { get; set; }
            public class FailInfo
            {
                public int index { get; set; }
                public string errCode { get; set; }
                public string errMsg { get; set; }
            }
 
            public class OKInfo
            {
                public string taskNo { get; set; }
                public string startStock { get; set; }
                public string startArea { get; set; }
                public string startBit { get; set; }
                public string endStock { get; set; }
                public string endArea { get; set; }
                public string endBit { get; set; }
            }
        }
 
 
 
 
        internal static void UpdatePriority(string taskNo, int priority) {
            //http://localhost:60116/api/AmsInf/UpdatePriority?taskNo=12312313&priority=2
            HardwareAccessObject AMSHao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_AMS);
            if (AMSHao != null) {
                string data = string.Format("taskNo={0}&priority={1}", taskNo, priority);
                CMMLog.Debug(data);
                //string resultCode = AMSHao._WebApiClientInstance.GetWebRequest(AMSHao._WebApiClientInstance.WebApiUrl + "UpdatePriority?" + data);
                WebApiHelper api = new WebApiHelper();
                api.WebPost(AMSHao._WebApiClientInstance.WebApiUrl + "UpdatePriority?" + data, "");
            }
        }
    }
}