liusuyi
2025-12-18 3814365a9c3380161886dc0b6cee6774783b0eb9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.ard.work.config;
 
import com.ard.work.domain.enums.RouteEvent;
import com.ard.work.domain.enums.RouteState;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.guard.Guard;
import org.springframework.statemachine.service.DefaultStateMachineService;
import org.springframework.statemachine.service.StateMachineService;
 
import java.util.EnumSet;
 
@Configuration
@EnableStateMachineFactory
public class RouteStateMachineConfig extends StateMachineConfigurerAdapter<RouteState, RouteEvent> {
 
    /**
     * 配置状态机的状态集
     */
    @Override
    public void configure(StateMachineStateConfigurer<RouteState, RouteEvent> states) throws Exception {
        states
                .withStates()
                .initial(RouteState.IDLE)  // 初始状态
                .states(EnumSet.allOf(RouteState.class)); // 注册所有状态
    }
 
    /**
     * 配置状态流转规则
     */
    @Override
    public void configure(StateMachineTransitionConfigurer<RouteState, RouteEvent> transitions)
            throws Exception {
 
        transitions
                // IDLE → UPLOADED
                .withExternal()
                .source(RouteState.IDLE)
                .target(RouteState.UPLOADED)
                .event(RouteEvent.UPLOAD_SUCCESS)
                .and()
 
                // UPLOADED → RUNNING
                .withExternal()
                .source(RouteState.UPLOADED)
                .target(RouteState.RUNNING)
                .event(RouteEvent.START_SUCCESS)
                .and()
 
                // RUNNING → PAUSED
                .withExternal()
                .source(RouteState.RUNNING)
                .target(RouteState.PAUSED)
                .event(RouteEvent.PAUSE_SUCCESS)
                .and()
 
                // PAUSED → RUNNING
                .withExternal()
                .source(RouteState.PAUSED)
                .target(RouteState.RUNNING)
                .event(RouteEvent.RESUME_SUCCESS)
                .and()
 
                // RUNNING / PAUSED → STOPPED
                .withExternal()
                .source(RouteState.RUNNING)
                .target(RouteState.STOPPED)
                .event(RouteEvent.STOP_SUCCESS)
                .and()
                .withExternal()
                .source(RouteState.PAUSED)
                .target(RouteState.STOPPED)
                .event(RouteEvent.STOP_SUCCESS)
                .and()
 
                // ⭐ STOPPED → IDLE(为下一条航线做准备)
                .withExternal()
                .source(RouteState.STOPPED)
                .target(RouteState.IDLE)
                .event(RouteEvent.RESET_ROUTE);
    }
 
 
    /**
     * 可选:守卫器 - 校验boxSn合法性(示例)
     */
    @Bean
    public Guard<RouteState, RouteEvent> validBoxSnGuard() {
        return context -> {
            // 从扩展变量中获取boxSn并校验
            String boxSn = (String) context.getMessageHeader("boxSn");
            return boxSn != null && !boxSn.isEmpty();
        };
    }
 
    /**
     * 状态机服务(用于管理多实例)
     */
    @Bean
    public StateMachineService<RouteState, RouteEvent> stateMachineService(
            org.springframework.statemachine.config.StateMachineFactory<RouteState, RouteEvent> factory) {
        return new DefaultStateMachineService<>(factory);
    }
}