liuxiaolong
2019-05-06 0bc4f4c791437b39b8c30624f5c21f8c855dc61d
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.cloud.retrieve.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.cloud.common.utils.RestTemplateUtil;
import com.cloud.model.common.Result;
import com.cloud.retrieve.dao.CulOrDevDao;
import com.cloud.retrieve.service.AnalyDevService;
import com.cloud.retrieve.service.DeviceService;
import com.cloud.retrieve.utils.EnumStr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
 
import java.io.UnsupportedEncodingException;
import java.util.Map;
 
@Service
@Slf4j
public class AnalyDevServiceImpl implements AnalyDevService {
 
    @Autowired
    private EnumStr enumStr;
    @Autowired
    private RestTemplateUtil restTemplateUtil;
    @Autowired
    private CulOrDevDao culOrDevDao;
    @Autowired
    private DeviceService deviceService;
    @Override
    public JSONObject getVideo(String videoNum, String picDate, String videoIp, String indeviceid,String imgKey) {
 
        String reqIp = null;
        Integer videoPort = 80;
        String videoPublicIp = "";
        //    nodeIp,   videoPublicPort
        if (StringUtils.isNotEmpty(videoNum)) {
            //  Map<String, Object> byIndeviceId = culOrDevDao.findByIndeviceId(indeviceid);
 
            JSONObject nodeJson = getNodeByVideoNum(videoNum);
            if(nodeJson !=null){
                reqIp = nodeJson.getString("nodeIp");
                videoPort = nodeJson.getInteger("videoPort");
                videoPublicIp = nodeJson.getString("videoPublicIp");
            }
        } else {
            reqIp = videoIp;
        }
        if (imgKey == null) {
            imgKey = "";
        }   //  兼容旧数据
        String bsUrl = "http://" + reqIp + ":" + enumStr.getCServerPort();
        String esBaseUrl = enumStr.getCServerGetVideoUrl();
        String reqJson = "{\"videoNum\": \"" + videoNum +
                "\",\"picDate\": \"" + picDate + "\",\"imgKey\": \"" + imgKey + "\"}";
        JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
        String respData = restTemplateUtil.post(bsUrl + esBaseUrl,
                dataBaseParams, MediaType.APPLICATION_JSON_UTF8, String.class, false);
        JSONObject cResult = new JSONObject();
        if (respData != null) {
            try {
                respData = new String(respData.getBytes("ISO-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            cResult = JSONObject.parseObject(respData);
            if (StringUtils.isNotEmpty(videoPublicIp) && cResult.getString("file_path") != null) {
                cResult.put("filepath", "http://" + videoPublicIp + ":" + videoPort + "/videosource" + cResult.getString("file_path").split("/cut")[1]);
            }
        }
        return cResult;
    }
 
    @Override
    public JSONObject getBigPic(String videoNum, String picDate, String videoIp, String indeviceid, String imgKey) {
 
        // 获取 服务ip
        String reqIp = null;
        Integer videoPort = 80;
        //    publicIp,   videoPublicPort
        if (StringUtils.isNotEmpty(videoNum)) {
//            Map<String, Object> byIndeviceId = culOrDevDao.findByIndeviceId(indeviceid);
            JSONObject nodeJson = getNodeByVideoNum(videoNum);
            if(nodeJson !=null){
                reqIp = nodeJson.getString("nodeIp");
                videoPort = nodeJson.getInteger("videoPort");
            }
//            log.info("数据库查询nodeIp:" + reqIp + "videoPort:" + videoPort);
            if (videoPort == null) videoPort = 80;
        } else {
            reqIp = videoIp;
        }
        if (imgKey == null) {
            imgKey = "";
        }   //  兼容旧数据
        log.info("reqIp:" + reqIp);
        String bsUrl = "http://" + reqIp + ":" + enumStr.getCServerPort();
        String esBaseUrl = enumStr.getCServerGetBigPicUrl();
        String reqJson = "{\"videoNum\": \"" + videoNum +
                "\",\"picDate\": \"" + picDate + "\",\"imgKey\": \"" + imgKey + "\"}";
        JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
//        log.info("查看大图:reqJson:" + dataBaseParams + ",请求地址:" + bsUrl + esBaseUrl);
        String respData = restTemplateUtil.post(bsUrl + esBaseUrl,
                dataBaseParams, MediaType.APPLICATION_JSON_UTF8, String.class, false);
        JSONObject cResult = new JSONObject();
        if (respData != null) {
//            log.info("查看大图c返回:" + respData);
            cResult = JSONObject.parseObject(respData);
        }
        return cResult;
    }
 
    /**
     * 根据摄像机编号获取节点信息
     * @param videoNum
     * @return
     */
    private JSONObject getNodeByVideoNum(String videoNum){
        JSONObject jsonResult = null;
        Result result = deviceService.getNodeByDeviceId(videoNum);
        if (result.isSuccess()) {
            JSONObject data = (JSONObject) result.getData();
            JSONObject node = data.getJSONObject("node");
            if (node == null) {
                throw new RuntimeException("设备(摄像机)查找不到所在节点信息。");
            }
            jsonResult = new JSONObject();
            jsonResult.put("nodeIp",node.getString("nodeIp"));
            jsonResult.put("videoPort", node.getInteger("videoPublicPort"));
            jsonResult.put("videoPublicIp",node.getString("publicIp"));
        }
        return jsonResult;
    }
 
    @Override
    public Result getDevSnapshot(String devId) {
        return deviceService.getDevSnapshot(devId);
    }
 
    public Map<String, Object> findByIndeviceId(String devId) {
        return deviceService.getNodeByDevId(devId);
    }
 
}