Browse Source

feat:新增喷涂点位推送

tags/1.0
白凤吉 5 months ago
parent
commit
10c52e6b78
  1. 19
      src/main/java/com/qyft/ms/app/config/SchedulerConfig.java
  2. 10
      src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor.java
  3. 69
      src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java
  4. 14
      src/main/java/com/qyft/ms/app/model/bo/SprayTaskPointCollectorPushBO.java

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

10
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<List<DeviceCommand>> 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);

69
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);
}
}
}

14
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;
}
}
Loading…
Cancel
Save