Jar*_* H. 4 java soap ws-discovery onvif
我正在尝试使用一些 Java 代码发现 ONVIF 设备。具体来说,我试图获取他们的设备服务地址(我相信这只是他们的 IP 地址?),正如ONVIF 核心规范指出(第 4.3 节)“成功的发现提供了设备服务地址。一旦客户端拥有设备服务地址它可以通过设备服务接收详细的设备信息...”。最终获得网络上 ONVIF 设备的详细信息是我的目标。一般来说,我也在寻找一些与使用 ONVIF 规范相关的指南。
我对网络服务世界(以及一般的网络)还是个新手,所以如果我说了什么蠢话,请原谅我。不过,我自己也为此付出了很多努力:我阅读了大量的ONVIF Core Spec、ONVIF Application Programmer's Guide和WS-Discovery Specific。如果可以的话,我会总结一下我所知道的,以便您可以告诉我我是否走在正确的道路上:
wsimport(由 JDK 提供)生成 WSDL 中的 Java 类以在我的代码中使用。但我知道调用这些方法将在设备发现之后进行,对吗? Probe消息,符合探测器限制的设备会发回一条消息,如《ONVIF 应用程序程序员指南》ProbeMatch第 13 和 14 页所述 这是我开始感到困惑的地方。我到底如何在 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>
Run Code Online (Sandbox Code Playgroud)
我还了解 WS-Discovery 技术使用地址 239.255.255.259 和 UDP 端口 3702...但这就是我得到的结果。如何在 Java 中将该 SOAP 消息发送到该地址和端口?我如何读取响应(我认为我会以注入 SOAP 的 XML 文档的形式返回 ProbeMatch 消息,因此我需要解析该 XML 来获取XAddrs,但不确定)。我是否需要以某种方式将该 SOAP 消息的 UDP 广播发送到该地址?
TL;DR:我相信要进行 ONVIF 设备发现,我需要将上面的 SOAP 消息发送到 UDP 端口 3702 上的地址 239.255.255.259。我不知道如何在 Java 中执行此操作,只是在寻找一些指导;我什至不确定我是否走在设备发现的正确轨道上。
通过多次尝试和错误,我找到了解决问题的(另一个)方法。在大多数情况下,mpromonet 可能是可行的方法,我只是想避免使用像 Apache 这样的大型依赖项。我还认为这应该可以通过一些简单的 UDP 消息传递来实现。
该解决方案也基于 SO 用户 Thomas 的有用代码(此处)。我主要通过删除线程来简化他的代码,并添加一些注释。同样,他的解决方案可能比我的更好(性能更高);但是,对于初学者(像我一样)来说,我的可能更容易理解。
这是代码:
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!
}
}
Run Code Online (Sandbox Code Playgroud)
使用此的一些注意事项:
要使用此解决方案,您需要知道要查看的“网络接口”。在我的代码中,这是 192.168.0.50 。这是我要查找的相机所在的网络。要找到它,请arp -a在 cmd 提示符中运行命令(不确定如何在 Mac 或 Linux 上执行此操作),然后找到相机的 IP。它所属的接口就是您要使用的接口“192.168.0.50”。根据我有限的理解,这些接口基本上会分割您的网络,因此您需要选择正确的接口来查找设备。我认为(?)Thomas 的代码通过查找所有这些网络接口来避免这个问题。这是在他的代码的第 81-100 行中完成的。
发送探针时,您必须为其提供唯一的 UUID。这是我这样做的错误之一;我在探测器(在WS_DISCOVERY_PROBE_MESSAGE)中使用硬编码的 UUID 进行了测试。这将有助于发现设备一次;但之后,如果您发送具有相同 UUID 的探测,设备似乎根本不会回复。您也不会得到任何错误响应,因此我很难找出这一点。就好像设备保留了其收到的所有探测器的 UUID 的一些内部日志;如果您发送带有旧 UUID 的探测,它只会拒绝它。或者至少,我正在测试的符合 ONVIF 标准的摄像机(AXIS M3045-V)就是这种情况。我不确定 ONVIF 规范是否需要此行为,但它至少在 AXIS M3045-V 中很明显。
注意:SOAP通常依赖HTTP进行传输;但这里我们在 UDP 之上使用它。
我希望这可以帮助任何尝试做类似事情的人。如果有什么我可以帮忙的,请告诉我;目前我已经阅读了大量文档,因此我也许可以提供帮助!