From 10c52e6b787f1c18e52716162bc93cc82c82b440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Wed, 19 Mar 2025 11:17:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E5=96=B7=E6=B6=82?= =?UTF-8?q?=E7=82=B9=E4=BD=8D=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/qyft/ms/app/config/SchedulerConfig.java | 19 ++++++ .../ms/app/device/spray/SprayTaskExecutor.java | 10 ++-- .../app/device/spray/SprayTaskPointCollector.java | 69 ++++++++++++++++++++++ .../model/bo/SprayTaskPointCollectorPushBO.java | 14 +++++ 4 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/qyft/ms/app/config/SchedulerConfig.java create mode 100644 src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java create mode 100644 src/main/java/com/qyft/ms/app/model/bo/SprayTaskPointCollectorPushBO.java diff --git a/src/main/java/com/qyft/ms/app/config/SchedulerConfig.java b/src/main/java/com/qyft/ms/app/config/SchedulerConfig.java new file mode 100644 index 0000000..3ddd441 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/config/SchedulerConfig.java @@ -0,0 +1,19 @@ +package com.qyft.ms.app.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; + +@Configuration +public class SchedulerConfig { + + @Bean + public TaskScheduler sprayPointCollectorScheduler() { + ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); + scheduler.setPoolSize(1); + scheduler.setThreadNamePrefix("spray-point-collector-"); + return scheduler; + } + +} diff --git a/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor.java b/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor.java index 6617114..a81a8e9 100644 --- a/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor.java +++ b/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor.java @@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit; public class SprayTaskExecutor { private final WebSocketService webSocketService; private final DeviceCommandService deviceCommandService; + private final SprayTaskPointCollector sprayTaskPointCollector; private Thread taskThread; @@ -67,8 +68,8 @@ public class SprayTaskExecutor { } int reCurrentStep = sprayTask.getCurrentStep(); - int currentStep = 0; - + int currentStep = 0; //当前喷涂步骤 + int currentSprayIndex = 0; //喷涂计数 for (List> deviceCommandList : sprayTask.getSpraySteps()) {//循环进行多次喷涂 DeviceCommand nozzleValveOpenCommand = DeviceCommandGenerator.nozzleValveOpen();//开启喷嘴阀 @@ -89,9 +90,9 @@ public class SprayTaskExecutor { commandFutureList.add(commandFuture); } CommandFuture[] commandFutureArray = commandFutureList.toArray(new CommandFuture[0]); - //TODO开启点位推送 + sprayTaskPointCollector.startCollecting(currentSprayIndex); //开启点位推送 commandWait(commandFutureArray); - //TODO关闭点位推送 + sprayTaskPointCollector.stopCollecting();//关闭点位推送 currentStep++; sprayTask.setCurrentStep(currentStep);//将当前的喷涂进度缓存 //将当前点位缓存 @@ -103,6 +104,7 @@ public class SprayTaskExecutor { Double yAxisPosition = motorXyzPositionGetCommandResult.getDouble("yAxisPosition"); sprayStepList.add(new Point2D(xAxisPosition, yAxisPosition)); } + currentSprayIndex++; sprayTask.setSuspendable(false);//不可暂停 sprayTask.getSprayedPoints().add(sprayStepList); diff --git a/src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java b/src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java new file mode 100644 index 0000000..1af08e8 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java @@ -0,0 +1,69 @@ +package com.qyft.ms.app.device.spray; + +import cn.hutool.json.JSONObject; +import com.qyft.ms.app.common.constant.WebSocketMessageType; +import com.qyft.ms.app.model.bo.Point2D; +import com.qyft.ms.app.model.bo.SprayTaskPointCollectorPushBO; +import com.qyft.ms.system.common.device.command.CommandFuture; +import com.qyft.ms.system.common.device.command.DeviceCommandGenerator; +import com.qyft.ms.system.model.bo.DeviceCommand; +import com.qyft.ms.system.service.WebSocketService; +import com.qyft.ms.system.service.device.DeviceCommandService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.stereotype.Component; + +import java.time.Duration; +import java.time.Instant; +import java.util.concurrent.ScheduledFuture; + +@Slf4j +@Component +@RequiredArgsConstructor +public class SprayTaskPointCollector { + private final WebSocketService webSocketService; + private final DeviceCommandService deviceCommandService; + + private final TaskScheduler taskScheduler; + + private ScheduledFuture scheduledFuture; + + private int currentSprayIndex; + + + public void startCollecting(int currentSprayIndex) { + if (scheduledFuture == null || scheduledFuture.isCancelled()) { + this.currentSprayIndex = currentSprayIndex; + // 每隔 100 毫秒执行一次采集任务 + scheduledFuture = taskScheduler.scheduleAtFixedRate( + this::collectPoints, + Instant.now(), + Duration.ofMillis(100) + ); + log.info("喷涂点位采集任务已启动"); + } + } + + public void stopCollecting() { + if (scheduledFuture != null) { + scheduledFuture.cancel(false); + log.info("喷涂点位采集任务已停止"); + } + } + + private void collectPoints() { + try { + DeviceCommand motorXyzPositionGetCommand = DeviceCommandGenerator.motorXyzPositionGet(); //生成电机XYZ相对原点坐标指令 + CommandFuture motorXyzPositionGetCommandFuture = deviceCommandService.sendCommand(motorXyzPositionGetCommand); + JSONObject motorXyzPositionGetCommandResult = motorXyzPositionGetCommandFuture.getResponseResult(); + Double xAxisPosition = motorXyzPositionGetCommandResult.getDouble("xAxisPosition"); + Double yAxisPosition = motorXyzPositionGetCommandResult.getDouble("yAxisPosition"); + Point2D point2D = new Point2D(xAxisPosition, yAxisPosition); + SprayTaskPointCollectorPushBO sprayTaskPointCollectorPushBO = new SprayTaskPointCollectorPushBO(currentSprayIndex, point2D); + webSocketService.pushMsg(WebSocketMessageType.SPRAY_POINT, sprayTaskPointCollectorPushBO); + } catch (Exception e) { + log.error("喷涂任务点位采集错误", e); + } + } +} diff --git a/src/main/java/com/qyft/ms/app/model/bo/SprayTaskPointCollectorPushBO.java b/src/main/java/com/qyft/ms/app/model/bo/SprayTaskPointCollectorPushBO.java new file mode 100644 index 0000000..ac49973 --- /dev/null +++ b/src/main/java/com/qyft/ms/app/model/bo/SprayTaskPointCollectorPushBO.java @@ -0,0 +1,14 @@ +package com.qyft.ms.app.model.bo; + +import lombok.Data; + +@Data +public class SprayTaskPointCollectorPushBO { + Integer index; + Point2D point; + + public SprayTaskPointCollectorPushBO(Integer index,Point2D point) { + this.index = index; + this.point = point; + } +}