152 lines
7.2 KiB
C#
152 lines
7.2 KiB
C#
using Heimdall.Application.DTOs;
|
|||
|
|
using Heimdall.Application.Services;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Camera discovery endpoints.
|
||
|
|
/// Provides network probing, camera database search, stream URL building,
|
||
|
|
/// stream testing, and discovery confirmation.
|
||
|
|
/// </summary>
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/discovery")]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class DiscoveryController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ICameraDiscoveryService _discoveryService;
|
||
|
|
|
||
|
|
public DiscoveryController(ICameraDiscoveryService discoveryService)
|
||
|
|
{
|
||
|
|
_discoveryService = discoveryService;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Probe a single IP address for camera services.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpPost("probe")]
|
||
|
|
[ProducesResponseType(typeof(ProbeResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> ProbeIp([FromBody] ProbeRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.ProbeIpAsync(request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Scan a subnet for camera devices.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpPost("scan")]
|
||
|
|
[ProducesResponseType(typeof(IReadOnlyList<ProbeResponse>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> ScanSubnet([FromBody] ScanRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.ScanSubnetAsync(request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Search the built-in camera database by brand, model, or preset.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpGet("search")]
|
||
|
|
[ProducesResponseType(typeof(SearchResponse), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> SearchDatabase([FromQuery] string? q, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.SearchDatabaseAsync(q ?? "", ct);
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Build stream URLs from camera database entries.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpPost("build-urls")]
|
||
|
|
[ProducesResponseType(typeof(StreamBuildResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> BuildStreamUrls([FromBody] StreamBuildRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.BuildStreamUrlsAsync(request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Test stream URLs. Returns a session ID for polling.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpPost("test-streams")]
|
||
|
|
[ProducesResponseType(typeof(StreamTestSession), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> TestStreams([FromBody] StreamTestRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.TestStreamsAsync(request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Poll a stream test session for updated results.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpGet("test/{sessionId:guid}")]
|
||
|
|
[ProducesResponseType(typeof(StreamTestSession), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetTestSession(Guid sessionId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.GetTestSessionAsync(sessionId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get a screenshot from a stream test session.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpGet("test/{sessionId:guid}/screenshot/{index}")]
|
||
|
|
[ProducesResponseType(typeof(byte[]), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetScreenshot(Guid sessionId, int index, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var screenshot = await _discoveryService.GetScreenshotAsync(sessionId, index, ct);
|
||
|
|
if (screenshot is null)
|
||
|
|
return NotFound(new { error = "Screenshot not available" });
|
||
|
|
return File(screenshot, "image/jpeg");
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Cancel a running stream test session.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpPost("test/{sessionId:guid}/cancel")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> CancelTestSession(Guid sessionId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.CancelTestSessionAsync(sessionId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok()
|
||
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Check if a camera already exists for this IP/MAC (anti-duplication).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpPost("check")]
|
||
|
|
[ProducesResponseType(typeof(CameraCheckResponse), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> CheckDuplicate([FromBody] CheckDuplicateRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.CheckDuplicateAsync(request, ct);
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Confirm a discovery result and create a Camera entity.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpPost("confirm")]
|
||
|
|
[ProducesResponseType(typeof(CameraResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(CameraCheckResponse), StatusCodes.Status409Conflict)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> ConfirmDiscovery([FromBody] ConfirmDiscoveryRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _discoveryService.ConfirmDiscoveryAsync(request, ct);
|
||
|
|
if (result.IsSuccess)
|
||
|
|
return Ok(result.Data);
|
||
|
|
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
}
|