DcPressPlcClient.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package com.mes.service;
  2. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  3. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  4. import com.mes.config.MesClientProperties;
  5. import com.mes.entity.DcPressSnapshot;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. /**
  9. * 单台压机 S7 客户端:按 yml 点表读过程参数。
  10. * IP 或冲次地址未配则不连,避免拖死其它站。
  11. */
  12. public class DcPressPlcClient {
  13. private static final Logger log = LoggerFactory.getLogger(DcPressPlcClient.class);
  14. private static final long RETRY_INITIAL_MS = 2L * 1000L;
  15. private static final long RETRY_MAX_MS = 30L * 1000L;
  16. private final MesClientProperties.PressConfig cfg;
  17. private S7PLC plc;
  18. private volatile boolean connected;
  19. private volatile long nextRetryAtMs;
  20. private volatile long currentRetryDelayMs = RETRY_INITIAL_MS;
  21. public DcPressPlcClient(MesClientProperties.PressConfig cfg) {
  22. this.cfg = cfg;
  23. }
  24. public MesClientProperties.PressConfig getCfg() {
  25. return cfg;
  26. }
  27. public String getStationSn() {
  28. return nvl(cfg.getStationSn());
  29. }
  30. public int getPressNo() {
  31. return cfg.getPressNo();
  32. }
  33. public boolean isConnected() {
  34. return connected && plc != null;
  35. }
  36. /** tick:未连接按退避重连 */
  37. public void maintainConnection() {
  38. if (isConnected()) {
  39. return;
  40. }
  41. connect();
  42. }
  43. /** 连接并用冲次探活;IP/冲次地址空则跳过 */
  44. public synchronized void connect() {
  45. if (isConnected()) {
  46. return;
  47. }
  48. long now = System.currentTimeMillis();
  49. if (now < nextRetryAtMs) {
  50. return;
  51. }
  52. String ip = nvl(cfg.getPlcIp());
  53. String strokeAddr = nvl(cfg.getStrokeCountAddr());
  54. if (ip.isEmpty() || strokeAddr.isEmpty()) {
  55. connected = false;
  56. long waitSec = scheduleRetryAfterFailure() / 1000L;
  57. log.warn("压机[{}] PLC IP 或冲次地址未配置,跳过连接({}s 后再试)",
  58. cfg.getPressNo(), Long.valueOf(waitSec));
  59. return;
  60. }
  61. try {
  62. int port = cfg.getPlcPort() <= 0 ? 102 : cfg.getPlcPort();
  63. plc = new S7PLC(EPlcType.S1200, ip, port);
  64. plc.readUInt32(strokeAddr);
  65. connected = true;
  66. resetRetryBackoff();
  67. log.info("压机[{}] {} PLC 已连接 {}:{}",
  68. Integer.valueOf(cfg.getPressNo()), nvl(cfg.getTitle()), ip, Integer.valueOf(port));
  69. } catch (Exception e) {
  70. connected = false;
  71. plc = null;
  72. long waitSec = scheduleRetryAfterFailure() / 1000L;
  73. log.warn("压机[{}] PLC 连接失败 — {}({}s 后再试)",
  74. Integer.valueOf(cfg.getPressNo()), e.getMessage(), Long.valueOf(waitSec));
  75. }
  76. }
  77. /** 轮询读本机过程参数;地址空则对应字段留空 */
  78. public DcPressSnapshot readSnapshot() {
  79. DcPressSnapshot s = new DcPressSnapshot();
  80. if (!ensurePlc()) {
  81. return s;
  82. }
  83. try {
  84. String strokeAddr = nvl(cfg.getStrokeCountAddr());
  85. if (!strokeAddr.isEmpty()) {
  86. long stroke = plc.readUInt32(strokeAddr);
  87. s.strokeCount = stroke;
  88. s.strokeCountText = String.valueOf(stroke);
  89. }
  90. s.dieHeight = readFloatText(cfg.getDieHeightAddr());
  91. s.cylinderPressure = readFloatText(cfg.getCylinderPressureAddr());
  92. String tonAddr = nvl(cfg.getTonnageAddr());
  93. if (!tonAddr.isEmpty()) {
  94. if (cfg.isTonnageDint()) {
  95. s.tonnage = String.valueOf(plc.readInt32(tonAddr));
  96. } else {
  97. s.tonnage = fmtFloat(plc.readFloat32(tonAddr));
  98. }
  99. }
  100. s.pressSpm = readFloatText(cfg.getPressSpmAddr());
  101. s.airCushion = readFloatText(cfg.getAirCushionAddr());
  102. s.ejectorStroke = readFloatText(cfg.getEjectorStrokeAddr());
  103. // 能耗 DINT
  104. String energyAddr = nvl(cfg.getEnergyAddr());
  105. if (!energyAddr.isEmpty()) {
  106. s.energyKwh = String.valueOf(plc.readInt32(energyAddr));
  107. }
  108. String runAddr = nvl(cfg.getDeviceRunAddr());
  109. if (!runAddr.isEmpty()) {
  110. s.deviceRunning = plc.readBoolean(runAddr);
  111. s.deviceStateText = s.deviceRunning ? "运行" : "停止";
  112. }
  113. } catch (Exception e) {
  114. log.warn("压机[{}] 读过程参数失败: {}", Integer.valueOf(cfg.getPressNo()), e.getMessage());
  115. markDisconnectedAndResetBackoff();
  116. }
  117. return s;
  118. }
  119. /** 仅读冲次;失败返回 null */
  120. public Long readStrokeCount() {
  121. String strokeAddr = nvl(cfg.getStrokeCountAddr());
  122. if (strokeAddr.isEmpty() || !ensurePlc()) {
  123. return null;
  124. }
  125. try {
  126. return Long.valueOf(plc.readUInt32(strokeAddr));
  127. } catch (Exception e) {
  128. markDisconnectedAndResetBackoff();
  129. return null;
  130. }
  131. }
  132. private String readFloatText(String addr) {
  133. if (isBlank(addr) || plc == null) {
  134. return "";
  135. }
  136. return fmtFloat(plc.readFloat32(addr.trim()));
  137. }
  138. private boolean ensurePlc() {
  139. if (isConnected()) {
  140. return true;
  141. }
  142. connect();
  143. return isConnected();
  144. }
  145. private void markDisconnectedAndResetBackoff() {
  146. connected = false;
  147. currentRetryDelayMs = RETRY_INITIAL_MS;
  148. nextRetryAtMs = System.currentTimeMillis() + currentRetryDelayMs;
  149. }
  150. private long scheduleRetryAfterFailure() {
  151. connected = false;
  152. long used = currentRetryDelayMs;
  153. if (used < RETRY_INITIAL_MS) {
  154. used = RETRY_INITIAL_MS;
  155. }
  156. if (used > RETRY_MAX_MS) {
  157. used = RETRY_MAX_MS;
  158. }
  159. nextRetryAtMs = System.currentTimeMillis() + used;
  160. long next = used * 2L;
  161. currentRetryDelayMs = next > RETRY_MAX_MS ? RETRY_MAX_MS : next;
  162. return used;
  163. }
  164. private void resetRetryBackoff() {
  165. currentRetryDelayMs = RETRY_INITIAL_MS;
  166. nextRetryAtMs = 0L;
  167. }
  168. private static boolean isBlank(String s) {
  169. return s == null || s.trim().isEmpty();
  170. }
  171. private static String nvl(String s) {
  172. return s == null ? "" : s.trim();
  173. }
  174. private static String fmtFloat(float v) {
  175. if (Float.isNaN(v) || Float.isInfinite(v)) {
  176. return "";
  177. }
  178. String s = String.valueOf(v);
  179. if (s.indexOf('.') > 0) {
  180. while (s.endsWith("0")) {
  181. s = s.substring(0, s.length() - 1);
  182. }
  183. if (s.endsWith(".")) {
  184. s = s.substring(0, s.length() - 1);
  185. }
  186. }
  187. return s;
  188. }
  189. }