From b46664a981918f4704df1b7b2108c0548c9c8187 Mon Sep 17 00:00:00 2001
From: qiuyao <qiuyao@hnkhzn.com>
Date: 星期二, 25 三月 2025 08:52:51 +0800
Subject: [PATCH] 派发任务

---
 项目代码/WIDESEAWCS_Server 正式/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs |  229 +++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 166 insertions(+), 63 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs"
index f2eea2a..286dd74 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs"
@@ -3,24 +3,35 @@
 using System.Drawing.Imaging;
 using System.IO.Ports;
 using System.Linq;
+using System.Net.WebSockets;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
-using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
+using AutoMapper;
+using HslCommunication.WebSocket;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
 using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
 using Quartz;
+using SqlSugar;
 using StackExchange.Profiling.Internal;
 using WIDESEA_ISerialPortRepository;
+using WIDESEA_SerialPortRepository;
+using WIDESEAWCS_Core;
+using WIDESEAWCS_Core.BaseRepository;
+using WIDESEAWCS_DTO.SerialPort;
+using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
 using WIDESEAWCS_QuartzJob;
 using WIDESEAWCS_QuartzJob.DTO;
-using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
-using WIDESEAWCS_Tasks.ConveyorLineJob;
-using WIDESEAWCS_Tasks.StackerCraneJob;
+using WIDESEAWCS_TaskInfoService;
+using WIDESEAWCS_Tasks.SerialPort;
 
 namespace WIDESEAWCS_Tasks
 {
     [DisallowConcurrentExecution]
-    public class SerialPortJob : JobBase, IJob, IDisposable
+    public class SerialPortJob : JobBase, IJob,IDisposable
     {
         public void Dispose()
         {
@@ -30,24 +41,32 @@
         public enum CommandType
         {
             None = 0,
-            Get = 1,
-            Set = 2,
+            Get = 1,//鍙戜俊鍙�
+            Set = 2,//璁惧��
         }
 
         public enum CommandResult
         {
-            SetOK = 0,
-            GetOK = 1,
-            GetError = 2,
+            SetOK = 0,//parseok
+            GetOK = 1,//03
+            GetError = 2,//04
         }
         private readonly IPutakeRepository _putakeRepository;
         private readonly IProcessRepository _processRepository;
         private readonly ITorqueOpRepository _orqueOpRepository;
-        public SerialPortJob(IPutakeRepository putakeRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository)
+
+        private WebSocketServer _webSocketContext;
+        private readonly IProcessServer _processServer;
+        private readonly IPutakeServer _putakeServer;
+
+        public SerialPortJob(IPutakeServer putakeServer,IProcessServer processServer, IPutakeRepository putakeRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository, WebSocketServer webSocketContext)
         {
             _putakeRepository = putakeRepository;
             _processRepository = processRepository;
             _orqueOpRepository = torqueOpRepository;
+            _webSocketContext = webSocketContext;
+            _processServer = processServer;
+            _putakeServer = putakeServer;
         }
 
         public Task Execute(IJobExecutionContext context)
@@ -65,30 +84,70 @@
                             DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
                             if (deviceProtocolDetail != null)
                             {
-                                //鎴戣鍓嶇杩斿洖缁欐垜涓�涓粍鍒紝鍜屼换鍔d
-                                string group = "";
-                                var putake = _putakeRepository.QueryFirst(x => x.Grouptype == group);
-                                //杩欓噷鍒ゆ柇缁勫埆涓変釜鐪嬫槸閭d釜锛屽氨鏌ラ偅涓伐鑹鸿〃鐨勫��
-                                if (group=="鐢垫皵")
+                                # region  Setvalue
+                                string group = item.DeviceProDataBlock;
+                                if (group == "鐢垫皵")
                                 {
-                                    //鍏堢湅宸ヨ壓琛ㄤ腑姝ラ涓�涓鎵殑鍊兼湁澶氬皯涓� 鍐嶆煡鐪婦t_TorqueOp琛ㄤ腑鏄惁鏈夎浠诲姟鍙穒d鍜屾楠ゆ�绘暟鏄惁婊¤冻宸ヨ壓琛�
-                                    var proces = _processRepository.QueryData(x => x.CraftType == group);
+                                    //鑾峰彇褰撳墠浠诲姟鍙�  //IPutakeServer閲岄潰鐨勫緟鎵ц鐨勭涓�鏉′换鍔★紱
+                                    var take = _putakeRepository.QueryData(x => x.Grouptype == group)
+                                        .OrderBy(x=>x.Dispatchtime)
+                                        .FirstOrDefault();
+                                    var takeid = take?.Njtakeid;//鎷垮埌浠诲姟鍙�
+                                
+                                    //璋冪敤Getcircuit鎷垮埌褰撳墠姝ラ
+                                    var process = _processServer.Getcircuit(group,takeid);
 
+                                    if (process != null && process.Status)
+                                    {
+                                        var list = JsonConvert.SerializeObject(process.Data);
+                                        var data = JsonConvert.DeserializeObject<ProcessData>(list);
+
+                                        if (data?.proNow != null)
+                                        {
+                                            
+                                            int setpNum = data.proNow.SetpNum;
+                                            int sum = data.proNow.TorqueSum;
+                                            double torqueone = data.proNow.TorqueOne;
+                                            int onequantity = data.proNow.TorqueOneQuantity;
+                                            int towquantity = data.proNow.TorqueTwoQuantity;
+                                            double torquetwo = data.proNow.TorqueTwo;
+
+                                            //鍒ゆ柇op琛ㄤ腑杩欎釜鍊兼槸鍚︽湁璇ユ潯浠诲姟鐨勬暟鎹�
+                                            var op = _orqueOpRepository.QueryData(x => x.TakeId == takeid && x.GroupOp == group&&x.ProcessSte== setpNum);
+                                            if (op.Any())//鍒ゆ柇鏄惁鏈夋暟鎹�
+                                            {
+                                                //鎵惧埌浜嗗氨瑕佸姣旂幇鍦╫p琛ㄤ腑鏈夊灏戞潯锛屾槸鍚﹀拰宸ヨ壓琛ㄤ腑鐨勭洰鏍囦竴鑷�
+                                               var oponecount=op.Count(x=>x.TorqueSize== torqueone);//绗竴涓壄鐨勬暟閲�
+                                                var optowcount = op.Count(x => x.TorqueSize == torquetwo);//绗簩涓壄鐨勬暟閲�
+                                                sum=oponecount+optowcount;
+                                                //绗竴绉嶏紝绗竴涓壄鍔涘�兼病鏈夋壄瀹�
+                                                if (oponecount< onequantity&& torqueone!=0)
+                                                {
+                                                    //璁惧��
+                                                    //var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
+                                                    //        .Replace("[setNum]", (torqueone * 1000).ToString().PadLeft(7, '0')) + "\r";
+                                                    var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
+                                                              .Replace("[setNum]", Math.Round(torqueone * 1000).ToString().PadLeft(7, '0')) + "\r";
+                                                    //鏍煎紡鍖栨垚鏁存暟
+
+                                                    serialPortDevice.Communicator.Write(com);
+                                                    WriteDebug("鍐欏叆", com);
+                                                }
+                                                //绗簩绉嶏紝绗竴涓�兼壄瀹屼簡锛岀浜屼釜娌℃湁鎵畬
+                                                if (oponecount==onequantity&& optowcount < towquantity&& torquetwo!=0)
+                                                {
+                                                    //璁惧��
+                                                    var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
+                                                            .Replace("[setNum]", Math.Round(torquetwo * 1000).ToString().PadLeft(7, '0')) + "\r";
+                                                    serialPortDevice.Communicator.Write(com);
+                                                }
+                                            }
+
+                                        }
+                                    }
                                 }
-                                if (group == "鏈烘")
-                                {
-
-                                }
-                                if (group == "鍦版矡")
-                                {
-
-                                }
-                                //g宸ヨ壓琛ㄧ殑鎵姏鍊�
-
-                                double num = 4.5;//0005000
-
-                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (num* 1000).ToString().PadLeft(7,'0'))+"\r");//鎵撳紑涓插彛鏃跺厛璁惧��
-                                item.DeviceProParamName = CommandType.Set.ToString();
+                                #endregion
+                                item.DeviceProParamName = CommandType.None.ToString();
                             }
                         }
 
