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; } /// /// 绝对移动 /// /// /// /// /// 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); } /// /// 相对移动 /// /// /// /// 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); } /// /// 持续移动 /// /// 0~100 /// 0~100 /// 0~100 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 GetStatusAsync() { return await PTZ.GetStatusAsync(profileToken); } #endregion PTZClient #region ImagingClient public bool IsImagingContected() { var r = IsDeviceContected(); return r; } #endregion ImagingClient } }