public async Task ConnectAsync(CancellationToken cancellationToken)
        {
            DateTime deviceTime = await GetDeviceTimeAsync();

            if (!_connectionParameters.Credentials.IsEmpty())
            {
                byte[] nonceBytes = new byte[20];
                var    random     = new Random();
                random.NextBytes(nonceBytes);

                var token = new SecurityToken(deviceTime, nonceBytes);

                _onvifClientFactory.SetSecurityToken(token);
            }

            cancellationToken.ThrowIfCancellationRequested();

            _deviceCapabilities = await GetDeviceCapabilitiesAsync();

            if (!_deviceCapabilities.Events.WSPullPointSupport)
            {
                throw new DeviceEventReceiverException("Device doesn't support pull point subscription");
            }
        }
示例#2
0
        private void Connect()
        {
            if (Initialized)
            {
                return;
            }
            try
            {
                DateTime deviceTime = GetDeviceTime();
                if (!_connectionParameters.Credentials.IsEmpty())
                {
                    byte[] nonceBytes = new byte[20];
                    var    random     = new Random();
                    random.NextBytes(nonceBytes);

                    var token = new SecurityToken(deviceTime, nonceBytes);

                    _onvifClientFactory.SetSecurityToken(token);
                }

                _deviceCapabilities = GetDeviceCapabilities();
                if (_deviceCapabilities?.Media?.XAddr == null)
                {
                    throw new ApplicationException("No media endpoints found");
                }

                var mediaUri    = new Uri(_deviceCapabilities.Media.XAddr);
                var ep          = new EndpointAddress(GetServiceUri(mediaUri.PathAndQuery));
                var mediaClient = _onvifClientFactory.CreateClient <Media>(ep, _connectionParameters, MessageVersion.Soap12, _timeout);

                var profiles = mediaClient.GetProfiles(new GetProfilesRequest()).Profiles.ToList();

                var streamSetup = new StreamSetup
                {
                    Stream    = StreamType.RTPUnicast,
                    Transport = new Transport {
                        Protocol = TransportProtocol.RTSP
                    }
                };
                List <MediaEndpoint> uris = new List <MediaEndpoint>();

                for (var i = 0; i < profiles.Count(); i++)
                {
                    var      p = profiles[i];
                    MediaUri l;
                    try
                    {
                        l = mediaClient.GetStreamUri(streamSetup, p.token);
                    }
                    catch (Exception ex)
                    {
                        profiles.Remove(p);
                        i--;
                        Logger.LogException(ex);
                        continue;
                    }

                    //make sure using correct ip address (for external access)
                    var u = new UriBuilder(l.Uri)
                    {
                        Host = ServiceUri.Host
                    };
                    if (_rtspPort > 0)
                    {
                        u.Port = _rtspPort;
                    }

                    l.Uri = u.ToString();
                    if (!string.IsNullOrEmpty(_credential.UserName))
                    {
                        l.Uri = l.Uri.ReplaceFirst("://",
                                                   "://" + Uri.EscapeDataString(_credential.UserName) + ":" +
                                                   Uri.EscapeDataString(_credential.Password) + "@");
                    }

                    var s = p.VideoEncoderConfiguration;
                    if (s != null)
                    {
                        uris.Add(new MediaEndpoint(l, s));
                    }
                    else
                    {
                        var e = p.VideoSourceConfiguration;
                        if (e != null)
                        {
                            uris.Add(new MediaEndpoint(l, e));
                        }
                    }
                }

                _mediaEndpoints = uris.ToArray();
                _profiles       = profiles.ToArray();

                try
                {
                    if (_deviceCapabilities.PTZ != null)
                    {
                        var ptzUri = new Uri(_deviceCapabilities.PTZ.XAddr);
                        ep  = new EndpointAddress(GetServiceUri(ptzUri.PathAndQuery));
                        PTZ = _onvifClientFactory.CreateClient <PTZ>(ep, _connectionParameters, MessageVersion.Soap12,
                                                                     _timeout);

                        //try
                        //{
                        //    var cfgptz = PTZ.GetCompatibleConfigurations(new GetCompatibleConfigurationsRequest());
                        //    if (cfgptz.PTZConfiguration.Length > 0)
                        //    {
                        //        var ptzc = cfgptz.PTZConfiguration[0];

                        //        DefaultPTSpeed = ptzc.DefaultPTZSpeed.PanTilt;
                        //        DefaultZSpeed = ptzc.DefaultPTZSpeed.Zoom;

                        //    }
                        //}
                        //catch (Exception ex)
                        //{
                        //    Logger.LogException(ex);
                        //}
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex, "Onvif PTZ");
                }

                Initialized = true;
            }
            catch (Exception ex)
            {
                _profiles       = new Profile[] { };
                _mediaEndpoints = new MediaEndpoint[] { };
                Logger.LogException(ex, "ONVIF Device");
            }
        }