private async Task PullPointAsync(EndpointAddress endPointAddress, CancellationToken cancellationToken)
        {
            var pullPointSubscriptionClient = _onvifClientFactory.CreateClient <PullPointSubscription>(endPointAddress, _connectionParameters,
                                                                                                       MessageVersion.Soap12WSAddressing10);
            var subscriptionManagerClient = _onvifClientFactory.CreateClient <SubscriptionManager>(endPointAddress, _connectionParameters,
                                                                                                   MessageVersion.Soap12WSAddressing10);

            var pullRequest = new PullMessagesRequest("PT1S", 1024, null);

            int renewIntervalMs   = (int)(_subscriptionTerminationTime.TotalMilliseconds / 2);
            int lastTimeRenewMade = Environment.TickCount;

            while (!cancellationToken.IsCancellationRequested)
            {
                PullMessagesResponse response = await pullPointSubscriptionClient.PullMessagesAsync(pullRequest);

                foreach (var messageHolder in response.NotificationMessage)
                {
                    if (messageHolder.Message == null)
                    {
                        continue;
                    }

                    var @event = new DeviceEvent(messageHolder.Message.InnerXml);
                    OnEventReceived(@event);
                }

                if (TimeUtil.IsTimeOver(lastTimeRenewMade, renewIntervalMs))
                {
                    lastTimeRenewMade = Environment.TickCount;
                    var renew = new Renew {
                        TerminationTime = GetTerminationTime()
                    };
                    await subscriptionManagerClient.RenewAsync(new RenewRequest(renew));
                }
            }

            await subscriptionManagerClient.UnsubscribeAsync(new UnsubscribeRequest(new Unsubscribe()));
        }
Beispiel #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");
            }
        }