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
using Autofac.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using Org.BouncyCastle.Asn1.Ocsp;
using System.Text;
using System.Text.RegularExpressions;
using WIDESEA_DTO.Agv;
using WIDESEA_External.Model;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.Agv;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_TaskInfoService;
using WIDESEAWCS_Tasks;
using WIDESEAWCS_Tasks.DBNames;
using static Dm.net.buffer.ByteArrayBuffer;
 
namespace WIDESEAWCS_Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AGVController : ControllerBase
    {
        private readonly IStationMangerRepository _stationMangerRepository;
        private readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterRepository _routerRepository;
 
        public AGVController(IStationMangerRepository stationMangerRepository, ITaskService taskService, ITaskRepository taskRepository,IRouterRepository routerRepository)
        {
            _stationMangerRepository = stationMangerRepository;
            _taskService = taskService;
            _taskRepository = taskRepository;
            _routerRepository = routerRepository;
        }
        /// <summary>
        /// AGV任务更新
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("Callback"), AllowAnonymous]
        public WebResponseContent? Callback([FromBody]AgvUpdateDTO agvUpdateDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                WriteLog.Write_Log("AGV任务更新接口", "AGV任务更新接口", "更新任务", $"任务:{agvUpdateDTO.ToJson()}");
                if (agvUpdateDTO.Message != null && agvUpdateDTO.Message == "ROBOT_ERROR-haiUnknownError")
                {
                    var task1 = _taskRepository.QueryFirst(x => (agvUpdateDTO.MissionCode ?? "") == x.GroupId);
                    if (task1 != null)
                    {
                        Dt_ErrorInfo errorInfo = new Dt_ErrorInfo()
                        {
                            RobotCode = agvUpdateDTO.RobotId,
                            Message = agvUpdateDTO.Message,
                            ErrorType = 2
                        };
                        if (task1.TaskType == 500)
                        {
                            errorInfo.Message = "入库异常";
                            errorInfo.StationCode = task1.CurrentAddress;
                        }
                        else
                        {
                            errorInfo.Message = "出库异常";
                            errorInfo.StationCode = task1.NextAddress;
                        }
                        _taskService.AgvSearchStatus1(errorInfo);
                    }
                }
                var task = _taskRepository.QueryFirst(x => (agvUpdateDTO.ContainerCode ?? "") == x.PalletCode);
                switch (agvUpdateDTO.MissionStatus)
                {
                    case nameof(AGVStatusEnum.PICKER_RECEIVE):
                        if (task == null) throw new Exception($"未找到料箱【{agvUpdateDTO.ContainerCode}】任务");
                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_TakeFinish);
                        Dt_ErrorInfo e1 = new Dt_ErrorInfo()
                        {
                            RobotCode = agvUpdateDTO.RobotId,
                            Message = agvUpdateDTO.Message,
                            ErrorType = 0
                        };
                        _taskService.AgvSearchStatus1(e1);
 
                        break;
                    case nameof(AGVStatusEnum.PICKER_SEND):
                        if (task == null) throw new Exception($"未找到料箱【{agvUpdateDTO.ContainerCode}】任务");
                        Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.PickStationCode == task.NextAddress);
                        if (task.IsCancel > 0 && stationManger != null && stationManger.StationCode != agvUpdateDTO.CurrentPosition)
                        {
                            _taskService.TaskCancelCompleted(task.TaskNum);
                        }
                        else
                        {
                            _taskService.TaskCompleted(task.TaskNum);
                        }
                        Dt_ErrorInfo e2 = new Dt_ErrorInfo()
                        {
                            RobotCode = agvUpdateDTO.RobotId,
                            Message = agvUpdateDTO.Message,
                            ErrorType = 0
                        };
                        _taskService.AgvSearchStatus1(e2);
                        break;
                    case nameof(AGVStatusEnum.WAITFEEDBACK):
                        //AGV放行
                        List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt());
                        if (!stationMangers.Select(x => x.StationCode).Contains(agvUpdateDTO.CurrentPosition))
                        {
                            WebResponseContent responseContent = _taskService.AgvTaskFlow(agvUpdateDTO.MissionCode);
                            if (!responseContent.Status) throw new Exception($"{responseContent.Message}");
                        }
                        Dt_ErrorInfo e3 = new Dt_ErrorInfo()
                        {
                            RobotCode = agvUpdateDTO.RobotId,
                            Message = agvUpdateDTO.Message,
                            ErrorType = 0
                        };
                        _taskService.AgvSearchStatus1(e3);
                        break;
                    default:
                        break;
                }
 
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// AGV任务请求
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("WorkRequest"), AllowAnonymous]
        public AgvTaskReqContent WorkRequest([FromBody] AgvTaskRequestDTO agvTaskRequestDTO)
        {
            AgvTaskReqContent content = new AgvTaskReqContent();
            try
            {
                WriteLog.Write_Log("AGV任务请求", "AGV任务请求接口", "任务", $"任务:{agvTaskRequestDTO.ToJson()}");
                //获取任务
                Dt_Task? taskExist = _taskRepository.QueryFirst(x=>x.PalletCode==agvTaskRequestDTO.ContainerCode) ?? throw new Exception($"未找到料箱{agvTaskRequestDTO.ContainerCode}任务");
                //获取站台
                Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == agvTaskRequestDTO.PositionId) ?? throw new Exception($"未找到{agvTaskRequestDTO.PositionId}站台位置");
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode) ?? throw new Exception($"未找到对应设备{stationManger.StationDeviceCode}");
 
                OtherDevice commonConveyorLine = (OtherDevice)device;
 
                if (stationManger.StationType==StationTypeEnum.StationType_OnlyOutbound.ObjToInt())
                {
                    short IsPut = commonConveyorLine.Communicator.Read<short>("0");
                    if (IsPut != 256) throw new Exception($"{agvTaskRequestDTO.PositionId}禁止放箱");
                }
                else
                {
                    short IsTake = commonConveyorLine.Communicator.Read<short>("11");
                    if (IsTake != 256) throw new Exception($"{agvTaskRequestDTO.PositionId}禁止取箱");
                }
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// AGV作业完成
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("WorkFinish"), AllowAnonymous]
        public AgvTaskReqContent WorkFinish([FromBody] AgvTaskRequestDTO agvTaskRequestDTO)
        {
            AgvTaskReqContent content = new AgvTaskReqContent();
            try
            {
                WriteLog.Write_Log("AGV作业完成", "AGV作业完成接口", "任务", $"任务:{agvTaskRequestDTO.ToJson()}");
                //获取站台
                Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == agvTaskRequestDTO.PositionId) ?? throw new Exception($"未找到{agvTaskRequestDTO.PositionId}站台位置");
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode) ?? throw new Exception($"未找到对应设备{stationManger.StationDeviceCode}");
 
                OtherDevice commonConveyorLine = (OtherDevice)device;
 
                if (stationManger.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt())
                {
                    commonConveyorLine.Communicator.Write("21", new byte[] { 1, 0 });
                }
                else
                {
                    commonConveyorLine.Communicator.Write("23", new byte[] { 1, 0 });
                }
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// AGV作业完成
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("AgvSearchStatus"), AllowAnonymous]
        public void AgvSearchStatus()
        {
            _taskService.AgvSearchStatus();
        }
    }
}