package com.mes.task.controller;
|
|
import com.mes.task.service.TaskStatusNotificationService;
|
import com.mes.vo.Result;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
/**
|
* 任务状态通知控制器
|
* 提供SSE端点用于实时推送任务执行状态
|
*
|
* @author mes
|
* @since 2025-01-XX
|
*/
|
@RestController
|
@RequestMapping("/api/plcSend/task/notification")
|
@Api(tags = "任务状态通知")
|
@RequiredArgsConstructor
|
public class TaskStatusNotificationController {
|
|
private final TaskStatusNotificationService notificationService;
|
|
@GetMapping(value = "/sse", produces = "text/event-stream")
|
@ApiOperation("创建SSE连接,监听任务状态变化")
|
public SseEmitter createConnection(@RequestParam(required = false) String taskId) {
|
SseEmitter emitter = notificationService.createConnection(taskId);
|
if (emitter == null) {
|
throw new RuntimeException("创建SSE连接失败");
|
}
|
return emitter;
|
}
|
|
@GetMapping(value = "/sse/all", produces = "text/event-stream")
|
@ApiOperation("创建SSE连接,监听所有任务状态变化")
|
public SseEmitter createConnectionForAllTasks() {
|
return createConnection(null);
|
}
|
|
@PostMapping("/close/{taskId}")
|
@ApiOperation("关闭指定任务的SSE连接")
|
public Result<Boolean> closeConnections(@PathVariable String taskId) {
|
notificationService.closeConnections(taskId);
|
return Result.success(true);
|
}
|
|
@PostMapping("/close/all")
|
@ApiOperation("关闭所有SSE连接")
|
public Result<Boolean> closeAllConnections() {
|
notificationService.closeAllConnections();
|
return Result.success(true);
|
}
|
}
|