wangxinhui
2025-05-28 b35e72bcf8011cf6e30182b0800e2f6e1a18e149
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
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 WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    public partial class AGV_FLJob
    {
        /// <summary>
        /// 下发AGV任务
        /// </summary>
        public void SendAGVTask()
        {
            try
            {
                var newTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => ((x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt() && x.TaskType>=TaskTypeEnum.Inbound.ObjToInt()) 
                || (x.TaskState == TaskStatusEnum.New.ObjToInt() && x.TaskType < TaskTypeEnum.Inbound.ObjToInt()))
                && nameof(AGV_FLJob).Contains(x.DeviceCode) 
                && !string.IsNullOrEmpty(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                foreach (var task in newTasks)
                {
                    try
                    {
                        if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt() || task.TaskType == TaskTypeEnum.OutEmpty.ObjToInt() || task.TaskType == TaskTypeEnum.OutAllocate.ObjToInt())
                            task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                        else
                            task.NextAddress = GetAGVAddress(task.NextAddress);
                        AgvTaskDTO taskDTO = new AgvTaskDTO()
                        {
                            TaskCode = task.AgvTaskNum,
                            ReqCode = DateTime.Now.ToString("yyyyMMdd") + task.AgvTaskNum,
                            TaskTyp = task.TaskType < TaskTypeEnum.Inbound.ObjToInt() ? "FLC" : "FLR",
                            ctnrCode = task.PalletCode,
                            PositionCodePath = new List<CodePath>()
                        {
                            new CodePath()
                            {
                                type="05",
                                positionCode=task.CurrentAddress
                            },
                            new CodePath()
                            {
                                type="05",
                                positionCode=task.NextAddress
                            }
                        },
                        };
                        WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_FLSendTask);
                        if (!content.Status) throw new Exception(content.Message);
 
                        task.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
 
                    }
                    catch (Exception ex)
                    {
                        task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                        task.ExceptionMessage = ex.Message;
                    }
                }
                _taskService.UpdateData(newTasks);
            }
            catch (Exception ex)
            {
                WriteError(nameof(AGV_FLJob), ex.Message, ex);
            }
        }
        /// <summary>
        /// AGV取放货回调
        /// </summary>
        public void ContinueAGVTask()
        {
            try
            {
                //获取是否有安全申请中的任务
                var continueTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt()) && nameof(AGV_FLJob).Contains(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                foreach (var continueTask in continueTasks)
                {
                    Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.NextAddress);
                    if (stationManger == null)
                    {
                        continue;
                    }
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                    if (device == null)
                    {
                        continue;
                    }
                    OtherDevice otherDevice = (OtherDevice)device;
                    bool canPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
                    if (!canPut)
                    {
                        continue;
                    }
                    //获取调入参数
                    AGVBoxApplyPassDTO boxApplyPassDTO = new AGVBoxApplyPassDTO()
                    {
                        ReqCode = Guid.NewGuid().ToString().Replace("-", ""),
                        ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        TaskCode = continueTask.AgvTaskNum
                    };
                    boxApplyPassDTO.Type = "2";
                    //请求料箱回调接口
                    WebResponseContent content = _taskService.AgvBoxApplyPass(boxApplyPassDTO);
                    if (content.Status && boxApplyPassDTO.TaskCode == continueTask.AgvTaskNum)
                    {
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.AGV_Puting);
                    }
                    else
                    {
                        continueTask.ExceptionMessage = content.Message;
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.Exception);
                    }
                }
 
            }
            catch (Exception ex)
            {
                WriteError(nameof(AGV_CPJob), ex.Message, ex);
            }
        }
        /// <summary>
        /// 获取AGV地址
        /// </summary>
        /// <param name="Address"></param>
        /// <returns></returns>
        public static string GetAGVAddress(string Address)
        {
            string[] targetCodes = Address.Split("-");
            if (targetCodes.Length == 5)
            {
                var Row = Convert.ToInt16(targetCodes[1]);
                var Column = Convert.ToInt16(targetCodes[2]);
                var Layer = Convert.ToInt16(targetCodes[3]);
                var a = Row switch
                {
                    1 => "A1",
                    2 => "B1",
                    3 => "C1",
                    4 => "D1",
                    5 => "E1",
                    _ => throw new Exception($"未定义的排,地址:【{Address}】"),
                };
                var b = Layer > 9 ? "" + Layer : "0" + Layer;
                var c = Column > 9 ? "" + Column : "0" + Column;
                if (Column == 10) c = "010";
                Address = a + b + c;
            }
            else
            {
                throw new Exception($"地址有误,地址:【{Address}】");
            }
            return Address;
        }
    }
}