From d0b662c161b6841d2110eed829c2cc44698fcdce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Thu, 20 Feb 2025 16:02:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E6=9A=82=E5=81=9C?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=B7=A5=E8=89=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/qyft/gd/controller/CraftsController.java | 14 +++++ .../com/qyft/gd/service/CraftsStepService.java | 64 +++++++++++++++++----- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/qyft/gd/controller/CraftsController.java b/src/main/java/com/qyft/gd/controller/CraftsController.java index 39a0c33..bc89c96 100644 --- a/src/main/java/com/qyft/gd/controller/CraftsController.java +++ b/src/main/java/com/qyft/gd/controller/CraftsController.java @@ -76,6 +76,20 @@ public class CraftsController { return Result.failed(); } + @Operation(summary = "暂停工艺") + @PostMapping("/pause") + public Result pauseCrafts(@Parameter(description = "加热区id") @RequestParam String heatId) { + craftsStepService.pauseCrafts(heatId); + return Result.success(); + } + + @Operation(summary = "恢复工艺") + @PostMapping("/resume") + public Result resumeCrafts(@Parameter(description = "加热区id") @RequestParam String heatId) { + craftsStepService.resumeCrafts(heatId); + return Result.success(); + } + @Operation(summary = "停止执行工艺") @PostMapping("/stop") public Result stopCrafts(@Parameter(description = "加热区id") @RequestParam String heatId) { diff --git a/src/main/java/com/qyft/gd/service/CraftsStepService.java b/src/main/java/com/qyft/gd/service/CraftsStepService.java index 05a400c..d4553b5 100644 --- a/src/main/java/com/qyft/gd/service/CraftsStepService.java +++ b/src/main/java/com/qyft/gd/service/CraftsStepService.java @@ -34,7 +34,8 @@ public class CraftsStepService { private final ExecutorService executorService = Executors.newCachedThreadPool(); private final ConcurrentHashMap> taskMap = new ConcurrentHashMap<>(); - + private final ConcurrentHashMap pausedMap = new ConcurrentHashMap<>(); + private final ConcurrentHashMap taskLocks = new ConcurrentHashMap<>(); /** * 启动工艺 * @@ -49,6 +50,7 @@ public class CraftsStepService { CraftsTask task = new CraftsTask(craftId, heatId); taskFuture = executorService.submit(task); taskMap.put(heatId, taskFuture); + taskLocks.put(heatId, new Object()); return true; } @@ -63,19 +65,40 @@ public class CraftsStepService { boolean cancelled = taskFuture.cancel(true); if (cancelled) { taskMap.remove(heatId); + pausedMap.remove(heatId); + taskLocks.remove(heatId); } - return cancelled; } return false; } + /** + * 暂停工艺 + * + * @param heatId 加热区id + */ + public synchronized void pauseCrafts(String heatId) { + pausedMap.put(heatId, true); + } + + /** + * 恢复工艺继续执行 + * + * @param heatId 加热区id + */ + public synchronized void resumeCrafts(String heatId) { + pausedMap.put(heatId, false); + synchronized (taskLocks.get(heatId)) { + taskLocks.get(heatId).notify(); + } + } + class CraftsTask implements Runnable { private final String heatId; private final Long craftId; private static String currentMethod = null; - private static int currentStatus = CraftsStepStatus.NOT_EXECUTED; public CraftsTask(Long craftId, String heatId) { this.heatId = heatId; @@ -84,7 +107,6 @@ public class CraftsStepService { @Override public void run() { - currentStatus = CraftsStepStatus.IN_PROGRESS; Crafts crafts = craftsService.findCraftsById(craftId); log.info("开始执行工艺,加热区: {}, 工艺: {}", heatId, crafts.getName()); String steps = crafts.getSteps(); @@ -114,6 +136,20 @@ public class CraftsStepService { pushMsg(heatId, CraftsStepStatus.ERROR); 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); + } + } } else { taskMap.remove(heatId); log.info("停止执行工艺,加热区: {}", heatId); @@ -132,7 +168,7 @@ public class CraftsStepService { */ private boolean upTray() { currentMethod = CraftsStepMethod.UP_TRAY; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); try { Thread.sleep(2000); } catch (InterruptedException e) { @@ -147,7 +183,7 @@ public class CraftsStepService { */ private boolean downTray() { currentMethod = CraftsStepMethod.DOWN_TRAY; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); try { Thread.sleep(2000); } catch (InterruptedException e) { @@ -162,7 +198,7 @@ public class CraftsStepService { */ private boolean addLiquid(JSONObject params) { currentMethod = CraftsStepMethod.ADD_LIQUID; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); JSONArray tubeSolJSONArray = params.getJSONArray("tubeSolList"); List tubeSolList = JSONUtil.toList(tubeSolJSONArray.toString(), TubeSol.class); @@ -180,7 +216,7 @@ public class CraftsStepService { */ private boolean moveToSol() { currentMethod = CraftsStepMethod.MOVE_TO_SOL; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); try { Thread.sleep(5000); @@ -196,7 +232,7 @@ public class CraftsStepService { */ private boolean moveToHeater() { currentMethod = CraftsStepMethod.MOVE_TO_HEATER; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); try { Thread.sleep(5000); @@ -212,7 +248,7 @@ public class CraftsStepService { */ private boolean shaking(JSONObject params) { currentMethod = CraftsStepMethod.SHAKING; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); Integer second = params.getInt("second"); try { @@ -229,7 +265,7 @@ public class CraftsStepService { */ private boolean startHeating(JSONObject params) { currentMethod = CraftsStepMethod.START_HEATING; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); Double temperature = params.getDouble("temperature"); try { @@ -246,7 +282,7 @@ public class CraftsStepService { */ private boolean stopHeating() { currentMethod = CraftsStepMethod.STOP_HEATING; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); try { Thread.sleep(1000); @@ -262,7 +298,7 @@ public class CraftsStepService { */ private boolean takePhoto() { currentMethod = CraftsStepMethod.TAKE_PHOTO; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); try { Thread.sleep(1000); @@ -292,7 +328,7 @@ public class CraftsStepService { */ private boolean delay(JSONObject params) { currentMethod = CraftsStepMethod.DELAY; - pushMsg(heatId, currentStatus, currentMethod); + pushMsg(heatId, CraftsStepStatus.IN_PROGRESS, currentMethod); Integer second = params.getInt("second"); return deviceStepService.delay(second);