zhangchengsong
2026-04-08 f590d457f629d4a7cf6a5e2f77f5454d807e90db
ÏîÄ¿´úÂë/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
@@ -1,4 +1,5 @@
using Autofac.Core;
using HslCommunication;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
@@ -10,6 +11,7 @@
using System.Linq;
using System.Net;
using System.Reflection.Metadata;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
@@ -57,48 +59,45 @@
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonStackerCrane != null)
                {
                    //if (!commonStackerCrane.IsEventSubscribed)
                    //{
                    //    commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件
                    //}
                    if ((int)commonStackerCrane.StackerCraneWorkStatusValue == 6)
                    //任务完成StackerCraneWorkStatus
                    if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.WorkCompleted)
                    {
                        //判断当前任务是否是盘点
                        Dt_Task? currtask = _taskService.CurrtaskInfo(commonStackerCrane.CurrentTaskNum);
                        if(currtask != null)
                        {
                            string PalletCode = "";
                            //判断任务类型
                            if(currtask.TaskType==  (int)TaskTypeEnum.Inventorybound)
                            if (currtask.TaskType==  (int)TaskTypeEnum.Inventorybound)
                            {
                                //读取条码,进行配对
                                DeviceProDTO? deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == "SCBarcode");
                                byte[] bytes = commonStackerCrane.Communicator.Read(deviceProDTO.DeviceProAddress, 64);
                                string result = Encoding.ASCII.GetString(bytes).Replace(" ", "");
                                bool pandianBort = false;
                                if(currtask.PalletCode== result) pandianBort= true;
                                //读取的托盘条码
                                PalletCode = Encoding.ASCII.GetString(bytes).Replace(" ", "");
                            }
                            WebResponseContent webResponse = _taskService.StackCraneTaskCompleted(commonStackerCrane.CurrentTaskNum);
                            WebResponseContent webResponse = _taskService.StackCraneTaskCompleted(commonStackerCrane.CurrentTaskNum, PalletCode);
                            if (webResponse.Status)
                            {
                                WriteLog.Write_Log("堆垛机任务上报完成信息", "堆垛机任务上报完成信息", $"【{commonStackerCrane.DeviceCode}】库堆垛机上报任务号:【{commonStackerCrane.CurrentTaskNum}】完成信号!");
                                bool ddjfk = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                            }
                        }
                    }
                    WriteLog.Write_Log("堆垛机当前状态", "堆垛机当前状态信息", $"堆垛机设备地址:【DB105.2.0】,读取的数据:【{commonStackerCrane.StackerCraneAutoStatusDes}】\n" +
                           $"堆垛机工作模式地址:【DB105.3.0】,读取的数据:【{commonStackerCrane.StackerCraneStatusDes}】\n" +
                           $"堆垛机作业状态地址:【DB105.22.0】,读取的数据:【{commonStackerCrane.StackerCraneWorkStatusDes}】\n" +
                           $"堆垛机当前任务执行中地址:【DB105.18.0】,读取的数据:【{commonStackerCrane.CurrentTaskNum}】\n");
                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal)
                    {
                        if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
                        {
                            Dt_Task? task = GetTask(commonStackerCrane);
                            if (task != null)
                            {
@@ -108,6 +107,8 @@
                                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                                    if (sendFlag)
                                    {
                                        commonStackerCrane.Communicator.Write("DB105.52", 1);
                                        WebResponseContent webResponseContent=_taskService.UpdateTaskStatusToNext(task.TaskNum);
                                    }
                                }
@@ -119,6 +120,8 @@
            catch (Exception ex)
            {
                Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
                WriteLog.Write_Log("堆垛机报错信息", "堆垛机报错信息", $"堆垛机报错信息:【{ex.ToString()}】");
            }
            return Task.CompletedTask;
        }
@@ -136,7 +139,7 @@
                if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5 && e.TaskNum !=0)
                {
                    //Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                    _taskService.StackCraneTaskCompleted(e.TaskNum);
                    //_taskService.StackCraneTaskCompleted(e.TaskNum,);
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                }
            }
@@ -162,13 +165,14 @@
                    {
                        if (taskItem.TaskType == (int)TaskTypeEnum.Outbound)
                        {
                            if (OutTaskStationIsOccupied(taskItem.NextAddress))
                            if (OutTaskStationIsOccupied(taskItem.NextAddress,taskItem.Roadway))
                            {
                                return taskItem;
                            }
                        }
                        else if (taskItem.TaskType == (int)TaskTypeEnum.Inbound || taskItem.TaskType == (int)TaskTypeEnum.Inventorybound)
                        {
                            InTaskStationIsOccupied(taskItem.SourceAddress, taskItem.Roadway);
                            return taskItem;
                        }
                    }
