浏览代码

科德上传 删除程序

oyq28 10 月之前
父节点
当前提交
31865cec41

+ 6 - 3
Kede/Form_Main.cs

@@ -77,7 +77,7 @@ namespace IMCS
                     StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                     string body = reader.ReadToEnd();
 
-                    YG.Log.Instance.WriteLogAdd(">>>===收到POST数据 : >>>>===" + body);
+                    YG.Log.Instance.WriteLogAdd(Thread.GetDomainID()+">>>===收到POST数据 : >>>>===" + body);
                     ResponseBody responseBody = new ResponseBody();
 
                     RequestBody opcBody = JsonConvert.DeserializeObject<RequestBody>(body);
@@ -87,9 +87,12 @@ namespace IMCS
                         case "1"://数据采集
                             responseBody = OpcUaUtils.OpcCaiji(opcBody.ServerUrl, opcBody.UserName, opcBody.Password, opcBody.Values);
                             break;
-                        case "2":
+                        case "2"://上传并设置为主程序
                             responseBody = SftpWinSCPUtils.sftpUpload(opcBody.LocalFilePath, opcBody.RemoteFilePath, opcBody.ServerUrl, opcBody.UserName, opcBody.Password, opcBody.Port);
                             break;
+                        case "4"://删除程序
+                            responseBody = SftpWinSCPUtils.SftpDelete(opcBody.RemoteFilePath, opcBody.ServerUrl, opcBody.UserName, opcBody.Password, opcBody.Port);
+                            break;
                     }
 
                     response.ContentType = "application/json;charset=UTF-8";
@@ -100,7 +103,7 @@ namespace IMCS
 
                     using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
                     {
-                        YG.Log.Instance.WriteLogAdd($"WMS--->>{responseBody.Json_SerializeObject()}--->>{body}\r\n");
+                        YG.Log.Instance.WriteLogAdd(Thread.GetDomainID() + $"返回--->>{responseBody.Json_SerializeObject()}--->>{body}\r\n");
 
                         writer.Write(responseBody.Json_SerializeObject());
                         writer.Close();

+ 56 - 0
Kede/Utils/SftpWinSCPUtils.cs

@@ -9,6 +9,7 @@ using WinSCP;
 using Kede;
 using System.Text;
 using System.IO;
+using Kede.Utils;
 
 namespace TestAgreement.utils
 {
@@ -82,6 +83,11 @@ namespace TestAgreement.utils
                     // 断开连接
                     session.Dispose();
                 }
+                if(responseBody.code == 1)
+                {
+                    SocketUtil.socket(ServerUrl, Path.GetFileName(localFilePath),responseBody);
+                }
+
                 return responseBody;
             }
 
@@ -141,5 +147,55 @@ namespace TestAgreement.utils
 
 
         }
+
+
+        /*
+          删除nc程序
+         */
+        public static ResponseBody SftpDelete(string remoteFilePath, string ServerUrl,string UserName,string Pasword,string PortNumber)
+        {
+            Console.WriteLine("====================sftp文件删除=========================");
+            // 远程文件路径
+            //string remoteFilePath = "/testNc/testNcQ.nc";
+            ResponseBody responseBody = new ResponseBody();
+            responseBody.code = 1;
+            // 设定 SFTP 连接选项
+            SessionOptions sessionOptions = new SessionOptions
+            {
+                Protocol = Protocol.Sftp,
+                HostName = ServerUrl, // SFTP 服务器地址
+                UserName = UserName,     // 用户名l
+                Password = Pasword,     // 密码
+                PortNumber = PortNumber.StringToInt(),
+            };
+            sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true;
+            // 使用 WinSCP 会话
+            using (Session session = new Session())
+            {
+                try
+                {
+                    // 连接到 SFTP 服务器
+                    session.Open(sessionOptions);
+                    Console.WriteLine("Connected to SFTP server.");
+
+                    // 执行下载
+                    session.RemoveFile(remoteFilePath);
+
+                }
+                catch (Exception e)
+                {
+                    Console.WriteLine("Error: " + e.Message);
+                    responseBody.code = 0;
+                    responseBody.msg = e.Message;
+                }
+                finally
+                {
+                    // 断开连接
+                    session.Dispose();
+                }
+            }
+
+            return responseBody;
+        }
     }
 }