@@ -99,28 +158,19 @@
                             if (deviceProtocolDetail != null)
                             {
                                 serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//鎵撳紑涓插彛鏃跺厛璁惧��
+
+                                WriteDebug("鍐欏叆", item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue);
                             }
                         }
-                        else if (CommandType.Set.ToString() == item.DeviceProParamName)
-                        {
-                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
-                            //璁惧��
-                            if (deviceProtocolDetail != null)
-                            {
-                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//鎵撳紑涓插彛鏃跺厛璁惧��
-                            }
-                        }
-
-
 
                         if (serialPortDevice.Communicator.Buffers.Count > 0)
                         {
                             string? receiveData = serialPortDevice.Communicator.ToString(Encoding.Default);
                             if (!string.IsNullOrEmpty(receiveData))
                             {
-                                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + receiveData);
+                                Console.WriteLine("serialPortDevice:" + DateTime.Now.ToString("HH:mm:ss.fff") + receiveData);
 
-                                if (item.DeviceProParamName == CommandType.Set.ToString())
+                                if (item.DeviceProParamName == CommandType.Set.ToString() || item.DeviceProParamName == CommandType.None.ToString())
                                 {
                                     DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.SetOK));
                                     //parseok 
@@ -128,43 +178,81 @@
                                     {
                                         item.DeviceProParamName = CommandType.Get.ToString();
 
-
-
-
                                     }
                                 }
                                 else if (item.DeviceProParamName == CommandType.Get.ToString())
                                 {
                                     DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetOK));
                                     //03鎴愬姛
