schangxiang@126.com
2025-09-04 e9269528ae01bc4572b68148b9c801b51f23b669
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using Newtonsoft.Json;
using System.Threading;
using yunneiWCS.EnumDefine;
using yunneiWCS.ORM;
using yunneiWCS.ExtendFunction;
 
namespace yunneiWCS.data
{
    public static class baseData
    {
        public static Mutex single = new Mutex();
        //static HTTPService api = new HTTPService("http://192.168.0.2:6546/api/v2/");//AGVapi基地址
        static HTTPService api = new HTTPService("http://192.168.0.3:6546/api/v2/");//AGVapi基地址
        /// <summary>发送堆垛机任务
        /// 
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <param name="sourcePlace">起始地址</param>
        /// <param name="toPlace">目标地址</param>
        /// <param name="isref">1=不是重发,2=重发</param>
        public static void sendSrmTask(task _task, long taskId, string SourcePlace, string ToPlace, int isref)
        {
            if (single.WaitOne())//增加线程锁防止并发
            {
                try
                {
                    //休眠800毫秒 【Editby shaocx,2024-10-09】
                    //再次校验 
                    if (!MyExtendHelper.IsAllowSendNewTaskToSrm(800))
                    {
                        //必须抛异常
                        throw new Exception("当前堆垛机不满足下发任务的条件,任务号:" + taskId + ",起点:" + SourcePlace + ",终点:" + ToPlace);
                    }
 
                    string[] sourcePlace = SourcePlace.Split('-');
                    string[] toPlace = ToPlace.Split('-');
                    Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_toPosX, Convert.ToInt16(toPlace[0]));//目标地址排
                    Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_toPosY, Convert.ToInt16(toPlace[1]));//目标地址列
                    Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_toPosZ, Convert.ToInt16(toPlace[2]));//目标地址层
                    Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_command, Convert.ToInt16(1));//执行方式 搬运
                    if (_task == null)
                    {
                        Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_TaskNo, Convert.ToInt32(taskId));//任务id
                    }
                    else
                    {
                        Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_TaskNo, Convert.ToInt32(_task.PlcTaskNo));//此处任务号更改 【EditBy shaocx,2022-03-28】
                    }
                    Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_sourcePosX, Convert.ToInt16(sourcePlace[0]));//起始地址排
                    Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_sourcePosY, Convert.ToInt16(sourcePlace[1]));//起始地址排
                    Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_sourcePosZ, Convert.ToInt16(sourcePlace[2]));//起始地址排
                    //记录任务记录
                    using (dbmodel mod = new dbmodel())
                    {
                        if (isref == 1)
                        {
                            taskRecord record = new taskRecord();
                            record.taskId = taskId;
                            record.sourcePlace = SourcePlace;
                            record.toPlace = ToPlace;
                            record.taskInfo = "堆垛机任务";
                            record.createTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
 
                            if (_task != null)
                            {//增加 【EditBy shaocx,2022-03-28】
                                record.AgvTaskNo = _task.AgvTaskNo;
                                record.PlcTaskNo = _task.PlcTaskNo;
                            }
 
                            mod.taskRecord.Add(record);
                            mod.SaveChanges();
                        }
 
                    }
                    int a = 0;
                    while (a < 5)//握手确认
                    {
                        if (Form1.srm.stb == "是")
                        {
                            Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_ack, Convert.ToInt16(1));//ack确认
                            break;
                        }
                        a++;
                        Thread.Sleep(1000);
                    }
                }
                catch (Exception ex)
                {
                    WZ.Useful.Commons.LogTextHelper.WriteLine("baseData", "sendSrmTask", ex.ToString());
                    throw ex;//必须抛出异常 【Editby shaocx,2024-10-09】
                }
                finally
                {
                    single.ReleaseMutex();
                }//一轮结束
            }
 
        }
        /// <summary>发送AGV任务
        /// 
        /// </summary>
        /// <param name="_task">任务对象</param>
        /// <param name="taskId">任务ID</param>
        /// <param name="sourcePlace">起始地址</param>
        /// <param name="toPlace">目标地址</param>
        /// <param name="isref">1=不是重发,2=重发</param>
        public static string sendAgvTask(task _task, long taskId, string sourcePlace, string toPlace, int isref)
        {
            //注意:_task肯定不为NULL
            string result = "失败";
            if (single.WaitOne())//增加线程锁防止并发
            {
                try
                {
                    var _agvTaskNo = _task.AgvTaskNo;
                    var _old_agvTaskNo = _task.AgvTaskNo;
                    if (isref == 2)
                    {
                        _agvTaskNo = TaskNoHelper.CreateTaskIdForAgvTask(_task.PlcTaskNo);//重发的时候,要重新生成任务号【Editby shaocx,2024-10-09】
                    }
                    var data = new
                    {
                        //id = taskId,
                        //id = _task.AgvTaskNo,//此处任务号更改 【EditBy shaocx,2022-03-28】
                        id = _agvTaskNo,//此处任务号更改 【EditBy shaocx,2022-03-28】
                        systemId = "WMS",
                        type = "LoadingAndUnloading",
                        source = sourcePlace,
                        destination = toPlace
                    };
                    string paprm = JsonConvert.SerializeObject(data);
                    Guid guid = System.Guid.NewGuid();
                    result = api.postContentForStringForAgv("orders", paprm, guid);
                    //记录AGV发送任务
                    if (result == "")
                    {
                        if (isref == 1)
                        {
                            using (dbmodel mod = new dbmodel())
                            {
                                taskRecord record = new taskRecord();
                                record.taskId = taskId;
                                record.sourcePlace = sourcePlace;
                                record.toPlace = toPlace;
                                record.taskInfo = "AGV任务";
                                record.createTime = DateTime.Now;
                                if (_task != null)
                                {//增加 【EditBy shaocx,2022-03-28】
                                    record.AgvTaskNo = _task.AgvTaskNo;
                                    record.PlcTaskNo = _task.PlcTaskNo;
                                }
 
                                mod.taskRecord.Add(record);
                                mod.SaveChanges();
                            }
                        }
                        else
                        {
                            //重发,需要更新AGV任务号 【Editby shaocx,2024-10-09】
                            using (dbmodel mod = new dbmodel())
                            {
                                var taskDb = mod.task.Where(t => t.taskId == _task.taskId).FirstOrDefault();
                                taskDb.AgvTaskNo = _agvTaskNo;
                                //子任务的agv任务号
                                var taskRecord = mod.taskRecord.Where(t => t.AgvTaskNo == _old_agvTaskNo).FirstOrDefault();
                                if (taskRecord == null)
                                {
                                    result = "根据agv任务号" + _old_agvTaskNo + "没有找到agv任务";
                                    return result;
                                }
                                taskRecord.AgvTaskNo = _agvTaskNo;
 
                                mod.SaveChanges();
                            }
                        }
 
                    }
                    else
                    {
                        AgvRetModel agvRet = JsonConvert.DeserializeObject<AgvRetModel>(result);
                        if (agvRet != null)
                        {
                            result = agvRet.message;
                        }
                        else
                        {
                            result = "AGV返回失败状态码";
                        }
 
                        WZ.Useful.Commons.LogTextHelper.WriteLine("baseData", "sendAgvTask", "发送AGV任务失败Taskid:" + taskId + " 发送结果: " + result + " 是否重发:" + (isref == 1 ? "不是" : "是"));
                    }
                }
                catch (Exception ex)
                {
                    result = "AGV发送异常:" + ex.Message;
                    WZ.Useful.Commons.LogTextHelper.WriteLine("baseData", "sendAgvTask" + "发送AGV任务异常Taskid:" + taskId, ex.ToString());
                }
 
                finally { single.ReleaseMutex(); }//一轮结束
 
            }
            return result;
        }
 
        /// <summary>堆垛机ACK确认
        /// 
        /// </summary>
        public static void sendSrmACK()
        {
            Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_ack, Convert.ToInt16(1));//ack确认
 
        }
        /// <summary>堆垛机任务确认
        /// 
        /// </summary>
        public static void srmConfirm()
        {
 
            Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_Task_Finish_ACK, Convert.ToInt16(1));//确认任务
        }
        public static void srmAlarmReset()
        {
            Form1.siemensTcpNet_srm.Write(Form1.srmdb.W_alarmAck, Convert.ToInt16(1));//确认任务
 
        }
 
        public static List<AgvStatus> readAgvs()
        {
            Guid guid = System.Guid.NewGuid();
            string res = api.getContentForString("agvStatus", guid);
 
            List<AgvStatus> result = JsonConvert.DeserializeObject<List<AgvStatus>>(res);
            return result;
 
        }
    }
}