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