修改测试用例

This commit is contained in:
孙小云 2025-12-16 19:12:22 +08:00
parent 22a81cff7e
commit 4a3b9dc2ea
4 changed files with 56 additions and 7 deletions

View File

@ -22,10 +22,10 @@ public class DjiExitAction extends ExitAction {
@Override
public void execute(StateContext<DrcState, DrcEvent> context) {
String machineId = (String) context.getExtendedState().getVariables().get("machineId");
log.info("[DJI] {} 退出DRC模式: %s 判断外部命令是否成功", machineId);
log.info("[DJI] {} 退出DRC模式: %s 外部命令执行", machineId);
/**
* 发生命令的结果是失败或者超时了这边需要抛出异常
* DjiExitAction 执行远程方法,远程方法执行成功则不抛出异常,否则抛出异常
*/
throw new RuntimeException("");
}
}

View File

@ -23,8 +23,8 @@ public class DjiCanExitGuard extends CanExitGuard {
@Override
public boolean evaluate(StateContext<DrcState, DrcEvent> context) {
String machineId = (String) context.getExtendedState().getVariables().get("machineId");
log.info("[DJI] {} 退出DRC模式: %s 执行外部命令", machineId);
// 外部命令执行成功返回true,执行失败返回false
log.info("[DJI] {} 退出DRC模式: %s 判断是否具备执行条件", machineId);
//具备执行添加返回true,不具备执行条件返回false
return true;
}
}

View File

@ -2,11 +2,20 @@ package com.tuoheng.machine.impl.dji.listener;
import com.tuoheng.machine.events.DrcEvent;
import com.tuoheng.machine.listener.DefaultDrcListener;
import com.tuoheng.machine.service.DrcMachineService;
import com.tuoheng.machine.status.DrcState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.state.State;
import org.springframework.statemachine.transition.Transition;
import org.springframework.stereotype.Component;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
/**
* DJI平台DRC状态监听器
*/
@ -14,6 +23,11 @@ import org.springframework.stereotype.Component;
@Slf4j
public class DjiDrcListener extends DefaultDrcListener {
@Autowired
@Lazy
DrcMachineService drcMachineService;
@Override
public String getName() {
return "DJI-DRC";
@ -24,4 +38,35 @@ public class DjiDrcListener extends DefaultDrcListener {
log.debug("[{}] 大疆进入状态: {}", getName(), state.getId());
}
public void transitionEnded(Transition<DrcState, DrcEvent> transition) {
if (transition.getSource() != null && transition.getTarget() != null) {
log.debug("[{}] 转换结束: {} -> {}",
getName(), transition.getSource().getId(), transition.getTarget().getId());
}
// 获取状态机
}
@Override
public void stateContext(StateContext<DrcState, DrcEvent> stateContext) {
// 默认不处理
// 只处理状态进入阶段
if (stateContext.getStage() == StateContext.Stage.STATE_ENTRY) {
DrcState currentState = stateContext.getTarget() != null ?
stateContext.getTarget().getId() : null;
// 当进入 EXITING 状态时自动发送 EXITED 事件
if (currentState == DrcState.EXITING) {
StateMachine<DrcState, DrcEvent> stateMachine = stateContext.getStateMachine();
String machineId = (String) stateMachine.getExtendedState()
.getVariables().get("machineId");
log.info("[{}] 进入 EXITING 状态,准备自动转换到 EXITED, 机巢ID: {}",
getName(), machineId);
/**
* 这个地方可以直接调用 drcMachineService 转换状态
*/
drcMachineService.sendEvent(machineId, DrcEvent.EXITED);
}
}
}
}

View File

@ -63,16 +63,20 @@ public class DrcStateMachineTest {
log.debug(drcMachineService.getCurrentStates("airport-001"));
/**
* 变成退出中;这个时候需要在 DjiCanExitGuard 中编写退出的代码调用三方接口
* DjiExitAction 里面判断状态是否真的变化了
* 变成退出中;这个时候需要在
* DjiCanExitGuard 判断是否可以执行
* DjiExitAction 执行远程方法,远程方法执行成功则不抛出异常,否则抛出异常
*/
log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.EXITING)));
/**
* 打印一下当前的状态
* 修改 stateContext 里面的实现,就可以跳过某些状态
*/
log.debug(drcMachineService.getCurrentStates("airport-001"));
;
// log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.EXITED)));
//
// log.debug(drcMachineService.getCurrentStates("airport-001"));