zhangzengfei
2020-06-18 8f14b89ef2a4e756b776db9ae4291ddd31f9781d
app/src/main/java/com/basic/security/utils/VideoPath.java
@@ -2,6 +2,11 @@
import com.basic.security.model.Alarm;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import org.apache.http.util.TextUtils;
@@ -14,84 +19,52 @@
import java.util.Map;
public class VideoPath {
    static Map<String, String> deviceMap = new HashMap<>();
    static Map<String, String> ngxMap = new HashMap<>();
    static Gson gson = new Gson();
    static Gson gson1 = new Gson();
    static {
        {
            deviceMap.put("DSVAD010120181119", "http://172.17.50.241:11111/getRecordVideoPath");
            deviceMap.put("DSVAD010220181119", "http://172.17.50.242:11111/getRecordVideoPath");
            deviceMap.put("DSVAD010320181119", "http://172.17.50.243:11111/getRecordVideoPath");
            deviceMap.put("DSVAD010420181119", "http://172.17.50.244:11111/getRecordVideoPath");
            ngxMap.put("DSVAD010120181119", "http://172.17.50.241/videosource");
            ngxMap.put("DSVAD010220181119", "http://172.17.50.242/videosource");
            ngxMap.put("DSVAD010320181119", "http://172.17.50.243/videosource");
            ngxMap.put("DSVAD010420181119", "http://172.17.50.244/videosource");
        }
        {
//            deviceMap.put("DSVAD010220181119", "http://58.118.225.79:51111/getRecordVideoPath");
//
//            ngxMap.put("DSVAD010120181119","http://58.118.225.79:44180/videosource");
//            ngxMap.put("DSVAD010220181119", "http://58.118.225.79:44280/videosource");
//            ngxMap.put("DSVAD010320181119", "http://58.118.225.79:44380/videosource");
//            ngxMap.put("DSVAD010420181119", "http://58.118.225.79:44480/videosource");
        }
    }
    public static String getVideoUrl(Map<String, String> alarm) {
        String videoUrl = "";
        try {
            String indeviceId = alarm.get(Alarm.indeviceid);//"DSVAD010120181119";
            String url = deviceMap.get(indeviceId);
            if (url == null) {
                return videoUrl;
            }
            String base_url = "192.168.20.10:6000/basic/api/androidAlarm/es/";
            String alarmId = alarm.get(Alarm.id);
            URL url1 = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
            URL url = new URL(base_url+alarmId);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setRequestMethod("GET");
            conn.connect();
            int responseCode = conn.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK){
                //得到响应流
                InputStream in = new BufferedInputStream(conn.getInputStream());
                String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
            Map<String, String> map1 = new HashMap<>();
            map1.put("imgKey", alarm.get(Alarm.imgKey));
            map1.put("picDate", alarm.get(Alarm.picDate));
            map1.put("videoNum", alarm.get(Alarm.videoReqNum));
            System.out.println("VideoPath.getVideoUrl " + map1);
            String json = gson1.toJson(map1);
            OutputStream os = conn.getOutputStream();
            os.write(json.getBytes("UTF-8"));
            os.close();
            InputStream in = new BufferedInputStream(conn.getInputStream());
            String out = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
            Map map = gson.fromJson(out, Map.class);
            String filePath = (String) map.get("file_path");
            if (filePath != null && filePath.contains("/cut")) {
                String ngxUrl = ngxMap.get(indeviceId);
                if (!TextUtils.isEmpty(ngxUrl)) {
                    videoUrl = ngxUrl + filePath.split("/cut")[1];
                System.out.println("videoUrl response :" + response);
                boolean isJSON = true;
                JsonParser parser = new JsonParser();
                JsonElement element = null;
                try {
                    element = parser.parse(response);
                } catch (JsonParseException e) {
                    System.out.println("exception: " + response + " " + e);
                    isJSON = false;
                }
                if (isJSON && element != null) {
                    Object obj =  element.getAsJsonObject().getAsJsonObject("data").get("videoUrl");
                    String result = obj.toString();
                    if (result.length() > 0) {
                        videoUrl = "http://" + result;
                    }
                }
                in.close();
                conn.disconnect();
            }
            in.close();
            conn.disconnect();
        } catch (Exception e) {
            System.out.println("VideoPath.getVideoUrl " + e.getMessage());
        }
        System.out.println("videoUrl :" + videoUrl);
        return videoUrl;
    }