Browse Source

fix:开机获取急停状态

tags/1.0
白凤吉 4 months ago
parent
commit
3fb4c444b9
  1. 17
      src/main/java/com/qyft/ms/app/controller/SelfTestController.java
  2. 42
      src/main/java/com/qyft/ms/app/core/listener/DeviceInitializerListener.java
  3. 14
      src/main/java/com/qyft/ms/system/controller/FrontCmdController.java
  4. 4
      src/main/java/com/qyft/ms/system/core/listener/DeviceTcpMessageEventListener.java

17
src/main/java/com/qyft/ms/app/controller/SelfTestController.java

@ -10,11 +10,10 @@ import com.qyft.ms.system.model.bo.DeviceCommand;
import com.qyft.ms.system.service.device.DeviceCommandService; import com.qyft.ms.system.service.device.DeviceCommandService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -31,7 +30,7 @@ public class SelfTestController {
private final DeviceStatus deviceStatus; private final DeviceStatus deviceStatus;
@Operation(summary = "获取自检状态") @Operation(summary = "获取自检状态")
@GetMapping("/")
@GetMapping("/status")
public Result<SelfTestVO> getSelfTestStatus() throws ExecutionException, InterruptedException, TimeoutException { public Result<SelfTestVO> getSelfTestStatus() throws ExecutionException, InterruptedException, TimeoutException {
DeviceCommand overallDeviceStatusGetCommand = DeviceCommandGenerator.overallDeviceStatusGet(); DeviceCommand overallDeviceStatusGetCommand = DeviceCommandGenerator.overallDeviceStatusGet();
CommandFuture overallDeviceStatusGetCommandFuture = deviceCommandService.sendCommandNoFront(overallDeviceStatusGetCommand); CommandFuture overallDeviceStatusGetCommandFuture = deviceCommandService.sendCommandNoFront(overallDeviceStatusGetCommand);
@ -39,15 +38,17 @@ public class SelfTestController {
boolean xAxisAtOrigin = overallDeviceStatusGetCommandFuture.getResponseResult().getJSONObject("data").getBool("xAxisAtOrigin"); boolean xAxisAtOrigin = overallDeviceStatusGetCommandFuture.getResponseResult().getJSONObject("data").getBool("xAxisAtOrigin");
boolean yAxisAtOrigin = overallDeviceStatusGetCommandFuture.getResponseResult().getJSONObject("data").getBool("yAxisAtOrigin"); boolean yAxisAtOrigin = overallDeviceStatusGetCommandFuture.getResponseResult().getJSONObject("data").getBool("yAxisAtOrigin");
boolean zAxisAtOrigin = overallDeviceStatusGetCommandFuture.getResponseResult().getJSONObject("data").getBool("zAxisAtOrigin"); boolean zAxisAtOrigin = overallDeviceStatusGetCommandFuture.getResponseResult().getJSONObject("data").getBool("zAxisAtOrigin");
selfTestService.getSelfTestStatus().setXAxisAtOrigin(xAxisAtOrigin); selfTestService.getSelfTestStatus().setXAxisAtOrigin(xAxisAtOrigin);
selfTestService.getSelfTestStatus().setYAxisAtOrigin(yAxisAtOrigin); selfTestService.getSelfTestStatus().setYAxisAtOrigin(yAxisAtOrigin);
selfTestService.getSelfTestStatus().setZAxisAtOrigin(zAxisAtOrigin); selfTestService.getSelfTestStatus().setZAxisAtOrigin(zAxisAtOrigin);
if (xAxisAtOrigin && yAxisAtOrigin && zAxisAtOrigin) {
deviceStatus.setSelfTestCompleted(true);
}
return Result.success(selfTestService.getSelfTestStatus()); return Result.success(selfTestService.getSelfTestStatus());
} }
@Operation(summary = "自检完毕")
@PostMapping("/finish")
public Result<SelfTestVO> selfTestFinish(){
deviceStatus.setSelfTestCompleted(true);
return Result.success();
}
} }

42
src/main/java/com/qyft/ms/app/core/listener/DeviceInitializerListener.java

