ONVIFServerIMpl.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.genersoft.iot.vmp.onvif.impl;
  2. import be.teletask.onvif.DiscoveryManager;
  3. import be.teletask.onvif.OnvifManager;
  4. import be.teletask.onvif.listeners.*;
  5. import be.teletask.onvif.models.*;
  6. import be.teletask.onvif.responses.OnvifResponse;
  7. import com.genersoft.iot.vmp.onvif.IONVIFServer;
  8. import com.genersoft.iot.vmp.onvif.dto.ONVIFCallBack;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.stereotype.Service;
  12. import java.net.URI;
  13. import java.net.URISyntaxException;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * 处理onvif的各种操作
  20. */
  21. @Service
  22. public class ONVIFServerIMpl implements IONVIFServer {
  23. private final static Logger logger = LoggerFactory.getLogger(ONVIFServerIMpl.class);
  24. @Override
  25. public void search(int timeout, ONVIFCallBack<List<String>> callBack) {
  26. DiscoveryManager manager = new DiscoveryManager();
  27. manager.setDiscoveryTimeout(timeout);
  28. Map<String, Device> deviceMap = new HashMap<>();
  29. // 搜索设备
  30. manager.discover(new DiscoveryListener() {
  31. @Override
  32. public void onDiscoveryStarted() {
  33. logger.info("Discovery started");
  34. }
  35. @Override
  36. public void onDevicesFound(List<Device> devices) {
  37. if (devices == null || devices.size() == 0) return;
  38. for (Device device : devices){
  39. System.out.println(device.getHostName());
  40. deviceMap.put(device.getHostName(), device);
  41. }
  42. }
  43. // 搜索结束
  44. @Override
  45. public void onDiscoveryFinished() {
  46. ArrayList<String> result = new ArrayList<>();
  47. for (Device device : deviceMap.values()) {
  48. System.out.println(device.getHostName());
  49. result.add(device.getHostName());
  50. }
  51. callBack.run(0, result);
  52. }
  53. });
  54. }
  55. @Override
  56. public void getRTSPUrl(int timeout, OnvifDevice device, ONVIFCallBack<String> callBack) {
  57. if (device.getHostName() == null ){
  58. callBack.run(400, null);
  59. }
  60. OnvifManager onvifManager = new OnvifManager();
  61. onvifManager.setOnvifResponseListener(new OnvifResponseListener(){
  62. @Override
  63. public void onResponse(OnvifDevice onvifDevice, OnvifResponse response) {
  64. System.out.println("[RESPONSE] " + onvifDevice.getHostName()
  65. + "======" + response.getErrorCode()
  66. + "======" + response.getErrorMessage());
  67. }
  68. @Override
  69. public void onError(OnvifDevice onvifDevice, int errorCode, String errorMessage) {
  70. System.out.println("[ERROR] " + onvifDevice.getHostName() + "======" + errorCode + "=======" + errorMessage);
  71. callBack.run(errorCode, errorMessage);
  72. }
  73. });
  74. try {
  75. onvifManager.getServices(device, (OnvifDevice onvifDevice, OnvifServices services) -> {
  76. if (services.getProfilesPath().equals("/onvif/Media")) {
  77. onvifDevice.setPath(services);
  78. onvifManager.getMediaProfiles(onvifDevice, new OnvifMediaProfilesListener() {
  79. @Override
  80. public void onMediaProfilesReceived(OnvifDevice device, List<OnvifMediaProfile> mediaProfiles) {
  81. for (OnvifMediaProfile mediaProfile : mediaProfiles) {
  82. System.out.println(mediaProfile.getName());
  83. System.out.println(mediaProfile.getToken());
  84. if (mediaProfile.getName().equals("mainStream")) {
  85. onvifManager.getMediaStreamURI(device, mediaProfile, (OnvifDevice onvifDevice,
  86. OnvifMediaProfile profile, String uri) -> {
  87. uri = uri.replace("rtsp://", "rtsp://"+ device.getUsername() + ":"+ device.getPassword() + "@");
  88. logger.info(onvifDevice.getHostName() + "的地址" + uri);
  89. callBack.run(0, uri);
  90. });
  91. }
  92. }
  93. }
  94. });
  95. }
  96. });
  97. }catch (Exception e) {
  98. callBack.run(400, e.getMessage());
  99. }
  100. }
  101. }