zhaowenyu
2026-01-24 9e5edc26d37d8fbe81125d644125bb0e80311c8f
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
package com.ard.web.controller.route;
 
import com.ard.common.core.domain.AjaxResult;
import com.ard.work.config.RouteStateMachineService;
import com.ard.work.domain.enums.RouteEvent;
import com.ard.work.domain.enums.RouteState;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
@Api(tags = "航线状态机")
@RestController
@RequestMapping("/api/route")
@RequiredArgsConstructor
public class RouteController {
 
    @Autowired
    private RouteStateMachineService stateMachineService;
 
    /**
     * 发送航线事件
     */
    @ApiOperation("航线状态测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "boxSn", value = "设备编号", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "event", value = "事件", required = true, dataType = "RouteEvent", paramType = "query")
    })
    @PostMapping("/{boxSn}/event")
    public AjaxResult sendRouteEvent(
            @PathVariable String boxSn,
            @RequestParam RouteEvent event) {
        boolean success = stateMachineService.sendEvent(boxSn, event);
        if (success) {
            RouteState currentState = stateMachineService.getCurrentState(boxSn);
            return AjaxResult.success("事件执行成功,设备" + boxSn + "当前状态:" + currentState);
        } else {
            return AjaxResult.error("事件执行失败(状态流转不允许),设备" + boxSn + "当前状态:" +
                    stateMachineService.getCurrentState(boxSn));
        }
 
    }
 
    /**
     * 获取设备当前航线状态
     */
    @ApiOperation("获取设备当前航线状态")
    @GetMapping("/{boxSn}/state")
    public AjaxResult getRouteState(@PathVariable String boxSn) {
        return AjaxResult.success(stateMachineService.getCurrentState(boxSn));
    }
 
    /**
     * 重置设备航线状态
     */
    @ApiOperation("重置设备航线状态")
    @PostMapping("/{boxSn}/reset")
    public AjaxResult resetRouteState(@PathVariable String boxSn) {
        stateMachineService.resetStateMachine(boxSn);
        return AjaxResult.success( "设备" + boxSn + "航线状态已重置为:" + RouteState.IDLE);
    }
}