|
|
@@ -29,6 +29,9 @@ public class PressPanel extends JPanel {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(PressPanel.class);
|
|
|
|
|
|
+ /** 采样缓冲上限:失败回塞时丢最旧,防服务挂了内存涨爆 */
|
|
|
+ private static final int SAMPLE_BUF_CAP = 100;
|
|
|
+
|
|
|
private final MesClientProperties.PressConfig cfg;
|
|
|
private final String serverIp;
|
|
|
/** 线体工位号(与自动化设备/取码工位相同,五台共用) */
|
|
|
@@ -36,6 +39,7 @@ public class PressPanel extends JPanel {
|
|
|
private final int sampleMs;
|
|
|
private final int uploadMax;
|
|
|
private final DcPressPlcClient plc;
|
|
|
+ /** 采样上报是否在途(防并发双刷) */
|
|
|
private final AtomicBoolean uploading = new AtomicBoolean(false);
|
|
|
|
|
|
/** 本批冲次起点;中途复位后按已产重定 */
|
|
|
@@ -48,7 +52,7 @@ public class PressPanel extends JPanel {
|
|
|
private volatile String boundBatchCode = "";
|
|
|
private volatile long lastSampleAtMs;
|
|
|
private volatile DcPressSnapshot lastSnap = new DcPressSnapshot();
|
|
|
- /** 待上报采样点(内存缓冲,满则批量上传) */
|
|
|
+ /** 待上报采样点(内存缓冲,满则批量上传;不落盘) */
|
|
|
private final List<Map<String, String>> sampleBuf = new ArrayList<Map<String, String>>();
|
|
|
|
|
|
private final StatusLight lampPlc;
|
|
|
@@ -221,17 +225,22 @@ public class PressPanel extends JPanel {
|
|
|
setText(txtTarget, target > 0 ? String.valueOf(target) : "");
|
|
|
|
|
|
if (!lineRun) {
|
|
|
- // 线体空闲:清本机批内状态,冲刷残留采样
|
|
|
+ // 线体空闲兜底:按本机绑定批次刷残留,再清进度(正式结案走 onLineBatchClosed)
|
|
|
if (!boundBatchCode.isEmpty() || producedQty > 0 || !sampleBuf.isEmpty() || finishTriggered) {
|
|
|
- flushSamplesAsync();
|
|
|
+ String codeSnap = boundBatchCode;
|
|
|
+ flushSamplesAsync(codeSnap);
|
|
|
resetLocalBatchProgress();
|
|
|
}
|
|
|
setText(txtProduced, "");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 批次码变化:重定冲次起点
|
|
|
+ // 批次码变化:先丢掉旧批残留缓冲,再重定冲次起点(防串批)
|
|
|
if (!batch.equalsIgnoreCase(boundBatchCode)) {
|
|
|
+ if (!boundBatchCode.isEmpty()) {
|
|
|
+ flushSamplesAsync(boundBatchCode);
|
|
|
+ }
|
|
|
+ clearSampleBuf();
|
|
|
boundBatchCode = batch;
|
|
|
finishTriggered = false;
|
|
|
producedQty = 0;
|
|
|
@@ -258,12 +267,12 @@ public class PressPanel extends JPanel {
|
|
|
|
|
|
maybeSample(snap, batch);
|
|
|
|
|
|
- // 压机5:冲次达标 → 主界面打印结案
|
|
|
+ // 压机5:冲次达标 → 主界面打印结案(尾点由结案回调短等刷出)
|
|
|
if (cfg.getPressNo() == 5 && !finishTriggered && target > 0 && producedQty >= target) {
|
|
|
finishTriggered = true;
|
|
|
log.info("压机5 达标 produced={} target={} batch={},触发主界面结案",
|
|
|
Integer.valueOf(producedQty), Integer.valueOf(target), batch);
|
|
|
- flushSamplesAsync();
|
|
|
+ flushSamplesAsync(batch);
|
|
|
MainFrame.requestFinishFromPress5(producedQty);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
@@ -271,27 +280,41 @@ public class PressPanel extends JPanel {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /** 线体结案瞬间冲刷缓冲(主界面调用) */
|
|
|
+ /**
|
|
|
+ * 线体结案瞬间:短等刷光本批采样,再清本机进度。
|
|
|
+ * 用绑定批次快照上报,避免线体上下文已 close 后 batch 变空丢尾点。
|
|
|
+ */
|
|
|
public void onLineBatchClosed() {
|
|
|
- flushSamplesAsync();
|
|
|
+ String codeSnap = boundBatchCode;
|
|
|
+ flushSamplesForBatch(codeSnap);
|
|
|
resetLocalBatchProgress();
|
|
|
}
|
|
|
|
|
|
/** 线体开批瞬间复位本机进度(主界面调用) */
|
|
|
public void onLineBatchOpened() {
|
|
|
+ clearSampleBuf();
|
|
|
resetLocalBatchProgress();
|
|
|
finishTriggered = false;
|
|
|
}
|
|
|
|
|
|
+ /** 清冲次/进度;顺带清空采样缓冲,避免旧批点挂到新批 */
|
|
|
private void resetLocalBatchProgress() {
|
|
|
baseStrokeCount = 0L;
|
|
|
lastStrokeCount = 0L;
|
|
|
producedQty = 0;
|
|
|
finishTriggered = false;
|
|
|
boundBatchCode = "";
|
|
|
+ clearSampleBuf();
|
|
|
DcLineBatchContext.setPressProduced(cfg.getPressNo(), 0);
|
|
|
}
|
|
|
|
|
|
+ /** 清空内存采样缓冲 */
|
|
|
+ private void clearSampleBuf() {
|
|
|
+ synchronized (sampleBuf) {
|
|
|
+ sampleBuf.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 有批次时按 sampleMs 采一点进缓冲,满 uploadMax 则上报。
|
|
|
*/
|
|
|
@@ -316,16 +339,63 @@ public class PressPanel extends JPanel {
|
|
|
pt.put("energyKwh", nvl(snap.energyKwh));
|
|
|
pt.put("deviceRunning", snap.deviceRunning ? "1" : "0");
|
|
|
pt.put("deviceStateText", nvl(snap.deviceStateText));
|
|
|
+ boolean needFlush = false;
|
|
|
synchronized (sampleBuf) {
|
|
|
sampleBuf.add(pt);
|
|
|
- if (sampleBuf.size() >= uploadMax) {
|
|
|
- flushSamplesAsync();
|
|
|
+ // 失败回塞过多时丢最旧点,避免内存涨
|
|
|
+ while (sampleBuf.size() > SAMPLE_BUF_CAP) {
|
|
|
+ sampleBuf.remove(0);
|
|
|
}
|
|
|
+ needFlush = sampleBuf.size() >= uploadMax;
|
|
|
+ }
|
|
|
+ if (needFlush) {
|
|
|
+ flushSamplesAsync(batch);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /** 异步批量上报采样;不堵 tick */
|
|
|
- private void flushSamplesAsync() {
|
|
|
+ /**
|
|
|
+ * 结案前:等在途上传结束,再刷光本批缓冲并短等,降低尾点丢失。
|
|
|
+ *
|
|
|
+ * @param batch 本批批次码快照
|
|
|
+ */
|
|
|
+ private void flushSamplesForBatch(String batch) {
|
|
|
+ if (batch == null || batch.isEmpty()) {
|
|
|
+ // 无绑定批次时仍尽量刷残留(用当前线体码兜底)
|
|
|
+ waitSampleUploadIdle(3000L);
|
|
|
+ flushSamplesAsync(DcLineBatchContext.getBatchCode());
|
|
|
+ waitSampleUploadIdle(5000L);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ waitSampleUploadIdle(3000L);
|
|
|
+ flushSamplesAsync(batch);
|
|
|
+ waitSampleUploadIdle(5000L);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 等采样上报线程空闲,最多 maxWaitMs */
|
|
|
+ private void waitSampleUploadIdle(long maxWaitMs) {
|
|
|
+ long deadline = System.currentTimeMillis() + maxWaitMs;
|
|
|
+ while (uploading.get() && System.currentTimeMillis() < deadline) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(50L);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步批量上报采样;不堵 tick。
|
|
|
+ *
|
|
|
+ * @param forceBatch 指定批次码(结案/切批用快照);空则用当前绑定批次
|
|
|
+ */
|
|
|
+ private void flushSamplesAsync(String forceBatch) {
|
|
|
+ final String batch = (forceBatch != null && !forceBatch.isEmpty())
|
|
|
+ ? forceBatch
|
|
|
+ : boundBatchCode;
|
|
|
+ if (batch == null || batch.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
final List<Map<String, String>> chunk;
|
|
|
synchronized (sampleBuf) {
|
|
|
if (sampleBuf.isEmpty()) {
|
|
|
@@ -338,20 +408,14 @@ public class PressPanel extends JPanel {
|
|
|
return;
|
|
|
}
|
|
|
if (!uploading.compareAndSet(false, true)) {
|
|
|
- // 上传进行中:把点塞回缓冲头,下轮再试
|
|
|
- synchronized (sampleBuf) {
|
|
|
- sampleBuf.addAll(0, chunk);
|
|
|
- }
|
|
|
+ // 已有上传在途:仅同批才回塞,下轮再刷
|
|
|
+ requeueSamplesIfSameBatch(batch, chunk);
|
|
|
return;
|
|
|
}
|
|
|
- final String batch = DcLineBatchContext.getBatchCode();
|
|
|
new Thread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
|
- if (batch == null || batch.isEmpty()) {
|
|
|
- return;
|
|
|
- }
|
|
|
JSONObject body = new JSONObject();
|
|
|
body.put("batchCode", batch);
|
|
|
// 五台共用线体工位号;机台区分靠 pressNo
|
|
|
@@ -366,21 +430,19 @@ public class PressPanel extends JPanel {
|
|
|
body.put("samples", arr);
|
|
|
JSONObject ret = MesDcApiClient.uploadPressParam(serverIp, body);
|
|
|
if (!MesDcApiClient.isSuccess(ret)) {
|
|
|
- log.warn("压机[{}] 采样上报失败: {}", Integer.valueOf(cfg.getPressNo()),
|
|
|
- MesDcApiClient.messageOf(ret));
|
|
|
- synchronized (sampleBuf) {
|
|
|
- sampleBuf.addAll(0, chunk);
|
|
|
- }
|
|
|
+ log.warn("压机[{}] 采样上报失败 batch={}: {}",
|
|
|
+ Integer.valueOf(cfg.getPressNo()), batch, MesDcApiClient.messageOf(ret));
|
|
|
+ // 仅本批仍绑定时回塞,避免串到下一批
|
|
|
+ requeueSamplesIfSameBatch(batch, chunk);
|
|
|
} else {
|
|
|
log.info("压机[{}] 采样上报成功 points={} batch={}",
|
|
|
Integer.valueOf(cfg.getPressNo()),
|
|
|
Integer.valueOf(chunk.size()), batch);
|
|
|
}
|
|
|
} catch (Exception ex) {
|
|
|
- log.warn("压机[{}] 采样上报异常: {}", Integer.valueOf(cfg.getPressNo()), ex.getMessage());
|
|
|
- synchronized (sampleBuf) {
|
|
|
- sampleBuf.addAll(0, chunk);
|
|
|
- }
|
|
|
+ log.warn("压机[{}] 采样上报异常 batch={}: {}",
|
|
|
+ Integer.valueOf(cfg.getPressNo()), batch, ex.getMessage());
|
|
|
+ requeueSamplesIfSameBatch(batch, chunk);
|
|
|
} finally {
|
|
|
uploading.set(false);
|
|
|
}
|
|
|
@@ -388,6 +450,29 @@ public class PressPanel extends JPanel {
|
|
|
}, "dc-press-upload-" + cfg.getPressNo()).start();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上报失败/冲突回塞:只有当前仍绑定同一批次才回塞,防串批。
|
|
|
+ *
|
|
|
+ * @param batch 上报用的批次码
|
|
|
+ * @param chunk 本包采样点
|
|
|
+ */
|
|
|
+ private void requeueSamplesIfSameBatch(String batch, List<Map<String, String>> chunk) {
|
|
|
+ if (chunk == null || chunk.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (batch == null || !batch.equals(boundBatchCode)) {
|
|
|
+ log.warn("压机[{}] 采样失败且批次已切换/已空闲,丢弃 points={} oldBatch={}",
|
|
|
+ Integer.valueOf(cfg.getPressNo()), Integer.valueOf(chunk.size()), batch);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ synchronized (sampleBuf) {
|
|
|
+ sampleBuf.addAll(0, chunk);
|
|
|
+ while (sampleBuf.size() > SAMPLE_BUF_CAP) {
|
|
|
+ sampleBuf.remove(sampleBuf.size() - 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/** 按冲次累计本批已产;冲次变小则保留已产并重定起点 */
|
|
|
private void updateProducedAgainstStroke(long strokeCount) {
|
|
|
if (strokeCount < lastStrokeCount) {
|