wangxinhui
2025-09-06 2b25f973bb6d72ce6971d6f9c3cdccf51b7962ab
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
using Autofac.Core;
using AutoMapper;
using Microsoft.AspNetCore.Components.Routing;
using Newtonsoft.Json;
using Quartz;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
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_QuartzJob.Service;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public partial class ConveyorLineJob_YL2ndFloor : JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly IMapper _mapper;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterRepository _routerRepository;
        private readonly IStationMangerRepository _stationMangerRepository;
 
        public ConveyorLineJob_YL2ndFloor(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository,IMapper mapper, IRouterRepository routerRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _stationMangerRepository = stationMangerRepository;
            _mapper = mapper;
            _routerRepository = routerRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    #region 站台方式
 
                    List<Dt_StationManger> stationManagers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == conveyorLine.DeviceCode);
 
                    foreach (var station in stationManagers)
                    {
                        ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.StationCode);
 
                        DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand.InteractiveSignal) && x.ProtocalDetailValue == command.InteractiveSignal.ToString());
                        if (deviceProtocolDetails != null)
                        {
                            MethodInfo? method = GetType().GetMethod(deviceProtocolDetails.ProtocolDetailType);
                            if (method != null)
                            {
                                method.Invoke(this, new object[] { conveyorLine, command, station });
                            }
                        }
                    }
                    #endregion 站台方式
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(ConveyorLineJob_YL2ndFloor) + ":" + DateTime.Now + ":" + ex.ToString(), ex.StackTrace);
            }
            finally
            {
            }
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 输送线请求入库
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="ProtocalDetailValue">线体当前bool读取偏移地址</param>
        public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, Dt_StationManger stationManger)
        {
            try
            {
                
                var task = _taskService.QueryLineExecuteTaskByBarcode(command.TaskNum, stationManger.StationCode);
                var log = $"时间:【{DateTime.Now}】【{conveyorLine.DeviceName}】托盘号:【{command.Barcode}】任务号:【{command.TaskNum}】设备编码:【{stationManger.StationCode}】";
                ConsoleHelper.WriteSuccessLine(log);
 
                WriteInfo(conveyorLine.DeviceName, log);
 
                if(task != null)
                {
                    ConveyorLineTaskCommandWrite taskCommand = _mapper.Map<ConveyorLineTaskCommandWrite>(task);
 
                    bool sendFlag = SendCommand(taskCommand, conveyorLine, stationManger.StationCode);
                    if (sendFlag)
                    {
                        _taskService.UpdateTaskStatusToNext(task, stationManger);
                    }
                }
                else
                {
                    HandleNewTask(conveyorLine, command, stationManger);
                }
                
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.ToString());
            }
        }
 
        /// <summary>
        /// 输送线入库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="ProtocalDetailValue">线体当前bool读取偏移地址</param>
        public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, Dt_StationManger stationManger)
        {
            var task = _taskService.QueryExecutingTaskByBarcode(command.TaskNum, stationManger.StationCode);
            if (task != null && task.TaskState != (int)TaskStatusEnum.Line_Finish)
            {
                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task, stationManger);
 
                if (content.Status)
                {
                    conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, stationManger.StationCode);
                }
                Console.Out.WriteLine(content.Serialize());
            }
        }
 
        public bool SendCommand(ConveyorLineTaskCommandWrite taskCommand, CommonConveyorLine conveyorLine, string childDeviceCode)
        {
            conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode);
            conveyorLine.SetValue(ConveyorLineDBName.TargetAddress, taskCommand.TargetAddress, childDeviceCode);
            conveyorLine.SetValue(ConveyorLineDBName.Weight, taskCommand.Weight, childDeviceCode);
            conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, childDeviceCode);
 
            for (int i = 0; i < 6; i++)
            {
                ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(childDeviceCode);
                if (command != null)
                {
                    if (command.TaskNum == taskCommand.TaskNum && command.TargetAddress == taskCommand.TargetAddress && command.Weight == taskCommand.Weight)
                    {
                        WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务成功写入次数{i}写入任务【{JsonConvert.SerializeObject(taskCommand)}】");
                        return true;
                    }
                    if (command.TaskNum != taskCommand.TaskNum)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode);
                        Thread.Sleep(100);
                    }
                    if (command.TaskNum != taskCommand.TaskNum)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName.TaskNum, taskCommand.TaskNum, childDeviceCode);
                        Thread.Sleep(100);
                    }
                    if (command.TargetAddress != taskCommand.TargetAddress)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName.TargetAddress, taskCommand.TargetAddress, childDeviceCode);
                        Thread.Sleep(100);
                    }
                }
                conveyorLine.SetValue(ConveyorLineDBName.ResponState, 86, childDeviceCode);
            }
            WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务成功任务号【{taskCommand.TaskNum}】托盘号【{taskCommand.Barcode}】目标地址【{taskCommand.TargetAddress}】当前节点【{childDeviceCode}】");
            return false;
        }
    }
}