java代码实现 使用ws-discovery库来发现网络上的ONVIF设备
时间: 2024-03-03 17:48:41 浏览: 28
好的,以下是使用ws-discovery库发现网络上的ONVIF设备的示例代码:
```java
import java.util.List;
import org.onvif.ver10.device.wsdl.Device;
import org.onvif.ver10.device.wsdl.GetDeviceInformationResponse;
import org.onvif.ver10.device.wsdl.GetServicesResponse;
import org.onvif.ver10.media.wsdl.Media;
import org.onvif.ver10.media.wsdl.GetCapabilitiesResponse;
import org.onvif.ver10.media.wsdl.Profile;
import org.onvif.ver10.media.wsdl.GetProfilesResponse;
import org.onvif.ver10.media.wsdl.GetStreamUriResponse;
import org.onvif.ver10.schema.Capabilities;
import org.onvif.ver10.schema.ProfileStatus;
import org.onvif.ver10.schema.StreamSetup;
import org.onvif.ver10.schema.Transport;
import org.onvif.ver10.schema.TransportProtocol;
import org.w3._2005._08.addressing.EndpointReferenceType;
import org.ws4d.java.communication.CommunicationException;
import org.ws4d.java.communication.DPWSDiscoveryBinding;
import org.ws4d.java.communication.TimeoutException;
import org.ws4d.java.communication.connection.ip.IPNetworkDetection;
import org.ws4d.java.communication.protocol.soap.client.SOAPoverUDPClient;
import org.ws4d.java.communication.structures.Binding;
import org.ws4d.java.communication.structures.CommunicationManager;
import org.ws4d.java.schema.Element;
import org.ws4d.java.schema.SchemaUtil;
import org.ws4d.java.service.DeviceReference;
import org.ws4d.java.service.reference.DeviceReferenceFactory;
import org.ws4d.java.types.EndpointReference;
import org.ws4d.java.types.QName;
public class OnvifDiscovery {
// 发现设备
public static void discoverOnvifDevices() {
try {
// 获取本机 IP 地址
String localIP = IPNetworkDetection.getInstance().getIPAddressOfThisMachine();
// 创建 DPWSDiscoveryBinding 对象
DPWSDiscoveryBinding dpwsDiscoveryBinding = CommunicationManager.createDPWSDiscoveryBinding();
// 设置 DPWSDiscoveryBinding 的搜索范围
dpwsDiscoveryBinding.setSearchScope(DPWSDiscoveryBinding.SCOPE_TYPE__ALL);
// 发送搜索请求
List<EndpointReferenceType> eprs = dpwsDiscoveryBinding.searchDevice(new QName("dn:NetworkVideoTransmitter"), localIP, 5000);
// 遍历搜索结果
for (EndpointReferenceType epr : eprs) {
// 创建 DeviceReference 对象
DeviceReference deviceRef = DeviceReferenceFactory.createDeviceReference(epr);
// 调用 GetCapabilities 操作获取设备能力
GetCapabilitiesResponse getCapabilitiesResponse = Media.create(deviceRef, localIP).getCapabilities(null);
// 获取设备能力
Capabilities capabilities = getCapabilitiesResponse.getCapabilities();
// 打印设备能力
System.out.println("Device Manufacturer: " + capabilities.getDevice().getManufacturer());
System.out.println("Device Model: " + capabilities.getDevice().getModel());
System.out.println("Device Firmware Version: " + capabilities.getDevice().getFirmwareVersion());
System.out.println("Supported Profiles: " + capabilities.getMedia().getProfile().size());
// 调用 GetProfiles 操作获取设备配置文件
GetProfilesResponse getProfilesResponse = Media.create(deviceRef, localIP).getProfiles(null);
// 遍历设备配置文件
for (Profile profile : getProfilesResponse.getProfiles()) {
// 调用 GetStreamUri 操作获取配置文件的 RTSP 流地址
StreamSetup streamSetup = new StreamSetup();
streamSetup.setStream(new org.onvif.ver10.schema.StreamType("RTP-Unicast"));
Transport transport = new Transport();
transport.setProtocol(TransportProtocol.RTSP);
StreamSetup setup = new StreamSetup();
setup.setStream(org.onvif.ver10.schema.StreamType.RTP_UNICAST);
setup.setTransport(transport);
GetStreamUriResponse getStreamUriResponse = Media.create(deviceRef, localIP).getStreamUri(streamSetup, profile.getToken());
// 获取 RTSP 流地址
String rtspUrl = getStreamUriResponse.getMediaUri().getUri();
// 打印设备配置文件和 RTSP 流地址
System.out.println("Profile Token: " + profile.getToken());
System.out.println("Profile Name: " + profile.getName());
System.out.println("RTSP URL: " + rtspUrl);
}
}
} catch (TimeoutException | CommunicationException e) {
e.printStackTrace();
}
}
// 示例代码运行
public static void main(String[] args) {
discoverOnvifDevices();
}
}
```
上述代码中,我们使用了 `ws4d-java` 库来发现网络上的 ONVIF 设备。在 `discoverOnvifDevices` 函数中,我们首先获取本机的 IP 地址,并创建了一个 `DPWSDiscoveryBinding` 对象。然后,我们设置了搜索范围为所有设备,并发送了一个搜索请求。我们遍历搜索结果,并为每个设备创建一个 `DeviceReference` 对象。
在 `DeviceReference` 对象上,我们调用了 `Media.create(deviceRef, localIP).getCapabilities(null)` 操作获取了设备的能力,并打印了设备制造商、型号、固件版本和支持的配置文件数量。接着,我们调用了 `Media.create(deviceRef, localIP).getProfiles(null)` 操作获取了设备的配置文件,并遍历了每个配置文件。对于每个配置文件,我们调用了 `Media.create(deviceRef, localIP).getStreamUri(streamSetup, profile.getToken())` 操作获取了 RTSP 流地址,并打印了配置文件的令牌、名称和 RTSP 流地址。
在示例代码运行时,我们调用 `discoverOnvifDevices` 函数发现设备,并打印设备的能力和配置文件信息。