+ 152 - 0
Kede/Utils/SocketUtil.cs

@@ -0,0 +1,152 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Net.Sockets;
+using System.Text;
+using System.Xml;
+using Microsoft.Extensions.Logging;
+
+
+namespace Kede.Utils
+
+{
+    public class SocketUtil
+    {
+
+
+        public static void socket(string deviceIp, string ncName,ResponseBody response)
+        {
+            // 机床 IP
+            //string deviceIp = "192.168.11.41";
+            // 机床端口
+            int devicePort = 62937;
+            // 通过 SFTP 上传的 NC 名称
+            //string ncName = "X1.nc";
+            // 通过 SFTP 上传到机床 NC 的地址,注意必须使用"/" 斜杠,不能使用"\", 末尾必须加上"/"
+            string ncPath = "D:/NCprog/testNc/";
+
+            StringBuilder xmlBuffer = new StringBuilder();
+            xmlBuffer.Append("<select>");
+            xmlBuffer.Append("<sub>exe</sub>");
+            xmlBuffer.Append("<level>file</level>");
+            xmlBuffer.Append("<st>nc1</st>");
+            xmlBuffer.Append("<name>").Append(ncName).Append("</name>");
+            xmlBuffer.Append("<path>").Append(ncPath).Append("</path>");
+            xmlBuffer.Append("<req>yes</req>");
+            xmlBuffer.Append("</select>");
+
+            YG.Log.Instance.WriteLogAdd($"选中nc程序请求id{deviceIp},port{devicePort},par{xmlBuffer}");
+            string xmlResult = SocketUtil.SendSocket(deviceIp, devicePort, xmlBuffer.ToString());
+            //<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>
+            YG.Log.Instance.WriteLogAdd($"选中nc程序请求返回结果{xmlResult}");
+
+            StringBuilder checkBuffer = new StringBuilder();
+            checkBuffer.Append("<ncda><var>prg</var></ncda>");
+            string checkResult = SocketUtil.SendSocket(deviceIp, devicePort, checkBuffer.ToString());
+            YG.Log.Instance.WriteLogAdd($"检查机床当前使用nc程序{checkResult}");
+            //<ncda><prg>D:/NCprog/testNc/X.nc</prg></ncda>
+            string xmlFileName = ExtractFileNameFromXml(xmlResult, "name");
+            string checkFileName = ExtractFileNameFromXml(checkResult, "prg");
+            if (xmlFileName != checkFileName)
+            {
+                response.msg = $"设置机床主程序nc程序{xmlFileName}失败";
+                response.code = 0;
+            }
+
+            // 若需要对返回的 XML 结果进行解析
+            // List<string> paramList = new List<string> { "AUTO IWO3" };
+            // Dictionary<string, object> resultMap = SocketUtil.ResultMap(xmlResult, paramList);
+            // log.Info($"请求结果解析{resultMap}");
+        }
+
+        public static string SendSocket(string hostName, int portNumber, string xmlBuilder)
+        {
+            string responseLine = null;
+            try
+            {
+                using (TcpClient client = new TcpClient(hostName, portNumber))
+                using (NetworkStream stream = client.GetStream())
+                using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true })
+                using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
+                {
+                    // 发送 XML 到服务器
+                    writer.WriteLine(xmlBuilder);
+                    writer.WriteLine(); // 空行表示 XML 的结束
+
+                    // 接收服务器的响应
+                    responseLine = reader.ReadLine();
+                    YG.Log.Instance.WriteLogAdd("sendSocket - 服务器的回应XML::\n" + responseLine);
+                }
+            }
+            catch (SocketException e)
+            {
+                YG.Log.Instance.WriteLogAdd("sendSocket - 无法连接到主机:" + hostName + " 错误原因:" + e.Message);
+                Environment.Exit(1);
+            }
+            catch (IOException e)
+            {
+                YG.Log.Instance.WriteLogAdd("I/O 错误 " + hostName + " 错误原因:" + e.Message);
+            }
+            return responseLine;
+        }
+
+        public static Dictionary<string, object> ResultMap(string xmlString, List<string> paramList)
+        {
+            Dictionary<string, object> dataMap = new Dictionary<string, object>();
+            try
+            {
+                // 加载 XML 字符串
+                XmlDocument doc = new XmlDocument();
+                doc.LoadXml(xmlString);
+
+                // 获取根元素
+                XmlElement rootElement = doc.DocumentElement;
+
+                foreach (string param in paramList)
+                {
+                    // 获取元素
+                    XmlNode valueElement = rootElement.GetElementsByTagName(param).Item(0);
+                    if (valueElement != null)
+                    {
+                        // 提取值
+                        string title = valueElement.InnerText;
+                        // 输出结果
+                        dataMap[param] = title;
+                    }
+                }
+            }
+            catch (Exception e)
+            {
+                YG.Log.Instance.WriteLogAdd("XML 解析错误:" + e.Message);
+            }
+            return dataMap;
+        }
+
+        private static string ExtractFileNameFromXml(string xml, string tagName)
+        {
+            XmlDocument doc = new XmlDocument();
+            doc.LoadXml(xml);
+            XmlNode node = doc.GetElementsByTagName(tagName).Item(0);
+
+            if (node != null)
+            {
+                if (tagName == "prg")
+                {
+                    // 从完整路径中提取文件名
+                    return System.IO.Path.GetFileName(node.InnerText);
+                }
+                else
+                {
+                    return node.InnerText;
+                }
+            }
+            return null;
+        }
+    }
+}
+

