DcPlcClient.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.entity.DcRunSnapshot;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. /**
  8. * 自动化大冲 S7 客户端:写 MES 允许运行;轮询读 DB260 过程参数码。
  9. * 断线后指数退避重连:2s→4s→8s→16s→30s。
  10. */
  11. public class DcPlcClient {
  12. private static final Logger log = LoggerFactory.getLogger(DcPlcClient.class);
  13. private static final long RETRY_INITIAL_MS = 2L * 1000L;
  14. private static final long RETRY_MAX_MS = 30L * 1000L;
  15. private final String stationSn;
  16. private final String plcIp;
  17. private final int plcPort;
  18. /** MES 允许运行 BOOL,空则只打日志不写 PLC */
  19. private final String mesAllowAddr;
  20. private S7PLC plc;
  21. private volatile boolean connected;
  22. private volatile long nextRetryAtMs;
  23. private volatile long currentRetryDelayMs = RETRY_INITIAL_MS;
  24. public DcPlcClient(String stationSn, String plcIp, int plcPort, String mesAllowAddr) {
  25. this.stationSn = stationSn == null ? "" : stationSn.trim();
  26. this.plcIp = plcIp == null ? "" : plcIp.trim();
  27. this.plcPort = plcPort <= 0 ? 102 : plcPort;
  28. this.mesAllowAddr = mesAllowAddr == null ? "" : mesAllowAddr.trim();
  29. }
  30. public String getStationSn() {
  31. return stationSn;
  32. }
  33. public String getPlcIp() {
  34. return plcIp;
  35. }
  36. public int getPlcPort() {
  37. return plcPort;
  38. }
  39. public String getMesAllowAddr() {
  40. return mesAllowAddr;
  41. }
  42. public boolean isConnected() {
  43. return connected && plc != null;
  44. }
  45. /**
  46. * tick 调用:未连接时按指数退避自动重连(2s→4s→8s→16s→30s)。
  47. * 已连接则直接返回,不探活。
  48. */
  49. public void maintainConnection() {
  50. if (isConnected()) {
  51. return;
  52. }
  53. connect();
  54. }
  55. /**
  56. * 连接并轻量读实际产量探活。
  57. * 失败 / IP 未配只记日志并指数退避,避免 500ms tick 狂刷 WARN。
  58. */
  59. public synchronized void connect() {
  60. if (isConnected()) {
  61. return;
  62. }
  63. long now = System.currentTimeMillis();
  64. if (now < nextRetryAtMs) {
  65. return;
  66. }
  67. if (plcIp.isEmpty()) {
  68. connected = false;
  69. long waitSec = scheduleRetryAfterFailure() / 1000L;
  70. log.warn("工位[{}] PLC IP 未配置,跳过连接({}s 后再试)", stationSn, Long.valueOf(waitSec));
  71. return;
  72. }
  73. try {
  74. plc = new S7PLC(EPlcType.S1200, plcIp, plcPort);
  75. plc.readInt16(DcPlcPointMap.ACTUAL_QTY);
  76. connected = true;
  77. resetRetryBackoff();
  78. log.info("工位[{}] 大冲 PLC 已连接 {}:{}(DB260)", stationSn, plcIp, Integer.valueOf(plcPort));
  79. } catch (Exception e) {
  80. connected = false;
  81. plc = null;
  82. long waitSec = scheduleRetryAfterFailure() / 1000L;
  83. log.warn("工位[{}] PLC 连接失败 {}:{} — {}({}s 后再试)",
  84. stationSn, plcIp, Integer.valueOf(plcPort), e.getMessage(), Long.valueOf(waitSec));
  85. }
  86. }
  87. /**
  88. * 写 MES 允许运行。点未配置时返回 false(业务仍可本地走完,便于联调)。
  89. */
  90. public boolean setMesAllow(boolean allow) {
  91. if (mesAllowAddr.isEmpty()) {
  92. log.warn("工位[{}] mes-allow-addr 未配置,跳过写 PLC allow={}", stationSn, Boolean.valueOf(allow));
  93. return false;
  94. }
  95. return writeBoolean(mesAllowAddr, allow);
  96. }
  97. /**
  98. * 轮询读一整包 DB260:当前零件名称、下一零件名称、实际产量、产线状态、生产节拍、停机时间。
  99. */
  100. public DcRunSnapshot readSnapshot() {
  101. DcRunSnapshot s = new DcRunSnapshot();
  102. if (!ensurePlc()) {
  103. return s;
  104. }
  105. try {
  106. s.currentPart = nvl(plc.readString(DcPlcPointMap.CURRENT_PART));
  107. s.nextPart = nvl(plc.readString(DcPlcPointMap.NEXT_PART));
  108. s.actualQty = plc.readInt16(DcPlcPointMap.ACTUAL_QTY);
  109. s.lineState = plc.readInt16(DcPlcPointMap.LINE_STATE);
  110. s.lineStateText = DcPlcPointMap.lineStateText(s.lineState);
  111. s.takt = plc.readFloat32(DcPlcPointMap.TAKT);
  112. s.downtime = plc.readInt16(DcPlcPointMap.DOWNTIME);
  113. } catch (Exception e) {
  114. log.warn("工位[{}] 读 DB260 失败: {}", stationSn, e.getMessage());
  115. markDisconnectedAndResetBackoff();
  116. }
  117. return s;
  118. }
  119. /** 仅读实际产量;失败返回 null */
  120. public Integer readActualQty() {
  121. if (!ensurePlc()) {
  122. return null;
  123. }
  124. try {
  125. return Integer.valueOf(plc.readInt16(DcPlcPointMap.ACTUAL_QTY));
  126. } catch (Exception e) {
  127. markDisconnectedAndResetBackoff();
  128. return null;
  129. }
  130. }
  131. private boolean ensurePlc() {
  132. if (isConnected()) {
  133. return true;
  134. }
  135. connect();
  136. return isConnected();
  137. }
  138. private boolean writeBoolean(String addr, boolean value) {
  139. if (isBlank(addr) || !ensurePlc() || plc == null) {
  140. return false;
  141. }
  142. try {
  143. plc.writeBoolean(addr, value);
  144. return true;
  145. } catch (Exception e) {
  146. log.warn("工位[{}] 写 {}={} 失败: {}", stationSn, addr, Boolean.valueOf(value), e.getMessage());
  147. markDisconnectedAndResetBackoff();
  148. return false;
  149. }
  150. }
  151. private void markDisconnectedAndResetBackoff() {
  152. connected = false;
  153. currentRetryDelayMs = RETRY_INITIAL_MS;
  154. nextRetryAtMs = System.currentTimeMillis() + currentRetryDelayMs;
  155. }
  156. private long scheduleRetryAfterFailure() {
  157. connected = false;
  158. long used = currentRetryDelayMs;
  159. if (used < RETRY_INITIAL_MS) {
  160. used = RETRY_INITIAL_MS;
  161. }
  162. if (used > RETRY_MAX_MS) {
  163. used = RETRY_MAX_MS;
  164. }
  165. nextRetryAtMs = System.currentTimeMillis() + used;
  166. long next = used * 2L;
  167. currentRetryDelayMs = next > RETRY_MAX_MS ? RETRY_MAX_MS : next;
  168. return used;
  169. }
  170. private void resetRetryBackoff() {
  171. currentRetryDelayMs = RETRY_INITIAL_MS;
  172. nextRetryAtMs = 0L;
  173. }
  174. private static boolean isBlank(String s) {
  175. return s == null || s.trim().isEmpty();
  176. }
  177. private static String nvl(String s) {
  178. return s == null ? "" : s.trim();
  179. }
  180. }