xiazhengtongxue
2026-01-04 46908c0f79e7aab8a3fa41bfdcd8390bbc3659f2
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
using Autofac.Core;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
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 System.Reflection.Metadata;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_DTO.TaskInfo;
 
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);
                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 AgvSearchStatus()
        {
            try
            {
                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();
                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 = "故障"
                            };
                            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);
                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);
            }
            
        }
    }
}