From 3d68b56bb5ecbc3d34b2901c671dc7f1f0f82f74 Mon Sep 17 00:00:00 2001
From: pedoc <pedoc@qq.com>
Date: 星期一, 29 一月 2024 18:05:17 +0800
Subject: [PATCH] 修复未配置sipdomain时出现异常

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java |  204 ++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 161 insertions(+), 43 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java
index cf71bf1..c5be487 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java
@@ -2,19 +2,27 @@
 
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
+import com.genersoft.iot.vmp.conf.exception.ControllerException;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
 import okhttp3.*;
 import okhttp3.logging.HttpLoggingInterceptor;
 import org.jetbrains.annotations.NotNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
 
 import java.io.IOException;
 import java.net.ConnectException;
+import java.net.MalformedURLException;
+import java.net.SocketTimeoutException;
+import java.net.URL;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 
 @Component
 public class AssistRESTfulUtils {
@@ -22,21 +30,41 @@
     private final static Logger logger = LoggerFactory.getLogger(AssistRESTfulUtils.class);
 
 
+    private OkHttpClient client;
+
+
     public interface RequestCallback{
         void run(JSONObject response);
     }
 
     private OkHttpClient getClient(){
-        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
-        if (logger.isDebugEnabled()) {
-            HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
-                logger.debug("http璇锋眰鍙傛暟锛�" + message);
-            });
-            logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
-            // OkHttp閫茶娣诲姞鏀旀埅鍣╨oggingInterceptor
-            httpClientBuilder.addInterceptor(logging);
+        return getClient(null);
+    }
+    
+    private OkHttpClient getClient(Integer readTimeOut){
+        if (client == null) {
+            if (readTimeOut == null) {
+                readTimeOut = 10;
+            }
+            OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
+            // 璁剧疆杩炴帴瓒呮椂鏃堕棿
+            httpClientBuilder.connectTimeout(8, TimeUnit.SECONDS);
+            // 璁剧疆璇诲彇瓒呮椂鏃堕棿
+            httpClientBuilder.readTimeout(readTimeOut,TimeUnit.SECONDS);
+            // 璁剧疆杩炴帴姹�
+            httpClientBuilder.connectionPool(new ConnectionPool(16, 5, TimeUnit.MINUTES));
+            if (logger.isDebugEnabled()) {
+                HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
+                    logger.debug("http璇锋眰鍙傛暟锛�" + message);
+                });
+                logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
+                // OkHttp閫茶娣诲姞鏀旀埅鍣╨oggingInterceptor
+                httpClientBuilder.addInterceptor(logging);
+            }
+            client = httpClientBuilder.build();
         }
-        return httpClientBuilder.build();
+        return client;
+
     }
 
 
@@ -50,11 +78,11 @@
             logger.warn("鏈惎鐢ˋssist鏈嶅姟");
             return null;
         }
-        StringBuffer stringBuffer = new StringBuffer();
-        stringBuffer.append(String.format("http://%s:%s/%s",  mediaServerItem.getIp(), mediaServerItem.getRecordAssistPort(), api));
+        StringBuilder stringBuffer = new StringBuilder();
+        stringBuffer.append(api);
         JSONObject responseJSON = null;
 
