Junjie
1 天以前 71d499e1ad7d4e2f2886627f7a57e0fadf0bc235
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.zy.asrs.controller;
 
import com.core.annotations.ManagerAuth;
import com.core.common.R;
import com.zy.asrs.domain.replay.ReplayChunk;
import com.zy.asrs.domain.replay.ReplaySeekResult;
import com.zy.asrs.service.DeviceLogReplayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping("/deviceLog/replay")
public class DeviceLogReplayController {
 
    private final DeviceLogReplayService deviceLogReplayService;
 
    public DeviceLogReplayController(DeviceLogReplayService deviceLogReplayService) {
        this.deviceLogReplayService = deviceLogReplayService;
    }
 
    @PostMapping("/session/auth")
    @ManagerAuth
    public R createSession(@RequestParam("day") String day,
                           @RequestParam(value = "type", required = false) String type,
                           @RequestParam(value = "deviceNo", required = false) Integer deviceNo,
                           @RequestParam(value = "stationId", required = false) Integer stationId,
                           @RequestParam(value = "targetTimestamp", required = false) Long targetTimestamp,
                           @RequestParam(value = "windowStartTimeMs", required = false) Long windowStartTimeMs,
                           @RequestParam(value = "windowEndTimeMs", required = false) Long windowEndTimeMs) {
        try {
            return R.ok(deviceLogReplayService.createSession(
                    day,
                    type,
                    deviceNo,
                    stationId,
                    targetTimestamp,
                    windowStartTimeMs,
                    windowEndTimeMs));
        } catch (DeviceLogReplayService.ReplayWindowException e) {
            log.warn("创建历史回放会话时间窗无效, day={}, targetTimestamp={}, windowStartTimeMs={}, windowEndTimeMs={}",
                    day, targetTimestamp, windowStartTimeMs, windowEndTimeMs, e);
            R result = R.error(e.getMessage());
            result.put("errorCode", e.getErrorCode());
            return result;
        } catch (Exception e) {
            log.warn("创建历史回放会话失败, day={}, type={}, deviceNo={}, stationId={}",
                    day, type, deviceNo, stationId, e);
            return R.error(e.getMessage());
        }
    }
 
    @GetMapping("/session/{sessionId}/timeline/auth")
    @ManagerAuth
    public R timeline(@PathVariable("sessionId") String sessionId) {
        try {
            return R.ok(deviceLogReplayService.loadTimeline(sessionId));
        } catch (Exception e) {
            log.warn("读取历史回放时间轴失败, sessionId={}", sessionId, e);
            return R.error(e.getMessage());
        }
    }
 
    @GetMapping("/session/{sessionId}/summary/auth")
    @ManagerAuth
    public R summary(@PathVariable("sessionId") String sessionId,
                     @RequestParam(value = "bucketCount", required = false) Integer bucketCount) {
        try {
            return R.ok(deviceLogReplayService.loadTimelineSummary(sessionId, bucketCount));
        } catch (Exception e) {
            log.warn("读取历史回放时间轴摘要失败, sessionId={}, bucketCount={}", sessionId, bucketCount, e);
            return R.error(e.getMessage());
        }
    }
 
    @PostMapping("/session/{sessionId}/seek/auth")
    @ManagerAuth
    public R seek(@PathVariable("sessionId") String sessionId,
                  @RequestParam(value = "timestamp", required = false) Long timestamp,
                  @RequestParam(value = "sampleSeq", required = false) Long sampleSeq) {
        try {
            ReplaySeekResult result = deviceLogReplayService.seek(sessionId, timestamp, sampleSeq);
            return R.ok(result);
        } catch (DeviceLogReplayService.ReplayWindowException e) {
            log.warn("回放 seek 时间窗无效, sessionId={}, timestamp={}, sampleSeq={}", sessionId, timestamp, sampleSeq, e);
            R result = R.error(e.getMessage());
            result.put("errorCode", e.getErrorCode());
            return result;
        } catch (Exception e) {
            log.warn("回放 seek 失败, sessionId={}", sessionId, e);
            return R.error(e.getMessage());
        }
    }
 
    @GetMapping("/session/{sessionId}/chunk/auth")
    @ManagerAuth
    public R chunk(@PathVariable("sessionId") String sessionId,
                   @RequestParam("chunkIndex") Integer chunkIndex,
                   @RequestParam(value = "full", required = false, defaultValue = "false") Boolean full) {
        try {
            ReplayChunk chunk = deviceLogReplayService.loadChunk(sessionId, chunkIndex, Boolean.TRUE.equals(full));
            return R.ok(chunk);
        } catch (Exception e) {
            log.warn("加载回放 chunk 失败, sessionId={}, chunkIndex={}", sessionId, chunkIndex, e);
            return R.error(e.getMessage());
        }
    }
 
    @GetMapping("/session/{sessionId}/device/auth")
    @ManagerAuth
    public R device(@PathVariable("sessionId") String sessionId,
                    @RequestParam("type") String type,
                    @RequestParam(value = "deviceNo", required = false) Integer deviceNo,
                    @RequestParam(value = "stationId", required = false) Integer stationId,
                    @RequestParam(value = "timestamp", required = false) Long timestamp) {
        try {
            Map<String, Object> data = deviceLogReplayService.resolveDevice(sessionId, type, deviceNo, stationId, timestamp);
            return R.ok(data);
        } catch (DeviceLogReplayService.ReplayWindowException e) {
            log.warn("解析回放设备锚点时间窗无效, sessionId={}, type={}, deviceNo={}, stationId={}, timestamp={}",
                    sessionId, type, deviceNo, stationId, timestamp, e);
            R result = R.error(e.getMessage());
            result.put("errorCode", e.getErrorCode());
            return result;
        } catch (Exception e) {
            log.warn("解析回放设备锚点失败, sessionId={}, type={}, deviceNo={}, stationId={}",
                    sessionId, type, deviceNo, stationId, e);
            return R.error(e.getMessage());
        }
    }
 
    @DeleteMapping("/session/{sessionId}/auth")
    @ManagerAuth
    public R close(@PathVariable("sessionId") String sessionId) {
        deviceLogReplayService.closeSession(sessionId);
        return R.ok();
    }
}