java - 如何使用WS-Discovery规范在Java中发现网络上的ONVIF设备?

标签 java soap onvif ws-discovery

我正在尝试通过一些Java代码来发现ONVIF设备。具体来说,我正在尝试获取他们的设备服务地址(我相信这只是他们的IP地址?),如ONVIF Core Spec指出的(在第4.3节中):“成功的发现将提供设备服务地址。设备服务地址,它可以通过设备服务接收详细的设备信息...”。我的目标是最终获得网络上ONVIF设备的详细信息。总的来说,我也在寻找有关使用ONVIF规范的指南。

我仍然对Web服务世界(以及一般而言的网络)还是陌生的,所以请原谅我。但是,我本人为此付出了很多努力:我读了很多ONVIF Core SpecONVIF Application Programmer's GuideWS-Discovery Specification。如果可以的话,我将总结一下我所知道的,以便您可以告诉我我是否走在正确的轨道上:


“ Web服务”是使用平台和语言无关的Web服务标准(例如,通过IP网络的XML,SOAP和WSDL)的标准技术的名称。基本思想是我们希望能够从任何编程语言中调用有效的方法/函数(服务)。
Web服务通常托管在服务器上。但是在ONVIF用例中,Web服务提供商是ONVIF设备(例如IP摄像机)。因此,为了以任何语言与设备进行交互,我们可以使用Web服务操作/调用,因为Web服务调用可以以任何语言实现。
XML是数据描述语法(之所以使用,是因为它与语言无关;任何语言都可以解析它)。 SOAP是用于来回获取注入SOAP的XML文档的通信协议(基本上是进行我们的方法调用)。 WSDL用于描述服务(它是Web服务接口的基于XML的描述)。我已经下载了用于设备管理的WSDL here,并通过WSDL编译器wsimport(由JDK提供)从WSDL中生成了Java类,以便在我的代码中使用。但我知道调用这些方法将在设备发现之后进行,对吗?
根据WS-Discovery规范发现了ONVIF设备。您发送Probe消息,并且符合探针约束的设备会发回ProbeMatch消息,如第13和14页in ONVIF Application Programmers Guide所述。


这是我开始感到困惑的地方。我如何用Java发送此消息? 《 ONVIF应用程序程序员指南》在第15页上提供了一些伪代码,但我不知道如何实现它。该指南明确指出了该指南中的4.3.1节。我知道“作用域”和“类型”只是您可以嵌入探针中的约束,但不是必需的(根据WS discovery spec的第5页)。因为我想发现所有设备,所以我认为启动不需要任何限制,对吗?

因此,该指南还在第110页上提供了用于发现的示例SOAP消息。从中删除类型声明(因为我不想使用该约束),我知道要发送的SOAP消息将是(我相信吗?):

<?xml version="1.0" encoding="UTF-8"?>
<e:Envelope xmlns:e="http://www.w3.org/2003/05/soap-envelope"
 xmlns:w="http://schemas.xmlsoap.org/ws/2004/08/addressing"
 xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery"
 xmlns:dn="http://www.onvif.org/ver10/network/wsdl">
 <e:Header>
 <w:MessageID>uuid:84ede3de-7dec-11d0-c360-f01234567890</w:MessageID>
 <w:To e:mustUnderstand="true">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To>
 <w:Action
a:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/Pr
obe</w:Action>
 </e:Header>
 <e:Body>
 <d:Probe>
 </d:Probe>
 </e:Body>
</e:Envelope>   


而且我也了解WS-Discovery技术使用的239.255.255.259地址和UDP端口3702 ...但这就是我的意思。如何将SOAP消息发送到Java中的该地址和端口?我如何阅读响应(我想我会以注入SOAP的XML文档的形式返回ProbeMatch消息,因此我需要解析该XML以获得XAddrs,但不确定) 。我是否需要以某种方式将该SOAP消息的UDP广播发送到该地址?

TL; DR:我相信要进行ONVIF设备发现,我需要将上面的SOAP消息发送到UDP端口3702上的地址239.255.255.259。我不知道如何在Java中进行该操作,只是在寻找一些指导;我什至不确定自己是否在进行设备发现的正确道路上。

最佳答案

经过反复试验,我找到了另一个解决方案。在大多数情况下,mpromonet可能是可行的方式,我只是想避免使用像Apache这样的大型依赖项。我还认为,仅使用一些简单的UDP消息传递就应该可行。

该解决方案还基于SO用户Thomas的有用代码here。我基本上只是通过删除线程来简化他的代码,并添加了一些注释。同样,他的解决方案可能比我的解决方案(表现更好)更好。但是,我的初学者(例如我)可能更容易理解。

这是代码:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.*;

import javax.xml.namespace.QName;
import javax.xml.soap.*;
import java.util.*;

public class ONVIFDeviceDiscoveryFIN {

   // Following constants are related to Discovery process
   public static final int WS_DISCOVERY_TIMEOUT = 4000; // 4 seconds. Time to wait to receive a packet
   public static final int WS_DISCOVERY_PORT = 3702; 
   public static final String WS_DISCOVERY_ADDRESS_IPv4 = "239.255.255.250";