-        if (param != null && param.keySet().size() > 0) {
+        if (param != null && !param.keySet().isEmpty()) {
             stringBuffer.append("?");
             int index = 1;
             for (String key : param.keySet()){
@@ -69,6 +97,7 @@
         }
 
         String url = stringBuffer.toString();
+        logger.info("[璁块棶assist]锛� {}", url);
         Request request = new Request.Builder()
                 .get()
                 .url(url)
@@ -124,13 +153,92 @@
         return responseJSON;
     }
 
+    public JSONObject sendPost(MediaServerItem mediaServerItem, String url,
+                               JSONObject param, ZLMRESTfulUtils.RequestCallback callback,
+                               Integer readTimeOut) {
+        OkHttpClient client = getClient(readTimeOut);
 
-    public JSONObject fileDuration(MediaServerItem mediaServerItem, String app, String stream, RequestCallback callback){
-        Map<String, Object> param = new HashMap<>();
-        param.put("app",app);
-        param.put("stream",stream);
-        param.put("recordIng",true);
-        return sendGet(mediaServerItem, "api/record/file/duration",param, callback);
+        if (mediaServerItem == null) {
+            return null;
+        }
+        JSONObject responseJSON = new JSONObject();
+        //-2鑷畾涔夋祦濯掍綋 璋冪敤閿欒鐮�
+        responseJSON.put("code",-2);
+        responseJSON.put("msg","ASSIST璋冪敤澶辫触");
+
+        RequestBody requestBodyJson = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), param.toString());
+
+        Request request = new Request.Builder()
+                .post(requestBodyJson)
+                .url(url)
+                .addHeader("Content-Type", "application/json")
+                .build();
+        if (callback == null) {
+            try {
+                Response response = client.newCall(request).execute();
+                if (response.isSuccessful()) {
+                    ResponseBody responseBody = response.body();
+                    if (responseBody != null) {
+                        String responseStr = responseBody.string();
+                        responseJSON = JSON.parseObject(responseStr);
+                    }
+                }else {
+                    response.close();
+                    Objects.requireNonNull(response.body()).close();
+                }
+            }catch (IOException e) {
+                logger.error(String.format("[ %s ]ASSIST璇锋眰澶辫触: %s", url, e.getMessage()));
+
+                if(e instanceof SocketTimeoutException){
+                    //璇诲彇瓒呮椂瓒呮椂寮傚父
+                    logger.error(String.format("璇诲彇ASSIST鏁版嵁澶辫触: %s, %s", url, e.getMessage()));
+                }
+                if(e instanceof ConnectException){
+                    //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144
+                    logger.error(String.format("杩炴帴ASSIST澶辫触: %s, %s", url, e.getMessage()));
+                }
+
+            }catch (Exception e){
+                logger.error(String.format("璁块棶ASSIST澶辫触: %s, %s", url, e.getMessage()));
+            }
+        }else {
+            client.newCall(request).enqueue(new Callback(){
+
+                @Override
+                public void onResponse(@NotNull Call call, @NotNull Response response){
+                    if (response.isSuccessful()) {
+                        try {
+                            String responseStr = Objects.requireNonNull(response.body()).string();
+                            callback.run(JSON.parseObject(responseStr));
+                        } catch (IOException e) {
+                            logger.error(String.format("[ %s ]璇锋眰澶辫触: %s", url, e.getMessage()));
+                        }
+
+                    }else {
+                        response.close();
+                        Objects.requireNonNull(response.body()).close();
+                    }
+                }
+
+                @Override
+                public void onFailure(@NotNull Call call, @NotNull IOException e) {
+                    logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+
+                    if(e instanceof SocketTimeoutException){
+                        //璇诲彇瓒呮椂瓒呮椂寮傚父
+                        logger.error(String.format("璇诲彇ZLM鏁版嵁澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+                    }
+                    if(e instanceof ConnectException){
+                        //鍒ゆ柇杩炴帴寮傚父锛屾垜杩欓噷鏄姤Failed to connect to 10.7.5.144
+                        logger.error(String.format("杩炴帴ZLM澶辫触: %s, %s", call.request().toString(), e.getMessage()));
+                    }
+                }
+            });
+        }
+
+
+
+        return responseJSON;
     }
 
     public JSONObject getInfo(MediaServerItem mediaServerItem, RequestCallback callback){
@@ -138,33 +246,43 @@
         return sendGet(mediaServerItem, "api/record/info",param, callback);
     }
 
-    public JSONObject addStreamCallInfo(MediaServerItem mediaServerItem, String app, String stream, String callId, RequestCallback callback){
-        Map<String, Object> param = new HashMap<>();
-        param.put("app",app);
-        param.put("stream",stream);
-        param.put("callId",callId);
-        return sendGet(mediaServerItem, "api/record/addStreamCallInfo",param, callback);
+    public JSONObject addTask(MediaServerItem mediaServerItem, String app, String stream, String startTime,
+                              String endTime, String callId, List<String> filePathList, String remoteHost) {
+        
+        JSONObject videoTaskInfoJSON = new JSONObject();
+        videoTaskInfoJSON.put("app", app);
+        videoTaskInfoJSON.put("stream", stream);
+        videoTaskInfoJSON.put("startTime", startTime);
+        videoTaskInfoJSON.put("endTime", endTime);
+        videoTaskInfoJSON.put("callId", callId);
+        videoTaskInfoJSON.put("filePathList", filePathList);
+        if (!ObjectUtils.isEmpty(remoteHost)) {
+            videoTaskInfoJSON.put("remoteHost", remoteHost);
+        }
+        String urlStr = String.format("%s/api/record/file/download/task/add",  remoteHost);;
+        return sendPost(mediaServerItem, urlStr, videoTaskInfoJSON, null, 30);
     }
 
-    public JSONObject getDateList(MediaServerItem mediaServerItem, String app, String stream, int year, int month) {
+    public JSONObject queryTaskList(MediaServerItem mediaServerItem, String app, String stream, String callId,
+                                    String taskId, Boolean isEnd, String scheme) {
         Map<String, Object> param = new HashMap<>();
-        param.put("app", app);
-        param.put("stream", stream);
-        param.put("year", year);
-        param.put("month", month);
-        return sendGet(mediaServerItem, "api/record/date/list", param, null);
+        if (!ObjectUtils.isEmpty(app)) {
+            param.put("app", app);
+        }
+        if (!ObjectUtils.isEmpty(stream)) {
+            param.put("stream", stream);
+        }
+        if (!ObjectUtils.isEmpty(callId)) {
+            param.put("callId", callId);
+        }
+        if (!ObjectUtils.isEmpty(taskId)) {
+            param.put("taskId", taskId);
+        }
+        if (!ObjectUtils.isEmpty(isEnd)) {
+            param.put("isEnd", isEnd);
+        }
+        String urlStr = String.format("%s://%s:%s/api/record/file/download/task/list",
+                scheme, mediaServerItem.getIp(), mediaServerItem.getRecordAssistPort());;
+        return sendGet(mediaServerItem, urlStr, param, null);
     }
-
-    public JSONObject getFileList(MediaServerItem mediaServerItem, int page, int count, String app, String stream,
-                                  String startTime, String endTime) {
-        Map<String, Object> param = new HashMap<>();
-        param.put("app", app);
-        param.put("stream", stream);
-        param.put("page", page);
-        param.put("count", count);
-        param.put("startTime", startTime);
-        param.put("endTime", endTime);
-        return sendGet(mediaServerItem, "api/record/file/listWithDate", param, null);
-    }
-
 }

--
Gitblit v1.8.0