initial
This commit is contained in:
128
src/DRS9.Dashboard.Server/Controllers/DevicesController.cs
Normal file
128
src/DRS9.Dashboard.Server/Controllers/DevicesController.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
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/[controller]")]
|
||||
public class DevicesController : ControllerBase
|
||||
{
|
||||
private readonly DeviceService _deviceService;
|
||||
private readonly DeviceManagementService _deviceManagementService;
|
||||
private readonly PlaylistService _playlistService;
|
||||
private readonly ILogger<DevicesController> _logger;
|
||||
|
||||
public DevicesController(DeviceService deviceService, DeviceManagementService deviceManagementService, PlaylistService playlistService, ILogger<DevicesController> logger)
|
||||
{
|
||||
_deviceService = deviceService;
|
||||
_deviceManagementService = deviceManagementService;
|
||||
_playlistService = playlistService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备注册 - 使用设备码激活设备
|
||||
/// </summary>
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<DeviceRegisterResponse>> Register([FromBody] DeviceRegisterRequest request)
|
||||
{
|
||||
_logger.LogInformation("Device registration attempt: {DeviceCode}", request.DeviceCode);
|
||||
|
||||
var result = await _deviceService.RegisterAsync(request);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
_logger.LogWarning("Device registration failed: {DeviceCode} - {Message}", request.DeviceCode, result.Message);
|
||||
return BadRequest(result);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Device registered successfully: {DeviceCode}", request.DeviceCode);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新设备心跳时间
|
||||
/// </summary>
|
||||
[HttpPost("heartbeat")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Heartbeat()
|
||||
{
|
||||
var deviceIdClaim = User.FindFirst("deviceId")?.Value;
|
||||
if (int.TryParse(deviceIdClaim, out var deviceId))
|
||||
{
|
||||
await _deviceService.UpdateLastSeenAsync(deviceId);
|
||||
return Ok(new { success = true });
|
||||
}
|
||||
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设备信息
|
||||
/// </summary>
|
||||
[HttpGet("info")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetInfo()
|
||||
{
|
||||
var deviceIdClaim = User.FindFirst("deviceId")?.Value;
|
||||
if (int.TryParse(deviceIdClaim, out var deviceId))
|
||||
{
|
||||
var device = await _deviceService.GetDeviceAsync(deviceId);
|
||||
if (device == null)
|
||||
{
|
||||
return NotFound(new { success = false, message = "设备不存在" });
|
||||
}
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
success = true,
|
||||
data = new
|
||||
{
|
||||
id = device.Id,
|
||||
deviceCode = device.DeviceCode,
|
||||
deviceName = device.DeviceName,
|
||||
deviceType = device.DeviceType,
|
||||
isEnabled = device.IsEnabled
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设备分配的内容
|
||||
/// </summary>
|
||||
[HttpGet("content")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetContent()
|
||||
{
|
||||
var deviceIdClaim = User.FindFirst("deviceId")?.Value;
|
||||
if (int.TryParse(deviceIdClaim, out var deviceId))
|
||||
{
|
||||
var result = await _deviceManagementService.GetDeviceContentAsync(deviceId);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设备播放列表(基于内容编排)
|
||||
/// </summary>
|
||||
[HttpGet("playlist")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetPlaylist()
|
||||
{
|
||||
var deviceIdClaim = User.FindFirst("deviceId")?.Value;
|
||||
if (int.TryParse(deviceIdClaim, out var deviceId))
|
||||
{
|
||||
var result = await _playlistService.GetDevicePlaylistAsync(deviceId);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
return Unauthorized();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user