Camera Information System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

259 lines
9.0 KiB

using Cis.Application.Cb;
using Cis.Application.Core.Component.Onvif;
using EC.Helper.Onvif;
namespace Cis.Application.Core;
/// <summary>
/// onvif 服务
/// </summary>
[ApiDescriptionSettings(CoreInfo.GroupName, Order = CoreInfo.OnvifGroupOrder)]
public class OnvifService : IDynamicApiController, ITransient
{
#region
private readonly SqlSugarRepository<CbCamera> _cbCameraRep;
private readonly IOnvifServer _onvifServer;
#endregion
public OnvifService(SqlSugarRepository<CbCamera> cbCameraRep, IOnvifServer onvifServer)
{
_cbCameraRep = cbCameraRep;
_onvifServer = onvifServer;
}
#region Base Method
/// <summary>
/// 注册 onvifClient
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <returns></returns>
[HttpPost]
public async Task<bool> 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;
}
/// <summary>
/// 注销 onvifClient
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <returns></returns>
[HttpPost]
public bool Delete([Required][FromForm] long cameraId)
{
return _onvifServer.Delete(cameraId);
}
/// <summary>
/// 是否存在 onvifClient
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <returns></returns>
[HttpGet]
public bool IsExists([Required] long cameraId)
{
return _onvifServer.IsExists(cameraId);
}
#endregion Base Method
#region Imaging Method
/// <summary>
/// 变焦绝对移动
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <param name="position">变焦移动绝对点:[-1,1]</param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>position:变焦移动绝对点:[-1,1]")]
public async Task<bool> 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;
}
/// <summary>
/// 变焦相对移动
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <param name="distance">变焦移动相对点:[-1,1]</param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>distance:变焦移动相对点:[-1,1]")]
public async Task<bool> 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;
}
/// <summary>
/// 变焦持续移动
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <param name="speed">持续移动方向:[-1,1]</param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>speed:持续移动方向:[-1,1]")]
public async Task<bool> 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;
}
/// <summary>
/// 停止变焦
/// </summary>
/// <param name="cameraId">cbCameraId</param>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId")]
public async Task<bool> 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
/// <summary>
/// 获取播放视频 URL
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <returns></returns>
[HttpGet]
public async Task<string> GetStreamUrl([Required] long cameraId)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return string.Empty;
string url = await client.GetStreamUrl();
return url;
}
/// <summary>
/// 获取抓图 URL
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <returns></returns>
[HttpGet]
public async Task<string> GetSnapshotUrl([Required] long cameraId)
{
bool ret = _onvifServer.TryGet(cameraId, out OnvifClient client);
if (!ret) return string.Empty;
string url = await client.GetSnapshotUrl();
return url;
}
/// <summary>
/// 获取抓图 Base64
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <returns></returns>
[HttpGet]
public async Task<string> 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
/// <summary>
/// 绝对移动
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <param name="pan">水平方向移动绝对点:[-1,1]</param>
/// <param name="tilt">垂直方向移动绝对点:[-1,1]</param>
/// <param name="zoom">变倍绝对点:[-1,1]</param>
/// <param name="atomDist">可以理解为移动速度:[0,1],默认 0.1</param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>pan:水平方向移动绝对点:[-1,1]<br/>tilt:[-1,1]<br/>" +
"zoom:变倍绝对点:[-1,1]<br/>atomDist:可以理解为移动速度:[0,1],默认 0.1")]
public async Task<bool> 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;
}
/// <summary>
/// 相对移动
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <param name="pan">水平方向移动相对点:[-1,1]</param>
/// <param name="tilt">垂直方向移动相对点:[-1,1]</param>
/// <param name="zoom">变倍相对点:[-1,1]</param>
/// <param name="atomSpeed">移动速度:[0,1],默认 0.1</param>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>pan:水平方向移动相对点:[-1,1]<br/> tilt:[-1,1]<br/>" +
"zoom:变倍相对点:[-1,1]<br/>atomSpeed:移动速度:[0,1],默认 0.1")]
public async Task<bool> 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;
}
/// <summary>
/// 持续移动
/// </summary>
/// <param name="cameraId">cbCameraId</param>
/// <param name="pan">水平方向移动方向:[-1,1]</param>
/// <param name="tilt">垂直方向移动方向:[-1,1]</param>
/// <param name="zoom">变倍移动方向:[-1,1]</param>
/// <param name="timeout">超时时间,ms</param>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId<br/>pan:水平方向移动方向:[-1,1]<br/>tilt:[-1,1]<br/>" +
"zoom:变倍移动方向:[-1,1]<br/>timeout:超时时间,ms")]
public async Task<bool> 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;
}
/// <summary>
/// 停止移动
/// </summary>
/// <param name="cameraId">cbCameraId</param>
[HttpPost]
[ApiDescriptionSettings(Description = "cameraId:cbCameraId")]
public async Task<bool> 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
}