111
cjs
2025-06-06 03e67373c4c3bef21936ec1f9037f2ebcd434583
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
using Hanhe.iWCS.Common;
using HH.AMS.BLL;
using HH.AMS.BLL.AdminUI.Basic;
using HH.AMS.BLL.MongoDB;
using HH.AMS.Entitys.AdminUI.Basic;
using HH.AMS.Entitys.AdminUI.Task;
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;
using System.Threading.Tasks;
 
namespace HH.AMS.APIThread
{
    /// <summary>
    /// 生成任务主线程
    /// </summary>
    public class TaskThread
    {
        /// <summary>
        /// 主任务线程
        /// </summary>
        private Thread _MainThread { get; set; }
 
        private static readonly TaskThread instance = new TaskThread();
 
        private TaskThread() { }
 
        /// <summary>
        /// 获取单实例
        /// </summary>
        public static TaskThread Instance
        {
            get
            {
                return instance;
            }
        }
 
        /// <summary>
        /// 构造函数 单例模式 只执行一次
        /// </summary>
        public void Initialize()
        {
            _MainThread = new Thread(MainThreadHandle);
            TaskCommandList = new List<AddTaskEntityMongodb>();
        }
 
        private List<AddTaskEntityMongodb> TaskCommandList = new List<AddTaskEntityMongodb>();
        private IMongoQuery cmdQuery = null;
        private SortByDocument cmdSortBy = new SortByDocument { { "createDate", 1 } };
        /// <summary>
        /// 工作线程
        /// </summary>
        private void MainThreadHandle()
        {
            while (true)
            {
                if (TaskCommandList.Count == 0)
                {
                    try
                    {
                        TaskCommandList = MongoDBSingleton.Instance.Find<AddTaskEntityMongodb>(cmdQuery, 1, "_id_", typeof(AddTaskEntityMongodb).Name, cmdSortBy);
                    }
                    catch (Exception ex) { CMMLog.Error("获取AddTaskEntityMongodb关于AGV指令出错:" + ex.Message); }
                }
                else
                {
                    #region 循环模式
                    int taskNum = TaskCommandList.Count;
                    WebAPICommon _WebAPICommon = new WebAPICommon();
                    InfResultEntity ier = new InfResultEntity() { success = false };
                    foreach (AddTaskEntityMongodb newCmd in TaskCommandList)
                    {
                        try
                        {
                            AddTaskEntity _task = new AddTaskEntity();
                            _task.deviceName = newCmd.deviceName;
                            _task.ext1 = newCmd.ext1;
                            _task.ext2 = newCmd.ext2;
                            _task.ext3 = newCmd.ext3;
                            _task.extParam = newCmd.extParam;
                            _task.locationFrom = newCmd.locationFrom;
                            _task.locationTo = newCmd.locationTo;
                            _task.priority = newCmd.priority;
                            _task.sysName = newCmd.sysName;
                            _task.taskNo = newCmd.taskNo;
                            _task.whNoFrom = newCmd.whNoFrom;
                            _task.whNoTo = newCmd.whNoTo;
 
                            InfResultEntity r = _WebAPICommon.AddSingleTask(_task);
                            if (!r.success)
                            {
                                ier.failList.Add(new Result()
                                {
                                    taskNo = newCmd.taskNo,
                                    errCode = r.errCode,
                                    errMsg = r.errMsg
                                });
                            }
                            else
                            {
                                //任务生成成功,删除队列元素
                                var removeQuery = Query.EQ("_id", newCmd._id);
                                MongoDBSingleton.Instance.Remove<AddTaskEntityMongodb>(removeQuery, typeof(AddTaskEntityMongodb).Name);
                            }
                        }
                        catch (Exception ex)
                        {
                            ier.errCode = "3";
                            ier.errMsg = ex.Message + ":" + ex.StackTrace;
                        }
 
                        #region 获取异常信息
                        if (ier.failList.Count == 0)
                        {
                            ier.success = true;
                            ier.errCode = "0";
                            ier.errMsg = "";
                        }
                        else
                        {
                        }
                        #endregion
 
                        if (ier.failList.Count == taskNum)
                        {
                            ier.errCode = ier.failList[0].errCode;
                            ier.success = false;
                        }
                        else
                        {
                            ier.errCode = "0";
                            ier.success = true;
                        }
                        
                    }
                    string errResultValue = JsonConvert.SerializeObject(ier);
                    TN_AM_REQ_INF_LOGEntity log = new TN_AM_REQ_INF_LOGEntity();
                    log.CN_S_GUID = Guid.NewGuid().ToString();
                    log.CN_S_INTERFACE = "AsynAddTask";
                    log.CN_S_INTERFACE_NOTE = "异步创建任务";
                    log.CN_S_REQUEST = Newtonsoft.Json.JsonConvert.SerializeObject(TaskCommandList);
                    log.CN_S_RESPONSE = Newtonsoft.Json.JsonConvert.SerializeObject(errResultValue);
                    BLLCreator.Create<TN_AM_REQ_INF_LOGBLL>().Add(log);   
 
                    TaskCommandList.Clear();
                    #endregion
                }
                Thread.Sleep(1000);
            }
        }
 
 
        /// <summary>
        /// 开启服务
        /// </summary>
        public void Start()
        {
            _MainThread.Start();
        }
 
        /// <summary>
        /// 停止服务
        /// </summary>
        public void Stop()
        {
            _MainThread.Abort();
        }
    }
}