2017-02-24 19 views
1

WCFを使用したOnvifカメラの検出の簡単な例は誰にもありますか? また、WCFを使用してOnvif規格を使用してカメラにコマンドを送信する他の例はありますか? 私はOnvif DM、OnvifデバイステストツールとOnvifプログラミングガイドを知っています。 しかし、実装方法はわかりません。 WCFとOnvifの単純な例

あなたは

+1

こんにちは、多分手遅れであり、あなたはすでにあなたの解決策を見つけたが、私は、ネットワーク上のONVIFデバイスを見つけることができるシンプルなC#のコードを持っている、それは非常に簡単です。私はあなたにWCFのソリューションを提供することができますが、私はいくつかの時間が必要です。 –

+0

ねえ、遅すぎることはありません。私はあなたの助けを感謝します:)))それは良い出発点になる可能性があります:)確かに、あなたの時間を取る –

答えて

1

System.ServiceModelSystem.ServiceModel.Discoveryアセンブリを追加します。ありがとうございました。 これはコードです:

// perform the onvif search (this is the MAIN) 
    public static IEnumerable<EndpointDiscoveryMetadata> SearchOnvifDevices() 
    { 
     // object used to define the search criteria to find onvif device on the network 
     var findCriteria = new FindCriteria(); 

     // what device type to find? this is required 
     var contractTypeName = "NetworkVideoTransmitter"; // the other possible value is 'Device' 
     var contractTypeNamespace = "http://www.onvif.org/ver10/network/wsdl"; 
     // those parametes are defined by onvif standard 
     findCriteria.ContractTypeNames.Add(new XmlQualifiedName(contractTypeName, contractTypeNamespace)); 

     // you can canfigure the search with TimeOut, MaxResults, Scope, DeviceType, MaxResponseDelay, TransportSettings.TimeToLive, ... 
     findCriteria.MaxResults = 100; 
     findCriteria.Duration = new TimeSpan(10000); 
     // ... 

     //// you can specify scopes to restrict the search 
     //SetScopes(findCriteria, new[] { "onvif://www.onvif.org/type/ptz" }); 


     // object used to search the devices using the FindCriteria. 
     var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint(DiscoveryVersion.WSDiscoveryApril2005)); 

     // search 
     var findResponse = discoveryClient.Find(findCriteria); 
     return findResponse.Endpoints; 
    } 

    // set scopes to find devices that only match the specifieds scopes 
    public static void SetScopes(FindCriteria findCriteria, IEnumerable<string> scopes) 
    { 
     findCriteria.ScopeMatchBy = FindCriteria.ScopeMatchByExact; 
     if (scopes != null) 
     { 
      foreach (var item in scopes) 
       findCriteria.Scopes.Add(new Uri(item)); 
     } 


     // for example: 
     // a device can have set the following scopes: 
     // onvif://www.onvif.org/type/ptz 
     // onvif://www.onvif.org/hardware/D1-566 
     // onvif://www.onvif.org/location/country/china 
     // onvif://www.onvif.org/location/city/bejing 
     // onvif://www.onvif.org/name/ARV-453 

     // then if you perform the search with the scope 'onvif://www.onvif.org/location/country/china' it will resolve the device 
     // but if the search include the scope 'onvif://www.onvif.org/hardware/D1' then it will not. 
    } 

    // you can use this method to get parse endpoints to device 
    public static IEnumerable<Device> GetDevices(IEnumerable<EndpointDiscoveryMetadata> endpoints) 
    { 
     var result = new List<Device>(); 

     var id = 0; 
     foreach (var endpoint in endpoints) 
     { 
      foreach (var listenUri in endpoint.ListenUris) 
      { 
       var newDevice = new Device 
       { 
        Id = id, 
        ListenUri = listenUri, 
        EndpointDiscoveryMetadata = endpoint 
       }; 
       result.Add(newDevice); 
       id++; 
      } 
     } 

     return result; 
    } 

    // class used to identify a Device 
    public class Device 
    { 
     // id to identify the device 
     public int Id; 

     // uri where the device is listening 
     public Uri ListenUri; 

     // endpoint where the device was founded 
     public EndpointDiscoveryMetadata EndpointDiscoveryMetadata; 
    } 

