huangxiaoqiang
2025-08-26 efc4ebd67444a34bc66ac04c23f2410fcdd47ef8
ÏîÄ¿´úÂë/WCS/WCSServices/WIDESEAWCS_Tasks/Ô­ÁÏ¿â/ConveyorLineJob_YL2ndFloor.cs
@@ -1,16 +1,21 @@
using Microsoft.AspNetCore.Components.Routing;
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;
@@ -26,65 +31,156 @@
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class ConveyorLineJob_YL2ndFloor : JobBase, IJob
    public partial class ConveyorLineJob_YL2ndFloor : JobBase, IJob
    {
        private readonly ICacheService _cacheService;
        private readonly ITaskService _taskService;
        private readonly IMapper _mapper;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IStationMangerRepository _stationMangerRepository;
        private readonly IRouterRepository _routerRepository;
        private readonly IRouterService _routerService;
        private readonly IRouterExtension _routerExtension;
        private readonly List<Dt_WarehouseDevice> warehouseDevices;
        private readonly IStationMangerRepository _stationMangerRepository;
        public ConveyorLineJob_YL2ndFloor(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository, IRouterService routerService, IRouterExtension routerExtension)
        public ConveyorLineJob_YL2ndFloor(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IStationMangerRepository stationMangerRepository,IMapper mapper, IRouterRepository routerRepository)
        {
            _cacheService = cacheService;
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _stationMangerRepository = stationMangerRepository;
            _mapper = mapper;
            _routerRepository = routerRepository;
            _routerService = routerService;
            _routerExtension = routerExtension;
            string? warehouseDevicesStr = _cacheService.Get<string>(nameof(Dt_WarehouseDevice));
            if (!string.IsNullOrEmpty(warehouseDevicesStr))
            {
                warehouseDevices = JsonConvert.DeserializeObject<List<Dt_WarehouseDevice>>(warehouseDevicesStr) ?? new List<Dt_WarehouseDevice>();
            }
            else
            {
                warehouseDevices = new List<Dt_WarehouseDevice>();
            }
        }
        public Task Execute(IJobExecutionContext context)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null)
            try
            {
                OtherDevice device = (OtherDevice)value;
                List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).ToList();
                //获取有协议的输送线-z
                List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode);
                //让每个输送线启动-z
                foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                    DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                    if (deviceProRead != 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
                    {
                        WriteError(item.StationName, $"未找到设备子编号{item.StationCode}的协议信息");
                    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);
            }
            return Task.CompletedTask;
                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.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)
                    {
                        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.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;
        }
    }
}