From a3a31b07fc2ff7d9e25e0d961a3bd4f27c2eaace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Tue, 25 Feb 2025 20:29:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=81=9C=E6=AD=A2=E6=89=A7=E8=A1=8C=E5=B7=A5?= =?UTF-8?q?=E8=89=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/qyft/gd/service/CraftsStepService.java | 89 +++++++++++----------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/qyft/gd/service/CraftsStepService.java b/src/main/java/com/qyft/gd/service/CraftsStepService.java index 1810fb9..905a972 100644 --- a/src/main/java/com/qyft/gd/service/CraftsStepService.java +++ b/src/main/java/com/qyft/gd/service/CraftsStepService.java @@ -69,6 +69,8 @@ public class CraftsStepService { pausedMap.remove(heatId); taskLocks.remove(heatId); } + log.info("停止执行工艺,加热区: {}", heatId); + pushMsg(heatId, CraftsStepStatus.STOPPED); return cancelled; } return false; @@ -80,7 +82,10 @@ public class CraftsStepService { * @param heatId 加热区id */ public synchronized void pauseCrafts(String heatId) { - pausedMap.put(heatId, true); + Future taskFuture = taskMap.get(heatId); + if (taskFuture != null) { + pausedMap.put(heatId, true); + } } /** @@ -89,9 +94,12 @@ public class CraftsStepService { * @param heatId 加热区id */ public synchronized void resumeCrafts(String heatId) { - pausedMap.put(heatId, false); - synchronized (taskLocks.get(heatId)) { - taskLocks.get(heatId).notify(); + Future taskFuture = taskMap.get(heatId); + if (taskFuture != null) { + synchronized (taskLocks.get(heatId)) { + pausedMap.remove(heatId); + taskLocks.get(heatId).notify(); + } } } @@ -125,49 +133,42 @@ public class CraftsStepService { String steps = crafts.getSteps(); JSONArray stepsJsonArray = JSONUtil.parseArray(steps); for (Object stepsJsonObject : stepsJsonArray) { - if (!Thread.currentThread().isInterrupted()) { - JSONObject stepsJson = (JSONObject) stepsJsonObject; - log.info("执行工艺步骤,加热区: {}, 执行步骤: {}", heatId, stepsJson.toString()); - String method = stepsJson.get("method").toString(); - JSONObject params = (JSONObject) stepsJson.get("params"); - boolean result = switch (method) { - case CraftsStepMethod.UP_TRAY -> this.upTray(); - case CraftsStepMethod.DOWN_TRAY -> this.downTray(); - case CraftsStepMethod.ADD_LIQUID -> this.addLiquid(params); - case CraftsStepMethod.MOVE_TO_SOL -> this.moveToSol(); - case CraftsStepMethod.MOVE_TO_HEAT -> this.moveToHeat(); - case CraftsStepMethod.SHAKING -> this.shaking(params); - case CraftsStepMethod.START_HEATING -> this.startHeating(params); - case CraftsStepMethod.STOP_HEATING -> this.stopHeating(); - case CraftsStepMethod.TAKE_PHOTO -> this.takePhoto(); - case CraftsStepMethod.DELAY -> this.delay(params); - default -> false; - }; - if (!result) { - taskMap.remove(heatId); - log.info("执行工艺错误,加热区: {}, 当前执行步骤: {}", heatId, stepsJsonArray.get(currentMethodNum).toString()); - pushMsg(heatId, CraftsStepStatus.ERROR, currentMethodNum); - return; - } + JSONObject stepsJson = (JSONObject) stepsJsonObject; + log.info("执行工艺步骤,加热区: {}, 执行步骤: {}", heatId, stepsJson.toString()); + String method = stepsJson.get("method").toString(); + JSONObject params = (JSONObject) stepsJson.get("params"); + boolean result = switch (method) { + case CraftsStepMethod.UP_TRAY -> this.upTray(); + case CraftsStepMethod.DOWN_TRAY -> this.downTray(); + case CraftsStepMethod.ADD_LIQUID -> this.addLiquid(params); + case CraftsStepMethod.MOVE_TO_SOL -> this.moveToSol(); + case CraftsStepMethod.MOVE_TO_HEAT -> this.moveToHeat(); + case CraftsStepMethod.SHAKING -> this.shaking(params); + case CraftsStepMethod.START_HEATING -> this.startHeating(params); + case CraftsStepMethod.STOP_HEATING -> this.stopHeating(); + case CraftsStepMethod.TAKE_PHOTO -> this.takePhoto(); + case CraftsStepMethod.DELAY -> this.delay(params); + default -> false; + }; + if (!result) { + taskMap.remove(heatId); + log.info("执行工艺错误,加热区: {}, 当前执行步骤: {}", heatId, stepsJsonArray.get(currentMethodNum).toString()); + pushMsg(heatId, CraftsStepStatus.ERROR, currentMethodNum); + return; + } - synchronized (taskLocks.get(heatId)) { - while (pausedMap.getOrDefault(heatId, false)) { - try { - log.info("暂停执行工艺,加热区: {}", heatId); - pushMsg(heatId, CraftsStepStatus.PAUSED); - taskLocks.get(heatId).wait(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return; - } - log.info("恢复执行工艺,加热区: {}", heatId); + synchronized (taskLocks.get(heatId)) { + while (pausedMap.getOrDefault(heatId, false)) { + try { + log.info("暂停执行工艺,加热区: {}", heatId); + pushMsg(heatId, CraftsStepStatus.PAUSED); + taskLocks.get(heatId).wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; } + log.info("恢复执行工艺,加热区: {}", heatId); } - } else { - taskMap.remove(heatId); - log.info("停止执行工艺,加热区: {}", heatId); - pushMsg(heatId, CraftsStepStatus.STOPPED); - return; } currentMethodNum++; }