   // note that the probe below MUST be given a unique urn:uuid. Devices will NOT reply if the urn:uuid is not unique! 
   public static final String WS_DISCOVERY_PROBE_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
        "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tds=\"http://www.onvif.org/ver10/device/wsdl\" xmlns:tns=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">\r\n" + 
        "   <soap:Header>\r\n" + 
        "      <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action>\r\n" + 
        "      <wsa:MessageID>urn:uuid:5e1cec36-03b9-4d8b-9624-0c5283982a00</wsa:MessageID>\r\n" + 
        "      <wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>\r\n" + 
        "   </soap:Header>\r\n" + 
        "   <soap:Body>\r\n" + 
        "      <tns:Probe>\r\n" + 
        "         <tns:Types>tds:Device</tns:Types>\r\n" + // Constraint to find just ONVIF devices hopefully? Recall we are sending a probe on the 192.168.0.50 network; if we have no constraints, it would find everything there! WS-Discovery generally is for much more than ONVIF, like printers and stuff
        "      </tns:Probe>\r\n" + 
        "   </soap:Body>\r\n" + 
        "</soap:Envelope>";

   private static ArrayList<String> getResponsesToProbe(String uuid) throws IOException{
       // TODO: add in ability to send scope and type constraints
       // NOTE: We do need to know the address of the network interface to discover devices on...
       // Function composes and sends a Probe to discover devices on the network. uuid is the urn:uuid to put in the probe. Functions returns all the SOAP-Infused XML responses (all the ProbeMatches).

       // give the probe a unique urn:uuid (we must do this for each probe!). This is generated outside function
       final String probe = WS_DISCOVERY_PROBE_MESSAGE.replaceAll("<wsa:MessageID>urn:uuid:.*</wsa:MessageID>", "<wsa:MessageID>urn:uuid:" + uuid + "</wsa:MessageID>");

       // set up the "sender and receiver"; this is the socket that we send our probe from, and where we receive back the ProbeMatch responses.
       // NOTE:  that we do need to know the address of the network interface to discover devices on... (port could be anything)
       final int port = 55000;
       DatagramSocket senderAndReceiver = new DatagramSocket(port, InetAddress.getByName("192.168.0.50")); // so you do need to know the address of your network interface to discover devices on...
       senderAndReceiver.setSoTimeout(WS_DISCOVERY_TIMEOUT);

       // send the probe 
       DatagramPacket probeMsg = new DatagramPacket(probe.getBytes(), probe.length(), InetAddress.getByName(WS_DISCOVERY_ADDRESS_IPv4), WS_DISCOVERY_PORT);
       senderAndReceiver.send(probeMsg);

       // read in the responses
       ArrayList<String> responses = new ArrayList(); // this is the collection of all SOAP-infused XML ProbeMatch responses
       byte[] receiverBuffer = new byte[8192];
       DatagramPacket receiverPacket = new DatagramPacket(receiverBuffer, receiverBuffer.length); // this is the packet that receive the response in. Get's updated with the next response on each call to .receive()
       while (true) {
           try {
               senderAndReceiver.receive(receiverPacket);
               responses.add(new String(receiverPacket.getData()));

           } catch (SocketTimeoutException e) {
               // System.out.println("Socket read timeout; taken to mean that there is no more responses -- i.e., no more Probe Matches");
               break;
           }
       }

       // close the socket
       senderAndReceiver.close();

       return responses;

   }

   public static void main(String[] args) throws IOException, SOAPException {


       final String uuid = UUID.randomUUID().toString(); // generate the uuid to add to the Probe message

       ArrayList<String> responses = getResponsesToProbe(uuid); // responses is a collection of all the SOAP-infused XML ProbeMatches . It's all of our responses to the probe; it's basically the devices we've discovered!

    }

}


有关使用此方法的一些注意事项:


要使用此解决方案,您需要知道“网络接口”才能继续查看。在我的代码中,这是192.168.0.50。这是我要查找的相机所在的网络。要找到此内容,请在cmd提示符下运行arp -a命令(不确定在Mac或Linux上如何执行此操作),然后找到相机的IP。它所属的接口是您要用作“ 192.168.0.50”的接口。以我的有限理解,这些接口基本上可以分割您的网络,因此您需要选择合适的接口来查找设备。我认为(?)Thomas的代码通过找到所有这些网络接口避免了这个问题,这是在his code中的第81-100行中完成的。
发送时,您必须给探针一个唯一的UUID。这是我这样做的错误之一;我在Probe(在WS_DISCOVERY_PROBE_MESSAGE中)中使用了硬编码的UUID进行了测试。这将一次发现设备;但是之后,如果您发送具有相同UUID的探测,则设备似乎根本不会回复。您也不会收到任何错误响应,因此为什么我很难找出答案。就像设备保留了所有收到的所有探针的UUID的内部日志一样。如果您发送带有旧UUID的探测,它只会拒绝它。至少,对于我正在测试的符合ONVIF的相机(AXIS M3045-V),情况就是如此。我不确定ONVIF规范是否需要此行为,但是至少在AXIS M3045-V中是明显的。
注意:SOAP通常依赖HTTP进行传输。但是这里我们在UDP之上使用它。


我希望这对尝试做类似事情的人有所帮助。让我知道我有什么可以帮助的;此时,我已经阅读了大量文档,因此我可以伸出援手!

关于java - 如何使用WS-Discovery规范在Java中发现网络上的ONVIF设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56379926/

相关文章:

c# - Magento API v2 和 C# - 在添加产品时设置自定义属性

java - 轴故障 : Transport error: 415 Error in AXIS2 client(JAVA)

c++ - Onvif - 获取放大倍数

c# - 无法在 ONVIF 中实现事件订阅

java - 如何使用 Retrofit 发出请求

java - 如何循环每个 hh :00 whithin a time period in Java?

Java:在map.entry上实现Treemap方法,可能吗?

java - JDeveloper ADF Mobile -- javax.crypto - 无法找到策略文件

c# - 从 WSDL 生成 WCF Web 服务 (SOAP) 以接受/返回 XML 而不是序列化类型

java - 从 Open ONVIF(网络视频接口(interface)论坛)设备录制的问题