ExecutionServer.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.zksy.lamp.server;
  2. import com.zksy.common.exception.CommonException;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. import javax.annotation.PostConstruct;
  8. import javax.annotation.PreDestroy;
  9. import java.io.*;
  10. import java.net.ServerSocket;
  11. import java.net.Socket;
  12. /**
  13. * @author Administrator
  14. * @version 1.0
  15. * @project dh-server-micro
  16. * @description 指令执行
  17. * @date 2025/2/10 16:25:14
  18. */
  19. @Component
  20. public class ExecutionServer {
  21. private static final Logger log = LoggerFactory.getLogger(ExecutionServer.class);
  22. private BufferedReader reader;
  23. private BufferedWriter writer;
  24. private ServerSocket server;
  25. private Socket client;
  26. private int serverPort = 15678;
  27. @PostConstruct
  28. public void init(){
  29. Runnable func0 = this::startServer;
  30. Thread thread = new Thread(func0);
  31. thread.start();
  32. }
  33. @PreDestroy
  34. public void closeResource(){
  35. try {
  36. if (reader != null) reader.close();
  37. if (writer != null) writer.close();
  38. if (client != null) client.close();
  39. if (server != null) server.close();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. /**
  45. * 启动服务器并等待客户端连接
  46. */
  47. public void startServer() {
  48. try {
  49. System.out.println("本机作为服务端");
  50. System.out.println("本机端口:" + serverPort);
  51. server = new ServerSocket(serverPort);
  52. System.out.println("等待客户机的连接");
  53. client = server.accept();
  54. System.out.println("连接成功");
  55. reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));
  56. writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "GB2312"));
  57. System.out.println("测试完成!");
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. public String ExecutionData(String msg) {
  63. try {
  64. String returnMsgReplace = "";
  65. System.out.println("第1路闭合,发送指令:"+msg);
  66. //发送数据
  67. writer.write(msg);
  68. writer.flush();
  69. //读数据
  70. String returnMsg = reader.readLine();
  71. System.out.println("returnMsg================:"+returnMsg);
  72. if (returnMsg != null) {
  73. returnMsgReplace = returnMsg.replace("\r\n", "\\r\\n");
  74. System.out.println("设备应答:" + returnMsgReplace);
  75. } else {
  76. System.out.println("接收到的消息为 null,重新建立连接...");
  77. // 关闭当前连接资源
  78. closeResource();
  79. // 重新启动服务器并等待客户端连接
  80. startServer();
  81. // 重新发送指令
  82. writer.write(msg);
  83. writer.flush();
  84. String returnMsg2 = reader.readLine();
  85. returnMsgReplace = returnMsg2.replace("\r\n", "\\r\\n");
  86. System.out.println("重新连接后接收到的消息:" + returnMsgReplace);
  87. }
  88. if("AT".equals(returnMsgReplace)) {
  89. String s = "AT+ACK\r\n";
  90. writer.write(s);
  91. writer.flush();
  92. throw new CommonException("当次设置失败,请再次设置",456);
  93. }
  94. System.out.println("指令执行成功!");
  95. return returnMsgReplace;
  96. } catch (Exception e) {
  97. log.info(e.toString());
  98. throw new RuntimeException("指令执行失败");
  99. }
  100. }
  101. }