Sie sind auf Seite 1von 4

ONVIF Workshop

Goal: Build your own ONVIF test client


Resources you need for the workshop can be found here:

http://www.onvif.org/ Network Share: \\storage03\n_ftp\pub_soft\adp\Lina\OnvifWorkshop

Task 1: Create code base from ONVIF WSDL files


Create a project folder OnvifTest on your computer. Download the desired WSDL files from ONVIF website and save them in a separate folder "OnvifInterface" in the project folder. In this workshop, you only need to use:

ONVIF Device Management Service WSDL, ver 1.4 ONVIF Schema, ver 1.4
Alternatively you can find them in the folder OnvifWorkshop/OnvifInterface. Create a subfolder WebServicesGenerationTools in the project folder. Copy tools "WsdlToClientCode.bat" and "SvcUtil.exe" from OnvifWorkshop/WebServicesGenerationTools to the newly created folder. Create a subfolder WebServicesGenerated in the project folder. This is where the code file will be generated. Run .bat file "WsdlToClientCode.bat" in cmd.exe to create code base for the desired web service based on the downloaded WSDL files. All the namespaces for WSDL files are defined in "WsdlToClientCode.bat", which then uses the tool "SvcUtil.exe" provided by Microsoft to create code base. The created file "WebServicesGenerated.cs" is located in OnvifTest/WebServicesGenerated. Ignore the errors.

Task 2: Get device information from the device


Step 1: Create a new Windows Form project OnvifTestClient in Microsoft Visual C# 2011 Express and save it in your project folder. Add WebSerivcesGenerated.cs to the project in Solution Explorer. Right click the project and choose Add reference -> .net -> System.Runtime.Serialization. This is used by WebServicesGenerated.cs. We also need to add the namespace WebServices to your Form1 class. Add this line to the very beginning of Form1.cs:
using WebServices;

Step 2: We will use UsernameToken as the authorization method when accessing ONVIF web services on the device. We have a library provided by Microsoft to do this. To get it ready to be used, copy the folder OnvifWorkshop/UsernameTokenLibrary to OnvifTestClient and add the project UsernameToken.csproj to the solution in Solution Explorer. Then add the reference to UsernameToken to the project OnvifTestClient, as well as the namespace by adding this line:
using Microsoft.ServiceModel.Samples.CustomToken;

Step 3: Now we can use UsernameToken to create a binding to the ONVIF web services. Right click the project and choose Add reference -> .net -> System.ServiceModel. The namespace System.ServiceModel.Channels should be added to the beginning of Form1.cs:
using System.ServiceModel.Channels;

Then add the following method to your Form1 class:


private static System.ServiceModel.Channels.Binding CreateBinding() { // NOTE: Since we use a non-secure transport here, the message will be visible to others. HttpTransportBindingElement httpTransport = new HttpTransportBindingElement(); // The transport security binding element will be configured to require a username token TransportSecurityBindingElement transportSecurity = new TransportSecurityBindingElement(); transportSecurity.EndpointSupportingTokenParameters.SignedEncrypted.Add( new UsernameTokenParameters()); // Here you can require secure transport, in which case you'd probably replace HTTP with HTTPS as well transportSecurity.AllowInsecureTransport = true; transportSecurity.IncludeTimestamp = false; TextMessageEncodingBindingElement me = new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8); return new CustomBinding(transportSecurity, me, httpTransport); }

Create a System.ServiceModel.Channels.Binding object binding by calling CreateBinding method in Form1():


private System.ServiceModel.Channels.Binding binding = CreateBinding();

Step 4: Now the binding is ready to use, we can create a client Webservices.Device to access an ONVIF device with it. The service address and ONVIF login details need to be specified when calling this method.
private static TChannel GetChannel<TChannel>( System.ServiceModel.Channels.Binding binding, string serviceAddressText, string serverUsername, string serverPassword) { EndpointAddress serviceAddress = new EndpointAddress(serviceAddressText); ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>(binding, serviceAddress);

//Configure the username credentials on the channel factory UsernameClientCredentials credentials = new UsernameClientCredentials(new UsernameInfo(serverUsername, serverPassword)); // replace ClientCredentials with UsernameClientCredentials channelFactory.Endpoint.Behaviors.Remove(typeof(ClientCredentials)); channelFactory.Endpoint.Behaviors.Add(credentials); return channelFactory.CreateChannel(); }

The following namespaces should be added to the beginning of Form1.cs:


using System.ServiceModel; using System.ServiceModel.Description;

Step 5: Now we are ready to use this client to send a GetDeviceInformation request to the device. Find out the standard ONVIF service entry point(serviceAddressText) from ONVIF specification. The IP address of your ONVIF device can be found in OnvifWorkshop/CameraList.txt. Information about GetDeviceInformation can be found in WebServicesGenerated.cs, as well as in the Device Management section in ONVIF specification. This function is provided by ONVIF Device Service. Note: The first time you access the device via ONVIF, no user credential is needed. You should always start with adding an administrator user via the device service. Then you can use this user to access all the services as well as adding other users. For now, you just send in empty user credentials or any to get yourself authorized. Create a button in Form1 to get device information. Also add a text box to show a confirmation message when this is successfully done. The UI would look like this:

Then in your button_click event, do something like this:


private void button1_Click(object sender, EventArgs e) { string model, firmwareVersion, serialNumber, hardwareId; deviceChannel = GetChannel<Device>(binding, serviceAddress, serverUsername, serverPassword);

deviceChannel.GetDeviceInformation(out model, out firmwareVersion, out serialNumber, out hardwareId); textBox1.Text = string.Format("Model: {0}\n Firmware Version: {1}\n Serial Number: {2}\n Hardware ID: {3}", model, firmwareVersion, serialNumber, hardwareId); }

Add the declaration of parameters to the beginning of Form1 class if you havent done so already:
private string serverUsername = ""; private string serverPassword = ""; private string serviceAddress = "http://<DeviceIP>/onvif/device_service";

Now run your codes.

Do you see the device information in your Form1? Welcome to the ONVIF world!

Das könnte Ihnen auch gefallen