wangxinhui
2026-03-26 ad05ef2341fe19a4220f7ada1987636f9ec4a1a9
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
using Autofac.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.Agv;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_TaskInfoService
{
    public partial class TaskService
    {
        // 创建一个使用小驼峰命名法的序列化设置
        JsonSerializerSettings settings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };
        public WebResponseContent AgvSendTask(AgvTaskSendDTO taskModel, APIEnum SendTask = APIEnum.AgvSendTask)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                
                string? apiAddress = _apiInfoRepository.QueryFirst(x => x.ApiCode == SendTask.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(apiAddress)) throw new Exception($"未找到发送AGV任务接口,请检查接口配置");
                string request = JsonConvert.SerializeObject(taskModel, settings);
                string response = HttpHelper.Post(apiAddress, request);
                WriteLog.Write_Log("AGV入库任务下发", "出库任务下发接口", "请求任务", $"请求:{request},回传:{response}");
                AgvResponseContent agvContent = response.DeserializeObject<AgvResponseContent>() ?? throw new Exception("AGV任务发送未返回结果");
                if (agvContent.Success)
                {
                    content.OK();
                }
                else
                {
                    content.Error(agvContent.Message);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// AGV查询调用及WMS故障上报
        /// </summary>
        public void AgvSearchStatus1(Dt_ErrorInfo e2)
        {
            WriteLog.Write_Log("AGV查询调用及WMS故障上报", "AGV查询调用及WMS故障接口", "请求", $"请求:{e2.ToJson()}");
            if (e2 != null)
            {
                // 查询是否有错误没有就添加,有就取消
                List<Dt_ErrorInfo> existingErrors = _errorInfoRepository.QueryData(x =>
                    x.RobotCode == e2.RobotCode &&
                    x.ErrorType == 2); // 只查询故障类型为2的记录
 
                string? ErrorBack = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.WMSErrorBack.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(ErrorBack))
                {
                    return;
                }else{
                    // 如果是故障上报(e2.ErrorType == 2)
                    if (e2.ErrorType == 2)
                    {
                        // 检查是否已存在相同机器人的故障记录
                        if (existingErrors.Count == 0)
                        {
                            _errorInfoRepository.AddData(e2);
                            // 发送给WMS - 故障上报
                            TaskError taskError = new TaskError()
                            {
                                MsgID = e2.Id,
                                StationCode = e2.StationCode,
                                MsgCode = 1, // 1表示故障
                                Msg = e2.Message
                            };
                            string reqErrorBack = JsonConvert.SerializeObject(taskError, settings);
                            HttpHelper.Post(ErrorBack, reqErrorBack);
                        }
                        // 如果已存在故障记录,则不重复添加
                    }
                    // 如果是正常状态(e2.ErrorType == 0)且存在故障记录,则执行故障恢复
                    else if (e2.ErrorType == 0 && existingErrors.Count > 0)
                    {
                        // 删除故障记录
                        if (_errorInfoRepository.DeleteData(existingErrors))
                        {
                            // 发送给WMS - 故障恢复
                            foreach (var error in existingErrors)
                            {
                                TaskError taskError = new TaskError()
                                {
                                    MsgID = error.Id,
                                    StationCode = error.StationCode,
                                    MsgCode = 0, // 0表示恢复
                                    Msg = "故障恢复"
                                };
                                string reqErrorBack = JsonConvert.SerializeObject(taskError, settings);
                                HttpHelper.Post(ErrorBack, reqErrorBack);
                            }
                        }
                    }
                    return;
                }
  
            }
        }
        /// <summary>
        /// AGV状态查询调用及WMS故障上报
        /// </summary>
        public void AgvSearchStatus()
        {
            try
            {
                // 查询是否有任务
                List<Dt_Task> tasks = BaseDal.QueryData();
                // 如果没有任务,直接返回
                if (tasks == null || tasks.Count == 0)
                {
                    return;
                }
      
                AgvSearchStatusDTO agvSearchStatusDTO = new AgvSearchStatusDTO();
                string? apiAddress = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.AgvSearchStatus.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(apiAddress)) throw new Exception($"未找到AGV状态查询接口,请检查接口配置");
                string? apiErrorBack = _apiInfoRepository.QueryFirst(x => x.ApiCode == APIEnum.WMSErrorBack.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(apiErrorBack)) throw new Exception($"未找到WMS故障上报,请检查接口配置");
                string request = JsonConvert.SerializeObject(agvSearchStatusDTO, settings);
                string response = HttpHelper.Post(apiAddress, request);
                AgvStatusContent agvContent = response.DeserializeObject<AgvStatusContent>() ?? throw new Exception("AGV状态查询未返回结果");
 
                //获取所有故障信息
                List <Dt_ErrorInfo> errorInfos = _errorInfoRepository.QueryData(x => x.ErrorType == 1);
                int errorId = errorInfos.Count > 0 ? errorInfos.Max(x => x.Id) : 0;
                List<Dt_ErrorInfo> delErrorInfos = new List<Dt_ErrorInfo>();
                List<Dt_ErrorInfo> addErrorInfos = new List<Dt_ErrorInfo>();
                //获取任务信息
                //List<Dt_Task> tasks = BaseDal.QueryData();
                if (agvContent.Success)
                {
                    foreach (var item in agvContent.Data.Where(x => errorInfos.Select(x => x.RobotCode).Contains(x.RobotId) && x.Status != 7))
                    {
                        //上报故障恢复
                        Dt_ErrorInfo errorInfo = errorInfos.FirstOrDefault(x => x.RobotCode == item.RobotId);
                        
                        delErrorInfos.Add(errorInfo);
                    }
                    foreach (var item in agvContent.Data.Where(x => !x.MissionCode.IsNullOrEmpty() && !errorInfos.Select(x => x.RobotCode).Contains(x.RobotId) && x.Status == 7))
                    {
                        Dt_Task? task = tasks.FirstOrDefault(x=>x.TaskNum == item.MissionCode.ObjToInt() || x.GroupId==item.MissionCode);
                        if (task != null)
                        {
                            Dt_ErrorInfo errorInfo = new Dt_ErrorInfo()
                            {
                                RobotCode = item.RobotId,
                                Message = "故障",
                                ErrorType = 1,
                            };
                            if (task.TaskType==TaskTypeEnum.Inbound.ObjToInt())
                            {
                                errorInfo.StationCode = task.CurrentAddress;
                            }
                            else
                            {
                                errorInfo.StationCode = task.NextAddress;
                            }
                            addErrorInfos.Add(errorInfo);
                        }
                    }
                }
                //数据库操作
                _unitOfWorkManage.BeginTran();
                _errorInfoRepository.DeleteData(delErrorInfos);
                _errorInfoRepository.AddData(addErrorInfos);
                _unitOfWorkManage.CommitTran();
                List<Dt_ErrorInfo> newErrInfos = _errorInfoRepository.QueryData(x=>x.Id > errorId && x.ErrorType == 1);
                if (delErrorInfos.Count>0)
                {
                    foreach (var item in delErrorInfos)
                    {
                        TaskError taskError = new TaskError()
                        {
                            MsgID = item.Id,
                            StationCode = item.StationCode,
                            MsgCode = 0,
                            Msg = "恢复"
                        };
                        string reqErrorBack = JsonConvert.SerializeObject(taskError, settings);
                        HttpHelper.Post(apiErrorBack, reqErrorBack);
 
                    }
                }
                if (newErrInfos.Count>0)
                {
                    //上传故障
                    foreach (var item in newErrInfos)
                    {
                        TaskError taskError = new TaskError()
                        {
                            MsgID = item.Id,
                            StationCode = item.StationCode,
                            MsgCode = 1,
                            Msg = item.Message
                        };
                        string reqErrorBack = JsonConvert.SerializeObject(taskError, settings);
                        HttpHelper.Post(apiErrorBack, reqErrorBack);
                    }
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                throw new Exception(ex.Message);
            }
            
        }
        public WebResponseContent AgvCancelTask(AgvTaskCancelDTO taskModel, APIEnum SendTask = APIEnum.AgvTaskCancel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string? apiAddress = _apiInfoRepository.QueryFirst(x => x.ApiCode == SendTask.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(apiAddress)) throw new Exception($"未找到发送AGV任务接口,请检查接口配置");
                string request = JsonConvert.SerializeObject(taskModel, settings);
                string response = HttpHelper.Post(apiAddress, request);
                AgvResponseContent agvContent = response.DeserializeObject<AgvResponseContent>() ?? throw new Exception("AGV任务发送未返回结果");
                if (agvContent.Success)
                {
                    content.OK();
                }
                else
                {
                    content.Error(agvContent.Message);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}