4 changed files with 108 additions and 4 deletions
-
19src/main/java/com/qyft/ms/app/config/SchedulerConfig.java
-
10src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor.java
-
69src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java
-
14src/main/java/com/qyft/ms/app/model/bo/SprayTaskPointCollectorPushBO.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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue