thingsboard-client-demo/src/test/java/com/tuoheng/old/DrcStateMachineTest.java

86 lines
2.7 KiB
Java
Raw Normal View History

2025-12-17 10:23:45 +08:00
package com.tuoheng.old;
2025-12-17 10:23:45 +08:00
import com.tuoheng.old.events.DrcEvent;
import com.tuoheng.old.service.DrcMachineService;
import com.tuoheng.old.status.DrcState;
2025-12-16 16:00:14 +08:00
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
2025-12-16 17:40:31 +08:00
import org.springframework.statemachine.StateMachine;
/**
* DRC状态机测试
* 测试DRC模式的完整状态转换流程
*/
@SpringBootTest
2025-12-16 16:00:14 +08:00
@Slf4j
public class DrcStateMachineTest {
@Autowired
2025-12-16 17:40:31 +08:00
DrcMachineService drcMachineService;
2025-12-16 17:40:31 +08:00
@Test
public void testDrcMachineService(){
2025-12-16 17:40:31 +08:00
String sn = "airport-001";
/**
* 不存在的会报错,需要在 MachinePlatTypeRepository 里面定义有这个机场编号
*/
try {
StateMachine<DrcState, DrcEvent> stateMachine =
drcMachineService.getOrCreateStateMachine("airport-001--2");
}catch (RuntimeException runtimeException){}
2025-12-16 17:40:31 +08:00
StateMachine<DrcState, DrcEvent> stateMachine = drcMachineService.getStateMachine("airport-001");
drcMachineService.getOrCreateStateMachine("airport-001");
stateMachine = drcMachineService.getStateMachine("airport-001");
/**
* 打印一下当前的状态
*/
log.debug(drcMachineService.getCurrentStates("airport-001"));
/**
* 从UnKnown状态可以走到任意状态
*/
drcMachineService.sendEvent(sn,DrcEvent.ENTERED);
/**
* ENTERED无法进入ENTERING DrcMachineConfig 里面配置的
*/
log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.ENTERING)));
/**
* 现在是 ENTERED,但是你还想进入ENTERED ,这个是不可以的
*/
log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.ENTERED)));
/**
* 打印一下当前的状态
*/
log.debug(drcMachineService.getCurrentStates("airport-001"));
2025-12-16 17:40:31 +08:00
/**
2025-12-16 19:12:22 +08:00
* 变成退出中;这个时候需要在
* DjiCanExitGuard 判断是否可以执行
2025-12-17 10:23:45 +08:00
* DjiExitAction 执行远程方法,远程方法本身执行成功则不抛出异常,否则抛出异常
2025-12-16 17:40:31 +08:00
*/
log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.EXITING)));
2025-12-16 17:40:31 +08:00
/**
* 打印一下当前的状态
2025-12-16 19:12:22 +08:00
* 修改 stateContext 里面的实现,就可以跳过某些状态
2025-12-16 17:40:31 +08:00
*/
log.debug(drcMachineService.getCurrentStates("airport-001"));
2025-12-16 19:12:22 +08:00
;
2025-12-16 17:40:31 +08:00
// log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.EXITED)));
//
// log.debug(drcMachineService.getCurrentStates("airport-001"));
}
}