wangxinhui
14 小时以前 ff24fa06ba2d3a5e271789b8f81f3a3131d19470
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
using HslCommunication;
using HslCommunication.WebSocket;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using Quartz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common;
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_IBasicInfoRepository;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonAGVJob : JobBase, IJob
    {
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskService _taskService;
        private readonly ISys_ConfigService _sys_ConfigService;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        public CommonAGVJob(ITaskRepository taskRepository,ITaskService taskService,ISys_ConfigService configService,IDt_StationManagerRepository stationManagerRepository) 
        {
            _taskRepository = taskRepository;
            _taskService = taskService;
            _sys_ConfigService = configService;
            _stationManagerRepository = stationManagerRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            var newTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskAGVCarryStatusEnum.AGV_CarryNew.ObjToInt())).ToList().OrderBy(x => x.Grade).ThenBy(x => x.TaskNum).ToList();
            #region 任务下发
            if (newTasks.Count > 0)
            {
                WriteLog.Write_Log("AGV任务下发", "任务下发接口", "添加任务", $"任务:{newTasks.ToJson()}");
                try
                {
                    AgvTaskSendDTO agvTaskSend = new AgvTaskSendDTO()
                    {
                        MissionData = new List<MissionDataItem>()
                    };
                    string taskGroupId = Guid.NewGuid().ToString().Replace("-", "");
 
                    foreach (var task in newTasks)
                    {
                        //获取目标点货位
                        Dt_StationManager stationManagerStart = _stationManagerRepository.QueryFirst(x => x.stationLocation == task.CurrentAddress);
                        //获取拣选出库站台
                        Dt_StationManager stationManagerEnd = _stationManagerRepository.QueryFirst(x => x.stationLocation == task.NextAddress);
 
                        if (stationManagerStart == null || stationManagerStart == null) throw new Exception($"未找到任务号${task.TaskNum}起始点{task.CurrentAddress}或目标点{task.NextAddress}位置信息");
                        agvTaskSend.RequestId = taskGroupId;
                        agvTaskSend.MissionCode = task.TaskNum.ToString();
                        agvTaskSend.ViewBoardType = "W01";
                        //料箱子搬运任务
                        MissionDataItem missionDataItem = new MissionDataItem()
                        {
                            Sequence = task.TaskNum,
                            BinCode = task.PalletCode,
                            StartPosition = stationManagerStart.stationLocation,
                            EndPosition = stationManagerEnd.stationLocation,
                            TakeActionConfirm = false,
                            TakeActionInform = false,
                            PutActionConfirm = true,
                            PutActionInform = true,
                        };
                        agvTaskSend.MissionData.Add(missionDataItem);
                    }
                    if (newTasks.OrderByDescending(x => x.Grade).FirstOrDefault()?.Grade == 0)
                    {
                        agvTaskSend.Priority = 99;
                    }
                    else
                    {
                        agvTaskSend.Priority = 99 - newTasks.OrderByDescending(x => x.Grade).FirstOrDefault().Grade;
                    }
                    //发送AGV任务
                    WebResponseContent content = _taskService.AgvSendTask(agvTaskSend, APIEnum.AgvSendTask);
                    if (!content.Status)
                        throw new Exception(content.Message);
                    newTasks.ForEach(x =>
                    {
                        x.Dispatchertime = DateTime.Now;
                        x.TaskState = TaskAGVCarryStatusEnum.AGV_CarryExecuting.ObjToInt();
                    });
                    _taskService.UpdateData(newTasks);
                    Thread.Sleep(500);
                }
                catch (Exception ex)
                {
                    newTasks.ForEach(x =>
                    {
                        x.TaskState = TaskAGVCarryStatusEnum.AGV_CarryException.ObjToInt();
                        x.ExceptionMessage = ex.Message;
                    });
                    _taskService.UpdateData(newTasks);
                }
 
            }
            #endregion
            return Task.CompletedTask;
        }
 
    }
}