using EC.Onvif.Common; using EC.Onvif.Device; using EC.Onvif.Imaging; using EC.Onvif.Media; using EC.Onvif.PTZ; using System; using Capabilities = EC.Onvif.Common.Capabilities; namespace EC.Onvif { public class OnvifClient { private string Hostname { get; set; } private string Username { get; set; } private string Password { get; set; } 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; } public static float atomDist { get; set; } = 0.01f; public static float atomSpeed { get; set; } = 0.1f; public OnvifClient(string hostname, string username, string password) { Hostname = hostname; Username = username; Password = password; InitAsync(); } public async void InitAsync() { try { device = await OnvifClientFactory.CreateDeviceClientAsync(Hostname, Username, Password); } catch (Exception e) { Console.WriteLine($"Exception Message : {e.Message}"); return; } 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; Console.WriteLine("Capabilities"); Console.WriteLine("\tDevice: " + caps.Device.XAddr); Console.WriteLine("\tEvents: " + caps.Events.XAddr); Console.WriteLine("\tImaging: " + caps.Imaging.XAddr); Console.WriteLine("\tMedia: " + caps.Media.XAddr); Console.WriteLine("\tPTZ: " + caps.PTZ.XAddr); } #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 void AbsoluteMoveAsync(float ptx, float pty, float zx) { if (!IsPTZContected()) return; ptz.AbsoluteMoveAsync(profileToken, new PTZVector { PanTilt = new Vector2D { x = ptx, y = pty }, Zoom = new Vector1D { x = zx } }, null); //new PTZSpeed //{ // PanTilt = new Vector2D // { // x = 0.1f, // y = 0.1f // }, // Zoom = new Vector1D // { // x = 0.1f // } //} } public void RelativeMoveAsync(float ptx, float pty, float zx) { if (!IsPTZContected()) return; ptz.RelativeMoveAsync(profileToken, new PTZVector { PanTilt = new Vector2D { x = ptx, y = pty }, Zoom = new Vector1D { x = zx } }, null); } /// /// /// /// 0~100 /// 0~100 /// 0~100 public void ContinuousMoveAsync(float ptx, float pty, float zx) { if (!IsPTZContected()) return; ptz.ContinuousMoveAsync(profileToken, new PTZSpeed { PanTilt = new Vector2D { x = ptx, y = pty }, Zoom = new Vector1D { x = zx } }, null); } public void StopAsync() { if (!IsPTZContected()) return; ptz.StopAsync(profileToken, true, true); } public PTZStatus GetStatusAsync() { if (!IsPTZContected()) return null; var ptz_status = ptz.GetStatusAsync(profileToken).Result; return ptz_status; } #endregion PTZClient #region ImagingClient public bool IsImagingContected() { var r = IsDeviceContected(); return r; } #endregion ImagingClient } }