-
                                     if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
                                     {
-                                        //杩欓噷鎴愬姛涔嬪悗锛岃瀛樺偍鍒扮湅Dt_TorqueOp锛堝瓨浠诲姟鍙凤紝缁勶紝鍊硷級
+                                        //鎶婅繖鏉℃暟鎹彃鍏p琛ㄤ腑
+                                        string group = item.DeviceProDataBlock; // 璁惧鎵�灞炵粍鍒�
+                                        string devicecode = item.DeviceChildCode;//璁惧缂栧彿
+                                        string torqueValue = ExtractTorqueValue(receiveData);
+
+                                        if (group == "鐢垫皵")
+                                        {
+                                            #region
+                                            var take = _putakeRepository.QueryData(x => x.Grouptype == group)
+                                                                .OrderBy(x => x.Dispatchtime)
+                                                                .FirstOrDefault();
+                                            var takeid = take?.Njtakeid;//鎷垮埌浠诲姟鍙�
+
+                                            //璋冪敤Getcircuit鎷垮埌褰撳墠姝ラ
+                                            var process = _processServer.Getcircuit(group, takeid);
+
+                                            if (process != null && process.Status)
+                                            {
+                                                var list = JsonConvert.SerializeObject(process.Data);
+                                                var dataz = JsonConvert.DeserializeObject<ProcessData>(list);
+
+                                                if (dataz?.proNow != null)
+                                                {
+
+                                                    int setpNum = dataz.proNow.SetpNum;
+                                                    int sum = dataz.proNow.TorqueSum;
+                                                    double torqueone = dataz.proNow.TorqueOne;
+                                                    int onequantity = dataz.proNow.TorqueOneQuantity;
+                                                    int towquantity = dataz.proNow.TorqueTwoQuantity;
+                                                    double torquetwo = dataz.proNow.TorqueTwo;
+                                                    Dt_TorqueOp Addop = new Dt_TorqueOp()
+                                                    {
+                                                        DeviceCode = devicecode,
+                                                        TakeId = takeid,
+                                                        GroupOp = group,
+                                                        ProcessSte = setpNum,
+                                                        TorqueSize = float.TryParse(torqueValue, out float torque) ? torque : 0.0f, // 杩欓噷杩涜瀛楃涓插埌float鐨勮浆鎹�
+                                                        CreateDate = DateTime.Now,
+
+                                                    };
+                                                }
+                                            }
 
 
+                                            #endregion
 
-                                        item.DeviceProParamName = CommandType.Set.ToString();
-
+                                            item.DeviceProParamName = CommandType.None.ToString();
+                                        }
+                                      
                                     }
-                                }
-                                else if (item.DeviceProParamName == CommandType.Get.ToString())
-                                {
-                                    DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetError));
-                                    if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
+
+                                    DeviceProtocolDetailDTO? deviceProtocolDetail2 = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetError));
+                                    if (deviceProtocolDetail2 != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail2.ProtocalDetailValue))
                                     {
-                                        item.DeviceProParamName = CommandType.Set.ToString();
-
-
-
+                                        item.DeviceProParamName = CommandType.None.ToString();
                                     }
                                 }
                             }
+
+
+                            //寤轰竴涓璞″皢鍏朵紶缁欏墠绔�
+                            //string data = JsonConvert.SerializeObject(serialPortDevice);//杩欓噷serialPortDevice鏄亣璁炬湁杩欎釜瀵硅薄
+                            //_webSocketContext.PublishAllClientPayload(data);
                         }
+
                     }
-
                 }
-
             }
             catch (Exception ex)
             {
@@ -173,5 +261,20 @@
             WriteDebug("CommonConveyorLineJob", "test");
             return Task.CompletedTask;
         }
+
+        /// <summary>
+        /// 灏�0004000杞负4.0
+        /// 0004500杞负4.5
+        /// </summary>
+        /// <param name="data"></param>
+        /// <returns></returns>
+        private string ExtractTorqueValue(string data)
+        {
+            Match match = Regex.Match(data, @"\b0*(\d{1,4})000\b");
+            return match.Success ? (double.Parse(match.Groups[1].Value) / 10.0).ToString("0.0") : "0.0";
+        }
+
+
+
     }
 }

--
Gitblit v1.9.3