From ba8aa925e7901381ceb394adb53eca8723d1c4c5 Mon Sep 17 00:00:00 2001
From: leiqunqing <zhengqifeng@hnkhzn.com>
Date: 星期一, 19 一月 2026 11:05:49 +0800
Subject: [PATCH] 完善工位界面

---
 代码管理/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_Tasks/PLCJob.cs |  415 ++++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 309 insertions(+), 106 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_Tasks/PLCJob.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_Tasks/PLCJob.cs"
index dc9ec1b..e373ad1 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_Tasks/PLCJob.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_Tasks/PLCJob.cs"
@@ -1,106 +1,309 @@
-锘縰sing Autofac.Core;
-using HslCommunication;
-using HslCommunication.Core;
-using Microsoft.VisualBasic.FileIO;
-using Quartz;
-using SixLabors.ImageSharp.PixelFormats;
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
-using WIDESEAWCS_BasicInfoService;
-using WIDESEAWCS_Communicator;
-using WIDESEAWCS_Core.Helper;
-using WIDESEAWCS_QuartzJob;
-using WIDESEAWCS_QuartzJob.DeviceBase;
-using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
-
-namespace WIDESEAWCS_Tasks
-{
-    [DisallowConcurrentExecution]
-    public class PLCJob : JobBase, IJob
-    {
-        private readonly IBoxingService _boxingService;
-        private readonly IBoxingDetailService _boxingDetailService;
-        private readonly IFormulaService _formulaService;
-        private readonly IFormulaDetailService _formulaDetailService;
-        private readonly IProcessInfoService _processInfoService;
-        public PLCJob(IBoxingService boxingService, IBoxingDetailService boxingDetailService, IFormulaService formulaService, IFormulaDetailService formulaDetailService, IProcessInfoService processInfoService)
-        {
-            _boxingService = boxingService;
-            _boxingDetailService = boxingDetailService;
-            _formulaService = formulaService;
-            _formulaDetailService = formulaDetailService;
-            _processInfoService = processInfoService;
-        }
-
-        public Task Execute(IJobExecutionContext context)
-        {
-
-            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
-            if (flag && value != null)
-            {
-                if (value is OtherDevice)
-                {
-                    OtherDevice device = (OtherDevice)value;
-                    try
-                    {
-                        var Heart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolHeart);
-                        var EMG = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolEMG);
-                        var AutoExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolAutoExecuting);
-                        var OnlineExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolOnlineExecuting);
-                        var Error = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolError);
-                        if (Heart && !EMG && AutoExecuting && OnlineExecuting && !Error)
-                        {
-                            #region 涓婄嚎鎵爜
-                            var Location1ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation1ScanStart);
-                            var Location1ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation1ScanDone);
-                            if (Location1ScanStart && !Location1ScanDone)//鎺у埗鎵爜鏋壂鎻忔墭鐩樼爜锛屾牴鎹墭鐩樼爜鏌ヨ缁勭洏鏁版嵁锛屽垽鏂墭鐩樼粦瀹氭垚鍝佹槸鍚﹀凡缁戝畾鎵�鏈夋墍闇�闆朵欢
-                            {
-                                device.SetValue(W_PLCDBName.wboolLocation1ScanDone, true);
-                            }
-                            else if (!Location1ScanDone && Location1ScanStart)
-                            {
-                                device.SetValue(W_PLCDBName.wboolLocation1ScanDone, false);
-                            }
-                            #endregion
-
-
-                            #region 鍘嬭鎵爜
-                            var Location2ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation2ScanStart);
-                            if (Location2ScanStart)//鎺у埗鎵爜鏋壂鎻忔墭鐩樼爜锛屾牴鎹墭鐩樼爜鏌ヨ鎵樼洏缁戝畾鎴愬搧鍔犲伐鎵�闇�鏁版嵁锛屽啓鍏LC
-                            {
-
-                            }
-                            #endregion
-
-                            #region 妫�娴嬫壂鐮�
-                            var Location3ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation3ScanStart);
-                            #endregion
-
-                            #region 涓嬬嚎鎵爜
-                            var Location4ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation4ScanStart);
-                            #endregion
-                        }
-
-
-                        //var a = _boxingService.Repository.QueryFirst(x => x.PalletCode == "1232123");
-                        //WriteInfo(device.DeviceName, "infoLog");
-
-                        //WriteDebug(device.DeviceName, "debugLog");
-                    }
-                    catch (Exception ex)
-                    {
-                        WriteError(device.DeviceName, "閿欒", ex);
-                    }
-                }
-            }
-            return Task.CompletedTask;
-        }
-    }
-
-
-}
+锘縰sing System.Text;
+using Quartz;
+using WIDESEAWCS_BasicInfoService;
+using WIDESEAWCS_Common.PLCEnum;
+using WIDESEAWCS_Communicator;
+using WIDESEAWCS_Core.BaseRepository;
+using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_IBasicInfoService;
+using WIDESEAWCS_Model.Models;
+using WIDESEAWCS_QuartzJob;
+
+namespace WIDESEAWCS_Tasks
+{
+    [DisallowConcurrentExecution]
+    public class PLCJob : JobBase, IJob
+    {
+        private readonly IBoxingService _boxingService;
+        private readonly IBoxingDetailService _boxingDetailService;
+        private readonly IFormulaService _formulaService;
+        private readonly IFormulaDetailService _formulaDetailService;
+        private readonly IProcessInfoService _processInfoService;
+        private readonly IProcessInfoDetailService _processDetailInfoService;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
+        private readonly IScanStationService _scanStationService;
+        //private readonly string PalletCode = "PL107";
+
+        public PLCJob(IBoxingService boxingService,
+            IBoxingDetailService boxingDetailService,
+            IFormulaService formulaService,
+            IFormulaDetailService formulaDetailService,
+            IProcessInfoService processInfoService,
+            IProcessInfoDetailService processDetailInfoService,
+            IUnitOfWorkManage unitOfWorkManage,
+            IScanStationService scanStationService
+            )
+        {
+            _boxingService = boxingService;
+            _boxingDetailService = boxingDetailService;
+            _formulaService = formulaService;
+            _formulaDetailService = formulaDetailService;
+            _processInfoService = processInfoService;
+            _processDetailInfoService = processDetailInfoService;
+            _unitOfWorkManage = unitOfWorkManage;
+            _scanStationService = scanStationService;
+        }
+        public Task Execute(IJobExecutionContext context)
+        {
+            
+            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
+            if (flag && value != null)
+            {
+                if (value is OtherDevice)
+                {
+                    OtherDevice device = (OtherDevice)value;
+                    try
+                    {
+
+                        var Heart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolHeart);
+                        var EMG = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolEMG);
+                        var AutoExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolAutoExecuting);
+                        var OnlineExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolOnlineExecuting);
+                        var Error = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolError);
+
+                        if (Heart && !EMG && AutoExecuting && OnlineExecuting && !Error)
+                        {
+                            #region 涓婄嚎鎵爜
+                            HandleOnlineScan(device);
+                            #endregion
+
+                            #region 鍘嬭鎵爜
+                            HandlePressScan(device);
+                            #endregion
+
+                            #region 妫�娴嬫壂鐮�
+                            HandleDetectScan(device);
+                            #endregion
+
+                            #region 涓嬬嚎鎵爜
+                            HandleOfflineScan(device);
+
+                            #endregion
+                        }
+
+                    }
+                    catch (Exception ex)
+                    {
+                        WriteError(device.DeviceName, "閿欒", ex);
+                    }
+                }
+            }
+            return Task.CompletedTask;
+        }
+
+        /// <summary>
+        /// 涓婄嚎鎵爜
+        /// </summary>
+        /// <param name="device"></param>
+        private void HandleOnlineScan(OtherDevice device)
+        {
+            var Location1ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation1ScanStart);
+            var Location1ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation1ScanDone);
+            if (Location1ScanStart && !Location1ScanDone)//鎺у埗鎵爜鏋壂鎻忔墭鐩樼爜锛屾牴鎹墭鐩樼爜鏌ヨ缁勭洏鏁版嵁锛屽垽鏂墭鐩樼粦瀹氭垚鍝佹槸鍚﹀凡缁戝畾鎵�鏈夋墍闇�闆朵欢
+            {
+                var PalletCode = TcpClientExample.Start("192.168.2.120", 2001);
+                try
+                {
+                    if (!PalletCode.IsNotEmptyOrNull())
+                    {
+                        throw new InvalidOperationException("鎵樼洏涓虹┖");
+                    }
+                    var boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+                    if (boxing == null)
+                    {
+                        throw new InvalidOperationException("鏈坊鍔犵粍鐩樹俊鎭�");
+                    }
+                    List<Dt_BoxingDetail> dt_BoxingDetails = _boxingDetailService.Repository.QueryData(x => x.BoxingId == boxing.Id);
+                    Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == boxing.ProductCode);
+                    if (dt_Formula == null)
+                    {
+                        throw new InvalidOperationException("鏃犳垚鍝侀厤鏂�");
+                    }
+                    List<Dt_FormulaDetail> dt_FormulaDetails = _formulaDetailService.Repository.QueryData(x => x.FormulaId == dt_Formula.Id && x.IsScanned == 1);
+                    //姣旇緝闆朵欢鏄惁榻愬叏
+                    if (!_boxingDetailService.IsComponentCodesEqual(dt_BoxingDetails, dt_FormulaDetails))
+                    {
+                        throw new InvalidOperationException("闆朵欢鏈夐棶棰橈紝璇峰鐞�");
+                    }
+                    device.SetValue(W_PLCDBName.wboolLocation1ScanDone, true);
+                }
+                catch (Exception ex)
+                {
+                    WriteError(device.DeviceName, "涓婄嚎鎵爜娴佺▼鎵ц寮傚父", ex);
+                }
+            }
+            else if (!Location1ScanStart && Location1ScanDone)
+            {
+                device.SetValue(W_PLCDBName.wboolLocation1ScanDone, false);
+            }
+        }
+
+        /// <summary>
+        /// 鍘嬭鎵爜
+        /// </summary>
+        /// <param name="device"></param>
+        private void HandlePressScan(OtherDevice device)
+        {
+            var Location2ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation2ScanStart);
+            var Location2ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation2ScanDone);
+            if (Location2ScanStart && !Location2ScanDone)//鎺у埗鎵爜鏋壂鎻忔墭鐩樼爜锛屾牴鎹墭鐩樼爜鏌ヨ鎵樼洏缁戝畾鎴愬搧鍔犲伐鎵�闇�鏁版嵁锛屽啓鍏LC
+            {
+                var PalletCode = TcpClientExample.Start("192.168.2.121", 2001);
+
+                try
+                {
+                    if (!PalletCode.IsNotEmptyOrNull())
+                    {
+                        throw new InvalidOperationException("鎵樼洏涓虹┖");
+                    }
+                    Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+                    Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == dt_Boxing.ProductCode);
+                    //鍐欏叆PLC
+                    device.SetValue(W_PLCDBName.wrealProductLength, dt_Formula.ProductLength);
+                    device.SetValue(W_PLCDBName.wrealProductWidth, dt_Formula.ProductWidth);
+                    device.SetValue(W_PLCDBName.wrealProductHeight, dt_Formula.ProductHeight);
+                    device.SetValue(W_PLCDBName.wrealScrewDownsetDistance, dt_Formula.ScrewDownsetDistance);
+                    device.SetValue(W_PLCDBName.wrealScrewTorqueOutput, dt_Formula.ScrewTorqueOutput);
+                    device.SetValue(W_PLCDBName.wDintAutoScrewOn, dt_Formula.DintAutoScrewOn);
+                    //鍘嬭鎵爜瀹屾垚
+                    device.SetValue(W_PLCDBName.wboolLocation2ScanDone, true);
+                    //鎺ユ敹鏁版嵁
+                    decimal PressHeight = (decimal)device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectPressHeight);
+                    decimal ScrewTorque = (decimal)device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectScrewTorque);
+                    decimal ScrewAngle = (decimal)device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectScrewAngle);
+                    //娣诲姞鍒版暟鎹簱
+                    Dt_ProcessInfo dt_ProcessInfo = new Dt_ProcessInfo()
+                    {
+                        PalletCode = PalletCode,
+                        ProductCode = dt_Formula.ProductCode,
+                        ProductName = dt_Formula.ProductName,
+                        PressHeight = PressHeight,
+                        ScrewTorque = ScrewTorque,
+                        ScrewAngle = ScrewAngle
+                    };
+                    _processInfoService.Repository.AddData(dt_ProcessInfo);
+                }
+                catch (Exception ex)
+                {
+                    WriteError(device.DeviceName, "鍘嬭鎵爜娴佺▼鎵ц寮傚父", ex);
+                }
+            }
+            else if (!Location2ScanStart && Location2ScanDone)
+            {
+                device.SetValue(W_PLCDBName.wboolLocation2ScanDone, false);
+            }
+        }
+
+        /// <summary>
+        /// 妫�娴嬫壂鐮�
+        /// </summary>
+        /// <param name="device"></param>
+        private void HandleDetectScan(OtherDevice device)
+        {
+            var Location3ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation3ScanStart);
+            var Location3ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation3ScanDone);
+            if (Location3ScanStart && !Location3ScanDone)
+            {
+                var PalletCode = TcpClientExample.Start("192.168.2.122", 2001);
+                try
+                {
+                    if (!PalletCode.IsNotEmptyOrNull())
+                    {
+                        throw new InvalidOperationException("鎵樼洏涓虹┖");
+                    }
+                    Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+                    Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == dt_Boxing.ProductCode);
+                    //鍐欏叆PLC
+                    device.SetValue(W_PLCDBName.wrealXDirectionDistance1, dt_Formula.XDirectionDistance1);
+                    device.SetValue(W_PLCDBName.wrealYDirectionHeight1, dt_Formula.YDirectionHeight1);
+                    device.SetValue(W_PLCDBName.wrealXDirectionDistance2, dt_Formula.XDirectionDistance2);
+                    device.SetValue(W_PLCDBName.wrealYDirectionHeight2, dt_Formula.YDirectionHeight2);
+                    device.SetValue(W_PLCDBName.wrealXDirectionDistance3, dt_Formula.XDirectionDistance3);
+                    device.SetValue(W_PLCDBName.wrealYDirectionHeight3, dt_Formula.YDirectionHeight3);
+
+                    //妫�娴嬫壂鐮佸畬鎴�
+                    device.SetValue(W_PLCDBName.wboolLocation3ScanDone, true);
+
+                    //鎺ユ敹鏁版嵁
+                    decimal Height1 = (decimal)device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectHeight1);
+                    decimal Height2 = (decimal)device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectHeight2);
+                    decimal Height3 = (decimal)device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectHeight3);
+
+                    Dt_ProcessInfo dt_ProcessInfo = _processInfoService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+                    if (dt_ProcessInfo == null)
+                    {
+                        throw new InvalidOperationException("鎴愬搧鏈帇瑁�");
+                    }
+                    dt_ProcessInfo.Height1 = Height1;
+                    dt_ProcessInfo.Height2 = Height2;
+                    dt_ProcessInfo.Height3 = Height3;
+                    _processInfoService.Repository.UpdateData(dt_ProcessInfo);
+                }
+                catch (Exception ex)
+                {
+                    WriteError(device.DeviceName, "妫�娴嬫壂鐮佹祦绋嬫墽琛屽紓甯�", ex);
+                }
+            }
+            else if (!Location3ScanStart && Location3ScanDone)
+            {
+                device.SetValue(W_PLCDBName.wboolLocation3ScanDone, false);
+            }
+
+        }
+
+        /// <summary>
+        /// 涓嬬嚎鎵爜
+        /// </summary>
+        /// <param name="device"></param>
+        private void HandleOfflineScan(OtherDevice device)
+        {
+            var Location4ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation4ScanStart);
+            var Location4ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation4ScanDone);
+            if (Location4ScanStart && !Location4ScanDone)//鎺у埗鎵爜鏋壂鎻忔墭鐩樼爜锛屾牴鎹墭鐩樼爜鏌ヨ缁勭洏鏁版嵁锛屽垽鏂墭鐩樼粦瀹氭垚鍝佹槸鍚﹀凡缁戝畾鎵�鏈夋墍闇�闆朵欢
+            {
+                var PalletCode = TcpClientExample.Start("192.168.2.123", 2001);
+                using (var uow = _unitOfWorkManage.CreateUnitOfWork())
+                {
+                    try
+                    {
+                        if (!PalletCode.IsNotEmptyOrNull())
+                        {
+                            throw new InvalidOperationException("鎵樼洏涓虹┖");
+                        }
+                        Dt_ProcessInfo dt_ProcessInfo = _processInfoService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+                        if (dt_ProcessInfo == null)
+                        {
+                            throw new InvalidOperationException("鎴愬搧鏈帇瑁呭拰妫�娴�");
+                        }
+                        Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
+                        _boxingService.Repository.DeleteData(dt_Boxing);
+                        List<Dt_BoxingDetail> dt_BoxingDetails = _boxingDetailService.Repository.QueryData(x => x.BoxingId == dt_Boxing.Id);
+                        _boxingDetailService.Repository.DeleteData(dt_BoxingDetails);
+
+                        Dt_ProcessInfoDetail dt_ProcessInfoDetail = new Dt_ProcessInfoDetail()
+                        {
+                            PalletCode = PalletCode,
+                            ProductCode = dt_ProcessInfo.ProductCode,
+                            ProductName = dt_ProcessInfo.ProductName,
+                            Creater = "admin",
+                            CreateDate = DateTime.Now
+                        };
+                        _processDetailInfoService.Repository.AddData(dt_ProcessInfoDetail);
+                        _processInfoService.DeleteData(dt_ProcessInfo);
+                        uow.Commit();
+                        //涓嬬嚎鎵爜瀹屾垚
+                        device.SetValue(W_PLCDBName.wboolLocation4ScanDone, true);
+                    }
+                    catch (Exception ex)
+                    {
+                        WriteError(device.DeviceName, "涓嬬嚎鎵爜娴佺▼鎵ц寮傚父", ex);
+                    }
+                }
+            }
+            else if (!Location4ScanStart && Location4ScanDone)
+            {
+                device.SetValue(W_PLCDBName.wboolLocation2ScanDone, false);
+            }
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3