刘磊
2 天以前 aacd99ebc27309c3fce8ddf2ccbdcae3f898c9e0
同步
已添加3个文件
1394 ■■■■■ 文件已修改
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine_CW.cs 342 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob_CW/CWTask/RequestInbound.cs 420 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob_CW/CommonConveyorLine_CWJob.cs 632 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine_CW.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,342 @@
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_QuartzJob
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼šä¸€èˆ¬è¾“送线实现类
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using HslCommunication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_QuartzJob.ConveyorLine.Enum;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
namespace WIDESEAWCS_QuartzJob
{
    [Description("输送线")]
    public class CommonConveyorLine_CW : IConveyorLine
    {
        #region Private Member
        /// <summary>
        /// å †åž›æœºé€šè®¯å¯¹è±¡
        /// </summary>
        private readonly BaseCommunicator _communicator;
        /// <summary>
        /// å †åž›æœºåè®®ä¿¡æ¯
        /// </summary>
        private readonly List<DeviceProDTO> _deviceProDTOs;
        /// <summary>
        /// å †åž›æœºåè®®æ˜Žç»†ä¿¡æ¯
        /// </summary>
        private readonly List<DeviceProtocolDetailDTO> _deviceProtocolDetailDTOs;
        /// <summary>
        /// è®¾å¤‡ç¼–号
        /// </summary>
        public readonly string _deviceCode;
        /// <summary>
        /// è®¾å¤‡åç§°
        /// </summary>
        public readonly string _deviceName;
        private bool _heartStatr = true;
        private bool _isConnected = true;
        #endregion
        #region Public Member
        /// <summary>
        /// è¾“送线通讯对象
        /// </summary>
        public BaseCommunicator Communicator => _communicator;
        /// <summary>
        /// è¾“送线协议信息
        /// </summary>
        public List<DeviceProDTO> DeviceProDTOs => _deviceProDTOs;
        /// <summary>
        /// è¾“送线协议明细信息
        /// </summary>
        public List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs;
        /// <summary>
        /// è®¾å¤‡ç¼–号
        /// </summary>
        public string DeviceCode => _deviceCode;
        /// <summary>
        /// è®¾å¤‡åç§°
        /// </summary>
        public string DeviceName => _deviceName;
        /// <summary>
        /// æ˜¯å¦æœ‰æ•…éšœ
        /// </summary>
        public bool IsFault => false;
        /// <summary>
        /// é€šè®¯æ˜¯å¦å·²è¿žæŽ¥
        /// </summary>
        public bool IsConnected => Communicator.IsConnected && _isConnected;
        /// <summary>
        /// è®¾å¤‡çŠ¶æ€
        /// </summary>
        public DeviceStatus Status => DeviceStatus.Offline;
        #endregion
        #region Constructor Function
        /// <summary>
        /// æž„造函数
        /// </summary>
        /// <param name="communicator">堆垛机通讯对象</param>
        /// <param name="deviceProDTOs">堆垛机协议信息</param>
        /// <param name="deviceProtocolDetailDTOs">堆垛机协议明细信息</param>
        /// <param name="deviceCode">设备编号</param>
        /// <param name="deviceName">设备名称</param>
        public CommonConveyorLine_CW(BaseCommunicator communicator, List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string deviceCode, string deviceName)
        {
            _communicator = communicator;
            _deviceProDTOs = deviceProDTOs;
            _deviceProtocolDetailDTOs = deviceProtocolDetailDTOs;
            _deviceCode = deviceCode;
            _deviceName = deviceName;
            CheckConnect();
        }
        #endregion
        #region Private Method
        private void CheckConnect()
        {
            Task.Run(() =>
            {
                while (_heartStatr)
                {
                    try
                    {
                        DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault();
                        if (devicePro == null)
                            _isConnected = false;
                        else
                            Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
                        _isConnected = true;
                    }
                    catch (Exception ex)
                    {
                        _isConnected = false;
                    }
                    Thread.Sleep(500);
                }
            });
        }
        #endregion
        #region Public Method
        /// <summary>
        /// è¯»å–PLC协议地址的数据
        /// </summary>
        /// <typeparam name="TEnum">协议信息的枚举对象信息。</typeparam>
        /// <typeparam name="TRsult">读取数据的类型对象信息。</typeparam>
        /// <param name="value">枚举值</param>
        /// <param name="deviceChildCode">设备子编号</param>
        /// <returns>读取到的数据</returns>
        public TRsult GetValue<TEnum, TRsult>(TEnum value, string deviceChildCode) where TEnum : Enum
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString() && x.DeviceChildCode == deviceChildCode);
            return devicePro == null ? throw new Exception() : (TRsult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
        }
        /// <summary>
        /// ä¸Žè®¾å¤‡çš„心跳
        /// </summary>
        public void Heartbeat()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command"></param>
        /// <param name="deviceChildCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool SendCommand<T>(T command, string deviceChildCode) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            {
                return false;
            }
            if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// è¯»å–PLC数据,返回自定义对象
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="deviceChildCode">子设备编号</param>
        /// <returns>返回自定义对象或抛出异常</returns>
        /// <exception cref="Exception"></exception>
        public T ReadCustomer<T>(string deviceChildCode) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == "DeviceCommand" && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            {
                throw new Exception($"【{_deviceCode}】--未找到【{deviceChildCode}】协议信息");
            }
            else
            {
                return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress);
            }
        }
        /// <summary>
        /// è¯»å–PLC数据,返回自定义对象
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="deviceChildCode">子设备编号</param>
        /// <param name="deviceProParamType">参数类型</param>
        /// <returns>返回自定义对象或抛出异常</returns>
        /// <exception cref="Exception"></exception>
        public T ReadCustomer<T>(string deviceChildCode, string deviceProParamType) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == deviceProParamType && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            {
                throw new Exception($"未找到【{deviceChildCode}】协议信息");
            }
            else
            {
                return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress);
            }
        }
        /// <summary>
        /// æ ¹æ®å‚数名称、设备子编号写入对应的数据。
        /// </summary>
        /// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
        /// <typeparam name="TValue">要写入的数据类型。</typeparam>
        /// <param name="enum">参数名称。</param>
        /// <param name="value">要写入的数据。</param>
        /// <param name="deviceChildCode">设备子编号写</param>
        /// <returns>返回写入成功或失败</returns>
        public bool SetValue<TEnum, TValue>(TEnum @enum, TValue value, string deviceChildCode)
            where TEnum : Enum
            where TValue : notnull
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode);
            return devicePro == null ? throw new Exception() : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
        }
        /// <summary>
        /// æ ¹æ®å‚数名称、设备子编号读取对应的数据。
        /// </summary>
        /// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
        /// <param name="enum">参数名称。</param>
        /// <param name="deviceChildCode">设备子编号写</param>
        /// <returns>返回写入成功或失败</returns>
        public object ReadValue<TEnum>(TEnum @enum, string deviceChildCode)
            where TEnum : Enum
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode);
            return devicePro == null ? throw new Exception() : Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
        }
        //public bool IsOccupied(string deviceChildCode)
        //{
        //    if (Communicator.IsConnected)
        //    {
        //    }
        //}
        /// <summary>
        ///
        /// </summary>
        /// <param name="deviceChildCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool IsOccupied(string deviceChildCode)
        {
            if (Communicator.IsConnected)
            {
                List<DeviceProDTO> devicePros = _deviceProDTOs.Where(x => x.DeviceChildCode == deviceChildCode && x.DeviceProParamName == "InteractiveSignal").ToList();
                if (devicePros.Count == 0)
                {
                    //todo åè®®ä¿¡æ¯æœªèŽ·å–åˆ°æ—¶æŠ›å‡ºå¼‚å¸¸
                    throw new Exception();
                }
                for (int i = 0; i < devicePros.Count; i++)
                {
                    object readStatus = Communicator.ReadAsObj(devicePros[i].DeviceProAddress, devicePros[i].DeviceDataType);
                    //todo åè®®æ˜Žç»†ä¿¡æ¯æœªèŽ·å–åˆ°æ—¶æŠ›å‡ºå¼‚å¸¸
                    DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception();
                    deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == "InteractiveSignal" && x.ProtocalDetailValue.Equals(readStatus.ToString()));
                    if (deviceProtocolDetail != null)
                    {
                        return true;
                    }
                    return false;
                }
            }
            //todo é€šè®¯æœªè¿žæŽ¥æ—¶æŠ›å‡ºå¼‚常
            return false;
        }
        public void Dispose()
        {
            _heartStatr = false;
            _communicator.Dispose();
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob_CW/CWTask/RequestInbound.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,420 @@
using Mapster;
using Masuit.Tools;
using Newtonsoft.Json;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
namespace WIDESEAWCS_Tasks
{
    public partial class CommonConveyorLine_CWJob
    {
        /// <summary>
        /// å¤„理出库任务
        /// </summary>
        private void HandleTaskOut(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode, int ProtocalDetailValue, Dt_Task taskOut)
        {
            if (taskOut == null) return;
            var taskCommand = MapTaskCommand(taskOut, command);
            bool isOutTray = taskOut.TaskType == (int)TaskOutboundTypeEnum.OutTray;
            bool isOutboundAndOutFinish = taskOut.TaskType == (int)TaskOutboundTypeEnum.Outbound && taskOut.TaskState == (int)TaskOutStatusEnum.SC_OutFinish;
            bool isOutboundAndLineOutExecuting = taskOut.TaskType == (int)TaskOutboundTypeEnum.Outbound && taskOut.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting;
            if (isOutTray || isOutboundAndOutFinish || !isOutboundAndLineOutExecuting)
            {
                conveyorLine.SendCommand(taskCommand, childDeviceCode);
                //ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                _taskService.UpdateTaskStatusToNext(taskOut);
            }
            else if (taskOut.TaskType == (int)TaskOutboundTypeEnum.OutTray && taskOut.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting)
            {
                CompleteWmsTask(taskOut, command, conveyorLine, childDeviceCode, ProtocalDetailValue);
            }
        }
        /// <summary>
        /// å¤„理新任务
        /// </summary>
        private void HandleNewTask(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode);
            if (stationManager == null)
            {
                // Handle the case where stationManager is not found, if necessary
                Console.WriteLine($"未找到{childDeviceCode}站台");
                return;
            }
            // æ ¹æ®ç«™ç±»åž‹æ‰§è¡Œç›¸åº”的方法
            switch (stationManager.stationType)
            {
                case 8:
                case 9:
                case 11:
                case 12:
                    var task = _taskService.QueryExecutingTaskByBarcode(command.ConveyorLineBarcode, childDeviceCode);
                    if (task != null)
                    {
                        ExecuteStationAction(stationManager, conveyorLine, command, childDeviceCode);
                    }
                    break;
                case 1:
                case 6:
                case 10:
                    ExecuteStationAction(stationManager, conveyorLine, command, childDeviceCode);
                    break;
                    //case 16:
                    //    ExecuteStationAction(stationManager, conveyorLine, command, childDeviceCode);
                    //    break;
            }
            #region
            //Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode);
            //if (stationManager.stationType == 8)
            //{
            //    var task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode);
            //    if (task != null)
            //    {
            //        RequestInNextAddress(conveyorLine, command, childDeviceCode);
            //    }
            //}
            //else if (stationManager.stationType == 9)
            //{
            //    var task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode);
            //    if (task != null)
            //    {
            //        ConveyorLineInFinish(conveyorLine, command, childDeviceCode);
            //    }
            //}
            //else if (stationManager.stationType == 1)
            //{
            //    if (stationManager.stationArea.Contains("GW"))
            //    {
            //        var taskGW = _taskRepository.QueryFirst(x => x.TargetAddress == childDeviceCode && x.TaskState == (int)TaskOutStatusEnum.OutFinish);
            //        if (taskGW != null)
            //        {
            //            command.ConveyorLineBarcode = taskGW.PalletCode;
            //        }
            //    }
            //    RequestWmsTask(conveyorLine, command, childDeviceCode);
            //}
            //else if (stationManager.stationType == 10)
            //{
            //    var task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
            //    if (task != null)
            //    {
            //        RequestOutbound(conveyorLine, command, childDeviceCode);
            //    }
            //}
            //else if (stationManager.stationType == 11)
            //{
            //    var task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode);
            //    if (task != null)
            //    {
            //        RequestOutNextAddress(conveyorLine, command, childDeviceCode);
            //    }
            //}
            //else if (stationManager.stationType == 12)
            //{
            //    var task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode);
            //    if (task != null)
            //    {
            //        ConveyorLineOutFinish(conveyorLine, command, childDeviceCode);
            //    }
            //}
            #endregion
        }
        private void ExecuteStationAction(Dt_StationManager stationManager, CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            switch (stationManager.stationType)
            {
                case 8:
                    RequestInNextAddress(conveyorLine, command, childDeviceCode);
                    break;
                case 9:
                    ConveyorLineInFinish(conveyorLine, command, childDeviceCode);
                    break;
                case 10:
                    RequestOutbound(conveyorLine, command, childDeviceCode);
                    break;
                case 11:
                    RequestOutNextAddress(conveyorLine, command, childDeviceCode);
                    break;
                case 12:
                    ConveyorLineOutFinish(conveyorLine, command, childDeviceCode);
                    break;
                case 6:
                    CreateAndSendEmptyTrayTask(conveyorLine, command, childDeviceCode);
                    break;
                case 16:
                    AbNormalStationBZTask(conveyorLine, command, childDeviceCode);
                    break;
                case 1:
                    //if (stationManager.stationArea.Contains("GW"))
                    //{
                    //var taskGW = _taskRepository.QueryFirst(x => x.TargetAddress == childDeviceCode && (x.TaskState == (int)TaskOutStatusEnum.OutFinish || x.TaskState == (int)TaskOutStatusEnum.OutPending));
                    //if (taskGW != null)
                    //{
                    //    command.ConveyorLineBarcode = taskGW.PalletCode;
                    //}
                    //}
                    //如高温出库后任务标记NG异常则将任务更新为异常排出任务   --冠宇取消超时检测后  æ‰˜ç›˜è¶…时不在出库至异常口
                    //Dt_Task NGtask = _taskRepository.QueryFirst(x => x.Remark == "NG" && x.Roadway == stationManager.Roadway && x.Roadway.Contains("GW"));
                    //if (NGtask != null)
                    //{
                    //    CreateAbNormalOutbound(conveyorLine, command, childDeviceCode, NGtask);
                    //    break;
                    //}
                    RequestWmsTask(conveyorLine, command, childDeviceCode, stationManager);
                    break;
            }
        }
        /// <summary>
        /// æ˜ å°„任务命令
        /// </summary>
        private ConveyorLineTaskCommand_After MapTaskCommand(Dt_Task task, ConveyorLineTaskCommand_After command)
        {
            var comm = _mapper.Map<ConveyorLineTaskCommand_After>(task);
            comm.InteractiveSignal = command.InteractiveSignal;
            return comm;
        }
        /// <summary>
        /// å®ŒæˆWMS任务
        /// </summary>
        private void CompleteWmsTask(Dt_Task taskOut, ConveyorLineTaskCommand_After command, CommonConveyorLine_CW conveyorLine, string childDeviceCode, int ProtocalDetailValue)
        {
            if (command.ConveyorLineBarcode == "NoRead")
            {
                var NGAddress = _platFormRepository.QueryFirst(x => x.PlatCode == taskOut.TargetAddress).Capacity;
                taskOut.TargetAddress = NGAddress.ToString();
            }
            var keys = new Dictionary<string, object>()
            {
                {"taskNum", taskOut.TaskNum}
            };
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
            var completeTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.CompleteTask)?.ConfigValue;
            if (wmsBase == null || completeTask == null)
            {
                throw new InvalidOperationException("WMS IP æœªé…ç½®");
            }
            var wmsIpAddress = wmsBase + completeTask;
            var result = HttpHelper.GetAsync(wmsIpAddress, keys).Result;
            WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (content.Status)
            {
                //ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                _taskService.UpdateTaskStatusToNext(taskOut);
            }
        }
        /// <summary>
        /// åˆ›å»ºå¹¶å‘送空托盘任务
        /// </summary>
        public void CreateAndSendEmptyTrayTask(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            if (command.ConveyorLineBarcode != "NoRead")
            {
                var taskDTO = CreateEmptyTrayTaskDto(command.ConveyorLineBarcode, childDeviceCode);
                if (_taskRepository.QueryFirst(x => x.PalletCode == command.ConveyorLineBarcode) != null)
                {
                    List<string> strings = new List<string>() { "1743", "1739", "1837", "1841" };
                    var taskExecuting = _taskRepository.QueryFirst(x => x.PalletCode == command.ConveyorLineBarcode && x.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting && strings.Contains(x.TargetAddress));
                    if (taskExecuting != null)
                    {
                        taskExecuting.ExceptionMessage = "未接收到线体完成信号系统内部自动完成";
                        _taskService.Delete(taskExecuting);
                    }
                    ConsoleHelper.WriteErrorLine($"当前托盘存在任务:【{command.ConveyorLineBarcode}】");
                    WriteInfo(conveyorLine.DeviceName, $"当前托盘存在任务{command.ConveyorLineBarcode}");
                }
                var content = CreateAndSendTask(taskDTO);
                if (content.Status)
                {
                    var task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode, command.ConveyorLineBarcode);
                    if (task != null)
                    {
                        var taskCommand = MapTaskCommand(task, command);
                        bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                        if (sendFlag)
                        {
                            _taskService.UpdateTaskStatusToNext(task);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// åˆ›å»ºç©ºæ‰˜ç›˜ä»»åŠ¡DTO
        /// </summary>
        private WMSTaskDTO CreateEmptyTrayTaskDto(string barcode, string childDeviceCode)
        {
            var request = new RequestTaskDto()
            {
                Position = childDeviceCode,
                PalletCode = barcode,
            };
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
            var requestTrayInTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestTrayInTask)?.ConfigValue;
            if (wmsBase == null || requestTrayInTask == null)
            {
                throw new InvalidOperationException("WMS IP æœªé…ç½®");
            }
            var wmsIpAddrss = wmsBase + requestTrayInTask;
            var result = HttpHelper.PostAsync(wmsIpAddrss, request.ToJsonString()).Result;
            if (result == null)
                return new WMSTaskDTO();
            WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (!content.Status)
                return new WMSTaskDTO();
            return JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
        }
        /// <summary>
        /// è¯·æ±‚WMS任务
        /// </summary>
        private async void RequestWmsTask(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode, Dt_StationManager stationManager)
        {
            try
            {
                if (command.ConveyorLineBarcode.IsNullOrEmpty()) return;
                var content = await _taskService.RequestWMSTask(command.ConveyorLineBarcode, childDeviceCode);
                if (content.Status)
                {
                    var task = _taskService.QueryBarCodeConveyorLineTask(command.ConveyorLineBarcode, childDeviceCode);
                    if (task != null)
                    {
                        if (childDeviceCode == "1039")
                        {
                            var GWTask = _taskRepository.QueryData(x => x.Roadway.Contains("GWSC2") && x.SourceAddress == "1039" && (x.TaskState == (int)TaskInStatusEnum.Line_InExecuting || x.TaskState == (int)TaskInStatusEnum.Line_InFinish)).ToList();
                            if (GWTask.Count >= 2 && childDeviceCode == "1039" && task.Roadway.Contains("GWSC2"))
                            {
                                ConsoleHelper.WriteErrorLine($"托盘号:【{command.ConveyorLineBarcode}】高温二已存在【{GWTask.Count}】个任务大于2个任务不可下发");
                                return;
                            }
                        }
                        ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(task);
                        //conveyorLine.SendCommand(taskCommand, childDeviceCode);
                        bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                        if (sendFlag)
                        {
                            conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                            _taskService.UpdateTaskStatusToNext(task);
                        }
                    }
                }
                else
                {
                    if (content.Message != "请求过于频繁,请稍后再试" && content.Message != "无法获取目标地址")
                    {
                        WriteInfo(conveyorLine.DeviceName, content.Message);
                        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, stationManager.stationNGChildCode, childDeviceCode);
                        conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                        ConsoleHelper.WriteErrorLine($"【{conveyorLine.DeviceName}】托盘号:【{command.ConveyorLineBarcode}】请求点位:【{childDeviceCode}】异常信息【{content.Message}】");
                        WriteInfo(conveyorLine.DeviceName, $"【{conveyorLine.DeviceName}】托盘号:【{command.ConveyorLineBarcode}】请求点位:【{childDeviceCode}】异常信息【{content.Message}】");
                    }
                    ConsoleHelper.WriteErrorLine($"【{conveyorLine.DeviceName}】托盘号:【{command.ConveyorLineBarcode}】请求点位:【{childDeviceCode}】异常信息【{content.Message}】");
                }
            }
            catch (Exception ex)
            {
                WriteInfo(conveyorLine.DeviceName, $"【{conveyorLine.DeviceName}】托盘号:【{command.ConveyorLineBarcode}】请求点位:【{childDeviceCode}】异常信息【{ex.Message}】异常行【{ex.StackTrace}】");
            }
        }
        /// <summary>
        /// é«˜æ¸©å‡ºåº“后任务完成 å¦‚果任务标识NG则将任务改为异常排出任务
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="childDeviceCode"></param>
        /// <param name="task"></param>
        /// <exception cref="Exception"></exception>
        private void CreateAbNormalOutbound(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode, Dt_Task task)
        {
            Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == childDeviceCode);
            if (stationManager == null || string.IsNullOrWhiteSpace(stationManager.stationNGChildCode) || string.IsNullOrWhiteSpace(stationManager.stationNGLocation))
            {
                throw new Exception("未配置站台的对应NG口信息");
            }
            task.SourceAddress = task.TargetAddress;
            task.TargetAddress = stationManager.stationNGLocation;
            task.TaskState = (int)TaskOutStatusEnum.OutNew;
            task.TaskType = (int)TaskOutboundTypeEnum.InToOut;
            task.Grade = 10;    //此处 å‡ºåº“至异常排出口的任务应除火警外最优先执行
            _taskRepository.UpdateData(task);
            //Dt_Task task= _taskRepository.QueryFirst(x=>)
            //_taskRepository.QueryFirst()
        }
        /// <summary>
        /// åŒ…装异常排出口逻辑
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="childDeviceCode"></param>
        private void AbNormalStationBZTask(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == childDeviceCode && x.stationPLC == conveyorLine.DeviceCode);
            if (command.ConveyorLineBarcode.IsNullOrEmpty())
            {
                conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                return;
            }
            if (conveyorLine.ReadValue(ConveyorLineDBName_After.InteractiveSignal, childDeviceCode).ObjToInt() == 0) //托盘正反信号
            {
            }
            ;
            conveyorLine.ReadValue(ConveyorLineDBName_After.InteractiveSignal, childDeviceCode);  //托盘有无电芯信号
        }
    }
}
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob_CW/CommonConveyorLine_CWJob.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,632 @@
#region MyRegion
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_Tasks.ConveyorLineJob
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼š
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using AutoMapper;
using HslCommunication;
using Masuit.Tools;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using Quartz;
using SqlSugar;
using System.ComponentModel.Design;
using System.Reflection;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_DTO.MOM;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IProcessRepository;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
using Platform = WIDESEAWCS_Model.Models.Platform;
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public partial class CommonConveyorLine_CWJob : JobBase, IJob
    {
        public readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IPlatFormRepository _platFormRepository;
        private readonly ISys_ConfigService _sys_ConfigService;
        private readonly IMapper _mapper;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly ICacheService _cacheService;
        private readonly INoticeService _noticeService;
        private readonly IDt_needBarcodeRepository _needBarcodeRepository;
        private readonly IDeviceInfoRepository _deviceInfoRepository;
        private static List<string>? userTokenIds;
        private static List<int>? userIds;
        private static List<string> childCodeList = new List<string>();
        public CommonConveyorLine_CWJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, ITaskRepository taskRepository, IPlatFormRepository platFormRepository, ISys_ConfigService sys_ConfigService, IDt_StationManagerRepository stationManagerRepository, ICacheService cacheService, INoticeService noticeService, IDt_needBarcodeRepository needBarcodeRepository, IDeviceInfoRepository deviceInfoRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _mapper = mapper;
            _taskRepository = taskRepository;
            _platFormRepository = platFormRepository;
            _sys_ConfigService = sys_ConfigService;
            _stationManagerRepository = stationManagerRepository;
            _cacheService = cacheService;
            _noticeService = noticeService;
            _needBarcodeRepository = needBarcodeRepository;
            _deviceInfoRepository = deviceInfoRepository;
        }
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonConveyorLine_CW conveyorLine = (CommonConveyorLine_CW)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    #region ç«™å°æ–¹å¼
                    //List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
                    //foreach (var station in stationManagers)
                    //{
                    //    ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(station.stationChildCode);
                    //    DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.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 ç«™å°æ–¹å¼
                    #region è·¯ç”±æ–¹å¼
                    List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    DateTime dateTime = DateTime.Now;
                    Console.WriteLine($"循环开始时间{dateTime}");
                    foreach (string childDeviceCode in childDeviceCodes)
                    {
                        //Thread.Sleep(1000);
                        //if (childCodeList.Contains(childDeviceCode))
                        //{
                        //    Console.WriteLine($"当前{childDeviceCode}执行中,已跳过");
                        //    continue;
                        //}
                        //childCodeList.Add(childDeviceCode);
                        //Console.WriteLine(childDeviceCode);
                        ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(childDeviceCode);
                        if (command == null) continue;
                        //if (command.InteractiveSignal == 0 && command.HasPallet != 1) continue;
                        if (command.ConveyorLineBarcode.Trim().Contains("\0")) command.ConveyorLineBarcode = "";
                        DeviceProtocolDetailDTO? deviceProtocolDetails = conveyorLine.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineTaskCommand_After.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, childDeviceCode });
                            }
                        }
                        if (childDeviceCode == "1670" || childDeviceCode == "1666" || childDeviceCode == "1548" || childDeviceCode == "1448")
                        {
                            Platform platform = _platFormRepository.QueryFirst(x => x.DeviceCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                            if (platform != null)
                            {
                                if (command.HasPallet != 1)
                                {
                                    MethodInfo? method = GetType().GetMethod(platform.ExecutionMethod);
                                    if (method != null)
                                    {
                                        //var strings = platform.Location.Split(',').ToList();
                                        int count = 1;
                                        method.Invoke(this, new object[] { conveyorLine, command, childDeviceCode, count, platform });
                                    }
                                }
                            }
                        }
                        //childCodeList.Remove(childDeviceCode);
                        #region è°ƒç”¨äº‹ä»¶æ€»çº¿é€šçŸ¥å‰ç«¯
                        var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                        if (tokenInfos == null || !tokenInfos.Any())
                        {
                            //throw new Exception(conveyorLine.DeviceName + "缓存中未找到Token缓存");
                            continue;
                        }
                        var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                        var userIds = tokenInfos?.Select(x => x.UserId).ToList();
                        object obj = new
                        {
                            childDeviceCode,
                            commandAfter = command,
                        };
                        _noticeService.LineData(userIds?.FirstOrDefault(), userTokenIds, new { conveyorLine.DeviceName, data = obj });
                        #endregion è°ƒç”¨äº‹ä»¶æ€»çº¿é€šçŸ¥å‰ç«¯
                    }
                    DateTime ENDdateTime = DateTime.Now;
                    Console.WriteLine($"循环结束时间{ENDdateTime}");
                    #endregion è·¯ç”±æ–¹å¼
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(CommonConveyorLine_CWJob) + ":" + DateTime.Now + ":" + 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>
        /// <param name="ProtocalDetailValue">线体当前bool读取偏移地址</param>
        public void RequestInbound(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            try
            {
                var task = _taskService.QueryBarCodeConveyorLineTask(command.ConveyorLineBarcode, childDeviceCode);
                var log = $"时间:【{DateTime.Now}】【{conveyorLine.DeviceName}】托盘号:【{command.ConveyorLineBarcode}】任务号:【{command.ConveyorLineTaskNum}】设备编码:【{childDeviceCode}】";
                ConsoleHelper.WriteSuccessLine(log);
                //_noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                WriteInfo(conveyorLine.DeviceName, log);
                if (task == null)
                {
                    HandleNewTask(conveyorLine, command, childDeviceCode);
                }
                else
                {
                    if (childDeviceCode == "1039")
                    {
                        var GWTask = _taskRepository.QueryData(x => x.Roadway.Contains("GWSC2") && x.SourceAddress == "1039" && (x.TaskState == (int)TaskInStatusEnum.Line_InExecuting || x.TaskState == (int)TaskInStatusEnum.Line_InFinish)).ToList();
                        if (GWTask.Count >= 2 && childDeviceCode == "1039" && task.Roadway.Contains("GWSC2"))
                        {
                            ConsoleHelper.WriteErrorLine($"时间:【{DateTime.Now}】托盘号:【{command.ConveyorLineBarcode}】高温二已存在【{GWTask.Count}】个任务大于2个任务不可下发");
                            return;
                        }
                    }
                    ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(task);
                    bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                    if (sendFlag)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                        _taskService.UpdateTaskStatusToNext(task);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.ToString());
            }
        }
        /// <summary>
        /// è¾“送线请求入库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestInNextAddress(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode);
            if (task != null)
            {
                if (command.ConveyorLineBarcode != task.PalletCode)
                {
                    conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                    return;
                }
                Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
                if (newTask != null)
                {
                    ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(newTask);
                    //conveyorLine.SendCommand(taskCommand, childDeviceCode);
                    bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                    if (sendFlag)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                        _taskService.UpdateData(newTask);
                    }
                }
            }
            //else
            //{
            //    //当前地址请求 å¯»æ‰¾å½“前地址的托盘号 ä»»åŠ¡å·çš„ä»»åŠ¡ï¼Œå¦‚å­˜åœ¨ä»»åŠ¡åˆ™é‡æ–°å†æ¬¡å†™å…¥æ–°ç›®æ ‡åœ°å€
            //    Dt_Task currentTask = _taskService.QueryExecutingCurrentConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode);
            //    if (currentTask != null)
            //    {
            //        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(currentTask.TargetAddress), childDeviceCode);
            //    }
            //}
        }
        /// <summary>
        /// è¾“送线入库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="ProtocalDetailValue">线体当前bool读取偏移地址</param>
        public void ConveyorLineInFinish(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            var task = _taskService.QueryExecutingTaskByBarcode(command.ConveyorLineBarcode, childDeviceCode);
            if (task != null && task.TaskState != (int)TaskInStatusEnum.Line_InFinish)
            {
                WebResponseContent content = _taskService.UpdateTaskStatusToNext(task);
                if (content.Status)
                {
                    conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                }
                Console.Out.WriteLine(content.Serialize());
            }
        }
        /// <summary>
        /// è¾“送线请求出信息
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="ProtocalDetailValue">线体当前bool读取偏移地址</param>
        public void RequestOutbound(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            var task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
            if (task != null)
            {
                ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(task);
                //conveyorLine.SendCommand(taskCommand, childDeviceCode);
                bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                if (sendFlag)
                {
                    conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                    _taskService.UpdateTaskStatusToNext(task);
                    if (task.TaskType == (int)TaskOutboundTypeEnum.OutTray)
                    {
                        _taskService.UpdateTaskStatusToNext(task);
                    }
                }
            }
        }
        /// <summary>
        /// è¾“送线请求出库下一地址
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void RequestOutNextAddress(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode);
            if (task != null)
            {
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.MOMIP_BASE)?.ConfigValue;
                var ipAddress = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.TrayCellsStatus)?.ConfigValue;
                if (wmsBase == null || ipAddress == null)
                {
                    throw new InvalidOperationException("MOM IP æœªé…ç½®");
                }
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode);
                TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
                {
                    Software = "WMS",
                    TrayBarcode = command.ConveyorLineBarcode,
                    EquipmentCode = stationManager.stationEquipMOM,
                    SessionId = Guid.NewGuid().ToString(),
                    EmployeeNo = "MITest",
                    SceneType = "1",
                    RequestTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
                };
                var MOMIpAddress = wmsBase + ipAddress;
                var result = HttpHelper.PostAsync(MOMIpAddress, trayCells.Serialize()).Result;
                WriteInfo("入站校验", $"【{childDeviceCode}】入站校验请求参数【{trayCells.Serialize()}】");
                WriteInfo("入站校验", "");
                WriteInfo("入站校验", $"【{childDeviceCode}】入站校验返回参数【{result}】");
                ResultTrayCellsStatus result1 = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(result);
                if (result1.Success || task.Remark != "NG")
                {
                    Dt_Task? newTask = _taskService.UpdatePosition(task.TaskNum, task.CurrentAddress);
                    if (newTask != null)
                    {
                        ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(newTask);
                        //conveyorLine.SendCommand(taskCommand, childDeviceCode);
                        bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                        if (sendFlag)
                        {
                            conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                            _taskService.UpdateData(newTask);
                        }
                    }
                }
                else
                {
                    ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(task);
                    taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationNGChildCode);
                    //conveyorLine.SendCommand(taskCommand, childDeviceCode);
                    bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                    if (sendFlag)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                        _taskService.UpdateTaskStatusToNext(task);
                    }
                }
            }
        }
        /// <summary>
        /// è¾“送线出库完成
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ConveyorLineOutFinish(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode)
        {
            var log = $"时间:【{DateTime.Now}】【{conveyorLine.DeviceName}】托盘号:【{command.ConveyorLineBarcode}】任务号:【{command.ConveyorLineTaskNum}】设备编码:【{childDeviceCode}】";
            ConsoleHelper.WriteSuccessLine(log);
            //_noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
            WriteInfo(conveyorLine.DeviceName, log);
            var task = _taskService.QueryExecutingConveyorLineTask(command.ConveyorLineTaskNum, childDeviceCode, command.ConveyorLineBarcode);
            if (task != null)
            {
                WebResponseContent content = new WebResponseContent();
                ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(task);
                taskCommand.InteractiveSignal = command.InteractiveSignal;
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode);
                if (task.PalletCode != command.ConveyorLineBarcode)
                {
                    taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationNGChildCode);
                }
                else
                {
                    taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationLocation);
                }
                if (stationManager.stationPLC == "1018" && stationManager.stationArea == "Cache")  //更新在途数据
                {
                    dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.productLine == stationManager.productLine && x.toArea == stationManager.stationChildCode);
                    if (needBarcode != null)
                    {
                        needBarcode.inLineNum--;
                        _needBarcodeRepository.UpdateData(needBarcode);
                    }
                }
                //conveyorLine.SendCommand(taskCommand, childDeviceCode);
                bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                if (sendFlag)
                {
                    conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                    content = _taskService.UpdateTaskStatusToNext(task);
                }
            }
            else
            {
                var taskNext = _taskService.QueryExecutingConveyorLineTask(childDeviceCode, command.ConveyorLineBarcode);
                if (taskNext != null)
                {
                    WebResponseContent content = new WebResponseContent();
                    ConveyorLineTaskCommand_After taskCommand = _mapper.Map<ConveyorLineTaskCommand_After>(taskNext);
                    taskCommand.InteractiveSignal = command.InteractiveSignal;
                    Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationPLC == conveyorLine.DeviceCode && x.stationChildCode == childDeviceCode);
                    if (taskNext.PalletCode != command.ConveyorLineBarcode)
                    {
                        taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationNGChildCode);
                    }
                    else
                    {
                        taskCommand.ConveyorLineTargetAddress = Convert.ToInt16(stationManager.stationLocation);
                    }
                    if (stationManager.stationPLC == "1018" && stationManager.stationArea == "Cache")  //更新在途数据
                    {
                        dt_needBarcode needBarcode = _needBarcodeRepository.QueryFirst(x => x.productLine == stationManager.productLine && x.toArea == stationManager.stationChildCode);
                        if (needBarcode != null)
                        {
                            needBarcode.inLineNum--;
                            _needBarcodeRepository.UpdateData(needBarcode);
                        }
                    }
                    //conveyorLine.SendCommand(taskCommand, childDeviceCode);
                    bool sendFlag = SendCommand(taskCommand, conveyorLine, childDeviceCode);
                    if (sendFlag)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ResponState, Convert.ToInt16(1), childDeviceCode);
                        taskNext.ExceptionMessage = log;
                        content = _taskService.UpdateTaskStatusToNext(taskNext);
                    }
                }
            }
        }
        /// <summary>
        /// ç›‘测空托盘实盘出库
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        /// <param name="command">读取的请求信息</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="index">线体当前bool读取偏移地址</param>
        public async void EmptyTrayReturn(CommonConveyorLine_CW conveyorLine, ConveyorLineTaskCommand_After command, string childDeviceCode, int index, WIDESEAWCS_Model.Models.Platform platform)
        {
            try
            {
                TaskOutboundTypeEnum taskOutboundTypeEnum;
                if (platform.PlatformType.Contains("OutTray"))
                    taskOutboundTypeEnum = TaskOutboundTypeEnum.OutTray;
                else
                    taskOutboundTypeEnum = TaskOutboundTypeEnum.Outbound;
                await CheckAndCreateTask(taskOutboundTypeEnum, childDeviceCode, index, platform);
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// æ£€æŸ¥ä»»åŠ¡å¹¶åˆ›å»ºæ–°ä»»åŠ¡
        /// </summary>
        private async Task CheckAndCreateTask(TaskOutboundTypeEnum taskType, string childDeviceCode, int index, Platform platform)
        {
            var tasks = _taskRepository.QueryData(x => x.TaskType == (int)taskType && x.TargetAddress == childDeviceCode);
            if (tasks.Count < platform.Capacity)
            {
                #region è°ƒç”¨WMS获取出库任务
                WMSTaskDTO taskDTO = new WMSTaskDTO();
                // èŽ·å–WMSip地址
                var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
                var requestTrayOutTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestTrayOutTask)?.ConfigValue;
                if (wmsBase == null || requestTrayOutTask == null)
                {
                    throw new InvalidOperationException("WMS IP æœªé…ç½®");
                }
                var wmsIpAddress = wmsBase + requestTrayOutTask;
                //var device = _deviceInfoRepository.QueryData(x => x.DeviceStatus == "1" && x.DeviceRemark == platform.Id.ToString());
                //var deviceCode = device.Select(x => x.DeviceCode).ToList();
                List<string> strings = platform.Location.Split(',').ToList();
                var result = await HttpHelper.PostAsync(wmsIpAddress, new { Position = childDeviceCode, Tag = (int)taskType, AreaCdoe = platform.Stacker, AreaCdoes = strings, platform.ProductionLine }.Serialize());
                //var result = await HttpHelper.PostAsync("http://localhost:5000/api/Task/RequestTrayOutTaskAsync", dynamic.ToJsonString());
                WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                // æ£€æŸ¥çŠ¶æ€å¹¶è¿”å›ž
                if (!content.Status)
                    return;
                taskDTO = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
                #endregion è°ƒç”¨WMS获取出库任务
                CreateAndSendTask(taskDTO);
            }
        }
        /// <summary>
        /// åˆ›å»ºä»»åŠ¡
        /// </summary>
        public WebResponseContent CreateAndSendTask(WMSTaskDTO taskDTO)
        {
            var content = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
            if (content.Status)
            {
                Console.WriteLine($"{taskDTO.TaskType}呼叫成功");
            }
            return content;
        }
        public bool SendCommand(ConveyorLineTaskCommand_After taskCommand, CommonConveyorLine_CW conveyorLine, string childDeviceCode)
        {
            conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(taskCommand.ConveyorLineTargetAddress), childDeviceCode);
            Thread.Sleep(100);
            conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, taskCommand.ConveyorLineBarcode, childDeviceCode);
            Thread.Sleep(100);
            conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTaskNum, taskCommand.ConveyorLineTaskNum, childDeviceCode);
            for (int i = 0; i < 6; i++)
            {
                ConveyorLineTaskCommand_After command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand_After>(childDeviceCode);
                if (command != null)
                {
                    if (command.ConveyorLineBarcode == taskCommand.ConveyorLineBarcode && command.ConveyorLineTaskNum == taskCommand.ConveyorLineTaskNum && command.ConveyorLineTargetAddress == taskCommand.ConveyorLineTargetAddress)
                    {
                        WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务成功写入次数{i}写入任务【{JsonConvert.SerializeObject(taskCommand)}】");
                        return true;
                    }
                    if (command.ConveyorLineTargetAddress != taskCommand.ConveyorLineTargetAddress)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTargetAddress, Convert.ToInt16(taskCommand.ConveyorLineTargetAddress), childDeviceCode);
                        Thread.Sleep(100);
                    }
                    if (command.ConveyorLineBarcode != taskCommand.ConveyorLineBarcode)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineBarcode, taskCommand.ConveyorLineBarcode, childDeviceCode);
                        Thread.Sleep(100);
                    }
                    if (command.ConveyorLineTaskNum != taskCommand.ConveyorLineTaskNum)
                    {
                        conveyorLine.SetValue(ConveyorLineDBName_After.ConveyorLineTaskNum, taskCommand.ConveyorLineTaskNum, childDeviceCode); Thread.Sleep(100);
                    }
                }
            }
            WriteInfo(conveyorLine.DeviceName, $"时间:【{DateTime.Now}】写入任务失败任务号【{taskCommand.ConveyorLineTaskNum}】托盘号【{taskCommand.ConveyorLineBarcode}】目标地址【{taskCommand.ConveyorLineTargetAddress}】当前节点【{childDeviceCode}】");
            return false;
        }
    }
}
#endregion