From 2f0c81709876d76b6b120cf6ac43f05cda6dfe4c Mon Sep 17 00:00:00 2001
From: yanjinhui <3306209981@qq.com>
Date: 星期六, 22 三月 2025 09:36:13 +0800
Subject: [PATCH] 修改websocket;

---
 项目代码/WIDESEAWCS_Server 正式/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs |  190 +++++++++++++++++++++++++++++++++++------------
 1 files changed, 142 insertions(+), 48 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 9e8c32a..d4f798f 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"
@@ -1,84 +1,178 @@
 锘縰sing System;
 using System.Collections.Generic;
+using System.Drawing.Imaging;
 using System.IO.Ports;
 using System.Linq;
+using System.Net.WebSockets;
 using System.Text;
 using System.Threading.Tasks;
+using HslCommunication.WebSocket;
+using Newtonsoft.Json;
 using Quartz;
+using SqlSugar;
+using StackExchange.Profiling.Internal;
+using WIDESEA_ISerialPortRepository;
+using WIDESEAWCS_Model.Models;
 using WIDESEAWCS_QuartzJob;
+using WIDESEAWCS_QuartzJob.DTO;
 
 namespace WIDESEAWCS_Tasks
 {
     [DisallowConcurrentExecution]
-    public class SerialPortJob : JobBase, IJob
+    public class SerialPortJob : JobBase, IJob,IDisposable
     {
+        public void Dispose()
+        {
+            GC.SuppressFinalize(this);
+        }
+
         public enum CommandType
         {
             None = 0,
-            Get = 1,
-            Set = 2,
+            Get = 1,//鍙戜俊鍙�
+            Set = 2,//璁惧��
         }
-        private static SerialPort serialPort;
-        private static CommandType commandType = CommandType.None;
-        private string commandGet = "824070241JNT\r";//璇诲��
-        private string commandSet = "824070241JNT,0005000,+0001000[+TOL],-0001000[-TOL]\r";//璁惧��
-        private string setOK = "ParseOK";
-        private string getOK = "82407024103";
-        private string getError = "82407024104";
+
+        public enum CommandResult
+        {
+            SetOK = 0,//parseok
+            GetOK = 1,//03
+            GetError = 2,//04
+        }
+        private readonly IPutakeRepository _putakeRepository;
+        private readonly IProcessRepository _processRepository;
+        private readonly ITorqueOpRepository _orqueOpRepository;
+        private WebSocketServer _webSocketContext;
+
+        public SerialPortJob(IPutakeRepository putakeRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository, WebSocketServer webSocketContext)
+        {
+            _putakeRepository = putakeRepository;
+            _processRepository = processRepository;
+            _orqueOpRepository = torqueOpRepository;
+            _webSocketContext = webSocketContext;
+        }
+
         public Task Execute(IJobExecutionContext context)
         {
             try
             {
-                if (serialPort == null) { serialPort = new SerialPort("COM3"); serialPort.DataReceived += SerialPort_DataReceived; }
-                if (!serialPort.IsOpen)
+                SerialPortDevice serialPortDevice = (SerialPortDevice)context.JobDetail.JobDataMap.Get("JobParams");
+                if (serialPortDevice != null)
                 {
-                    serialPort.Open();
+
+                    List<DeviceProDTO> deviceProDTOs = serialPortDevice.DeviceProDTOs;
+                    foreach (var item in deviceProDTOs)
+                    {
+                        if (item.DeviceProParamName != CommandType.Get.ToString() && item.DeviceProParamName != CommandType.Set.ToString())
+                        {
+                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
+                            if (deviceProtocolDetail != null)
+                            {
+                                # region  Setvalue
+                                string group = item.DeviceProDataBlock;
+                                if (group == "鐢垫皵")
+                                {
+                                    //鏌ョ湅姝ラ
+                                    var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (5 * 1000).ToString().PadLeft(7, '0'))+"\r";
+                                    serialPortDevice.Communicator.Write(com+"\r");
+                                    //鎵撳紑涓插彛鏃跺厛璁惧��
+                                    
+                                     
+                                }
+                                #endregion
+                                item.DeviceProParamName = CommandType.Set.ToString();
+
+
+                            }
+                        }
+
+                        if (CommandType.Get.ToString() == item.DeviceProParamName)
+                        {
+                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Get));
+                            //鍙戦�佽澶囧彿
+                            if (deviceProtocolDetail != null)
+                            {
+                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//鎵撳紑涓插彛鏃跺厛璁惧��
+                            }
+                        }
+                        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");//鎵撳紑涓插彛鏃跺厛璁惧��
+                            }
+                        }
 
 
 
-                    serialPort.Write(commandSet);//鎵撳紑涓插彛鏃跺厛璁惧��
-                    commandType = CommandType.Set;
-                }
-                switch (commandType)
-                {
-                    case CommandType.Get:
-                        serialPort.Write(commandGet);
-                        break;
-                    case CommandType.Set:
-                        serialPort.Write(commandSet);
-                        break;
+                        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);
+
+                                if (item.DeviceProParamName == CommandType.Set.ToString())
+                                {
+                                    DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.SetOK));
+                                    //parseok 
+                                    if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
+                                    {
+                                        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))
+                                    {
+
+                                        string group = item.DeviceProDataBlock; // 璁惧鎵�灞炵粍鍒�
+                                        if (group == "鐢垫皵")
+                                        {
+
+                                            item.DeviceProParamName = CommandType.Set.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))
+                                            //{
+                                            //    item.DeviceProParamName = CommandType.Set.ToString();
+
+
+
+                                            //}
+                                        }
+                                    }
+                                }
+                            }
+
+
+                            //寤轰竴涓璞″皢鍏朵紶缁欏墠绔�
+                            string data = JsonConvert.SerializeObject(serialPortDevice);//杩欓噷serialPortDevice鏄亣璁炬湁杩欎釜瀵硅薄
+                            _webSocketContext.PublishAllClientPayload(data);
+                        }
+
+                    }
                 }
             }
             catch (Exception ex)
             {
-                throw new Exception(ex.Message);
+                WriteError("CommonConveyorLineJob", "test", ex);
             }
+            WriteDebug("CommonConveyorLineJob", "test");
             return Task.CompletedTask;
         }
-        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
-        {
-            byte[] buffers = new byte[1024];
-            while (serialPort.BytesToRead > 0)
-            {
-                serialPort.Read(buffers, 0, serialPort.BytesToRead > buffers.Length ? buffers.Length : serialPort.BytesToRead);
-                string data = Encoding.Default.GetString(buffers);
-                if (data.Contains(setOK) && commandType == CommandType.Set)
-                {
-                    commandType = CommandType.Get;
-                }
-                else if (data.Contains(getOK) && commandType == CommandType.Get)
-                {
-                    commandType = CommandType.Set;
+       
 
 
-                }
-                else if (data.Contains(getError) && commandType == CommandType.Get)
-                {
-                    commandType = CommandType.Set;
-
-
-                }
-            }
-        }
     }
 }

--
Gitblit v1.9.3