|
@@ -1,6 +1,10 @@
|
|
|
package com.github.zuihou.business.util;
|
|
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
+import com.github.zuihou.common.util.StringUtil;
|
|
|
+import jline.internal.InputStreamReader;
|
|
|
+import net.schmizz.sshj.SSHClient;
|
|
|
+import net.schmizz.sshj.sftp.SFTPClient;
|
|
|
+import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
|
|
@@ -10,25 +14,42 @@ import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
|
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
|
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
|
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.w3c.dom.Document;
|
|
|
+import org.w3c.dom.Element;
|
|
|
+import org.xml.sax.InputSource;
|
|
|
|
|
|
+import javax.xml.parsers.DocumentBuilder;
|
|
|
+import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
+import java.io.*;
|
|
|
+import java.net.Socket;
|
|
|
+import java.net.UnknownHostException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
@Component
|
|
|
public class KEDEUtils {
|
|
|
-
|
|
|
+ private static Logger log = LoggerFactory.getLogger(KEDEUtils.class);
|
|
|
private List<NodeId> list=new ArrayList<NodeId>(){};
|
|
|
|
|
|
private OpcUaClient opcUaClient = null;
|
|
|
|
|
|
private String endPointUrl = "opc.tcp://192.168.11.63:12686";
|
|
|
|
|
|
+ private static final String USER = "root";
|
|
|
+ private static final String HOST = "192.168.11.39";
|
|
|
+ private static final int PORT = 8081;
|
|
|
+ private static final String PASSWORD = "root";
|
|
|
+
|
|
|
public OpcUaClient createOpcClient(String endPointUrl) throws Exception {
|
|
|
//opc ua服务端地址
|
|
|
//String endPointUrl = "opc.tcp://192.168.11.63:12686";
|
|
@@ -81,4 +102,173 @@ public class KEDEUtils {
|
|
|
|
|
|
return listCompletableFuture;
|
|
|
}
|
|
|
+
|
|
|
+ public static void uploadFile(String url){
|
|
|
+ String localFilePath = "D:\\temp2.txt";
|
|
|
+ String remoteDir = "/testNc/";
|
|
|
+ String path = StringUtil.isEmpty(url)? HOST:url;
|
|
|
+ SSHClient ssh = new SSHClient();
|
|
|
+ try {
|
|
|
+ // Disable host key checking for demo purposes
|
|
|
+ ssh.addHostKeyVerifier(new PromiscuousVerifier());
|
|
|
+
|
|
|
+ // Connect to the remote host
|
|
|
+ ssh.connect(path, PORT);
|
|
|
+
|
|
|
+ // Authenticate with the provided username and password
|
|
|
+ ssh.authPassword(USER, PASSWORD);
|
|
|
+
|
|
|
+ // Open SFTP session
|
|
|
+ SFTPClient sftpClient = ssh.newSFTPClient();
|
|
|
+
|
|
|
+ // Upload file to the remote directory
|
|
|
+ File localFile = new File(localFilePath);
|
|
|
+ String remoteFilePath = remoteDir + localFile.getName(); // Remote file path
|
|
|
+ sftpClient.put(localFilePath, remoteFilePath);
|
|
|
+
|
|
|
+ System.out.println("File uploaded successfully to " + remoteFilePath);
|
|
|
+
|
|
|
+ // Close the SFTP session
|
|
|
+ sftpClient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ ssh.disconnect();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void downloadFile(String url) {
|
|
|
+ String remoteFilePath = "/testNc/temp2.txt";
|
|
|
+ String localDirectoryPath = "D:/"; // Local directory path (ensure this path is writable)
|
|
|
+ String path = StringUtil.isEmpty(url)? HOST:url;
|
|
|
+ try (SSHClient ssh = new SSHClient()) {
|
|
|
+ // Disable host key checking for demo purposes
|
|
|
+ ssh.addHostKeyVerifier(new PromiscuousVerifier());
|
|
|
+ // Connect and authenticate
|
|
|
+ ssh.connect(path, PORT);
|
|
|
+ ssh.authPassword(USER, PASSWORD);
|
|
|
+
|
|
|
+ // Open SFTP session
|
|
|
+ try (SFTPClient sftpClient = ssh.newSFTPClient()) {
|
|
|
+ // Download file
|
|
|
+ sftpClient.get(remoteFilePath,localDirectoryPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deFile(String url) {
|
|
|
+ String remoteFilePath = "/testNc/temp2.txt";
|
|
|
+ String path = StringUtil.isEmpty(url)? HOST:url;
|
|
|
+ try (SSHClient ssh = new SSHClient()) {
|
|
|
+ ssh.addHostKeyVerifier(new PromiscuousVerifier());
|
|
|
+ ssh.connect(path, PORT);
|
|
|
+ ssh.authPassword(USER, PASSWORD);
|
|
|
+
|
|
|
+ // Open SFTP session
|
|
|
+ try (SFTPClient sftpClient = ssh.newSFTPClient()) {
|
|
|
+ // Download file
|
|
|
+ sftpClient.rm(remoteFilePath);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * socket 请求
|
|
|
+ * @param hostName
|
|
|
+ * @param portNumber
|
|
|
+ * @param xmlBuilder
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String sendSocket(String hostName, int portNumber,String xmlBuilder) {
|
|
|
+ String responseLine = null;
|
|
|
+ try (Socket echoSocket = new Socket(hostName, portNumber);
|
|
|
+ PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
|
|
|
+ BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
|
|
|
+ // 发送XML到服务器
|
|
|
+ out.println(xmlBuilder);
|
|
|
+ out.println(); // 空行表示XML的结束
|
|
|
+ // 接收服务器的响应
|
|
|
+ responseLine = in.readLine();
|
|
|
+ log.info("sendSocket - 服务器的回应XML::\n" + responseLine);
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
+ log.info("sendSocket - 无法找到主机:" + hostName +"错误原因"+ e.getMessage());
|
|
|
+ System.exit(1);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("I/O错误" + hostName +"错误原因"+ e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return responseLine;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<String, Object> resultMap(String xmlString, List<String> paramList) {
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+ try {
|
|
|
+ // 创建DocumentBuilderFactory实例
|
|
|
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
+ // 创建DocumentBuilder实例
|
|
|
+ DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
+ // 使用StringReader包装XML字符串,并将其解析为Document对象
|
|
|
+ Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
|
|
|
+ // 规范化文档(可选)
|
|
|
+ doc.getDocumentElement().normalize();
|
|
|
+ // 获取根元素
|
|
|
+ Element rootElement = doc.getDocumentElement();
|
|
|
+ for(String param : paramList){
|
|
|
+ // 获取元素
|
|
|
+ Element valueElement = (Element) rootElement.getElementsByTagName(param).item(0);
|
|
|
+ // 提取值
|
|
|
+ String title = valueElement.getTextContent();
|
|
|
+ // 输出结果
|
|
|
+ dataMap.put(param,title);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dataMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void test(){
|
|
|
+ // 机床ip
|
|
|
+ String deviceIp = "192.168.11.39";
|
|
|
+ // 机床端口
|
|
|
+ Integer devicePost = 62937;
|
|
|
+ // 通过sftp上传的nc名称
|
|
|
+ String ncName = "testN.nc";
|
|
|
+ // 通过sftp上传到机床NC的地址,注①必须使用"/" 斜杠,不能使用"\",②末尾必须加上/
|
|
|
+ String ncPath = "D:/NCprog/testNc/";
|
|
|
+
|
|
|
+ StringBuffer xmlBuffer = new StringBuffer();
|
|
|
+ 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>"); // NC程序名
|
|
|
+ xmlBuffer.append("<path>").append(ncPath).append("</path>"); // NC程序地址①必须使用"/" 斜杠,不能使用"\",②末尾必须加上/
|
|
|
+ xmlBuffer.append("<req>yes</req>");// 固定写法
|
|
|
+ xmlBuffer.append("</select>");// 固定写法
|
|
|
+
|
|
|
+ log.info("选中nc程序请求id{},port{},par{}", deviceIp, devicePost, xmlBuffer);
|
|
|
+ String xmlResult = this.sendSocket(deviceIp, devicePost, xmlBuffer.toString());
|
|
|
+ log.info("选中nc程序请求返回结果{}", xmlResult);
|
|
|
+
|
|
|
+ StringBuffer checkBuffer = new StringBuffer();
|
|
|
+ checkBuffer.append("<ncda><var>prg</var></ncda>");
|
|
|
+ String checkResult = this.sendSocket(deviceIp, devicePost, checkBuffer.toString());
|
|
|
+ log.info("检查机床当前使用nc程序{}", checkResult);
|
|
|
+//
|
|
|
+// List<String> paramList = new ArrayList<>();
|
|
|
+// paramList.add("AUTO IWO3");
|
|
|
+// Map<String, Object> stringObjectMap = SocketUtil.resultMap(xmlResult, paramList);
|
|
|
+// log.info("请求结果解析{}", stringObjectMap);
|
|
|
+ }
|
|
|
}
|