duyongjia
2024-11-27 3d07ccb8342a92b32bc8858ecf56d257d6b500c9
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
@@ -1,5 +1,6 @@
using HslCommunication;
using Microsoft.AspNetCore.Components.Routing;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
@@ -7,6 +8,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common.ServiceLog;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core.Enums;
@@ -47,7 +49,10 @@
        public Task Execute(IJobExecutionContext context)
        {
            CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
            WriteDebug("大堆垛机运行日志", "开始时间" + DateTime.Now);
            WriteDebug("大堆垛机运行日志", $"大堆垛机任务执行中...");
            //CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
            CommonStackerCrane commonStackerCrane = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "SC01") as CommonStackerCrane;
            try
            {  
                if (commonStackerCrane != null)
@@ -66,6 +71,7 @@
                            Dt_Task? task = GetTask(commonStackerCrane);
                            if (task != null)
                            {
                                WriteDebug("大堆垛机运行日志", $"大堆垛机任务号{task.TaskNum}");
                                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                                if (stackerCraneTaskCommand != null)
                                {
@@ -104,147 +110,188 @@
            //查询提升机/组立机信号
            //Task.Run(delegate
            //{
                try
            try
            {
                if (commonStackerCrane != null)
                {
                    if (commonStackerCrane != null)
                    Dt_Task? task1001 = _taskService.QueryStackerCraneTaskByAGVRequest("SC01");
                    if (task1001 != null)
                    {
                        Dt_Task? task1001 = _taskService.QueryStackerCraneTaskByAGVRequest("SC01");
                        if (task1001 != null)
                        //读取提升机信号,确定是否可以进入
                        int IsAllow = 0;
                        if (task1001.SourceAddress == "SC01-002-000-001")//WMS 1001站点
                        {
                            //读取提升机信号,确定是否可以进入
                            int IsAllow = 0;
                            if (task1001.SourceAddress == "SC01-002-000-001")//WMS 1001站点
                            IsAllow = commonStackerCrane.Communicator.Read<short>("DB2.0.0");
                        }
                        else if (task1001.SourceAddress == "SC01-002-000-002")//WMS 1002站点
                        {
                            IsAllow = commonStackerCrane.Communicator.Read<short>("DB2.8.0");
                        }
                        if (IsAllow == 1)//如果为1,则允计AGV进入,回传WMS
                        {
                            MESSendCMD sendcmd = new MESSendCMD { cmd = 2001, task_id = task1001.TaskNum, status = 6 };
                            MESResponse res = ApiInvoke.SendTaskCMD(sendcmd);
                            if (res != null && res.code != 0)
                            {
                                IsAllow = commonStackerCrane.Communicator.Read<short>("DB2.0.0");
                                WriteDebug(nameof(CommonStackerCraneJob), $"允计AGV进入提升机,回传WMS失败!任务号{task1001.TaskNum}");
                            }
                            else if (task1001.SourceAddress == "SC01-002-000-002")//WMS 1002站点
                            else
                            {
                                IsAllow = commonStackerCrane.Communicator.Read<short>("DB2.8.0");
                            }
                            if (IsAllow == 1)//如果为1,则允计AGV进入,回传WMS
                            {
                                MESSendCMD sendcmd = new MESSendCMD { cmd = 2001, task_id = task1001.TaskNum, status = 6 };
                                MESResponse res = ApiInvoke.SendTaskCMD(sendcmd);
                                if (res != null && res.code != 0)
                                {
                                    WriteDebug(nameof(CommonStackerCraneJob), $"提升机提升完成,回传WMS失败!任务号{task1001.TaskNum}");
                                }
                                WriteDebug(nameof(CommonStackerCraneJob), $"允计AGV进入提升机,回传WMS成功!任务号{task1001.TaskNum}");
                            }
                        }
                        Dt_Task? task3001 = _taskService.QueryStackerCraneTaskByAGVPutFinish("SC01");
                        if (task3001 != null)
                        else
                        {
                            int IsFinish = 0;
                            int IsWriteOne = 0;
                            int IsWriteTwo = 0;
                            if (task3001.SourceAddress == "SC01-002-000-001")//WMS 1001站点
                            WriteDebug(nameof(CommonStackerCraneJob), $"未读取到提升机允计进入信号,暂不允计AGV进入提升机,任务号{task1001.TaskNum}");
                        }
                    }
                    Dt_Task? task3001 = _taskService.QueryStackerCraneTaskByAGVPutFinish("SC01");
                    if (task3001 != null)
                    {
                        int IsFinish = 0;
                        int IsWriteOne = 0;
                        int IsWriteTwo = 0;
                        if (task3001.SourceAddress == "SC01-002-000-001")//WMS 1001站点
                        {
                            //提升机是否处于就绪状态
                            int IsReady = commonStackerCrane.Communicator.Read<short>("DB2.0.0");
                            WriteDebug(nameof(CommonStackerCraneJob), $"提升机状态:{IsReady}任务号:{task3001.TaskNum}");
                            //写入提升信息
                            IsWriteOne = commonStackerCrane.Communicator.Read<short>("DB1.0.0");
                            if (IsWriteOne == 0 && IsReady == 1)
                            {
                                //写入提升信息
                                IsWriteOne = commonStackerCrane.Communicator.Read<short>("DB1.0.0");
                                if(IsWriteOne == 0)
                                {
                                    commonStackerCrane.Communicator.Write<short>("DB1.0.0", 1);
                                }
                                //读取提升机信号,确定是否提升完成
                                commonStackerCrane.Communicator.Read<short>("DB2.2.0");
                                commonStackerCrane.Communicator.Write<short>("DB1.0.0", 1);
                            }
                            else if (task3001.SourceAddress == "SC01-002-000-002")//WMS 1002站点
                            else
                            {
                                //写入提升信息
                                IsWriteTwo = commonStackerCrane.Communicator.Read<short>("DB1.6.0");
                                if (IsWriteTwo == 0)
                                {
                                    commonStackerCrane.Communicator.Write<short>("DB1.6.0", 1);
                                }
                                //读取提升机信号,确定是否提升完成
                                IsFinish = commonStackerCrane.Communicator.Read<short>("DB2.10.0");
                                WriteDebug(nameof(CommonStackerCraneJob), $"未写入提升信号,可能是已写入或者提升机未处理就绪状态,任务号:{task3001.TaskNum}");
                            }
                            if (IsFinish == 1)//如果为1,则表示提升完成,回传WMS
                            {
                                MESSendCMD sendcmd = new MESSendCMD { cmd = 2002, task_id = task3001.TaskNum, status = 6 };
                                MESResponse res = ApiInvoke.SendTaskCMD(sendcmd);
                                if (res != null && res.code != 0)
                                {
                                    WriteDebug(nameof(CommonStackerCraneJob), $"提升机提升完成,回传WMS失败!任务号{task3001.TaskNum}");
                                }
                            //读取提升机信号,确定是否提升完成
                            IsFinish = commonStackerCrane.Communicator.Read<short>("DB2.2.0");
                        }
                        else if (task3001.SourceAddress == "SC01-002-000-002")//WMS 1002站点
                        {
                            //提升机是否处于就绪状态
                            int IsReady = commonStackerCrane.Communicator.Read<short>("DB2.0.0");
                            WriteDebug(nameof(CommonStackerCraneJob), $"提升机状态:{IsReady}任务号:{task3001.TaskNum}");
                            //写入提升信息
                            IsWriteTwo = commonStackerCrane.Communicator.Read<short>("DB1.6.0");
                            if (IsWriteTwo == 0 && IsReady == 1)
                            {
                                commonStackerCrane.Communicator.Write<short>("DB1.6.0", 1);
                            }
                            else
                            {
                                WriteDebug(nameof(CommonStackerCraneJob), $"未写入提升信号,可能是信号已写入或者提升机未处理就绪状态,任务号:{task3001.TaskNum}");
                            }
                            //读取提升机信号,确定是否提升完成
                            IsFinish = commonStackerCrane.Communicator.Read<short>("DB2.10.0");
                        }
                        if (IsFinish == 1)//如果为1,则表示提升完成,回传WMS
                        {
                            MESSendCMD sendcmd = new MESSendCMD { cmd = 2002, task_id = task3001.TaskNum, status = 6 };
                            MESResponse res = ApiInvoke.SendTaskCMD(sendcmd);
                            string inparam = JsonConvert.SerializeObject(sendcmd);
                            WriteDebug(nameof(CommonStackerCraneJob), $"提升机提升完成,回传WMS,入参:{inparam}");
                            if (res != null && res.code != 0)
                            {
                                WriteDebug(nameof(CommonStackerCraneJob), $"提升机提升完成,回传WMS失败!任务号{task3001.TaskNum}");
                            }
                        }
                        //小堆垛机出库完成任务
                        Dt_Task? taskSmall = _taskService.QueryStackerCraneFinishTask();
                        if(taskSmall != null)
                        else
                        {
                            //根据任务源地址确定写入PLC位置 Depth
                            Dt_LocationInfo location = _locationInfoRepository.QueryFirst(x=>x.LocationCode==taskSmall.SourceAddress);
                            if(location != null)
                            WriteDebug(nameof(CommonStackerCraneJob), $"提升机提升中...,任务号:{task3001.TaskNum}");
                        }
                    }
                    //小堆垛机出库完成任务
                    Dt_Task? taskSmall = _taskService.QueryStackerCraneFinishTask();
                    if (taskSmall != null)
                    {
                        //根据任务源地址确定写入PLC位置 Depth
                        Dt_LocationInfo location = _locationInfoRepository.QueryFirst(x => x.LocationCode == taskSmall.SourceAddress);
                        if (location != null)
                        {
                            string[] Levels = taskSmall.Remark.Split("-");
                            if (Levels.Length == 2)
                            {
                                string[] Levels = taskSmall.Remark.Split("-");
                                if (Levels.Length == 2)
                                if (!string.IsNullOrEmpty(Levels[0]) && !string.IsNullOrEmpty(Levels[1]))
                                {
                                    if (!string.IsNullOrEmpty(Levels[0]) && !string.IsNullOrEmpty(Levels[1]))
                                    int level = Convert.ToInt16(Levels[0]) - Convert.ToInt16(Levels[1]);
                                    //剩余层数写入
                                    bool issuccess = commonStackerCrane.Communicator.Write<short>("DB28." + location.Depth + ".0", Convert.ToInt16(level));
                                    WriteDebug(nameof(CommonStackerCraneJob), $"剩余层数信号写入{level}-任务号{taskSmall.TaskNum}");
                                    //如果当前提取层数等于总层数,则表示料车所有的层数取完
                                    if (level == 0)
                                    {
                                        int level = Convert.ToInt16(Levels[0]) - Convert.ToInt16(Levels[1]);
                                        //剩余层数写入
                                        bool issuccess = commonStackerCrane.Communicator.Write<short>("DB28." + location.Depth + ".0", Convert.ToInt16(level));
                                        //如果当前提取层数等于总层数,则表示料车所有的层数取完
                                        if (level == 0)
                                        //源地址(暂存位)对应的目标地址(组立机)location.Remark
                                        if (!string.IsNullOrEmpty(location.Remark))
                                        {
                                            //源地址(暂存位)对应的目标地址(组立机)location.Remark
                                            if (!string.IsNullOrEmpty(location.Remark))
                                            //根据目标地址查询组立机站点信息,获取写入整车完成的DB块 Depth
                                            Dt_LocationInfo location2 = _locationInfoRepository.QueryFirst(x => x.LocationCode == location.Remark);
                                            //整车完成写入层数
                                            int sn = 0;
                                            if (taskSmall.SourceAddress == "SC01-001-006-001" || taskSmall.SourceAddress == "SC01-001-012-001" || taskSmall.SourceAddress == "SC01-001-018-001" || taskSmall.SourceAddress == "SC01-001-022-001" || taskSmall.SourceAddress == "SC01-001-026-001")
                                            {
                                                //根据目标地址查询组立机站点信息,获取写入整车完成的DB块 Depth
                                                Dt_LocationInfo location2 = _locationInfoRepository.QueryFirst(x => x.LocationCode == location.Remark);
                                                //整车完成写入
                                                bool issuccess2 = commonStackerCrane.Communicator.Write<short>("DB28." + location.Depth+".0", 1);
                                                if (issuccess2&&issuccess)
                                                {
                                                    _taskService.UpdateTaskStatus(taskSmall.TaskId, (int)TaskOutStatusEnum.OutFinish);
                                                    _taskExecuteDetailService.AddTaskExecuteDetail(taskSmall.TaskId, $"出库任务完成");
                                                }
                                                else
                                                {
                                                    WriteDebug(nameof(CommonStackerCraneJob), $"整车完成信号写入失败!任务号{taskSmall.TaskNum}");
                                                }
                                                sn = 1;
                                            }
                                        }
                                        else
                                        {
                                            if (issuccess)
                                            else if (taskSmall.SourceAddress == "SC01-001-007-001" || taskSmall.SourceAddress == "SC01-001-013-001" || taskSmall.SourceAddress == "SC01-001-019-001" || taskSmall.SourceAddress == "SC01-001-023-001" || taskSmall.SourceAddress == "SC01-001-027-001")
                                            {
                                                sn = 2;
                                            }
                                            else
                                            {
                                                sn = 3;
                                            }
                                            bool issuccess2 = commonStackerCrane.Communicator.Write<short>("DB28." + location2.Depth + ".0", Convert.ToInt16(sn));
                                            if (issuccess2 && issuccess)
                                            {
                                                _taskService.UpdateTaskStatus(taskSmall.TaskId, (int)TaskOutStatusEnum.OutFinish);
                                                _taskExecuteDetailService.AddTaskExecuteDetail(taskSmall.TaskId, $"出库任务完成");
                                            }
                                            else
                                            {
                                                WriteDebug(nameof(CommonStackerCraneJob), $"剩余层数信号写入失败!任务号{taskSmall.TaskNum}");
                                                WriteDebug(nameof(CommonStackerCraneJob), $"整车完成信号写入失败!任务号{taskSmall.TaskNum}");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (issuccess)
                                        {
                                            _taskService.UpdateTaskStatus(taskSmall.TaskId, (int)TaskOutStatusEnum.OutFinish);
                                            _taskExecuteDetailService.AddTaskExecuteDetail(taskSmall.TaskId, $"出库任务完成");
                                        }
                                        else
                                        {
                                            WriteDebug(nameof(CommonStackerCraneJob), $"剩余层数信号写入失败!任务号{taskSmall.TaskNum}");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteError(nameof(CommonStackerCraneJob), "查询或写入PLC状态失败!", ex);
                }
                }
            }
            catch (Exception ex)
            {
                WriteError(nameof(CommonStackerCraneJob), "查询或写入PLC状态失败!", ex);
            }
            WriteDebug("大堆垛机运行日志", "结束时间" + DateTime.Now);
            //});
            return Task.CompletedTask;
        }