package com.mes.service; import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; import com.github.xingshuangs.iot.protocol.s7.service.S7PLC; import com.mes.entity.DcRunSnapshot; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 自动化大冲 S7 客户端:写 MES 允许运行;轮询读 DB260 过程参数码。 * 断线后指数退避重连:2s→4s→8s→16s→30s。 */ public class DcPlcClient { private static final Logger log = LoggerFactory.getLogger(DcPlcClient.class); private static final long RETRY_INITIAL_MS = 2L * 1000L; private static final long RETRY_MAX_MS = 30L * 1000L; private final String stationSn; private final String plcIp; private final int plcPort; /** MES 允许运行 BOOL,空则只打日志不写 PLC */ private final String mesAllowAddr; private S7PLC plc; private volatile boolean connected; private volatile long nextRetryAtMs; private volatile long currentRetryDelayMs = RETRY_INITIAL_MS; public DcPlcClient(String stationSn, String plcIp, int plcPort, String mesAllowAddr) { this.stationSn = stationSn == null ? "" : stationSn.trim(); this.plcIp = plcIp == null ? "" : plcIp.trim(); this.plcPort = plcPort <= 0 ? 102 : plcPort; this.mesAllowAddr = mesAllowAddr == null ? "" : mesAllowAddr.trim(); } public String getStationSn() { return stationSn; } public String getPlcIp() { return plcIp; } public int getPlcPort() { return plcPort; } public String getMesAllowAddr() { return mesAllowAddr; } public boolean isConnected() { return connected && plc != null; } /** * tick 调用:未连接时按指数退避自动重连(2s→4s→8s→16s→30s)。 * 已连接则直接返回,不探活。 */ public void maintainConnection() { if (isConnected()) { return; } connect(); } /** * 连接并轻量读实际产量探活。 * 失败 / IP 未配只记日志并指数退避,避免 500ms tick 狂刷 WARN。 */ public synchronized void connect() { if (isConnected()) { return; } long now = System.currentTimeMillis(); if (now < nextRetryAtMs) { return; } if (plcIp.isEmpty()) { connected = false; long waitSec = scheduleRetryAfterFailure() / 1000L; log.warn("工位[{}] PLC IP 未配置,跳过连接({}s 后再试)", stationSn, Long.valueOf(waitSec)); return; } try { plc = new S7PLC(EPlcType.S1200, plcIp, plcPort); plc.readInt16(DcPlcPointMap.ACTUAL_QTY); connected = true; resetRetryBackoff(); log.info("工位[{}] 大冲 PLC 已连接 {}:{}(DB260)", stationSn, plcIp, Integer.valueOf(plcPort)); } catch (Exception e) { connected = false; plc = null; long waitSec = scheduleRetryAfterFailure() / 1000L; log.warn("工位[{}] PLC 连接失败 {}:{} — {}({}s 后再试)", stationSn, plcIp, Integer.valueOf(plcPort), e.getMessage(), Long.valueOf(waitSec)); } } /** * 写 MES 允许运行。点未配置时返回 false(业务仍可本地走完,便于联调)。 */ public boolean setMesAllow(boolean allow) { if (mesAllowAddr.isEmpty()) { log.warn("工位[{}] mes-allow-addr 未配置,跳过写 PLC allow={}", stationSn, Boolean.valueOf(allow)); return false; } return writeBoolean(mesAllowAddr, allow); } /** * 轮询读一整包 DB260:当前零件名称、下一零件名称、实际产量、产线状态、生产节拍、停机时间。 */ public DcRunSnapshot readSnapshot() { DcRunSnapshot s = new DcRunSnapshot(); if (!ensurePlc()) { return s; } try { s.currentPart = nvl(plc.readString(DcPlcPointMap.CURRENT_PART)); s.nextPart = nvl(plc.readString(DcPlcPointMap.NEXT_PART)); s.actualQty = plc.readInt16(DcPlcPointMap.ACTUAL_QTY); s.lineState = plc.readInt16(DcPlcPointMap.LINE_STATE); s.lineStateText = DcPlcPointMap.lineStateText(s.lineState); s.takt = plc.readFloat32(DcPlcPointMap.TAKT); s.downtime = plc.readInt16(DcPlcPointMap.DOWNTIME); } catch (Exception e) { log.warn("工位[{}] 读 DB260 失败: {}", stationSn, e.getMessage()); markDisconnectedAndResetBackoff(); } return s; } /** 仅读实际产量;失败返回 null */ public Integer readActualQty() { if (!ensurePlc()) { return null; } try { return Integer.valueOf(plc.readInt16(DcPlcPointMap.ACTUAL_QTY)); } catch (Exception e) { markDisconnectedAndResetBackoff(); return null; } } private boolean ensurePlc() { if (isConnected()) { return true; } connect(); return isConnected(); } private boolean writeBoolean(String addr, boolean value) { if (isBlank(addr) || !ensurePlc() || plc == null) { return false; } try { plc.writeBoolean(addr, value); return true; } catch (Exception e) { log.warn("工位[{}] 写 {}={} 失败: {}", stationSn, addr, Boolean.valueOf(value), e.getMessage()); markDisconnectedAndResetBackoff(); return false; } } private void markDisconnectedAndResetBackoff() { connected = false; currentRetryDelayMs = RETRY_INITIAL_MS; nextRetryAtMs = System.currentTimeMillis() + currentRetryDelayMs; } private long scheduleRetryAfterFailure() { connected = false; long used = currentRetryDelayMs; if (used < RETRY_INITIAL_MS) { used = RETRY_INITIAL_MS; } if (used > RETRY_MAX_MS) { used = RETRY_MAX_MS; } nextRetryAtMs = System.currentTimeMillis() + used; long next = used * 2L; currentRetryDelayMs = next > RETRY_MAX_MS ? RETRY_MAX_MS : next; return used; } private void resetRetryBackoff() { currentRetryDelayMs = RETRY_INITIAL_MS; nextRetryAtMs = 0L; } private static boolean isBlank(String s) { return s == null || s.trim().isEmpty(); } private static String nvl(String s) { return s == null ? "" : s.trim(); } }