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.

191 lines
3.7 KiB

using EC.Onvif.Common;
using EC.Onvif.Device;
using EC.Onvif.Imaging;
using EC.Onvif.Media;
using EC.Onvif.PTZ;
using System;
using System.Threading.Tasks;
using Capabilities = EC.Onvif.Common.Capabilities;
namespace EC.Onvif
{
public class OnvifClient
{
#region Attr
private string Hostname { get; set; }
private string Username { get; set; }
private string Password { get; set; }
public static float atomDist { get; set; } = 0.01f;
public static float atomSpeed { get; set; } = 0.1f;
#endregion Attr
#region Client Attr
private DeviceClient Device { get; set; }
private MediaClient Media { get; set; }
private PTZClient PTZ { get; set; }
private ImagingClient Imaging { get; set; }
private Capabilities Caps { get; set; }
private string profileToken { get; set; }
#endregion Client Attr
public OnvifClient(string hostname, string username, string password)
{
Hostname = hostname;
Username = username;
Password = password;
}
public async Task InitAsync()
{
try
{
Device = await OnvifClientFactory.CreateDeviceClientAsync(Hostname, Username, Password);
Media = await OnvifClientFactory.CreateMediaClientAsync(Hostname, Username, Password);
PTZ = await OnvifClientFactory.CreatePTZClientAsync(Hostname, Username, Password);
var profiles = await Media.GetProfilesAsync();
foreach (var profile in profiles.Profiles)
{
if (string.IsNullOrEmpty(profileToken))
{
profileToken = profile.token;
break;
}
}
Caps = (await Device.GetCapabilitiesAsync(new CapabilityCategory[] { CapabilityCategory.All })).Capabilities;
}
catch (Exception)
{
throw;
}
}
#region DeviceClient
public bool IsDeviceContected()
{
return Device != null;
}
#endregion DeviceClient
#region MediaClient
public bool IsMediaContected()
{
var r = IsDeviceContected();
return r;
}
#endregion MediaClient
#region PTZClient
public bool IsPTZContected()
{
var r = IsDeviceContected() && (PTZ != null) && !string.IsNullOrEmpty(profileToken);
return r;
}
/// <summary>
/// 绝对移动
/// </summary>
/// <param name="pan"></param>
/// <param name="tilt"></param>
/// <param name="zoom"></param>
/// <returns></returns>
public async Task AbsoluteMoveAsync(float pan, float tilt, float zoom)
{
await PTZ.AbsoluteMoveAsync(profileToken, new PTZVector
{
PanTilt = new Vector2D
{
x = pan,
y = tilt
},
Zoom = new Vector1D
{
x = zoom
}
}, null);
}
/// <summary>
/// 相对移动
/// </summary>
/// <param name="pan"></param>
/// <param name="tilt"></param>
/// <param name="zoom"></param>
public async Task RelativeMoveAsync(float pan, float tilt, float zoom)
{
await PTZ.RelativeMoveAsync(profileToken, new PTZVector
{
PanTilt = new Vector2D
{
x = pan,
y = tilt
},
Zoom = new Vector1D
{
x = zoom
}
}, null);
}
/// <summary>
/// 持续移动
/// </summary>
/// <param name="pan">0~100</param>
/// <param name="tilt">0~100</param>
/// <param name="zoom">0~100</param>
public async Task ContinuousMoveAsync(float pan, float tilt, float zoom)
{
await PTZ.ContinuousMoveAsync(profileToken, new PTZSpeed
{
PanTilt = new Vector2D
{
x = pan,
y = tilt
},
Zoom = new Vector1D
{
x = zoom
}
}, null);
}
public async Task StopMoveAsync()
{
await PTZ.StopAsync(profileToken, true, true);
}
public async Task<PTZStatus> GetStatusAsync()
{
return await PTZ.GetStatusAsync(profileToken);
}
#endregion PTZClient
#region ImagingClient
public bool IsImagingContected()
{
var r = IsDeviceContected();
return r;
}
#endregion ImagingClient
}
}