Exemplo n.º 1
0
 protected override void UpdateAddress()
 {
     if (_client != null)
     {
         string address = SMC.SmcData.Context.Instance.General.EventsServiceAddress;
         if (address != _client.Endpoint.Address.Uri.AbsoluteUri)
         {
             _client = null;
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Creates NotificationProducer client (using endpoint address got via GetCapabiliites)
        /// </summary>
        /// <returns></returns>
        NotificationProducerClient CreateNotificationProducerClient()
        {
            EnsureServiceAddressNotEmpty();

            Binding binding = CreateEventServiceBinding(_eventServiceAddress);

            _notificationProducerClient = new NotificationProducerClient(binding, new EndpointAddress(_eventServiceAddress));

            System.Net.ServicePointManager.Expect100Continue = false;
            AddSecurityBehaviour(_notificationProducerClient.Endpoint);

            SetupTimeout(_notificationProducerClient.InnerChannel);

            return(_notificationProducerClient);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Subscribes to all events provided by the device and directs to :8080/subscription-1 Http listener
        /// </summary>
        /// <param name="uri">XAddr URI for Onvif events</param>
        /// <param name="username">User</param>
        /// <param name="password">Password</param>
        public void Subscribe(string uri, double deviceTimeOffset, string username, string password)
        {
            EventPortTypeClient eptc = OnvifServices.GetEventClient(uri, deviceTimeOffset, username, password);

            string localIP = GetLocalIp();

            // Producer client
            NotificationProducerClient npc = OnvifServices.GetNotificationProducerClient(uri, deviceTimeOffset, username, password); // ip, port, username, password);

            npc.Endpoint.Address = eptc.Endpoint.Address;

            Subscribe s = new Subscribe();
            // Consumer reference tells the device where to Post messages back to (the client)
            EndpointReferenceType clientEndpoint = new EndpointReferenceType()
            {
                Address = new AttributedURIType()
                {
                    Value = string.Format("http://{0}:8080/subscription-1", localIP)
                }
            };

            s.ConsumerReference      = clientEndpoint;
            s.InitialTerminationTime = "PT60S";

            try
            {
                SubscribeResponse sr = npc.Subscribe(s);

                // Store the subscription URI for use in Renew
                SubRenewUri = sr.SubscriptionReference.Address.Value;

                // Start timer to periodically check if a Renew request needs to be issued
                // Use PC time for timer in case camera time doesn't match PC time
                // This works fine because the renew command issues a relative time (i.e. PT60S) so PC/Camera mismatch doesn't matter
                SubTermTime = System.DateTime.UtcNow.AddSeconds(50); // sr.TerminationTime;
                SubRenewTimer.Start();
                SubRenewTimer.Interval = 1000;
                SubRenewTimer.Elapsed += SubRenewTimer_Elapsed;

                OnNotification(string.Format("Initial Termination Time: {0} (Current Time: {1})", SubTermTime, System.DateTime.UtcNow));

                SubscriptionManagerClient = OnvifServices.GetSubscriptionManagerClient(SubRenewUri, deviceTimeOffset, username, password);
            }
            catch (Exception e)
            {
                OnNotification(string.Format("{0} Unable to subscribe to events on device [{1}]", System.DateTime.UtcNow, uri)); // ip));
            }
        }
Exemplo n.º 4
0
        public static NotificationProducerClient GetNotificationProducerClient(string ip, int port)
        {
            EndpointAddress serviceAddress = new EndpointAddress(string.Format("http://{0}:{1}/onvif/event_service", ip, port));

            HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();

            httpBinding.AuthenticationScheme = AuthenticationSchemes.Digest;

            var messageElement = new TextMessageEncodingBindingElement();

            messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
            CustomBinding bind = new CustomBinding(messageElement, httpBinding);

            NotificationProducerClient client = new NotificationProducerClient(bind, serviceAddress);

            return(client);
        }
Exemplo n.º 5
0
        public void Subscribe(string ip, int port)
        {
            EventPortTypeClient eptc = OnvifServices.GetEventClient(ip, port);

            string localIP = GetLocalIp(); // "172.16.5.111";

            // Producer client
            NotificationProducerClient npc = OnvifServices.GetNotificationProducerClient(ip, port);

            npc.Endpoint.Address = eptc.Endpoint.Address;

            Subscribe s = new Subscribe();
            // Consumer reference tells the device where to Post messages back to (the client)
            EndpointReferenceType clientEndpoint = new EndpointReferenceType()
            {
                Address = new AttributedURIType()
                {
                    Value = string.Format("http://{0}:8080/subscription-1", localIP)
                }
            };

            s.ConsumerReference      = clientEndpoint;
            s.InitialTerminationTime = "PT60S";

            SubscribeResponse sr = npc.Subscribe(s);

            // Store the subscription URI for use in Renew
            SubRenewUri = sr.SubscriptionReference.Address.Value;

            // Start timer to periodically check if a Renew request needs to be issued
            // Use PC time for timer in case camera time doesn't match PC time
            // This works fine because the renew command issues a relative time (i.e. PT60S) so PC/Camera mismatch doesn't matter
            SubTermTime = DateTime.UtcNow.AddSeconds(50); // sr.TerminationTime;
            SubRenewTimer.Start();
            SubRenewTimer.Interval = 1000;
            SubRenewTimer.Tick    += SubRenewTimer_Tick;

            listBox1.Items.Add(string.Format("Initial Termination Time: {0} (Current Time: {1})", SubTermTime, DateTime.UtcNow));

            SubscriptionManagerClient = OnvifServices.GetSubscriptionManagerClient(SubRenewUri); // oAux1.Address.Value);
        }
Exemplo n.º 6
0
        public static NotificationProducerClient GetNotificationProducerClient(string uri, double deviceTimeOffset, string username = "", string password = "")
        {
            EndpointAddress serviceAddress = new EndpointAddress(uri);

            HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();

            httpBinding.AuthenticationScheme = AuthenticationSchemes.Digest;

            var messageElement = new TextMessageEncodingBindingElement();

            messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10);
            CustomBinding bind = new CustomBinding(messageElement, httpBinding);

            NotificationProducerClient client = new NotificationProducerClient(bind, serviceAddress);

            if (username != string.Empty)
            {
                // Handles adding of SOAP Security header containing User Token (user, nonce, pwd digest)
                PasswordDigestBehavior behavior = new PasswordDigestBehavior(username, password, deviceTimeOffset);
                client.Endpoint.Behaviors.Add(behavior);
            }

            return(client);
        }
Exemplo n.º 7
0
        public override async Task <bool> InitalizeAsync()
        {
            if (this.m_onVifDevice.ServicesResponse == null)
            {
                bool b = await this.m_onVifDevice.InitalizeDeviceAsync();

                if (this.m_onVifDevice.ServicesResponse == null)
                {
                    return(false);
                }
            }

            try
            {
                foreach (var service in this.m_onVifDevice.ServicesResponse.Service)
                {
                    if (service.Namespace == "http://www.onvif.org/ver10/events/wsdl")
                    {
                        string serviceAdress = service.XAddr;

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



                        /*
                         * this.m_EventPortTypeClient = new EventPortTypeClient(binding, new EndpointAddress($"http://{ this.m_onVifDevice.Hostname}/onvif/event_service"));
                         * this.m_EventPortTypeClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetPasswordDigestBehavior);
                         * CreatePullPointSubscriptionRequest request = new CreatePullPointSubscriptionRequest();
                         * request.InitialTerminationTime = "PT60S";
                         * request.SubscriptionPolicy = new CreatePullPointSubscriptionSubscriptionPolicy();
                         *
                         *
                         * m_PullPointResponse = await this.m_EventPortTypeClient.CreatePullPointSubscriptionAsync(request);
                         *
                         *
                         *
                         *
                         * PullMessagesRequest message = new PullMessagesRequest("PT1S", 1024, null);
                         *
                         * await m_PullPointSubscriptionClient.PullMessagesAsync(message);
                         *
                         * GetEventPropertiesRequest eventrequest = new GetEventPropertiesRequest();
                         * m_EventPropertiesResponse =await this.m_EventPortTypeClient.GetEventPropertiesAsync(eventrequest);
                         */
                        OnvifDevice.Capabilities response = await this.m_onVifDevice.GetDeviceCapabilitiesAsync();



                        NotificationConsumerClient consclient = new NotificationConsumerClient(binding, new EndpointAddress(serviceAdress));

                        consclient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetPasswordDigestBehavior);
                        consclient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetBasicBasicAuthBehaviour);

                        OnvifEvent10.Notify request = new OnvifEvent10.Notify();



                        //  await consclient.NotifyAsync(request);



                        //     await consclient.OpenAsync();

                        NotificationConsumerService _notificationConsumerService = new NotificationConsumerService();
                        //        _notificationConsumerService.NewNotification += _notificationConsumerService_NewNotification;
                        //       ServiceHost _notificationConsumerServiceHost = new ServiceHost(_notificationConsumerService, new Uri("http://localhost:8085/NotificationConsumerService"));
                        //       _notificationConsumerServiceHost.Open();


                        NotificationProducerClient client = new NotificationProducerClient(binding, new EndpointAddress(serviceAdress));
                        client.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetPasswordDigestBehavior);
                        client.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetBasicBasicAuthBehaviour);

                        var subScribe = new Subscribe()
                        {
                            ConsumerReference = new EndpointReferenceType
                            {
                                Address = new AttributedURIType {
                                    Value = consclient.Endpoint.Address.Uri.ToString()
                                },
                            }
                        };


                        subScribe.InitialTerminationTime = "PT2H";
                        SubscribeResponse1 resp = await client.SubscribeAsync(subScribe);



                        if ((response.Events != null) && response.Events.WSSubscriptionPolicySupport)
                        {
                            m_PullPointSubscriptionClient = new PullPointSubscriptionClient(binding, new EndpointAddress(serviceAdress));
                            m_SubscriptionManagerClient   = new SubscriptionManagerClient(binding, new EndpointAddress(serviceAdress));

                            m_PullPointSubscriptionClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetPasswordDigestBehavior);
                            m_PullPointSubscriptionClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetBasicBasicAuthBehaviour);
                            m_SubscriptionManagerClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetPasswordDigestBehavior);
                            m_SubscriptionManagerClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetBasicBasicAuthBehaviour);
                            return(true);
                        }
                        else
                        {
                            //    throw new DeviceEventReceiverException("Device doesn't support pull point subscription");
                        }


                        if ((response.Events != null) && response.Events.WSPullPointSupport)
                        {
                            m_PullPointSubscriptionClient = new PullPointSubscriptionClient(binding, new EndpointAddress(serviceAdress));
                            m_SubscriptionManagerClient   = new SubscriptionManagerClient(binding, new EndpointAddress(serviceAdress));

                            m_PullPointSubscriptionClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetPasswordDigestBehavior);
                            m_PullPointSubscriptionClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetBasicBasicAuthBehaviour);
                            m_SubscriptionManagerClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetPasswordDigestBehavior);
                            m_SubscriptionManagerClient.Endpoint.EndpointBehaviors.Add(this.m_onVifDevice.GetBasicBasicAuthBehaviour);
                            return(true);
                        }
                        else
                        {
                            throw new DeviceEventReceiverException("Device doesn't support pull point subscription");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                m_ErrorMessage = ex.Message;
                return(false);
            }

            return(m_initialised);
        }