二进制
Kede/bin/Debug/net5.0-windows/Kede.dll


二进制
Kede/bin/Debug/net5.0-windows/Kede.exe


二进制
Kede/bin/Debug/net5.0-windows/Kede.pdb


+ 106 - 0
Kede/bin/Debug/net5.0-windows/YGLog/2024-08-15.txt

@@ -647,3 +647,109 @@
     "port":"8081"
 }
 
+2024-08-15 18:32:00 430-->>>>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X.nc",
+    "remoteFilePath":"/testNc/X.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-15 18:32:06 612-->>>选中nc程序请求id192.168.11.39,port62937,par<select><sub>exe</sub><level>file</level><st>nc1</st><name>X.nc</name><path>D:/NCprog/testNc/</path><req>yes</req></select>
+2024-08-15 18:32:12 811-->>>sendSocket - 服务器的回应XML::
+<select><sub>exe</sub><level>file</level><name>X.nc</name></select>
+2024-08-15 18:32:13 900-->>>选中nc程序请求返回结果<select><sub>exe</sub><level>file</level><name>X.nc</name></select>
+2024-08-15 18:32:41 823-->>>sendSocket - 服务器的回应XML::
+<ncda><prg>D:/NCprog/testNc/X.nc</prg></ncda>
+2024-08-15 18:32:43 209-->>>检查机床当前使用nc程序<ncda><prg>D:/NCprog/testNc/X.nc</prg></ncda>
+2024-08-15 18:32:53 827-->>>WMS--->>{"code":0,"result":true,"msg":"设置机床主程序nc程序<select><sub>exe</sub><level>file</level><name>X.nc</name></select>失败","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X.nc",
+    "remoteFilePath":"/testNc/X.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+
+2024-08-15 18:33:47 400-->>>>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X.nc",
+    "remoteFilePath":"/testNc/X.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-15 18:33:47 423-->>>WMS--->>{"code":0,"result":true,"msg":"File 'D:\\X.nc' does not exist.","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X.nc",
+    "remoteFilePath":"/testNc/X.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+
+2024-08-15 18:34:29 674-->>>>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-15 18:34:35 609-->>>选中nc程序请求id192.168.11.39,port62937,par<select><sub>exe</sub><level>file</level><st>nc1</st><name>X2.nc</name><path>D:/NCprog/testNc/</path><req>yes</req></select>
+2024-08-15 18:34:49 004-->>>sendSocket - 服务器的回应XML::
+<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>
+2024-08-15 18:34:49 725-->>>选中nc程序请求返回结果<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>
+2024-08-15 18:35:29 050-->>>sendSocket - 服务器的回应XML::
+<ncda><prg>D:/NCprog/testNc/X.nc</prg></ncda>
+2024-08-15 18:35:29 073-->>>检查机床当前使用nc程序
+2024-08-15 18:35:29 095-->>>WMS--->>{"code":0,"result":true,"msg":"设置机床主程序nc程序<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>失败","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+
+2024-08-15 18:36:32 461-->>>>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-15 18:37:19 453-->>>>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-15 18:37:28 934-->>>选中nc程序请求id192.168.11.39,port62937,par<select><sub>exe</sub><level>file</level><st>nc1</st><name>X2.nc</name><path>D:/NCprog/testNc/</path><req>yes</req></select>
+2024-08-15 18:37:32 575-->>>sendSocket - 服务器的回应XML::
+<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>
+2024-08-15 18:37:33 758-->>>选中nc程序请求返回结果<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>
+2024-08-15 18:37:39 015-->>>sendSocket - 服务器的回应XML::
+<ncda><prg>D:/NCprog/testNc/X2.nc</prg></ncda>
+2024-08-15 18:37:39 039-->>>检查机床当前使用nc程序<ncda><prg>D:/NCprog/testNc/X2.nc</prg></ncda>
+2024-08-15 18:37:39 149-->>>WMS--->>{"code":0,"result":true,"msg":"设置机床主程序nc程序<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>失败","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+

+ 104 - 0
Kede/bin/Debug/net5.0-windows/YGLog/2024-08-16.txt

@@ -0,0 +1,104 @@
+2024-08-16 08:28:45 662-->>>>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-16 08:29:31 429-->>>WMS--->>{"code":0,"result":true,"msg":"Network error: Connection to \"192.168.11.39\" timed out.","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+
+2024-08-16 09:47:48 209-->>>>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-16 09:47:55 998-->>>选中nc程序请求id192.168.11.39,port62937,par<select><sub>exe</sub><level>file</level><st>nc1</st><name>X2.nc</name><path>D:/NCprog/testNc/</path><req>yes</req></select>
+2024-08-16 09:48:06 632-->>>sendSocket - 服务器的回应XML::
+<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>
+2024-08-16 09:48:06 692-->>>选中nc程序请求返回结果<select><sub>exe</sub><level>file</level><name>X2.nc</name></select>
+2024-08-16 09:48:06 713-->>>sendSocket - 服务器的回应XML::
+<ncda><prg>D:/NCprog/testNc/X2.nc</prg></ncda>
+2024-08-16 09:48:06 734-->>>检查机床当前使用nc程序<ncda><prg>D:/NCprog/testNc/X2.nc</prg></ncda>
+2024-08-16 09:48:06 755-->>>1返回--->>{"code":1,"result":true,"msg":"调用成功","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"2",
+    "localFilePath":"D:\\X2.nc",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+
+2024-08-16 09:49:49 291-->>>1>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"4",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-16 09:49:50 068-->>>1返回--->>{"code":0,"result":true,"msg":"SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOptions.SshHostKeyFingerprint is not set.","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"4",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+
+2024-08-16 09:50:31 440-->>>1>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"4",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-16 09:54:13 914-->>>1>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"4",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-16 09:54:17 948-->>>1返回--->>{"code":0,"result":true,"msg":"SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOptions.SshHostKeyFingerprint is not set.","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"4",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+
+2024-08-16 09:56:20 632-->>>1>>>===收到POST数据 : >>>>==={
+    "serverUrl":"192.168.11.39",
+    "type":"4",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+2024-08-16 09:56:24 315-->>>1返回--->>{"code":1,"result":true,"msg":"调用成功","statusCode":200,"values":null,"toolsInfo":"","errorsInfo":"","runDatasInfo":""}--->>{
+    "serverUrl":"192.168.11.39",
+    "type":"4",
+    "remoteFilePath":"/testNc/X2.nc",
+    "userName":"root",
+    "password":"root",
+    "port":"8081"
+}
+