82 lines
2.3 KiB
C#
82 lines
2.3 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using YD_TrainingCompetition_Api.Models;
|
||
|
|
using YD_TrainingCompetition_Api.Options;
|
||
|
|
using YD_TrainingCompetition_Api.Services;
|
||
|
|
|
||
|
|
namespace YD_TrainingCompetition_Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public class HikvisionController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IHikvisionAlarmStore _alarmStore;
|
||
|
|
private readonly ILogger<HikvisionController> _logger;
|
||
|
|
|
||
|
|
public HikvisionController(IHikvisionAlarmStore alarmStore, ILogger<HikvisionController> logger)
|
||
|
|
{
|
||
|
|
_alarmStore = alarmStore;
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取所有报警记录
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("alarms")]
|
||
|
|
public ActionResult<IReadOnlyCollection<AlarmRecord>> GetAlarms(
|
||
|
|
[FromQuery] string? deviceId = null)
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrWhiteSpace(deviceId))
|
||
|
|
{
|
||
|
|
return Ok(_alarmStore.GetByDeviceId(deviceId));
|
||
|
|
}
|
||
|
|
return Ok(_alarmStore.GetAll());
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取最新报警
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("alarms/latest")]
|
||
|
|
public ActionResult<AlarmRecord?> GetLatestAlarm(
|
||
|
|
[FromQuery] string? deviceId = null)
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrWhiteSpace(deviceId))
|
||
|
|
{
|
||
|
|
var record = _alarmStore.GetLatestByDevice(deviceId);
|
||
|
|
return record is null ? NotFound() : Ok(record);
|
||
|
|
}
|
||
|
|
|
||
|
|
var latest = _alarmStore.GetLatest();
|
||
|
|
return latest is null ? NotFound() : Ok(latest);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取所有摄像头状态
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("cameras")]
|
||
|
|
public ActionResult<IReadOnlyCollection<HikvisionCameraStatus>> GetCameraStatuses()
|
||
|
|
{
|
||
|
|
return Ok(_alarmStore.GetAllCameraStatuses());
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取单个摄像头状态
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("cameras/{deviceId}")]
|
||
|
|
public ActionResult<HikvisionCameraStatus> GetCameraStatus(string deviceId)
|
||
|
|
{
|
||
|
|
var status = _alarmStore.GetCameraStatus(deviceId);
|
||
|
|
return status is null ? NotFound() : Ok(status);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 清空报警记录
|
||
|
|
/// </summary>
|
||
|
|
[HttpDelete("alarms")]
|
||
|
|
public IActionResult ClearAlarms()
|
||
|
|
{
|
||
|
|
_alarmStore.ClearAlarms();
|
||
|
|
_logger.LogInformation("Alarms cleared via API");
|
||
|
|
return Ok(new { message = "All alarms cleared" });
|
||
|
|
}
|
||
|
|
}
|