From 3f27d40401fd399228fd0119c41963d243a24557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Wed, 19 Feb 2025 18:53:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=89=A7=E8=A1=8C=E5=B7=A5=E8=89=BA?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qyft/gd/device/service/DeviceStepService.java | 14 +-- .../java/com/qyft/gd/service/CraftsService.java | 2 + .../com/qyft/gd/service/CraftsStepService.java | 133 +++++++++++++++++++-- .../qyft/gd/service/impl/CraftsServiceImpl.java | 6 +- 4 files changed, 138 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/qyft/gd/device/service/DeviceStepService.java b/src/main/java/com/qyft/gd/device/service/DeviceStepService.java index 6cd83de..4eb7a83 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceStepService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceStepService.java @@ -23,8 +23,8 @@ public class DeviceStepService { * * @param heaterId 加热区id */ - public void upTray(String heaterId) { - deviceService.raiseTray(heaterId); + public boolean upTray(String heaterId) { + return deviceService.raiseTray(heaterId); } /** @@ -32,8 +32,8 @@ public class DeviceStepService { * * @param heaterId 加热区id */ - public void downTray(String heaterId) { - deviceService.lowerTray(heaterId); + public boolean downTray(String heaterId) { + return deviceService.lowerTray(heaterId); } /** @@ -126,11 +126,11 @@ public class DeviceStepService { /** * 等待 * - * @param millis 毫秒 + * @param second 秒 */ - private boolean delay(int millis) { + public boolean delay(int second) { try { - Thread.sleep(millis); + Thread.sleep(second * 1000L); return true; } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/src/main/java/com/qyft/gd/service/CraftsService.java b/src/main/java/com/qyft/gd/service/CraftsService.java index 3c5486a..f30ae48 100644 --- a/src/main/java/com/qyft/gd/service/CraftsService.java +++ b/src/main/java/com/qyft/gd/service/CraftsService.java @@ -20,4 +20,6 @@ public interface CraftsService extends IService { boolean deleteCrafts(String idsStr); + Crafts findCraftsById(Long oresId); + } diff --git a/src/main/java/com/qyft/gd/service/CraftsStepService.java b/src/main/java/com/qyft/gd/service/CraftsStepService.java index b56f2ce..0ae58b3 100644 --- a/src/main/java/com/qyft/gd/service/CraftsStepService.java +++ b/src/main/java/com/qyft/gd/service/CraftsStepService.java @@ -1,8 +1,16 @@ package com.qyft.gd.service; +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.qyft.gd.device.service.DeviceStepService; +import com.qyft.gd.model.bo.TubeSol; +import com.qyft.gd.model.entity.Crafts; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -11,14 +19,17 @@ import java.util.concurrent.Future; /** * 工艺操作 */ +@Slf4j @Service @RequiredArgsConstructor public class CraftsStepService { + private final CraftsService craftsService; + private final DeviceStepService deviceStepService; + private final ExecutorService executorService = Executors.newCachedThreadPool(); private final ConcurrentHashMap> taskMap = new ConcurrentHashMap<>(); - // 启动任务的方法 public synchronized boolean startCrafts(Long craftId, Long heatId) { Future taskFuture = taskMap.get(heatId); @@ -45,7 +56,7 @@ public class CraftsStepService { } - static class CraftsTask implements Runnable { + class CraftsTask implements Runnable { private final Long heatId; private final Long craftId; @@ -56,16 +67,120 @@ public class CraftsStepService { @Override public void run() { - try { - while (!Thread.currentThread().isInterrupted()) { - System.out.println("加热区 " + heatId + " 工艺运行中..."); - Thread.sleep(1000); + while (!Thread.currentThread().isInterrupted()) { + Crafts crafts = craftsService.findCraftsById(craftId); + log.info("开始执行工艺,加热区 {},工艺 {}", heatId, crafts.getName()); + String steps = crafts.getSteps(); + JSONArray stepsJsonArray = JSONUtil.parseArray(steps); + for (Object stepsJsonObject : stepsJsonArray) { + JSONObject stepsJson = (JSONObject) stepsJsonObject; + String method = stepsJson.get("method").toString(); + JSONObject params = (JSONObject) stepsJson.get("params"); + boolean result = switch (method) { + case "upTray" -> upTray(params); + case "downTray" -> downTray(params); + case "addLiquid" -> addLiquid(params); + case "moveToSol" -> moveToSol(params); + case "moveToHeater" -> moveToHeater(params); + case "shaking" -> shaking(params); + case "startHeating" -> startHeating(params); + case "stopHeating" -> stopHeating(params); + case "takePhoto" -> takePhoto(); + case "moveToExc" -> delay(params); + default -> false; + }; + if (!result) { + //TODO 向前端反馈执行失败 + return; + } } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - System.out.println("加热区 " + heatId + " 工艺被中断"); } + } } + + /** + * 抬起托盘 + */ + private boolean upTray(JSONObject params) { + String heaterId = params.get("heaterId").toString(); + return deviceStepService.upTray(heaterId); + } + + /** + * 降下托盘 + */ + private boolean downTray(JSONObject params) { + String heaterId = params.get("heaterId").toString(); + return deviceStepService.downTray(heaterId); + } + + /** + * 添加溶液 + */ + private boolean addLiquid(JSONObject params) { + JSONArray tubeSolJSONArray = params.getJSONArray("tubeSolList"); + List tubeSolList = JSONUtil.toList(tubeSolJSONArray.toString(), TubeSol.class); + return deviceStepService.addLiquid(tubeSolList); + } + + /** + * 将指定加热区的托盘移至加液区 + */ + private boolean moveToSol(JSONObject params) { + String heaterId = params.get("heaterId").toString(); + return deviceStepService.moveToSol(heaterId); + } + + /** + * 移至加热 + */ + private boolean moveToHeater(JSONObject params) { + String heaterId = params.get("heaterId").toString(); + return deviceStepService.moveToHeater(heaterId); + } + + //摇匀 + private boolean shaking(JSONObject params) { + Integer second = params.getInt("second"); + return deviceStepService.shaking(second); + } + + //开始加热 + private boolean startHeating(JSONObject params) { + String heaterId = params.get("heaterId").toString(); + Double temperature = params.getDouble("temperature"); + return deviceStepService.startHeating(heaterId, temperature); + } + + //停止加热 + private boolean stopHeating(JSONObject params) { + String heaterId = params.get("heaterId").toString(); + return deviceStepService.stopHeating(heaterId); + } + + //拍照 + private boolean takePhoto() { + return deviceStepService.takePhoto(); + } + + //移至异常 + private boolean moveToExc() { + return true; + } + + //移除异常 + private boolean moveOutToExc() { + return true; + } + + /** + * 等待 + */ + private boolean delay(JSONObject params) { + Integer second = params.getInt("second"); + return deviceStepService.delay(second); + } + } diff --git a/src/main/java/com/qyft/gd/service/impl/CraftsServiceImpl.java b/src/main/java/com/qyft/gd/service/impl/CraftsServiceImpl.java index 8be1450..2cd477d 100644 --- a/src/main/java/com/qyft/gd/service/impl/CraftsServiceImpl.java +++ b/src/main/java/com/qyft/gd/service/impl/CraftsServiceImpl.java @@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.qyft.gd.mapper.CraftsMapper; import com.qyft.gd.model.entity.Crafts; import com.qyft.gd.service.CraftsService; -import com.qyft.gd.service.CraftsStepService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -47,4 +46,9 @@ public class CraftsServiceImpl extends ServiceImpl impleme return this.removeByIds(ids); } + @Override + public Crafts findCraftsById(Long id) { + return this.baseMapper.selectById(id); + } + }