ONVIF相机没有抛出事件

时间:2015-04-21 08:19:42

标签: c# events onvif

我有几个符合ONVIF标准的相机和编码器,我想从中接收事件,例如动作报警。

到目前为止,我设法订阅(我认为)事件wsdl但没有事件被抛出。我检查了设备中的设置以确保它使用运动检测,当运动存在时我可以听到继电器翻转,因此设置正确。

我使用this question作为我尝试的参考,但由于它没有得到一个接受的芒果,我会再次问它。

这是我如何设置我的wsdl文件:

ServicePointManager.Expect100Continue = false;
EndpointAddress endPointAddress = new EndpointAddress("http://" + CameraInformation.IpAddress + "/onvif/device_service");
HttpTransportBindingElement httpTransportBinding = new HttpTransportBindingElement { AuthenticationScheme = AuthenticationSchemes.Digest };
httpTransportBinding.KeepAliveEnabled = true;
TextMessageEncodingBindingElement textMessageEncodingBinding = new TextMessageEncodingBindingElement { MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10) };
PasswordDigestBehavior passwordDigestBehavior = new PasswordDigestBehavior(CameraInformation.Username, CameraInformation.Password);

CustomBinding customBinding = new CustomBinding(textMessageEncodingBinding, httpTransportBinding);
customBinding.SendTimeout = new TimeSpan(0, 0, 10);

以下是我如何初始化我的消费者服务:

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

NotificationConsumerService类:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, AddressFilterMode = AddressFilterMode.Any)]
public class NotificationConsumerService : OnvifEvents.NotificationConsumer
{
      public event EventHandler<EventArgs<OnvifEvents.Notify1>> NewNotification;

      public void Notify(OnvifEvents.Notify1 request)
      {
          var threadSafeEventHandler = NewNotification;
          if (threadSafeEventHandler != null)
              threadSafeEventHandler.Invoke(this, new EventArgs<OnvifEvents.Notify1>(request));
      }

      public Task NotifyAsync(System21.OnvifEvents.Notify1 request)
      {
         return new Task(() =>
            {
                 //return something?
            });
     }
}

     public class EventArgs<T> : EventArgs
     {
        public EventArgs(T data)
        {
            Data = data;
        }
        public T Data { get; set; }
     }

加载通知生成器客户端:

    var serviceAddress = new EndpointAddress(Capabilities.Events.XAddr.ToString());
    if (!string.IsNullOrWhiteSpace(CameraInformation.Username))
    {
         NotificationProducerClient.ClientCredentials.UserName.UserName = CameraInformation.Username;
         NotificationProducerClient.ClientCredentials.UserName.Password = CameraInformation.Password;
    }

最后添加事件订阅:

if (Capabilities.Events == null)
      throw new ApplicationException("The streamer info does not support event");
try
{
       if (NotificationProducerClient == null)
            LoadNotificationProducerClient();

       var subScribe = new OnvifEvents.Subscribe()
       {
            ConsumerReference = new OnvifEvents.EndpointReferenceType
            {
                  Address = new OnvifEvents.AttributedURIType { Value = _notificationConsumerServiceHost.BaseAddresses.First().ToString() },
            }
       };
       if (!string.IsNullOrWhiteSpace(initialTerminationTime))
            subScribe.InitialTerminationTime = initialTerminationTime;

       OnvifEvents.SubscribeResponse response = NotificationProducerClient.Subscribe(subScribe);
}

Catch (FaultException ex)
{
    Console.Write(ex.ToString());
}
catch (Exception ex)
{
    Console.Write(ex.ToString());
}

相关问题的一个可能的问题是,当AddressingVersion被设置为WSAddressing10时,它被设置为None但我之前遇到过这个问题,所以它不是解决这个问题的方法。

2 个答案:

答案 0 :(得分:0)

我认为继电器翻转你感觉是关于IR模式而不是运动检测。试着在ODM打开你的相机。它是ONVIF发现和事件订阅的开源实现。

答案 1 :(得分:0)

不确定这是否相关,但只是通过查看此代码,不应该调用

_notificationConsumerServiceHost = new ServiceHost(_notificationConsumerService, new Uri("http://localhost:8085/NotificationConsumerService"));

类似于:

_notificationConsumerServiceHost = new ServiceHost(_notificationConsumerService, new Uri("http://<RemoteIP>:8085/NotificationConsumerService"));

即。 “localhost”可能无法解析摄像头上的主机,在这种情况下,呼叫请求会丢失。