public async Task <Tuple <string, GetDeviceInformationResponse> > GetDeviceInfoAsync(string deviceAddress, string userName, string password)
        {
            var messageElement = new TextMessageEncodingBindingElement
            {
                MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
            };
            HttpTransportBindingElement httpBinding = new HttpTransportBindingElement
            {
                //AuthenticationScheme = AuthenticationSchemes.Digest
            };
            CustomBinding bind = new CustomBinding(messageElement, httpBinding);

            //绑定服务地址
            EndpointAddress serviceAddress = new EndpointAddress(deviceAddress);
            DeviceClient    deviceClient   = new DeviceClient(bind, serviceAddress);

            //给每个请求都添加认证信息
            deviceClient.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior(userName, password));

            //查看系统时间
            var date = await deviceClient.GetSystemDateAndTimeAsync();

            Console.WriteLine(date.UTCDateTime.Date.Month.ToString());

            var deviceInfo = await deviceClient.GetDeviceInformationAsync(new GetDeviceInformationRequest()
            {
            });

            //查看设备能力
            GetCapabilitiesResponse cap = await deviceClient.GetCapabilitiesAsync(new CapabilityCategory[] { CapabilityCategory.All });

            return(Tuple.Create <string, GetDeviceInformationResponse>(cap.Capabilities.Media.XAddr.ToString(), deviceInfo));
        }
Ejemplo n.º 2
0
        public async Task GetDeviceInfoAsync(string deviceAddress)
        {
            var messageElement = new TextMessageEncodingBindingElement
            {
                MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
            };
            HttpTransportBindingElement httpBinding = new HttpTransportBindingElement
            {
                AuthenticationScheme = AuthenticationSchemes.Digest
            };
            CustomBinding bind = new CustomBinding(messageElement, httpBinding);

            //绑定服务地址
            EndpointAddress serviceAddress = new EndpointAddress(deviceAddress);
            DeviceClient    deviceClient   = new DeviceClient(bind, serviceAddress);

            //查看系统时间
            var date = await deviceClient.GetSystemDateAndTimeAsync();

            Console.WriteLine(date.UTCDateTime.Date.Month.ToString());
            //查看设备能力
            GetCapabilitiesResponse cap = await deviceClient.GetCapabilitiesAsync(new CapabilityCategory[] { CapabilityCategory.All });

            Console.WriteLine(cap.Capabilities.Media.XAddr.ToString());
        }
Ejemplo n.º 3
0
        static async Task <TimeSpan> GetDeviceTimeShift(DeviceClient device)
        {
            var utc = (await device.GetSystemDateAndTimeAsync()).UTCDateTime;
            var dt  = new System.DateTime(utc.Date.Year, utc.Date.Month, utc.Date.Day,
                                          utc.Time.Hour, utc.Time.Minute, utc.Time.Second);

            return(dt - System.DateTime.UtcNow);
        }
Ejemplo n.º 4
0
        public async Task <bool> GetServicesAsync()
        {
            try
            {
                if (m_services != null)
                {
                    return(true);
                }


                HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
                httpBinding.AuthenticationScheme = AuthenticationSchemes.Digest;
                var messageElement = new TextMessageEncodingBindingElement();
                messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
                CustomBinding binding = new CustomBinding(messageElement, httpBinding);

                EndpointAddress address = new EndpointAddress($@"http://{m_Hostname}/onvif/device_service");

                DeviceClient device = new DeviceClient(binding, address);

                bool   useAuth = !string.IsNullOrEmpty(m_BasicBasicAuthBehaviour.Username) && !string.IsNullOrEmpty(m_BasicBasicAuthBehaviour.Password);
                double diff    = 0;
                if (useAuth)
                {
                    device.Endpoint.EndpointBehaviors.Add(m_BasicBasicAuthBehaviour);
                }
                try
                {
                    device.Endpoint.EndpointBehaviors.Add(m_PasswordDigestBehavior);
                    //ensure date and time are in sync
                    //add basic auth for compat with some cameras
                    var sdt = await device.GetSystemDateAndTimeAsync();

                    var d = sdt.UTCDateTime.Date;
                    var t = sdt.UTCDateTime.Time;

                    var dt = new System.DateTime(d.Year, d.Month, d.Day, t.Hour, t.Minute, t.Second);

                    diff = (System.DateTime.UtcNow - dt).TotalSeconds;
                    m_PasswordDigestBehavior.TimeOffset = diff;

                    GetDeviceInformationRequest request = new GetDeviceInformationRequest();


                    m_deviceInformations = await device.GetDeviceInformationAsync(request);


                    m_HostnameInformation = await device.GetHostnameAsync();
                }
                catch (Exception ex)
                {
                    m_ErrorMessage = ex.Message;
                    return(false);
                }



                //   device.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(1);
                binding.SendTimeout = binding.CloseTimeout = binding.ReceiveTimeout = binding.OpenTimeout = TimeSpan.FromSeconds(5);

                m_services = await device.GetServicesAsync(true);



                foreach (var service in m_services.Service)
                {
                    if (service.Namespace == "http://www.onvif.org/ver10/media/wsdl")
                    {
                    }
                }
            }

            catch (Exception ex)
            {
                m_ErrorMessage = ex.Message;
                return(false);
            }
            return(true);
        }
Ejemplo n.º 5
0
        public async Task <bool> InitalizeDeviceAsync()
        {
            try
            {
                if (m_services != null)
                {
                    return(true);
                }


                HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
                httpBinding.AuthenticationScheme = AuthenticationSchemes.Digest;
                var messageElement = new TextMessageEncodingBindingElement();
                messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
                CustomBinding binding = new CustomBinding(messageElement, httpBinding);

                EndpointAddress address = new EndpointAddress($@"http://{m_Hostname}/onvif/device_service");

                binding.SendTimeout = binding.CloseTimeout = binding.ReceiveTimeout = binding.OpenTimeout = TimeSpan.FromSeconds(15);

                m_device = new DeviceClient(binding, address);
                //    var discoverymode = m_device.GetRemoteDiscoveryModeAsync();

                bool   useAuth = !string.IsNullOrEmpty(m_BasicBasicAuthBehaviour.Username) && !string.IsNullOrEmpty(m_BasicBasicAuthBehaviour.Password);
                double diff    = 0;


                if (useAuth)
                {
                    m_device.Endpoint.EndpointBehaviors.Add(m_BasicBasicAuthBehaviour);
                    m_device.Endpoint.EndpointBehaviors.Add(m_PasswordDigestBehavior);
                }

                //ensure date and time are in sync
                //add basic auth for compat with some cameras
                m_SystemDateTime = await m_device.GetSystemDateAndTimeAsync();


                var d = m_SystemDateTime.UTCDateTime.Date;
                var t = m_SystemDateTime.UTCDateTime.Time;

                var dt = new System.DateTime(d.Year, d.Month, d.Day, t.Hour, t.Minute, t.Second);

                diff = (System.DateTime.UtcNow - dt).TotalSeconds;
                m_PasswordDigestBehavior.TimeOffset = diff;

                GetDeviceInformationRequest request = new GetDeviceInformationRequest();
                m_deviceInformations = await m_device.GetDeviceInformationAsync(request);

                this.m_HostnameInformation = await m_device.GetHostnameAsync();

                m_services = await m_device.GetServicesAsync(true);
            }

            catch (Exception ex)
            {
                m_ErrorMessage = ex.Message;
                throw new OnVifException("OnVifDevice.InitalizeDeviceAsync", ex);
                //            return false;
            }

            return(m_services != null);
        }