【问题标题】:ONVIF - beginning of Device discoveryONVIF - 设备发现的开始
【发布时间】:2023-03-15 21:15:01
【问题描述】:

我打算做一个 java onvif 应用程序。我创建了一个新项目并从 devicemgmt.wsdl 生成了源代码。还从 remote discovery.wsdl 生成了类。 如何使用这些生成的类发现网络中的设备? 感谢您的帮助。

【问题讨论】:

标签: java camera ip ip-camera onvif


【解决方案1】:

devicemgmt.wsdl 与发现过程无关,ONVIF 发现过程基于http://specs.xmlsoap.org/ws/2005/04/discovery 它使用 SOAP over UDP。

如果你使用的是apache-cxf,这可以使用

org.apache.cxf.ws.discovery.WSDiscoveryClient

一个简单的示例代码可以是:

import java.util.List;
import javax.xml.ws.EndpointReference;
import org.apache.cxf.ws.discovery.WSDiscoveryClient;

public class Main 
{
    public static void main(String[] args) 
    {
        WSDiscoveryClient client = new WSDiscoveryClient();
        client.setVersion10(); // use WS-discovery 1.0
        client.setDefaultProbeTimeout(1000); // timeout 1s

        System.out.println("Probe:" + client.getAddress());
        List<EndpointReference> references = client.probe();

        System.out.println("Nb answsers:" + references.size());
        for (EndpointReference ref : references)
        {
            System.out.println(ref.toString());
        }
    }
}

【讨论】:

  • 如果 WSDiscoveryClient() 找到了设备,可能是什么原因(我正在使用 cxf),但没有被任何生产质量的 onvif 客户端(如“SmartICRSS”或“Onvifier”android 应用程序)找到
  • @4ntoine 可能是肥皂版?你应该问一个问题,提供一些网络捕获细节
  • 你可能是对的。我截获了发现请求/响应 - 请阅读我单独的 SO 问题:stackoverflow.com/questions/27191245/…
【解决方案2】:

我也遇到了同样的问题,CXF 太大了,请检查我的方法:https://github.com/thhart/javaWsDiscovery 的 JavaWsDiscovery。

它使用 Onvif 标准建议的简单网络探测来识别本地网络上的任何设备,以下行将返回所有可用设备:

final Collection urls = DeviceDiscovery.discoverWsDevicesAsUrls("^http$", ".onvif.");

【讨论】:

    【解决方案3】:

    简单完整的纯Java示例

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.SocketTimeoutException;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     *
     * @author Ronald
     */
    public class TestDiscoveryPureJava {
        
        public static void main(String cor[]) throws SocketException{
            discoverWsDevices();
        }
    
        public static  void discoverWsDevices() throws SocketException {
            final int WS_DISCOVERY_PORT = 3702;
            final String WS_DISCOVERY_ADDRESS_IPv4 = "239.255.255.250";
    
            Thread thread = new Thread() {
                @Override
                public void run() {
                    
                    final String probe = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">\n" +
    "    <s:Header>\n" +
    "        <a:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</a:Action>\n" +
    "        <a:MessageID>uuid:f0ded492-301a-4891-882b-cb2d7cac2e45</a:MessageID>\n" +
    "        <a:ReplyTo>\n" +
    "            <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>\n" +
    "        </a:ReplyTo>\n" +
    "        <a:To s:mustUnderstand=\"1\">urn:schemas-xmlsoap-org:ws:2005:04:discovery</a:To>\n" +
    "    </s:Header>\n" +
    "    <s:Body>\n" +
    "        <Probe xmlns=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\">\n" +
    "            <d:Types xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:dp0=\"http://www.onvif.org/ver10/network/wsdl\">dp0:Device</d:Types>\n" +
    "        </Probe>\n" +
    "    </s:Body>\n" +
    "</s:Envelope>";
    
                    DatagramSocket datagramSocket = null;
    
                    try {
                        datagramSocket = new DatagramSocket();
                        datagramSocket.setBroadcast(true);
                        datagramSocket.setSoTimeout(9000);
                    } catch (SocketException e) {
                        System.out.println( "In discoverWsDevices datagram socket exception" + datagramSocket);
                        e.printStackTrace();
                    }
    
                    byte[] soapMessageByteArray = probe.getBytes();
                    DatagramPacket datagramPacketSend = null;
                    try {
                        datagramPacketSend = new DatagramPacket(
                                soapMessageByteArray,
                                soapMessageByteArray.length,
                                InetAddress.getByName(WS_DISCOVERY_ADDRESS_IPv4),
                                WS_DISCOVERY_PORT);
                    } catch (UnknownHostException e) {
                        System.out.println("Unknown host in send packet");
                        e.printStackTrace();
                    }
    
                    try {
                        System.out.println("Send package");
                        datagramSocket.send(datagramPacketSend);
                        System.out.println("package sent");
                    } catch (IOException e) {
                        System.out.println("In discoverWsDevices datagram socket IOException send " + datagramSocket);
                        e.printStackTrace();
                    }
                    System.out.println("Sending data");
                    System.out.println(datagramPacketSend.getAddress().getHostName()+":"+WS_DISCOVERY_PORT);
    
                    List<ByteArrayInputStream> probeMatches = new ArrayList<>();
                    while (true) {
    
                        byte[] responseMessageByteArray = new byte[9000];
    
                        DatagramPacket datagramPacketRecieve = new DatagramPacket(responseMessageByteArray,responseMessageByteArray.length);
                        try {
                            System.out.println("Waiting response...");
                            datagramSocket.receive(datagramPacketRecieve);
                        } catch (SocketTimeoutException e) {
                            datagramSocket.close();
                            System.out.println("In discoverWsDevices datagram socket timeout exception");
                            break;
                        } catch (IOException e) {
                            System.out.println("In discoverWsDevices datagram socket ioexception");
                            e.printStackTrace();
                            break;
                        }
                        probeMatches.add(new ByteArrayInputStream(datagramPacketRecieve.getData(), 0, datagramPacketRecieve.getLength()));
                    }
    
                    for (ByteArrayInputStream input : probeMatches) {
                        byte[] bytes = new byte[input.available()];
                        input.read(bytes, 0, input.available());
                        String stream = new String(bytes);
                        System.out.println("stream" + stream);
                    }
                }
            };
            thread.start();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多