wcs
huanghongfeng
2025-01-16 b701e82fd920e401658b72a5b1c53feaf9ae5b77
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
using Autofac.Core;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonStackerCraneJob : IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
 
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonStackerCrane != null)
                {
                    //添加判断agv状态是否可下发
                    if (ReadAGVstatus(commonStackerCrane) == 1 && ReadAGVworkingmode(commonStackerCrane) == 1)
                    {
                        GetTask(commonStackerCrane);
                    }
 
                    //处理agv是否完成任务
                        
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 任务完成事件订阅的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
        {
            CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
            if (commonStackerCrane != null)
            {
                if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                {
                    Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                    _taskService.StackCraneTaskCompleted(e.TaskNum);
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                }
            }
        }
 
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="commonStackerCrane">堆垛机对象</param>
        /// <returns></returns>
        private void GetTask(CommonStackerCrane commonStackerCrane)
        {
            Dt_Task task;
            task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
            if (task != null)
            {
                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                if (stackerCraneTaskCommand != null)
                {
                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                    if (sendFlag)
                    {
                        _taskService.UpdateTaskStatusToNext(task.TaskNum);
                    }
                }
            }
        }
 
 
        //读取AGV状态
        public int ReadAGVstatus(CommonStackerCrane commonStackerCrane)
        {
            var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVCraneStatus");
            return deviceProDTO != null ? commonStackerCrane.Communicator.Read<int>(deviceProDTO.DeviceProAddress) : 99;
        }
        //读取AGV工作模式
        public int ReadAGVworkingmode(CommonStackerCrane commonStackerCrane)
        {
            var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVWorkingmode");
            return deviceProDTO != null ? commonStackerCrane.Communicator.Read<int>(deviceProDTO.DeviceProAddress) : 99;
        }
 
        //读取任务号
        public int Readtasknumber(CommonStackerCrane commonStackerCrane)
        {
            var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVWorkingmode");
            return deviceProDTO != null ? commonStackerCrane.Communicator.Read<int>(deviceProDTO.DeviceProAddress) : 99;
        }
 
 
        /// <summary>
        /// 任务实体转换成命令Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
 
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            stackerCraneTaskCommand.TrayType = 0;
            stackerCraneTaskCommand.StartAdder = Convert.ToInt16(task.SourceAddress);
            stackerCraneTaskCommand.EndAdder = Convert.ToInt16(task.TargetAddress);
 
            return stackerCraneTaskCommand;
 
            
        }
    }
}