liurunyu
2025-08-14 989f463760203b62e9e5343e9a5ed1129c7e02d3
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
package com.dy.pipIrrWechat.remote;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.webUtil.BaseResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
 
/**
 * 远程视频服务调用类
 * 用于调用pipIrr-web-remote模块的video接口
 * 
 * @author zuoxiao
 * @date 2025-01-21
 * @description 微信模块调用远程控制模块的视频服务类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RemoteService {
 
    @Value("${remote.service.base-url:http://localhost:8081/remote}")
    private String remoteServiceBaseUrl;
 
    private final OkHttpClient httpClient;
 
    /**
     * 调用远程视频监控接口 - 获取摄像机数据
     * 
     * @param queryParams 查询参数字符串
     * @return 远程调用结果
     */
    public BaseResponse<Object> getVideoSome(String queryParams) {
        return callRemoteApi("/video/some?" + (queryParams != null ? queryParams : ""), null, "GET");
    }
 
    /**
     * 通用远程API调用方法
     * 
     * @param endpoint 接口端点
     * @param requestBody 请求体(GET请求时为null)
     * @param method HTTP方法
     * @return 调用结果
     */
    private BaseResponse<Object> callRemoteApi(String endpoint, String requestBody, String method) {
        String url = remoteServiceBaseUrl + endpoint;
        Request request;
 
        try {
            Request.Builder requestBuilder = new Request.Builder()
                    .url(url)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json");
 
            if ("POST".equalsIgnoreCase(method) && requestBody != null) {
                RequestBody body = RequestBody.create(requestBody, MediaType.get("application/json; charset=utf-8"));
                request = requestBuilder.post(body).build();
            } else {
                request = requestBuilder.get().build();
            }
 
            log.info("调用远程接口: {} {}", method, url);
            log.debug("请求体: {}", requestBody);
 
            try (Response response = httpClient.newCall(request).execute()) {
                if (!response.isSuccessful()) {
                    log.error("远程调用失败: HTTP {}", response.code());
                    return BaseResponse.<Object>builder()
                            .success(false)
                            .msg("远程服务调用失败: HTTP " + response.code())
                            .build();
                }
 
                String responseBody = response.body() != null ? response.body().string() : "";
                log.debug("远程接口响应: {}", responseBody);
 
                // 解析远程服务返回的BaseResponse
                JSONObject jsonResponse = JSON.parseObject(responseBody);
                return BaseResponse.<Object>builder()
                        .success(jsonResponse.getBooleanValue("success"))
                        .msg(jsonResponse.getString("msg"))
                        .content(jsonResponse.get("content"))
                        .build();
 
            }
        } catch (IOException e) {
            log.error("调用远程接口异常: {}", e.getMessage(), e);
            return BaseResponse.<Object>builder()
                    .success(false)
                    .msg("远程服务调用异常: " + e.getMessage())
                    .build();
        }
    }