1
z8018
2025-04-08 e69f814f50fd59739dbedd88518dc8cb8d2ed3ee
ÏîÄ¿´úÂë/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs
@@ -1,128 +1,148 @@
//#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
///*----------------------------------------------------------------
// * å‘½åç©ºé—´ï¼šWIDESEAWCS_Tasks.ConveyorLineJob
// * åˆ›å»ºè€…:胡童庆
// * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
// * ç‰ˆæœ¬ï¼šV1.0.0
// * æè¿°ï¼š
// *
// * ----------------------------------------------------------------
// * ä¿®æ”¹äººï¼š
// * ä¿®æ”¹æ—¶é—´ï¼š
// * ç‰ˆæœ¬ï¼šV1.0.1
// * ä¿®æ”¹è¯´æ˜Žï¼š
// *
// *----------------------------------------------------------------*/
//#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
#region << ç‰ˆ æœ¬ æ³¨ é‡Š >>
/*----------------------------------------------------------------
 * å‘½åç©ºé—´ï¼šWIDESEAWCS_Tasks.ConveyorLineJob
 * åˆ›å»ºè€…:胡童庆
 * åˆ›å»ºæ—¶é—´ï¼š2024/8/2 16:13:36
 * ç‰ˆæœ¬ï¼šV1.0.0
 * æè¿°ï¼š
 *
 * ----------------------------------------------------------------
 * ä¿®æ”¹äººï¼š
 * ä¿®æ”¹æ—¶é—´ï¼š
 * ç‰ˆæœ¬ï¼šV1.0.1
 * ä¿®æ”¹è¯´æ˜Žï¼š
 *
 *----------------------------------------------------------------*/
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
//using AutoMapper;
//using Quartz;
//using System.Text.RegularExpressions;
//using WIDESEA_Common.Log;
//using WIDESEAWCS_Common.TaskEnum;
//using WIDESEAWCS_DTO.Enum;
//using WIDESEAWCS_ISystemServices;
//using WIDESEAWCS_ITaskInfoService;
//using WIDESEAWCS_Model.Models;
//using WIDESEAWCS_Model.Models.System;
//using WIDESEAWCS_QuartzJob;
//using WIDESEAWCS_QuartzJob.Service;
//using WIDESEAWCS_Tasks.ConveyorLineJob;
using AutoMapper;
using HslCommunication;
using Quartz;
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WIDESEA_Common.Log;
using WIDESEAWCS_DTO.BasicInfo;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Model.Models.System;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.ConveyorLineJob;
//namespace WIDESEAWCS_Tasks
//{
//    [DisallowConcurrentExecution]
//    public class CommonConveyorLineJob : IJob
//    {
//        private readonly ITaskService _taskService;
//        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
//        private readonly IRouterService _routerService;
//        private readonly IOrderDetailsService _OrderDetailsService;
//        private readonly IMapper _mapper;
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonConveyorLineJob : JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterService _routerService;
        private readonly IOrderDetailsService _OrderDetailsService;
        private readonly IMapper _mapper;
//        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IOrderDetailsService orderDetails, IMapper mapper)
//        {
//            _taskService = taskService;
//            _taskExecuteDetailService = taskExecuteDetailService;
//            _routerService = routerService;
//            _OrderDetailsService = orderDetails;
//            _mapper = mapper;
//        }
        public CommonConveyorLineJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IOrderDetailsService orderDetails, IMapper mapper)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
            _OrderDetailsService = orderDetails;
            _mapper = mapper;
        }
//        public Task Execute(IJobExecutionContext context)
//        {
//            try
//            {
        static string barcode = string.Empty; //条码
               
