373 lines
12 KiB
C#
373 lines
12 KiB
C#
using DRS9.Dashboard.Application.DTOs;
|
|
using DRS9.Dashboard.Application.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DRS9.Dashboard.Server.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/admin/devices")]
|
|
// [Authorize] - 暂时禁用认证以便测试 // TODO: 后续添加管理员角色验证
|
|
public class DevicesManagementController : ControllerBase
|
|
{
|
|
private readonly DeviceManagementService _deviceManagementService;
|
|
private readonly DashboardWebSocketManager _wsManager;
|
|
private readonly ILogger<DevicesManagementController> _logger;
|
|
|
|
public DevicesManagementController(
|
|
DeviceManagementService deviceManagementService,
|
|
DashboardWebSocketManager wsManager,
|
|
ILogger<DevicesManagementController> logger)
|
|
{
|
|
_deviceManagementService = deviceManagementService;
|
|
_wsManager = wsManager;
|
|
_logger = logger;
|
|
}
|
|
|
|
// ===== 设备管理 =====
|
|
|
|
/// <summary>
|
|
/// 获取所有设备
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<ActionResult<DeviceListResponse>> GetAll()
|
|
{
|
|
var result = await _deviceManagementService.GetAllDevicesAsync();
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取在线设备
|
|
/// </summary>
|
|
[HttpGet("online")]
|
|
public async Task<ActionResult<DeviceListResponse>> GetOnline()
|
|
{
|
|
var result = await _deviceManagementService.GetOnlineDevicesAsync();
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取设备详情
|
|
/// </summary>
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<DeviceDto>> GetById(int id)
|
|
{
|
|
var result = await _deviceManagementService.GetDeviceByIdAsync(id);
|
|
if (result == null)
|
|
{
|
|
return NotFound(new { success = false, message = "设备不存在" });
|
|
}
|
|
return Ok(new { success = true, data = result });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建设备(生成设备码)
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<ActionResult<object>> Create([FromBody] DeviceCreateRequest request)
|
|
{
|
|
var result = await _deviceManagementService.CreateDeviceAsync(request);
|
|
if (result == null)
|
|
{
|
|
return BadRequest(new { success = false, message = "设备码已存在" });
|
|
}
|
|
_logger.LogInformation("Device created: {DeviceCode}", request.DeviceCode);
|
|
return Ok(new { success = true, data = result, message = "创建成功" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新设备
|
|
/// </summary>
|
|
[HttpPut("{id}")]
|
|
public async Task<ActionResult<object>> Update(int id, [FromBody] DeviceUpdateRequest request)
|
|
{
|
|
var result = await _deviceManagementService.UpdateDeviceAsync(id, request);
|
|
if (result == null)
|
|
{
|
|
return NotFound(new { success = false, message = "设备不存在" });
|
|
}
|
|
_logger.LogInformation("Device updated: {Id}", id);
|
|
return Ok(new { success = true, data = result, message = "更新成功" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除设备
|
|
/// </summary>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult> Delete(int id)
|
|
{
|
|
var success = await _deviceManagementService.DeleteDeviceAsync(id);
|
|
if (!success)
|
|
{
|
|
return NotFound(new { success = false, message = "设备不存在" });
|
|
}
|
|
_logger.LogInformation("Device deleted: {Id}", id);
|
|
return Ok(new { success = true, message = "删除成功" });
|
|
}
|
|
|
|
// ===== 设备内容分配 =====
|
|
|
|
/// <summary>
|
|
/// 获取设备分配的内容
|
|
/// </summary>
|
|
[HttpGet("{id}/content")]
|
|
public async Task<ActionResult<DeviceContentResponse>> GetContent(int id)
|
|
{
|
|
var result = await _deviceManagementService.GetDeviceContentAsync(id);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 为设备分配内容
|
|
/// </summary>
|
|
[HttpPost("{id}/content")]
|
|
public async Task<ActionResult> AssignContent(int id, [FromBody] DeviceAssignmentRequest request)
|
|
{
|
|
if (request.DeviceId != id)
|
|
{
|
|
return BadRequest(new { success = false, message = "设备ID不匹配" });
|
|
}
|
|
|
|
var success = await _deviceManagementService.AssignContentToDeviceAsync(id, request.ApplicationIds);
|
|
if (!success)
|
|
{
|
|
return NotFound(new { success = false, message = "设备不存在" });
|
|
}
|
|
|
|
_logger.LogInformation("Content assigned to device {DeviceId}: {Count} applications", id, request.ApplicationIds.Count);
|
|
return Ok(new { success = true, message = "内容分配成功,已通知设备刷新" });
|
|
}
|
|
|
|
// ===== 推送控制 =====
|
|
|
|
/// <summary>
|
|
/// 刷新设备内容
|
|
/// </summary>
|
|
[HttpPost("{id}/push/refresh")]
|
|
public async Task<ActionResult> PushRefresh(int id)
|
|
{
|
|
var success = await _deviceManagementService.PushMessageAsync(id, "content_refresh", new { message = "请刷新内容" });
|
|
if (!success)
|
|
{
|
|
return NotFound(new { success = false, message = "设备不在线或不存在" });
|
|
}
|
|
_logger.LogInformation("Push refresh to device: {DeviceId}", id);
|
|
return Ok(new { success = true, message = "推送成功" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重启设备应用
|
|
/// </summary>
|
|
[HttpPost("{id}/push/restart")]
|
|
public async Task<ActionResult> PushRestart(int id)
|
|
{
|
|
var success = await _deviceManagementService.PushMessageAsync(id, "app_restart", new { message = "请重启应用" });
|
|
if (!success)
|
|
{
|
|
return NotFound(new { success = false, message = "设备不在线或不存在" });
|
|
}
|
|
_logger.LogInformation("Push restart to device: {DeviceId}", id);
|
|
return Ok(new { success = true, message = "推送成功" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 WebSocket 连接统计
|
|
/// </summary>
|
|
[HttpGet("stats/connections")]
|
|
public ActionResult GetConnectionStats()
|
|
{
|
|
var totalConnections = _wsManager.GetConnectionCount();
|
|
return Ok(new
|
|
{
|
|
success = true,
|
|
data = new
|
|
{
|
|
totalConnections,
|
|
timestamp = DateTime.UtcNow
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// ===== 设备分组控制器 =====
|
|
|
|
[ApiController]
|
|
[Route("api/admin/device-groups")]
|
|
// [Authorize] - 暂时禁用认证以便测试
|
|
public class DeviceGroupsController : ControllerBase
|
|
{
|
|
private readonly DeviceManagementService _deviceManagementService;
|
|
private readonly ILogger<DeviceGroupsController> _logger;
|
|
|
|
public DeviceGroupsController(DeviceManagementService deviceManagementService, ILogger<DeviceGroupsController> logger)
|
|
{
|
|
_deviceManagementService = deviceManagementService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有设备分组
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<ActionResult<DeviceGroupListResponse>> GetAll()
|
|
{
|
|
var result = await _deviceManagementService.GetAllGroupsAsync();
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建设备分组
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<ActionResult<object>> Create([FromBody] DeviceGroupCreateRequest request)
|
|
{
|
|
var result = await _deviceManagementService.CreateGroupAsync(request);
|
|
if (result == null)
|
|
{
|
|
return BadRequest(new { success = false, message = "创建失败" });
|
|
}
|
|
_logger.LogInformation("Device group created: {Name}", request.Name);
|
|
return Ok(new { success = true, data = result, message = "创建成功" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新设备分组
|
|
/// </summary>
|
|
[HttpPut("{id}")]
|
|
public async Task<ActionResult<object>> Update(int id, [FromBody] DeviceGroupUpdateRequest request)
|
|
{
|
|
var result = await _deviceManagementService.UpdateGroupAsync(id, request);
|
|
if (result == null)
|
|
{
|
|
return NotFound(new { success = false, message = "分组不存在" });
|
|
}
|
|
_logger.LogInformation("Device group updated: {Id}", id);
|
|
return Ok(new { success = true, data = result, message = "更新成功" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除设备分组
|
|
/// </summary>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult> Delete(int id)
|
|
{
|
|
var success = await _deviceManagementService.DeleteGroupAsync(id);
|
|
if (!success)
|
|
{
|
|
return NotFound(new { success = false, message = "分组不存在" });
|
|
}
|
|
_logger.LogInformation("Device group deleted: {Id}", id);
|
|
return Ok(new { success = true, message = "删除成功" });
|
|
}
|
|
}
|
|
|
|
// ===== 批量管理控制器 =====
|
|
|
|
[ApiController]
|
|
[Route("api/admin/batch")]
|
|
// [Authorize] - 暂时禁用认证以便测试
|
|
public class BatchManagementController : ControllerBase
|
|
{
|
|
private readonly BatchManagementService _batchService;
|
|
private readonly ILogger<BatchManagementController> _logger;
|
|
|
|
public BatchManagementController(BatchManagementService batchService, ILogger<BatchManagementController> logger)
|
|
{
|
|
_batchService = batchService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量为设备分配内容
|
|
/// </summary>
|
|
[HttpPost("assign-content")]
|
|
public async Task<ActionResult<BatchOperationResponse>> BatchAssignContent([FromBody] BatchAssignContentRequest request)
|
|
{
|
|
var result = await _batchService.BatchAssignContentAsync(request);
|
|
_logger.LogInformation("Batch assign content: {Count} devices", request.DeviceIds.Count);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量推送消息到设备
|
|
/// </summary>
|
|
[HttpPost("push")]
|
|
public async Task<ActionResult<BatchOperationResponse>> BatchPush([FromBody] BatchPushRequest request)
|
|
{
|
|
var result = await _batchService.BatchPushAsync(request);
|
|
_logger.LogInformation("Batch push: {Type} to {Count} devices", request.MessageType, request.DeviceIds.Count);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量推送消息到设备分组
|
|
/// </summary>
|
|
[HttpPost("push-to-groups")]
|
|
public async Task<ActionResult<BatchOperationResponse>> BatchPushToGroups([FromBody] BatchPushToGroupRequest request)
|
|
{
|
|
var result = await _batchService.BatchPushToGroupAsync(request);
|
|
_logger.LogInformation("Batch push to groups: {Type} to {Count} groups", request.MessageType, request.DeviceGroupIds.Count);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量启用/禁用设备
|
|
/// </summary>
|
|
[HttpPost("toggle-devices")]
|
|
public async Task<ActionResult<BatchOperationResponse>> BatchToggleDevices([FromBody] BatchToggleDevicesRequest request)
|
|
{
|
|
var result = await _batchService.BatchToggleDevicesAsync(request);
|
|
_logger.LogInformation("Batch toggle devices: {Enabled} for {Count} devices", request.IsEnabled, request.DeviceIds.Count);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量分配播放列表到分组
|
|
/// </summary>
|
|
[HttpPost("assign-playlist")]
|
|
public async Task<ActionResult<BatchOperationResponse>> BatchAssignPlaylist([FromBody] BatchAssignPlaylistRequest request)
|
|
{
|
|
var result = await _batchService.BatchAssignPlaylistToGroupAsync(request);
|
|
_logger.LogInformation("Batch assign playlist: {PlaylistId} to {Count} groups", request.PlaylistId, request.DeviceGroupIds.Count);
|
|
return Ok(result);
|
|
}
|
|
}
|
|
|
|
// ===== 仪表盘统计控制器 =====
|
|
|
|
[ApiController]
|
|
[Route("api/admin/stats")]
|
|
// [Authorize] - 暂时禁用认证以便测试
|
|
public class DashboardController : ControllerBase
|
|
{
|
|
private readonly DeviceManagementService _deviceManagementService;
|
|
private readonly ApplicationService _applicationService;
|
|
|
|
public DashboardController(
|
|
DeviceManagementService deviceManagementService,
|
|
ApplicationService applicationService)
|
|
{
|
|
_deviceManagementService = deviceManagementService;
|
|
_applicationService = applicationService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取仪表盘统计数据
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<ActionResult<DashboardStatsDto>> GetStats()
|
|
{
|
|
var devices = await _deviceManagementService.GetAllDevicesAsync();
|
|
var applications = await _applicationService.GetAllAsync();
|
|
|
|
var stats = new DashboardStatsDto
|
|
{
|
|
TotalDevices = devices.Data.Count,
|
|
OnlineDevices = devices.Data.Count(d => d.IsEnabled),
|
|
TotalApplications = applications.Total
|
|
};
|
|
|
|
return Ok(stats);
|
|
}
|
|
}
|