hutongqing
2024-10-15 0d07b90fd906e52ce486484aa53a6850983b1325
WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs
@@ -15,124 +15,208 @@
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using AutoMapper;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.ConveyorLineJob;
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonConveyorLineJob : IJob
    public class CommonConveyorLineJob : JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IStationManagerService _stationManagerService;
        private readonly IRouterService _routerService;
        private readonly IMapper _mapper;
        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IStationManagerService stationManagerService)
        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _stationManagerService = stationManagerService;
            _routerService = routerService;
            _mapper = mapper;
        }
        public Task Execute(IJobExecutionContext context)
        {
            //Console.Out.WriteLine(DateTime.Now);
            try
            {
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                List<Dt_StationManager> stationManagers = _stationManagerService.GetStationManagers();
                foreach (var item in stationManagers)
                if (conveyorLine != null)
                {
                    try
                    List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    List<Task> tasks = new List<Task>();
                    foreach (string childDeviceCode in childDeviceCodes)
                    {
                        ushort value = conveyorLine.GetValue<ConveyorLineDBName, ushort>(ConveyorLineDBName.InteractiveSignal, item.StationCode);
                        DeviceProtocolDetailDTO? deviceProtocolDetail = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.ProtocalDetailValue == value.ToString());
                        if (deviceProtocolDetail != null)
                        //Task task = Task.Run(() =>
                        //{
                        ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(childDeviceCode);
                        if (command != null)
                        {
                            ConveyorLineTaskCommand? command = new ConveyorLineTaskCommand()
                            DeviceProtocolDetailDTO? deviceProtocolDetail = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.ProtocalDetailValue == command.InteractiveSignal.ToString() && x.DeviceProParamName == nameof(ConveyorLineTaskCommand.InteractiveSignal));
                            if (deviceProtocolDetail != null)
                            {
                                InteractiveSignal = 0,
                                Barcode = "",
                                TargetAddress = Convert.ToInt32(item.DeviceCode),
                                TaskNum = 9988,
                                TrayType = 3
                            };
                            SignalType signalType = Enum.Parse<SignalType>(deviceProtocolDetail.ProtocolDetailType);
                            switch (signalType)
                            {
                                case SignalType.InEntranceRequest:
                                    command.Barcode = "InEntranceRequest";
                                    break;
                                case SignalType.InEntranceResponse:
                                    command.Barcode = "InEntranceResponse";
                                    break;
                                case SignalType.InEntranceRequestReset:
                                    command.Barcode = "InEntranceRequestReset";
                                    break;
                                case SignalType.OutEntranceRequest:
                                    command.Barcode = "OutEntranceRequest";
                                    break;
                                case SignalType.OutEntranceResponse:
                                    command.Barcode = "OutEntranceResponse";
                                    break;
                                case SignalType.OutEntranceRequestReset:
                                    command.Barcode = "OutEntranceRequestReset";
                                    break;
                                case SignalType.InStationRequest:
                                    command.Barcode = "InStationRequest";
                                    break;
                                case SignalType.InStationResponse:
                                    command.Barcode = "InStationResponse";
                                    break;
                                case SignalType.InStationRequestReset:
                                    command.Barcode = "InStationRequestReset";
                                    break;
                                case SignalType.OutStationRequest:
                                    command.Barcode = "OutStationRequest";
                                    break;
                                case SignalType.OutStationResponse:
                                    command.Barcode = "OutStationResponse";
                                    break;
                                case SignalType.OutStationRequestReset:
                                    command.Barcode = "OutStationRequestReset";
                                    break;
                                default:
                                    command = null;
                                    break;
                                MethodInfo? method = GetType().GetMethod(deviceProtocolDetail.ProtocolDetailType);
                                if (method != null)
                                {
                                    method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode });
                                }
                                else
                                {
                                    //todo 未找到方法时
                                }
                            }
                            if (command != null)
                            {
                                conveyorLine.SendCommand(command, item.StationCode);
                                Console.Out.WriteLine(command.Serialize());
                            }
                        }
                        //});
                        //tasks.Add(task);
                    }
                    }
                    catch (Exception ex)
                    {
                        Console.Out.WriteLine(item.StationCode + ":" + ex.ToString());
                    }
                    Task.WaitAll(tasks.ToArray());
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
                //Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
            }
            finally
            {
                WriteDebug("CommonConveyorLineJob", "test");
                //Console.Out.WriteLine(DateTime.Now);
            }
            return Task.CompletedTask;
        }
        /// <summary>
        /// 输送线请求入库
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestInbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            if (_taskService.RequestWMSTask(command.Barcode, childDeviceCode).Status)
            {
                Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
                if (task != null)
                {
                    ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(task);
                    taskCommand.InteractiveSignal = command.InteractiveSignal;
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                    _taskService.UpdateTaskStatusToNext(task);
                }
            }
        }
        /// <summary>
        /// 输送线请求入库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestInNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
                if (newTask != null)
                {
                    ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(newTask);
                    taskCommand.InteractiveSignal = command.InteractiveSignal;
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                }
            }
        }
        /// <summary>
        /// 输送线入库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ConveyorLineInFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, 0, childDeviceCode);
                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task);
                Console.Out.WriteLine(content.Serialize());
            }
        }
        /// <summary>
        /// 输送线请求出信息
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestOutbound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
            if (task != null)
            {
                ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(task);
                taskCommand.InteractiveSignal = command.InteractiveSignal;
                conveyorLine.SendCommand(taskCommand, childDeviceCode);
                _taskService.UpdateTaskStatusToNext(task);
            }
        }
        /// <summary>
        /// 输送线请求出库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestOutNextAddress(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
                if (newTask != null)
                {
                    ConveyorLineTaskCommand taskCommand = _mapper.Map<ConveyorLineTaskCommand>(newTask);
                    taskCommand.InteractiveSignal = command.InteractiveSignal;
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                }
            }
        }
        /// <summary>
        /// 输送线出库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ConveyorLineOutFinish(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNum, childDeviceCode);
            if (task != null)
            {
                conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, 0, childDeviceCode);
                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task);
                Console.Out.WriteLine(content.Serialize());
            }
        }
    }
}