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 index f3f876d..724cf7c 100644 --- a/src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java +++ b/src/main/java/com/qyft/ms/app/device/spray/SprayTaskPointCollector.java @@ -2,15 +2,19 @@ package com.qyft.ms.app.device.spray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.qyft.ms.app.common.constant.WebSocketMessageType; import com.qyft.ms.app.device.status.SprayTask; import com.qyft.ms.app.model.bo.Point2D; import com.qyft.ms.app.model.bo.SprayTaskPointCollectorPushBO; +import com.qyft.ms.app.model.entity.Position; +import com.qyft.ms.app.service.PositionService; 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 jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.TaskScheduler; @@ -26,6 +30,7 @@ import java.util.concurrent.ScheduledFuture; public class SprayTaskPointCollector { private final WebSocketService webSocketService; private final DeviceCommandService deviceCommandService; + private final PositionService positionService; private final TaskScheduler taskScheduler; @@ -33,6 +38,21 @@ public class SprayTaskPointCollector { private int currentSprayIndex; + private Double[][] slideArr; + + @PostConstruct + private void init(){ + Position slidePosition1 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position1")); + Position slidePosition2 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position2")); + Position slidePosition3 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position3")); + Position slidePosition4 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position4")); + slideArr = new Double[][] { + {slidePosition1.getX(), slidePosition1.getY()}, + {slidePosition2.getX(), slidePosition2.getY()}, + {slidePosition3.getX(), slidePosition3.getY()}, + {slidePosition4.getX(), slidePosition4.getY()} + }; + } public void startCollecting(int currentSprayIndex) { if (scheduledFuture == null || scheduledFuture.isCancelled()) { @@ -60,10 +80,12 @@ public class SprayTaskPointCollector { stopCollecting(); } DeviceCommand motorXyzPositionGetCommand = DeviceCommandGenerator.motorXyzPositionGet(); //生成电机XYZ相对原点坐标指令 - CommandFuture motorXyzPositionGetCommandFuture = deviceCommandService.sendCommand(motorXyzPositionGetCommand); + CommandFuture motorXyzPositionGetCommandFuture = deviceCommandService.sendCommandNoFront(motorXyzPositionGetCommand); JSONObject motorXyzPositionGetCommandResult = motorXyzPositionGetCommandFuture.getResponseResult(); Double xAxisPosition = motorXyzPositionGetCommandResult.getDouble("xAxisPosition"); Double yAxisPosition = motorXyzPositionGetCommandResult.getDouble("yAxisPosition"); + xAxisPosition = xAxisPosition - slideArr[currentSprayIndex][0]; + yAxisPosition = 75.5 - yAxisPosition; Point2D point2D = new Point2D(xAxisPosition, yAxisPosition); SprayTaskPointCollectorPushBO sprayTaskPointCollectorPushBO = new SprayTaskPointCollectorPushBO(currentSprayIndex, point2D); log.info("喷涂点位采集,采集到点位:{}", JSONUtil.toJsonStr(sprayTaskPointCollectorPushBO)); diff --git a/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java b/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java index c0a0771..3c90c11 100644 --- a/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java +++ b/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java @@ -41,6 +41,30 @@ public class DeviceCommandService { return cmdFuture; } + + public CommandFuture sendCommandNoFront(DeviceCommand deviceCommand) { + CommandFuture commandFuture = executeCommand(deviceCommand); + commandFuture.getAckFuture().thenApply(result -> { + Boolean status = result.getBool("status"); + if (!status) { //ack失败 + String message = deviceCommand.getCommandName() + "指令,设备ack错误"; + throw new RuntimeException(message); + } + return result; + }); + + commandFuture.getResponseFuture().thenApply(result -> { + Boolean status = result.getBool("status"); + if (!status) { //response失败 + String message = deviceCommand.getCommandName() + "指令,设备response错误"; + throw new RuntimeException(message); + } + return result.get("data"); + }); + + return commandFuture; + } + public CommandFuture sendCommand(DeviceCommand deviceCommand) throws IOException { return sendCommand(deviceCommand, null, null); }