我有几个符合 ONVIF 标准的摄像机和编码器,我想从中接收事件,例如运动警报。
到目前为止,我设法(我认为)订阅了事件 wsdl,但没有抛出任何事件。我检查了设备中的设置以确保它使用运动检测,并且当存在运动时我可以听到继电器翻转,因此设置正确。
我用这个问题作为我尝试的参考,但由于它没有得到接受的遮阳篷,我会再问一次。
以下是我设置 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());
}
来自相关问题的一个可能的 awnser 是 AddressingVersion 在应该设置为 WSAddressing10 时设置为 None 但我之前遇到过这个问题,所以它不是解决这个问题的方法。