、あなたはこのようにそれを使用することができます:

// discover endpoints 
    var endpoints = SearchOnvifDevices(); 

    // if you want, parse to devices 
    var devices = GetDevices(endpoints); 

してください、私は、手に任意のデバイスを持っていないため、テスタはできませんが、このコード働かなければならない。任意の提案またはエラー、コメント。

+0

応答とコードを共有するための大きな感謝!!私はまだこのコードでOnvifカメラを発見しようとしています。コードを使用すると、ネットワークをまったく検索しないようです。だから、あなたのコードに基づいてコミュニケーションをとる方法を探し続けます。 –

+0

@BorisP findCriteriaの期間(findCriteria.Duration = new TimeSpan(100000000);)を増やし、デバイスが検出可能であることを確認し、そのデバイスを設定する必要があるかもしれません。 –

+0

こんにちは。私は最近、私たちのネットワーク管理者がWSの検出を何とか無効にしていることを知っています> :(あなたの解決策は大変ありがとう!:)しかし、私のカメラはまだ見つかりません。 Onvif Device Managerは私のものを含めてネットワーク上のすべてのカメラを見つけることができますが、あなたのソリューションは4つのうち3つしか見つけません。私は答えとしてあなたのコードの変更を投稿しますが、あなたは私が感謝する任意の提案がある場合:) –

0

変更

#region SearchOnvifDev-Proba1 
    private static void SearchOnvifDev() 
    { 
     ServicePointManager.Expect100Continue = false; 

     var contractTypeName = "Device"; // the other possible value is 'Device' or 'NetworkVideoTransmitter' 
     var contractTypeNamespace = "http://www.onvif.org/ver10/network/wsdl"; 

     var endPoint = new UdpDiscoveryEndpoint(DiscoveryVersion.WSDiscoveryApril2005); 

     var discoveryClient = new DiscoveryClient(endPoint); 

     FindCriteria findCriteria = new FindCriteria(); 
     //visak ispod 
     findCriteria.ContractTypeNames.Add(new XmlQualifiedName(contractTypeName, contractTypeNamespace)); 
     findCriteria.Duration = TimeSpan.MaxValue; 
     findCriteria.MaxResults = 1000000; 
     discoveryClient.FindAsync(findCriteria); 
     //events 
     discoveryClient.FindProgressChanged += discoveryClient_FindProgressChanged; 
     discoveryClient.FindCompleted += discoveryClient_FindCompleted; 

     Console.ReadKey(); 

    } 
    #endregion 

    #region endEvent for discovery client 
    static void discoveryClient_FindCompleted(object sender, FindCompletedEventArgs e) 
    { 
     Console.WriteLine("the end"); 
    } 
    #endregion 

    #region changeEvent for discovery client 
    static void discoveryClient_FindProgressChanged(object sender, FindProgressChangedEventArgs e) 
    { 
     var lines = "\r\n--------" + DateTime.Now.ToString() + "\r\n" + e.EndpointDiscoveryMetadata.Address.Uri.AbsoluteUri.ToString(); 
     Console.WriteLine("\r\n--------" + DateTime.Now.ToString() + "\r\n" + e.EndpointDiscoveryMetadata.Address.Uri.AbsoluteUri.ToString()); 

     using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"info.txt", true)) 
     { 
      file.WriteLine(lines); 
     } 

     foreach (var item in e.EndpointDiscoveryMetadata.ListenUris) 
     { 
      string uri = item.OriginalString; 

      Console.WriteLine(uri.ToString()); 

      //this camera isnt found :(
      if (uri.Contains("http://192.168.4.230/")) 
      { 
       Console.WriteLine("BINGO"); 
      } 
     } 
    } 
    #endregion