@@ -188,16 +192,93 @@
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private bool OutTaskStationIsOccupied(string NextAddress)
        private bool OutTaskStationIsOccupied(string NextAddress,string Roadway)
        {
            IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1003");
            if (device == null) return false;
            CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
            DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "StationFree");
            if (deviceProDTO == null) return false;
            return conveyorLine.Communicator.Read<bool>(deviceProDTO.DeviceProAddress);
        }
            try
            {
                if (Roadway == "C")
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1004");
                    if (device == null) return false;
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "StationNum");
                    DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "HandShake");
                    if (deviceProDTO == null || HandShakeprodto == null) return false;
                    int StationNum = conveyorLine.Communicator.Read<ushort>(deviceProDTO.DeviceProAddress);
                    int HandShake = conveyorLine.Communicator.Read<ushort>(HandShakeprodto.DeviceProAddress);
                    WriteLog.Write_Log("出库库口状态信息", "出库库口状态信息", $"库口是【{NextAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】\n" +
                         $"读取的数据:【{HandShake}】;库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
                    if (StationNum == 0 && HandShake == 0 && Roadway == "C")
                    {
                        return true;
                    }
                }
                else
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1003");
                    if (device == null) return false;
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "StationNum");
                    DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "HandShake");
                    if (deviceProDTO == null || HandShakeprodto == null) return false;
                    int StationNum = conveyorLine.Communicator.Read<ushort>(deviceProDTO.DeviceProAddress);
                    int HandShake = conveyorLine.Communicator.Read<ushort>(HandShakeprodto.DeviceProAddress);
                    WriteLog.Write_Log("出库库口状态信息", "出库库口状态信息", $"库口是【{NextAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】,读取的数据:【{HandShake}】\n" +
                        $"库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
                    if ((StationNum == 1 && HandShake == 0 && Roadway == "B") || (StationNum == 0 && HandShake == 0 && Roadway != "B"))
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取站台信息错误:"+ex.Message);
            }
            return false;
        }
        /// <summary>
        /// å…¥åº“任务库口有货无货状态
        /// </summary>
        private void InTaskStationIsOccupied(string SourceAddress, string Roadway)
        {
            try
            {
                if (Roadway == "C")
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1004");
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "StationNum");
                    DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "HandShake");
                    int StationNum = conveyorLine.Communicator.Read<ushort>(deviceProDTO.DeviceProAddress);
                    int HandShake = conveyorLine.Communicator.Read<ushort>(HandShakeprodto.DeviceProAddress);
                    WriteLog.Write_Log("入库库口状态信息", "入库库口状态信息", $"库口是【{SourceAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】,读取的数据:【{HandShake}】\n" +
                        $"库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
                }
                else
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1003");
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "StationNum");
                    DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SourceAddress && x.DeviceProParamName == "HandShake");
                    int StationNum = conveyorLine.Communicator.Read<ushort>(deviceProDTO.DeviceProAddress);
                    int HandShake = conveyorLine.Communicator.Read<ushort>(HandShakeprodto.DeviceProAddress);
                    WriteLog.Write_Log("入库库口状态信息", "入库库口状态信息", $"库口是【{SourceAddress}】,申请状态地址:【{HandShakeprodto.DeviceProAddress}】,读取的数据:【{HandShake}】\n" +
                        $"库口是否有货状态地址:【{deviceProDTO.DeviceProAddress}】,读取的数据【{StationNum}】");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取站台信息错误:" + ex.Message);
            }
        }
        /// <summary>
        /// ä»»åŠ¡å®žä½“è½¬æ¢æˆå‘½ä»¤Model
@@ -209,6 +290,7 @@
        {
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
            stackerCraneTaskCommand.Barcode = task.PalletCode;
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
@@ -217,7 +299,6 @@
            {
                //获取站台编号
                Dt_PlatformStation dt_Platform=_DtPlatformStationService.getStatiomInfo(task.SourceAddress);
                string[] targetCodest = dt_Platform.Station_storey.Split("-");
                if (targetCodest.Length == 3)
                {
@@ -236,7 +317,9 @@
                if (EndCodest.Length == 3)
                {
                    string EndRow = EndCodest[0].Substring(1);
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(EndRow);
                    string SCEndRow = LaneModification(task.Roadway, int.Parse(EndRow));
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(SCEndRow );
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(EndCodest[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
                }
@@ -253,7 +336,8 @@
                if (sourceCodes.Length == 3)
                {
                    string StartRow = sourceCodes[0].Substring(1);
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(StartRow);
                    string SCStartRow = LaneModification(task.Roadway, int.Parse(StartRow));
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(SCStartRow);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                }
@@ -265,7 +349,7 @@
                }
                //获取站台编号
                Dt_PlatformStation dt_Platform = _DtPlatformStationService.getStatiomInfo(task.SourceAddress);
                Dt_PlatformStation dt_Platform = _DtPlatformStationService.getStatiomInfo(task.NextAddress);
                string[] EndCodest = dt_Platform.Station_storey.Split("-");
                if (EndCodest.Length == 3)
                {
@@ -293,12 +377,15 @@
                if (EndCodest.Length == 3)
                {
                    string EndRow = EndCodest[0].Substring(1);
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(EndRow);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(EndCodest[1]);
                   string SCEnRow=LaneModification(task.Roadway,int.Parse(EndRow));
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(SCEnRow);
                    stackerCraneTaskCommand.EndColumn = (short)(Convert.ToInt16(EndCodest[1]) + 1);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
@@ -308,5 +395,20 @@
            }
            return stackerCraneTaskCommand;
        }
        public string LaneModification(string Roadway,int SCRow)
        {
            if (Roadway == "G")
            {
                return SCRow == 1 ? "3" : "4";
            }else if(Roadway == "C")
            {
                return SCRow == 1 ? "5" : "6";
            }
            else
            {
                return SCRow.ToString();
            }
        }
    }
}