package com.tuoheng.machine; import com.tuoheng.machine.events.DrcEvent; import com.tuoheng.machine.service.DrcMachineService; import com.tuoheng.machine.status.DrcState; 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; import org.springframework.statemachine.StateMachine; /** * DRC状态机测试 * 测试DRC模式的完整状态转换流程 */ @SpringBootTest @Slf4j public class DrcStateMachineTest { @Autowired DrcMachineService drcMachineService; @Test public void testDrcMachineService(){ String sn = "airport-001"; /** * 不存在的会报错,需要在 MachinePlatTypeRepository 里面定义有这个机场编号 */ try { StateMachine stateMachine = drcMachineService.getOrCreateStateMachine("airport-001--2"); }catch (RuntimeException runtimeException){} StateMachine 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")); /** * 变成退出中;这个时候需要在 DjiCanExitGuard 中编写退出的代码(调用三方接口) * DjiExitAction 里面判断状态是否真的变化了 */ log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.EXITING))); /** * 打印一下当前的状态 */ log.debug(drcMachineService.getCurrentStates("airport-001")); // log.debug(String.valueOf(drcMachineService.sendEvent(sn,DrcEvent.EXITED))); // // log.debug(drcMachineService.getCurrentStates("airport-001")); } }