2017-02-24 142 views
1

沒有人有使用WCF的Onvif相機發現的簡單示例嗎? 還是其他任何使用WCF向Onvif標準的相機發送命令的例子? 我知道Onvif DM,Onvif設備測試工具和Onvif編程指南。 但不知道如何實施。WCF和Onvif簡單示例

謝謝

+1

嗨,也許是太晚了,你已經找到了你的解決方案,但我有一個簡單的C#代碼來查找網絡上的ONVIF設備,這是非常簡單的。我可以爲您提供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); 

請,我沒有任何設備的手,所以A不能測試,但是這個代碼必須工作。任何建議或錯誤,評論。

+0

非常感謝您回覆並分享代碼!我仍然試圖用這段代碼發現我的Onvif相機。當我使用代碼時,它似乎根本不搜索網絡。所以,我會繼續尋找一種基於您的代碼進行溝通的方式。 –

+0

@BorisP可能需要增加findCriteria的持續時間(findCriteria.Duration = new TimeSpan(100000000);),並確保設備可被發現,您需要爲此配置設備。 –

+0

再次嗨。我最近搞清楚了,我們的網絡管理員以某種方式禁用WS發現> :(你的解決方案有效!非常感謝!:)但是,我仍然無法找到我的相機。 Onvif設備管理器可以找到包括我在內的所有網絡攝像機,但您的解決方案只能找到三個。該死的;)我會修改你的代碼作爲答案,但如果你有任何建議,我會感激:) –

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