diff --git a/src/main/java/com/qyft/gd/controller/CraftsController.java b/src/main/java/com/qyft/gd/controller/CraftsController.java index 7c4c1e8..6457019 100644 --- a/src/main/java/com/qyft/gd/controller/CraftsController.java +++ b/src/main/java/com/qyft/gd/controller/CraftsController.java @@ -2,6 +2,7 @@ package com.qyft.gd.controller; import com.qyft.gd.model.entity.Crafts; import com.qyft.gd.service.CraftsService; +import com.qyft.gd.service.CraftsStepService; import com.qyft.gd.system.common.result.Result; import com.qyft.gd.system.common.result.ResultCode; import io.swagger.v3.oas.annotations.Operation; @@ -20,6 +21,7 @@ import java.util.List; @Slf4j public class CraftsController { private final CraftsService craftsService; + private final CraftsStepService craftsStepService; @Operation(summary = "根据矿石id获取工艺列表") @GetMapping("/list/{oresId}") @@ -72,12 +74,20 @@ public class CraftsController { @Operation(summary = "开始执行工艺") @PostMapping("/start") public Result startCrafts(@Parameter(description = "工艺id") @RequestParam Long craftId, @Parameter(description = "加热区id") @RequestParam Long heatId) { - return Result.success(); + boolean isSuccess = craftsStepService.startCrafts(craftId, heatId); + if (isSuccess) { + return Result.success(); + } + return Result.failed(); } @Operation(summary = "停止执行工艺") @PostMapping("/stop") public Result stopCrafts(@Parameter(description = "加热区id") @RequestParam Long heatId) { - return Result.success(); + boolean isSuccess = craftsStepService.stopCrafts(heatId); + if (isSuccess) { + return Result.success(); + } + return Result.failed(); } } diff --git a/src/main/java/com/qyft/gd/device/service/DeviceOperationService.java b/src/main/java/com/qyft/gd/device/service/DeviceOperationService.java deleted file mode 100644 index 828fba3..0000000 --- a/src/main/java/com/qyft/gd/device/service/DeviceOperationService.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.qyft.gd.device.service; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -/** - * 设备步骤操作 - */ -@Service -@RequiredArgsConstructor -public class DeviceOperationService { - - private final DeviceService deviceService; - - //抬起托盘 - public void upTray(int heaterId){ - deviceService.raiseTray("11"); - } - //降下托盘 - public void downTray(int heaterId){ - deviceService.lowerTray("11"); - } - //添加溶液(先固定16个试管全进行加液) - public void addLiquid(){ - - } - //移至加液 - public void moveToSol(){ - - } - //移至加热 - public void moveToHeater(int heaterId){ - - } - //摇匀 - public void shaking(){ - - } - //开始加热 - public void startHeating(int heaterId){ - - } - //停止加热 - public void stopHeating(int heaterId){ - - } - //拍照 - public void takePhoto(){ - - } - //移至异常 - public void moveToExc(){ - - } - //移除异常 - public void moveOutToExc(){ - - } - /** - * 等待 - * @param millis 毫秒 - */ - private boolean delay(int millis) { - try { - Thread.sleep(millis); - return true; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - return false; - } -} diff --git a/src/main/java/com/qyft/gd/service/CraftsService.java b/src/main/java/com/qyft/gd/service/CraftsService.java index d39144d..3c5486a 100644 --- a/src/main/java/com/qyft/gd/service/CraftsService.java +++ b/src/main/java/com/qyft/gd/service/CraftsService.java @@ -19,4 +19,5 @@ public interface CraftsService extends IService { boolean updateCrafts(Crafts crafts); boolean deleteCrafts(String idsStr); + } diff --git a/src/main/java/com/qyft/gd/service/CraftsStepService.java b/src/main/java/com/qyft/gd/service/CraftsStepService.java new file mode 100644 index 0000000..2b5b2f8 --- /dev/null +++ b/src/main/java/com/qyft/gd/service/CraftsStepService.java @@ -0,0 +1,144 @@ +package com.qyft.gd.service; + +import com.qyft.gd.device.service.DeviceService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * 设备步骤操作 + */ +@Service +@RequiredArgsConstructor +public class CraftsStepService { + + private final DeviceService deviceService; + + 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); + if (taskFuture != null) { + return false; + } + CraftsTask task = new CraftsTask(craftId, heatId); + taskFuture = executorService.submit(task); + taskMap.put(heatId, taskFuture); + return true; + } + + // 停止任务的方法,通过ID来指定 + public synchronized boolean stopCrafts(Long heatId) { + Future taskFuture = taskMap.get(heatId); + if (taskFuture != null) { + boolean cancelled = taskFuture.cancel(true); + if (cancelled) { + taskMap.remove(heatId); + } + return cancelled; + } + return false; + } + + + static class CraftsTask implements Runnable { + private final Long heatId; + private final Long craftId; + + public CraftsTask(Long craftId, Long heatId) { + this.heatId = heatId; + this.craftId = craftId; + } + + @Override + public void run() { + try { + while (!Thread.currentThread().isInterrupted()) { + System.out.println("加热区 " + heatId + " 工艺运行中..."); + Thread.sleep(1000); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("加热区 " + heatId + " 工艺被中断"); + } + } + } + + + //抬起托盘 + public void upTray(int heaterId) { + deviceService.raiseTray("11"); + } + + //降下托盘 + public void downTray(int heaterId) { + deviceService.lowerTray("11"); + } + + //添加溶液(先固定16个试管全进行加液) + public void addLiquid() { + + } + + //移至加液 + public void moveToSol() { + + } + + //移至加热 + public void moveToHeater(int heaterId) { + + } + + //摇匀 + public void shaking() { + + } + + //开始加热 + public void startHeating(int heaterId) { + + } + + //停止加热 + public void stopHeating(int heaterId) { + + } + + //拍照 + public void takePhoto() { + + } + + //移至异常 + public void moveToExc() { + + } + + //移除异常 + public void moveOutToExc() { + + } + + /** + * 等待 + * + * @param millis 毫秒 + */ + private boolean delay(int millis) { + try { + Thread.sleep(millis); + return true; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + return false; + } +} 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 b6e97f7..8be1450 100644 --- a/src/main/java/com/qyft/gd/service/impl/CraftsServiceImpl.java +++ b/src/main/java/com/qyft/gd/service/impl/CraftsServiceImpl.java @@ -4,6 +4,7 @@ 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; @@ -45,4 +46,5 @@ public class CraftsServiceImpl extends ServiceImpl impleme .collect(Collectors.toList()); return this.removeByIds(ids); } + }