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++; }