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 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(); } }