海康摄像头增加了云台控制等

This commit is contained in:
2025-12-28 13:14:40 +08:00
parent 2ee25a4f7c
commit 231247c80f
12 changed files with 774 additions and 107 deletions

View File

@@ -399,4 +399,126 @@ public class CamerasController : ControllerBase
// 3. 返回 JSON 给前端
return Ok(options);
}
/// <summary>
/// 获取设备时间 (支持海康/大华等具备此能力的设备)
/// </summary>
[HttpGet("{id}/time")]
public async Task<IActionResult> GetDeviceTime(long id)
{
var device = _manager.GetDevice(id);
if (device == null) return NotFound(new { error = "Device not found" });
if (!device.IsPhysicalOnline) return BadRequest(new { error = "Device is offline" });
// 【核心】模式匹配:判断这个设备是否实现了“时间同步接口”
if (device is ITimeSyncFeature timeFeature)
{
try
{
var time = await timeFeature.GetTimeAsync();
return Ok(new { deviceId = id, currentTime = time });
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
// 如果是 RTSP/USB 等不支持的设备
return BadRequest(new { error = "This device does not support time synchronization." });
}
/// <summary>
/// 设置设备时间
/// </summary>
[HttpPost("{id}/time")]
public async Task<IActionResult> SetDeviceTime(long id, [FromBody] DateTime time)
{
var device = _manager.GetDevice(id);
if (device == null) return NotFound();
if (device is ITimeSyncFeature timeFeature)
{
try
{
await timeFeature.SetTimeAsync(time);
return Ok(new { success = true, message = $"Time synced to {time}" });
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
return BadRequest(new { error = "This device does not support time synchronization." });
}
/// <summary>
/// 远程重启设备
/// </summary>
[HttpPost("{id}/reboot")]
public async Task<IActionResult> RebootDevice(long id)
{
var device = _manager.GetDevice(id);
if (device == null) return NotFound(new { error = "Device not found" });
// 依然是两道防线:先检查在线,再检查能力
if (!device.IsOnline) return BadRequest(new { error = "Device is offline" });
if (device is IRebootFeature rebootFeature)
{
try
{
await rebootFeature.RebootAsync();
// 记录审计日志 (建议加上)
device.AddAuditLog("用户执行了远程重启");
return Ok(new { success = true, message = "重启指令已发送,设备将在几分钟后重新上线。" });
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
return BadRequest(new { error = "This device does not support remote reboot." });
}
[HttpPost("{id}/ptz")]
public async Task<IActionResult> PtzControl(long id, [FromBody] PtzControlDto dto)
{
var device = _manager.GetDevice(id);
if (device == null) return NotFound();
if (!device.IsOnline) return BadRequest("Device offline");
if (device is IPtzFeature ptz)
{
try
{
// 逻辑分流
if (dto.Duration > 0)
{
// 场景:点动模式 (一次调用,自动停止)
// 建议限制一下最大时长,防止前端传个 10000秒 导致云台转疯了
int safeDuration = Math.Clamp(dto.Duration, 50, 2000);
await ptz.PtzStepAsync(dto.Action, safeDuration, dto.Speed);
}
else
{
// 场景:手动模式 (按下/松开)
await ptz.PtzControlAsync(dto.Action, dto.Stop, dto.Speed);
}
return Ok();
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}//
}
return BadRequest("Device does not support PTZ.");
}
}