using Cis.Application.Cb;
using Cis.Application.Core.Component.Onvif;
using EC.Helper.Onvif;
namespace Cis.Application.Core;
///
/// onvif 服务
///
[ApiDescriptionSettings(CoreInfo.GroupName, Order = CoreInfo.OnvifGroupOrder)]
public class OnvifService : IDynamicApiController, ITransient
{
#region
private readonly SqlSugarRepository _cbCameraRep;
private readonly OnvifServer _onvifServer;
#endregion
public OnvifService(SqlSugarRepository cbCameraRep, OnvifServer onvifServer)
{
_cbCameraRep = cbCameraRep;
_onvifServer = onvifServer;
}
#region Base Method
///
/// 注册 onvifClient
///
/// cbCameraId
///
[HttpPost]
public async Task Register([Required][FromForm] long cameraId)
{
CbCamera camera = await _cbCameraRep.GetByIdAsync(cameraId);
if (camera == null) return false;
bool ret = _onvifServer.IsExists(camera.Id);
if (ret) return false;
ret = await _onvifServer.RegisterAsync(camera);
return ret;
}
///
/// 注销 onvifClient
///
/// cbCameraId
///
[HttpPost]
public bool Delete([Required][FromForm] long cameraId)
{
return _onvifServer.Delete(cameraId);
}
///
/// 是否存在 onvifClient
///
/// cbCameraId
///
[HttpGet]
public bool IsExists([Required] long cameraId)
{
return _onvifServer.IsExists(cameraId);
}
#endregion Base Method
#region Imaging Method
///
/// 变焦绝对移动
///
/// cbCameraId
/// 变焦移动绝对点:[-1,1]
///
[HttpPost]
public async Task FocusAbsoluteMove([Required][FromForm] long cameraId, [Required][FromForm] float position)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.FocusAbsoluteMove(position);
return true;
}
///
/// 变焦相对移动
///
/// cbCameraId
/// 变焦移动相对点:[-1,1]
///
[HttpPost]
public async Task FocusRelativeMove([Required][FromForm] long cameraId, [Required][FromForm] float distance)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.FocusRelativeMove(distance);
return true;
}
///
/// 变焦持续移动
///
/// cbCameraId
/// 持续移动方向:[-1,1]
///
[HttpPost]
public async Task FocusContinuousMove([Required][FromForm] long cameraId, [Required][FromForm] float speed)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.FocusContinuousMove(speed);
return true;
}
///
/// 停止变焦
///
/// cbCameraId
[HttpPost]
public async Task FocusStopMove([Required][FromForm] long cameraId)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.FocusStopMove();
return true;
}
#endregion Imaging Method
#region Media Method
///
/// 获取播放视频 URL
///
/// cbCameraId
///
[HttpGet]
public async Task GetStreamUrl([Required] long cameraId)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return string.Empty;
string url = await client.GetStreamUrl();
return url;
}
///
/// 获取抓图 URL
///
/// cbCameraId
///
[HttpGet]
public async Task GetSnapshotUrl([Required] long cameraId)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return string.Empty;
string url = await client.GetSnapshotUrl();
return url;
}
///
/// 获取抓图 Base64
///
/// cbCameraId
///
[HttpGet]
public async Task GetSnapshot([Required] long cameraId)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return string.Empty;
string snapshotBase64 = await client.GetSnapshot();
return snapshotBase64;
}
#endregion Media Method
#region Ptz Method
///
/// 绝对移动
///
/// cbCameraId
/// 水平方向移动绝对点:[-1,1]
/// 垂直方向移动绝对点:[-1,1]
/// 变倍绝对点:[-1,1]
/// 可以理解为移动速度:[0,1],默认 0.1
///
[HttpPost]
public async Task AbsoluteMove([Required][FromForm] long cameraId,
[Required][FromForm] float pan, [Required][FromForm] float tilt, [Required][FromForm] float zoom, [FromForm] float atomDist = 0.1f)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.AbsoluteMove(pan, tilt, zoom, atomDist);
return true;
}
///
/// 相对移动
///
/// cbCameraId
/// 水平方向移动相对点:[-1,1]
/// 垂直方向移动相对点:[-1,1]
/// 变倍相对点:[-1,1]
/// 移动速度:[0,1],默认 0.1
[HttpPost]
public async Task RelativeMove([Required][FromForm] long cameraId,
[Required][FromForm] float pan, [Required][FromForm] float tilt, [Required][FromForm] float zoom, [FromForm] float atomSpeed = 0.1f)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.RelativeMove(pan, tilt, zoom, atomSpeed);
return true;
}
///
/// 持续移动
///
/// cbCameraId
/// 水平方向移动方向:[-1,1]
/// 垂直方向移动方向:[-1,1]
/// 变倍移动方向:[-1,1]
/// 超时时间,ms
[HttpPost]
public async Task ContinuousMove([Required][FromForm] long cameraId,
[Required][FromForm] float pan, [Required][FromForm] float tilt, [Required][FromForm] float zoom, [FromForm] string timeout = "")
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.ContinuousMove(pan, tilt, zoom, timeout);
return true;
}
///
/// 停止移动
///
/// cbCameraId
[HttpPost]
public async Task StopMove([Required][FromForm] long cameraId)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return false;
await client.StopMove();
return true;
}
#endregion Ptz Method
}