//                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
                if (flag && value != null && value is OtherDevice)
                {
                    OtherDevice otherDevice = (OtherDevice)value;
                
//                if (conveyorLine != null)
//                {
//                    if (conveyorLine.Communicator.Read<bool>("DB7.3030.0"))   //申请
//                    {
                    bool request = otherDevice.Communicator.Read<bool>("DB15.22.0");   //申请
                    bool response = otherDevice.Communicator.Read<bool>("DB15.22.1");   //应答
//                        string Barcodes = conveyorLine.Communicator.Read<string>("DB7.3032");   //读条码
//                        string pattern = @"\d+";  // åŒ¹é…æ•°å­—
//                        Match match = Regex.Match(Barcodes, pattern);
//                        string barcodeNumber = match.Value;
//                        if (barcodeNumber != "")
//                        {
//                            int toplc = _OrderDetailsService.GetOrderDetails(barcodeNumber);
//                            if (toplc != -1)
//                            {
//                                conveyorLine.Communicator.Write("DB7.3022", (int)toplc);   //写入去向
//                                conveyorLine.Communicator.Write("DB7.3028.0", (bool)true);    //写入反馈
//                                WriteLog.Write_Log("扫码枪", "扫码站台", "成功", new { ä¿¡æ¯ = $"条码:{barcodeNumber},写入去向{toplc}" });
//                                if (conveyorLine.Communicator.Read<bool>("DB7.3116"))
//                                {
//                                    conveyorLine.Communicator.Write("DB7.3022", (int)0);   //清除写入去向
//                                    conveyorLine.Communicator.Write("DB7.3028.0", (bool)false);   //清除写入反馈
//                                    WriteLog.Write_Log("扫码枪", "扫码站台", "成功", new { ä¿¡æ¯ = $"条码:{barcodeNumber},清除输送线反馈成功" });
//                                    //调取反馈MES托盘条码
                    bool wcsResponse = otherDevice.Communicator.Read<bool>("DB15.0.0");   //应答
//                                   var datast= _OrderDetailsService.ToMesBarc(int.Parse(barcodeNumber));
//                                    if (datast.code==1)
//                                    {
//                                        WriteLog.Write_Log("ToMes", "条码反馈Mes", "成功", new { ä¿¡æ¯ = $"条码:{barcodeNumber},反馈成功" });
//                                    }
//                                    else
//                                    {
//                                        WriteLog.Write_Log("ToMes", "条码反馈Mes", "失败", new { ä¿¡æ¯ = $"条码:{barcodeNumber},反馈失败" });
//                                    }
                    if (request && !response && !wcsResponse)
                    {
                        bool[] useables = otherDevice.Communicator.Read<bool>("DB15.22.3", 3);
                        int[] useableArray = GetIndexArray(useables, false);
                                    
//                                }
//                                else
//                                {
//                                    WriteLog.Write_Log("扫码枪", "扫码站台", "错误", new { ä¿¡æ¯ = $"条码:{barcodeNumber},清除输送线反馈失败" });
//                                }
//                            }
//                            else
//                            {
//                                conveyorLine.Communicator.Write("DB7.3028.1", (bool)true);
//                                WriteLog.Write_Log("扫码枪", "扫码站台", "错误", new { ä¿¡æ¯ = $"条码:{barcodeNumber},未找到数据明细详情长度,写入报警信息" });
//                            }
//                        }
//                        else
//                        {
//                            WriteLog.Write_Log("扫码枪", "扫码站台", "错误", new { ä¿¡æ¯ = $"未读取到托盘条码,条码错误{barcodeNumber}" });
//                        }
//                    }
//                    else
//                    {
                        List<int> useableStations = useableArray.ToList();
                        for (int i = 0; i < useableStations.Count; i++)
                        {
                            useableStations[i] += 1;
                        }
//                        WriteLog.Write_Log("扫码枪", "扫码站台", "错误", new { ä¿¡æ¯ = "未读取到扫码枪申请" });
//                    }
//                }
                        barcode = otherDevice.Communicator.Read<string>("DB15.32");   //条码
//            }
//            catch (Exception ex)
//            {
//                //Console.Out.WriteLine(nameof(CommonConveyorLineJob) + ":" + ex.ToString());
//            }
//            return Task.CompletedTask;
//        }
                        List<byte> bytes = otherDevice.Communicator.Read("DB15.34", 40).ToList();
                        byte[] temp = bytes.ToArray().SelectMiddle(0, bytes.IndexOf(0));
        
//    }
//}
                        barcode = Encoding.Default.GetString(temp);
                        string pattern = @"\d+";  // åŒ¹é…æ•°å­—
                        Match match = Regex.Match(barcode, pattern);
                        string barcodeNumber = match.Value;
                        if (!string.IsNullOrEmpty(barcodeNumber))
                        {
                            // 1. èŽ·å–åŽ»å‘
                            int toplc = _OrderDetailsService.GetOrderDetails(barcodeNumber, useableStations, out ProductInfoDTO productInfo);
                            if (toplc > 0)//获取到有效去向
                            {
                                otherDevice.Communicator.Write("DB15.10", productInfo.Length);
                                otherDevice.Communicator.Write("DB15.14", productInfo.Width);
                                otherDevice.Communicator.Write("DB15.18", productInfo.Height);
                                otherDevice.Communicator.Write("DB15.6", toplc);   //写入去向
                                otherDevice.Communicator.Write("DB15.0", true);
                                var datast = _OrderDetailsService.ToMesBarc(int.Parse(barcode));
                            }
                            else if (toplc == -1)//板材无条码
                            {
                                otherDevice.Communicator.Write("DB15.0.3", true);
                            }
                            else if (toplc == -2) //板材大小不在区间
                            {
                                otherDevice.Communicator.Write("DB15.0.2", true);
                            }
                        }
                    }
                    else
                    {
                        if (wcsResponse)
                            otherDevice.Communicator.Write("DB15.0", false);  //清除响应
                    }
                }
            }
            catch (Exception ex)
            {
                WriteError(nameof(CommonConveyorLineJob), ex.ToString(), ex);
            }
            return Task.CompletedTask;
        }
        public int[] GetIndexArray<T>(T[] values, T value)
        {
            List<int> result = new List<int>();
            for (int i = 0; i < values.Length; i++)
            {
                if (value.Equals(values[i]))
                {
                    result.Add(i);
                }
            }
            return result.ToArray();
        }
    }
}