@ -0,0 +1,42 @@
package com.qyft.ms.app.core.listener;
import com.qyft.ms.app.device.status.DeviceStatus;
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.device.DeviceCommandService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@RequiredArgsConstructor
public class DeviceInitializerListener {
private final DeviceCommandService deviceCommandService;
private final DeviceStatus deviceStatus;
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
Runnable task = () -> {
try {
DeviceCommand overallDeviceStatusGetCommand = DeviceCommandGenerator.overallDeviceStatusGet();
CommandFuture overallDeviceStatusGetCommandFuture = deviceCommandService.sendCommandNoFront(overallDeviceStatusGetCommand);
overallDeviceStatusGetCommandFuture.getResponseFuture().get(5, TimeUnit.SECONDS);
boolean emergencyStop = overallDeviceStatusGetCommandFuture.getResponseResult().getJSONObject("data").getBool("emergencyStop");
deviceStatus.setStopPressed(emergencyStop);
scheduler.shutdown();
} catch (Exception e) {
log.error("开机获取急停状态失败", e);
}
};
scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
}
}

14
src/main/java/com/qyft/ms/system/controller/FrontCmdController.java

@ -18,6 +18,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@Tag(name = "前端调用指令") @Tag(name = "前端调用指令")
@ -30,17 +33,18 @@ public class FrontCmdController {
private final WebSocketService webSocketService; private final WebSocketService webSocketService;
private final DeviceStatus deviceStatus; private final DeviceStatus deviceStatus;
private static final Set<String> NO_SELF_TEST_CMDS =
new HashSet<>(Arrays.asList("device_self_test", "motor_x_to_home", "motor_y_to_home", "motor_z_to_home"));
@Operation(summary = "前端统一调用一个接口") @Operation(summary = "前端统一调用一个接口")
@PostMapping @PostMapping
public Result<?> controlMethod(@RequestBody FrontCmdControlForm form) { public Result<?> controlMethod(@RequestBody FrontCmdControlForm form) {
String cmdId = form.getCmdId(); String cmdId = form.getCmdId();
String cmdCode = form.getCmdCode(); String cmdCode = form.getCmdCode();
try { try {
if(!"device_self_test".equals(cmdCode)){
if (!deviceStatus.isSelfTestCompleted()) {
log.error("未进行自检,无法执行业务指令");
return Result.failed("未进行自检,无法执行业务指令");
}
if (!NO_SELF_TEST_CMDS.contains(cmdCode) && !deviceStatus.isSelfTestCompleted()) {
log.error("未进行自检,无法执行业务指令");
return Result.failed("未进行自检,无法执行业务指令");
} }
if(deviceStatus.isStopPressed()){ if(deviceStatus.isStopPressed()){
log.error("设备急停中"); log.error("设备急停中");

4
src/main/java/com/qyft/ms/system/core/listener/DeviceTcpMessageEventListener.java

@ -27,9 +27,9 @@ public class DeviceTcpMessageEventListener {
log.info("设备反馈信息{}", JSONUtil.toJsonStr(deviceResult)); log.info("设备反馈信息{}", JSONUtil.toJsonStr(deviceResult));
String tag = deviceResult.getStr("tag"); String tag = deviceResult.getStr("tag");
if ("ACK".equals(tag)) { if ("ACK".equals(tag)) {
log.info("ACK {}", JSONUtil.toJsonStr(deviceResult));
// log.info("ACK {}", JSONUtil.toJsonStr(deviceResult));
} else if ("RESPONSE".equals(tag)) { } else if ("RESPONSE".equals(tag)) {
log.info("RESPONSE {}", JSONUtil.toJsonStr(deviceResult));
// log.info("RESPONSE {}", JSONUtil.toJsonStr(deviceResult));
deviceCommandService.completeCommandResponse(deviceResult); deviceCommandService.completeCommandResponse(deviceResult);
} else if ("EVENT".equals(tag)) { } else if ("EVENT".equals(tag)) {
String eventType = deviceResult.getJSONObject("data").getStr("event_type"); String eventType = deviceResult.getJSONObject("data").getStr("event_type");

Loading…
Cancel
Save