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 _logger; public HikvisionController(IHikvisionAlarmStore alarmStore, ILogger logger) { _alarmStore = alarmStore; _logger = logger; } /// /// 获取所有报警记录 /// [HttpGet("alarms")] public ActionResult> GetAlarms( [FromQuery] string? deviceId = null) { if (!string.IsNullOrWhiteSpace(deviceId)) { return Ok(_alarmStore.GetByDeviceId(deviceId)); } return Ok(_alarmStore.GetAll()); } /// /// 获取最新报警 /// [HttpGet("alarms/latest")] public ActionResult 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); } /// /// 获取所有摄像头状态 /// [HttpGet("cameras")] public ActionResult> GetCameraStatuses() { return Ok(_alarmStore.GetAllCameraStatuses()); } /// /// 获取单个摄像头状态 /// [HttpGet("cameras/{deviceId}")] public ActionResult GetCameraStatus(string deviceId) { var status = _alarmStore.GetCameraStatus(deviceId); return status is null ? NotFound() : Ok(status); } /// /// 清空报警记录 /// [HttpDelete("alarms")] public IActionResult ClearAlarms() { _alarmStore.ClearAlarms(); _logger.LogInformation("Alarms cleared via API"); return Ok(new { message = "All alarms cleared" }); } }