wangxinhui
12 小时以前 67348f250a1b7970059698002949a5e0a5f3c52f
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_DTO.Agv;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_Tasks;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.DBNames;
 
namespace WIDESEAWCS_Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AGVController : ControllerBase
    {
        private readonly IStationMangerRepository _stationMangerRepository;
        private readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
 
        public AGVController(IStationMangerRepository stationMangerRepository, ITaskService taskService, ITaskRepository taskRepository)
        {
            _stationMangerRepository = stationMangerRepository;
            _taskService = taskService;
            _taskRepository = taskRepository;
        }
 
        
 
        /// <summary>
        /// 安全信号申请 AGV-WCS
        /// </summary>
        /// <param name="secureApplyModel"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("AgvSecureApply"), AllowAnonymous]
        public AgvResponseContent AgvSecureApply([FromBody] AgvSecureApplyDTO secureApplyModel)
        {
            AgvResponseContent agvResponseContent = new AgvResponseContent();
            agvResponseContent.ReqCode = secureApplyModel.ReqCode;
            try
            {
                var task = _taskRepository.QueryFirst(x => secureApplyModel.TaskCode == x.AgvTaskNum);
                if (task == null) throw new Exception("未找到任务");
                if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt())
                {
                    var content = TakeRequest(task.CurrentAddress);
                    //if (!content.Status)
                    //{
                    //    throw new Exception(content.Message);
                    //}
                }
                else
                {
                    var content = PutRequest(task.NextAddress, task.PalletType);
                    //if (!content.Status)
                    //{
                    //    throw new Exception(content.Message);
                    //}
                }
                task.TaskState = TaskStatusEnum.AGV_WaitToExecute.ObjToInt();
                var up = _taskRepository.UpdateData(task);
                agvResponseContent.Code = up ? "0" : "1";
                agvResponseContent.Message = up ? "成功" : "失败";
            }
            catch (Exception ex)
            {
                agvResponseContent.Code = "1";
                agvResponseContent.Message = ex.Message;
            }
            return agvResponseContent;
            //return _taskService.AgvSecureApply(secureApplyModel);
        }
        /// <summary>
        /// 提升机申请 AGV-WCS
        /// </summary>
        /// <param name="secureApplyModel"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("AgvHoisterApply"), AllowAnonymous]
        public AgvResponse AgvHoisterApply([FromBody] AgvHoisterApplyDTO agvHoisterApplyDTO)
        {
            AgvResponse agvResponse = new AgvResponse();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == agvHoisterApplyDTO.HoisterCode);
                if (device == null)
                {
                    return agvResponse.Error($"未找到对应设备{agvHoisterApplyDTO.HoisterCode}");
                }
                OtherDevice otherDevice = (OtherDevice)device;
                //获取设备协议
                DeviceProDTO? deviceProRead = otherDevice.DeviceProDTOs.Where(x => x.DeviceChildCode == device.DeviceCode && x.DeviceProParamType == nameof(R_HoisterDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                DeviceProDTO? deviceProWrite = otherDevice.DeviceProDTOs.Where(x => x.DeviceChildCode == device.DeviceCode && x.DeviceProParamType == nameof(W_HoisterDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                if (deviceProRead == null || deviceProWrite == null)
                {
                    return agvResponse.Error($"未找到设备{device.DeviceCode}协议");
                }
                //获取读取和写入
                R_HoisterInfo r_HoisterInfo = otherDevice.Communicator.ReadCustomer<R_HoisterInfo>(deviceProRead.DeviceProAddress);
                W_HoisterInfo w_HoisterInfo = otherDevice.Communicator.ReadCustomer<W_HoisterInfo>(deviceProWrite.DeviceProAddress);
                if (r_HoisterInfo == null || w_HoisterInfo == null)
                {
                    return agvResponse.Error($"未读取到设备{device.DeviceCode}信号");
                }
                if (r_HoisterInfo.R_HoisterSysStatus!=3)
                {
                    return agvResponse.Error($"设备状态不为自动");
                }
                QuartzLogger.WriteLogToFile("Info_" + device.DeviceCode, $"r_HoisterInfo:{r_HoisterInfo.Serialize()}{Environment.NewLine}w_HoisterInfo:{w_HoisterInfo.Serialize()}{Environment.NewLine}");
                bool IsSuccess = false;
                switch (agvHoisterApplyDTO.Request)
                {
                    case "1":
                        //判断提升机是否在当前层 如果当前层,关门状态,无货待命直接写入开门信号
                        if (r_HoisterInfo.R_HoisterCurrent == agvHoisterApplyDTO.CurrentLayer && r_HoisterInfo.R_HoisterLoadStatus == 2 && r_HoisterInfo.R_HoisterDoorStatus == 2)
                        {
                            otherDevice.SetValue(W_HoisterDB.W_HoisterDoorStatus, 1);
                            IsSuccess = true;
                        }
                        else if (r_HoisterInfo.R_HoisterCurrent == agvHoisterApplyDTO.CurrentLayer && r_HoisterInfo.R_HoisterLoadStatus == 2 && r_HoisterInfo.R_HoisterDoorStatus == 1)
                        {
                            IsSuccess = true;
                        }
                        else if (r_HoisterInfo.R_HoisterCurrent != agvHoisterApplyDTO.CurrentLayer && r_HoisterInfo.R_HoisterLoadStatus == 2 && r_HoisterInfo.R_HoisterDoorStatus == 1)
                        {
                            otherDevice.SetValue(W_HoisterDB.W_HoisterDoorStatus, 2);
                        }
                        //判断提升机不在当前层 如果不在当前层,关门状态,无货待命直接写入移动开门信号(当前)
                        else if (r_HoisterInfo.R_HoisterCurrent != agvHoisterApplyDTO.CurrentLayer && r_HoisterInfo.R_HoisterLoadStatus == 2 && r_HoisterInfo.R_HoisterDoorStatus == 2)
                        {
                            otherDevice.SetValue(W_HoisterDB.W_HoisterTarget, agvHoisterApplyDTO.CurrentLayer);
                            otherDevice.SetValue(W_HoisterDB.W_HoisterTask, 99);
                        }
                        return IsSuccess ? agvResponse.OK($"请求{agvHoisterApplyDTO.Request}成功,任务{agvHoisterApplyDTO.ReceiveTaskID}","1"): agvResponse.Error($"请求{agvHoisterApplyDTO.Request}失败,任务{agvHoisterApplyDTO.ReceiveTaskID}");
                    case "2":
                        //判断提升机是否在当前层 如果当前层,开门状态,无货待命 放行
                        if (r_HoisterInfo.R_HoisterCurrent == agvHoisterApplyDTO.CurrentLayer && r_HoisterInfo.R_HoisterLoadStatus == 2 && r_HoisterInfo.R_HoisterDoorStatus == 1)
                        {
                            IsSuccess = true;
                        }
                        return IsSuccess ? agvResponse.OK($"请求{agvHoisterApplyDTO.Request}成功,任务{agvHoisterApplyDTO.ReceiveTaskID}", "2") : agvResponse.Error($"请求{agvHoisterApplyDTO.Request}失败,任务{agvHoisterApplyDTO.ReceiveTaskID}");
                    case "3":
                        //判断提升机是否在当前层 如果当前层,开门状态,有货待命写入关门信号
                        if (r_HoisterInfo.R_HoisterCurrent == agvHoisterApplyDTO.CurrentLayer && r_HoisterInfo.R_HoisterLoadStatus == 1 && r_HoisterInfo.R_HoisterDoorStatus == 1 && w_HoisterInfo.W_HoisterDoorStatus!=2)
                        {
                            otherDevice.SetValue(W_HoisterDB.W_HoisterDoorStatus, 2);
                        }
                        //判断提升机是否在当前层 如果当前层,关门状态,有货待命写入移动开门信号(目的)
                        if (r_HoisterInfo.R_HoisterCurrent == agvHoisterApplyDTO.CurrentLayer && r_HoisterInfo.R_HoisterLoadStatus == 1 && r_HoisterInfo.R_HoisterDoorStatus == 2)
                        {
                            otherDevice.SetValue(W_HoisterDB.W_HoisterTarget, agvHoisterApplyDTO.TargetLayer);
                            otherDevice.SetValue(W_HoisterDB.W_HoisterDoorStatus, 1);
                            otherDevice.SetValue(W_HoisterDB.W_HoisterTask, 99);
                            IsSuccess = true;
                        }
                        return IsSuccess ? agvResponse.OK($"请求{agvHoisterApplyDTO.Request}成功,任务{agvHoisterApplyDTO.ReceiveTaskID}", "3") : agvResponse.Error($"请求{agvHoisterApplyDTO.Request}失败,任务{agvHoisterApplyDTO.ReceiveTaskID}");
                    case "4":
                        //判断提升机是否在目的层 如果目的层,开门状态,有货待命 放行
                        if (r_HoisterInfo.R_HoisterCurrent == agvHoisterApplyDTO.TargetLayer && r_HoisterInfo.R_HoisterLoadStatus == 1 && r_HoisterInfo.R_HoisterDoorStatus == 1)
                        {
                            IsSuccess = true;
                        }
                        return IsSuccess ? agvResponse.OK($"请求{agvHoisterApplyDTO.Request}成功,任务{agvHoisterApplyDTO.ReceiveTaskID}", "4") : agvResponse.Error($"请求{agvHoisterApplyDTO.Request}失败,任务{agvHoisterApplyDTO.ReceiveTaskID}");
                    case "5":
                        //判断提升机是否在目的层 如果目的层,开门状态,无货待命写入关门信号
                        if (r_HoisterInfo.R_HoisterCurrent == agvHoisterApplyDTO.TargetLayer && r_HoisterInfo.R_HoisterLoadStatus == 2 && r_HoisterInfo.R_HoisterDoorStatus == 1)
                        {
                            otherDevice.SetValue(W_HoisterDB.W_HoisterDoorStatus, 2);
                            IsSuccess = true;
                        }
                        return IsSuccess ? agvResponse.OK($"请求{agvHoisterApplyDTO.Request}成功,任务{agvHoisterApplyDTO.ReceiveTaskID}", "5") : agvResponse.Error($"请求{agvHoisterApplyDTO.Request}失败,任务{agvHoisterApplyDTO.ReceiveTaskID}");
                    default:
                        return agvResponse.Error($"未找到对应请求,Request:{agvHoisterApplyDTO.Request}");
                }
            }
            catch (Exception ex)
            {
                agvResponse.Error($"请求{agvHoisterApplyDTO.Request}失败,错误{ex.Message},任务{agvHoisterApplyDTO.ReceiveTaskID}");
            }
            return agvResponse;
        }
        /// <summary>
        /// AGV任务更新/完成
        /// </summary>
        /// <param name="agvUpdateModel"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("AgvCallback"), AllowAnonymous]
        public AgvResponseContent AgvUpdateTask([FromBody] AgvUpdateDTO agvUpdateModel)
        {
            AgvResponseContent agvResponseContent = new AgvResponseContent();
            try
            {
                if (agvUpdateModel == null) throw new Exception("未获取到请求参数");
                agvResponseContent.ReqCode = agvUpdateModel.ReqCode;
                var task = _taskRepository.QueryFirst(x => agvUpdateModel.TaskCode == x.AgvTaskNum);
                if (task == null) throw new Exception($"未找到任务,任务号【{agvUpdateModel.TaskCode}】");
                switch (agvUpdateModel.Method)
                {
                    case "start":
                        break;
                    case "outbin"://出库根据这个信号判断取货完成
                        if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt())
                        {
                            var content = TakeFinish(task.CurrentAddress);
                            if (!content.Status) throw new Exception(content.Message);
                            task.TaskState = TaskStatusEnum.AGV_Finish.ObjToInt();
                            var up = _taskRepository.DeleteAndMoveIntoHty(task, OperateTypeEnum.自动完成);
                            _taskService.TaskCompleted(task.TaskNum);
                            agvResponseContent.Code = up ? "0" : "1";
                            agvResponseContent.Message = up ? "成功" : "失败";
                            return agvResponseContent;
                        }
                        break;
                    case "getSafetySignal"://安全信号申请
                        {
                            if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt())
                            {
                                var content = TakeRequest(task.CurrentAddress);
                                //if (!content.Status) throw new Exception(content.Message);
                            }
                            else
                            {
                                var content = PutRequest(task.NextAddress, task.PalletType);
                                //if (!content.Status) throw new Exception(content.Message);
                            }
                            task.TaskState = TaskStatusEnum.AGV_WaitToExecute.ObjToInt();
                            var up = _taskRepository.UpdateData(task);
                            agvResponseContent.Code = up ? "0" : "1";
                            agvResponseContent.Message = up ? "成功" : "失败";
                            return agvResponseContent;
                        }
                    case "end"://入库根据这个信号判断放货完成
                        if (task.TaskType != TaskTypeEnum.Outbound.ObjToInt())
                        {
                            var content = PutFinish(task.NextAddress);
                            if (!content.Status) throw new Exception(content.Message);
                            Dt_StationManger dt_Station = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == task.NextAddress);
                            if (dt_Station == null)
                            {
                                throw new Exception($"{task.NextAddress}AGV站台未定义");
                            }
                            task.CurrentAddress = dt_Station.StationCode;
                            task.NextAddress = "";
                            task.TaskState = TaskStatusEnum.AGV_Finish.ObjToInt();
                            task.DeviceCode = "SC01_CSJ";
                            var up = _taskRepository.UpdateData(task);
                            agvResponseContent.Code = up ? "0" : "1";
                            agvResponseContent.Message = up ? "成功" : "失败";
                            return agvResponseContent;
                        }
                        break;
                    case "cancel":
                        task.TaskState = TaskStatusEnum.Cancel.ObjToInt();
                        _taskRepository.UpdateData(task);
                        break;
                    default:
                        throw new Exception($"未定义方法名【{agvUpdateModel.Method}】");
                }
                agvResponseContent.Code = "0";
                agvResponseContent.Message = "成功";
            }
            catch (Exception ex)
            {
                agvResponseContent.Code = "1";
                agvResponseContent.Message = ex.Message;
            }
            return agvResponseContent;
            //return _taskService.AgvUpdateTask(agvUpdateModel);
        }
 
        /// <summary>
        /// 放货请求
        /// </summary>
        /// <param name="code"></param>
        /// <param name="palletType"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("PutRequest"), AllowAnonymous]
        public WebResponseContent PutRequest(string code, int palletType)
        {
            try
            {
                Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code);
                if (stationManger == null)
                {
                    return WebResponseContent.Instance.Error($"未找到站台配置");
                }
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                if (device == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应设备");
                }
 
                OtherDevice otherDevice = (OtherDevice)device;
 
                bool canPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
                if (canPut)
                {
                    otherDevice.SetValue(GroundStationDBName.W_PutRequest, true, stationManger.StationCode);
                    return WebResponseContent.Instance.OK();
                }
                else
                {
                    otherDevice.SetValue(GroundStationDBName.W_PutRequest, true, stationManger.StationCode);
                    Thread.Sleep(1000);
                    canPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
                    if (canPut)
                    {
                        return WebResponseContent.Instance.OK();
                    }
                    else
                    {
                        return WebResponseContent.Instance.Error($"放货申请中");
                    }
                }
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        //[HttpPost, HttpGet, Route("PutFinish"), AllowAnonymous]
        /// <summary>
        /// 放货完成
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("PutFinish"), AllowAnonymous]
        public WebResponseContent PutFinish(string code)
        {
            try
            {
                Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code);
                if (stationManger == null)
                {
                    return WebResponseContent.Instance.Error($"未找到站台配置");
                }
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                if (device == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应设备");
                }
 
                OtherDevice otherDevice = (OtherDevice)device;
                otherDevice.SetValue(GroundStationDBName.W_PutFinish, true, stationManger.StationCode);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        //[HttpPost, HttpGet, Route("TakeRequest"), AllowAnonymous]
        /// <summary>
        /// 取货请求
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("TakeRequest"), AllowAnonymous]
        public WebResponseContent TakeRequest(string code)
        {
            try
            {
                Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code);
                if (stationManger == null)
                {
                    return WebResponseContent.Instance.Error($"未找到站台配置");
                }
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                if (device == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应设备");
                }
 
                OtherDevice otherDevice = (OtherDevice)device;
 
                bool canPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanTake, stationManger.StationCode);
                if (canPut)
                {
                    otherDevice.SetValue(GroundStationDBName.W_TakeRequest, true, stationManger.StationCode);
                    return WebResponseContent.Instance.OK();
                }
                else
                {
                    otherDevice.SetValue(GroundStationDBName.W_TakeRequest, true, stationManger.StationCode);
                    Thread.Sleep(1000);
                    canPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanTake, stationManger.StationCode);
                    if (canPut)
                    {
                        return WebResponseContent.Instance.OK();
                    }
                    else
                    {
                        return WebResponseContent.Instance.Error($"取货申请中");
                    }
                }
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        //[HttpPost, HttpGet, Route("TakeFinish"), AllowAnonymous]
        /// <summary>
        /// 取货完成
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("TakeFinish"), AllowAnonymous]
        public WebResponseContent TakeFinish(string code)
        {
            try
            {
                Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code);
                if (stationManger == null)
                {
                    return WebResponseContent.Instance.Error($"未找到站台配置");
                }
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                if (device == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应设备");
                }
 
                OtherDevice otherDevice = (OtherDevice)device;
                otherDevice.SetValue(GroundStationDBName.W_TakeFinish, true, stationManger.StationCode);
                Thread.Sleep(1000);
                otherDevice.SetValue(GroundStationDBName.W_TakeFinish, false, stationManger.StationCode);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}