|
|
@@ -0,0 +1,195 @@
|
|
|
+package com.middle.platform.stream.biz.service;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.middle.platform.common.core.utils.OkHttpUtils;
|
|
|
+import com.middle.platform.stream.biz.pojo.ApiInfo;
|
|
|
+import com.middle.platform.stream.biz.properties.StreamMediaProperties;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 接口代理实现
|
|
|
+ *
|
|
|
+ * @author xucaiqin
|
|
|
+ * @date 2024-04-26 13:47:08
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class ApiProxy {
|
|
|
+ private final StreamMediaProperties streamMediaProperties;
|
|
|
+
|
|
|
+ private String encode(String url) {
|
|
|
+ return URLEncoder.encode(url, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getHttp(ApiInfo apiInfo, Map<String, Object> para) {
|
|
|
+ log.debug("{}入参->{}", apiInfo.getDesc(), JSONObject.toJSONString(para));
|
|
|
+ OkHttpUtils okHttpUtils = OkHttpUtils.builder().url(streamMediaProperties.getUrl() + apiInfo.getUrl());
|
|
|
+ if (CollUtil.isNotEmpty(para)) {
|
|
|
+ for (Map.Entry<String, Object> p : para.entrySet()) {
|
|
|
+ //跳过非空参数
|
|
|
+ Object v = p.getValue();
|
|
|
+ String k = p.getKey();
|
|
|
+ if (Objects.isNull(v)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (v instanceof Integer i) {
|
|
|
+ okHttpUtils.addPara(k, String.valueOf(i));
|
|
|
+ } else if (v instanceof Long i) {
|
|
|
+ okHttpUtils.addPara(k, String.valueOf(i));
|
|
|
+ } else if (v instanceof String i) {
|
|
|
+ okHttpUtils.addPara(k, i);
|
|
|
+ } else if ((v.getClass().isArray()) && v instanceof String[] l) {
|
|
|
+ for (int i = 0; i < l.length; i++) {
|
|
|
+ okHttpUtils.addPara(k + "[" + i + "]", l[i]);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ okHttpUtils.addPara(k, v.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String sync;
|
|
|
+ try {
|
|
|
+ sync = okHttpUtils.get().sync();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("接口服务异常", e.getCause());
|
|
|
+ throw new RuntimeException("接口服务异常!");
|
|
|
+ }
|
|
|
+ log.debug("{}:返回值->{}", apiInfo.getDesc(), sync);
|
|
|
+ return sync;
|
|
|
+ }
|
|
|
+// private String postHttp(PayCenterEnum payCenterEnum, Map<String, Object> para) {
|
|
|
+// log.info("{}入参->{}", payCenterEnum.getDesc(), JSONObject.toJSONString(para));
|
|
|
+// OkHttpUtils okHttpUtils = OkHttpUtils.builder().url(payCenterAddr + payCenterEnum.getAddr());
|
|
|
+// if (!CollectionUtils.isEmpty(para)) {
|
|
|
+// for (Map.Entry<String, Object> p : para.entrySet()) {
|
|
|
+// //跳过非空参数
|
|
|
+// Object v = p.getValue();
|
|
|
+// String k = p.getKey();
|
|
|
+// if (Objects.isNull(v)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// if (v instanceof ChannelEnum channelEnum) {
|
|
|
+// okHttpUtils.addBodyPara(k, channelEnum.getChannel());
|
|
|
+// } else if (v instanceof Integer i) {
|
|
|
+// okHttpUtils.addBodyPara(k, String.valueOf(i));
|
|
|
+// } else if (v instanceof Long i) {
|
|
|
+// okHttpUtils.addBodyPara(k, String.valueOf(i));
|
|
|
+// } else if (v instanceof String i) {
|
|
|
+// okHttpUtils.addBodyPara(k, i);
|
|
|
+// } else if ((v.getClass().isArray()) && v instanceof String[] l) {
|
|
|
+// for (int i = 0; i < l.length; i++) {
|
|
|
+// okHttpUtils.addPara(k + "[" + i + "]", l[i]);
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// okHttpUtils.addBodyPara(k, JSONObject.toJSONString(v));
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// String sync;
|
|
|
+// try {
|
|
|
+// sync = okHttpUtils.post(true).sync();
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("中台服务异常", e.getCause());
|
|
|
+// throw new RuntimeException("支付服务异常!");
|
|
|
+// }
|
|
|
+// log.info("{}返回值->{}", payCenterEnum.getDesc(), sync);
|
|
|
+// return changeRes(sync);
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有rtmp列表
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object rtmpList() {
|
|
|
+ String sync = getHttp(ApiInfo.RTMP_LIST, new HashMap<>());
|
|
|
+ return JSONObject.parseObject(sync, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送rtmp流
|
|
|
+ *
|
|
|
+ * @param streamPath 流标识
|
|
|
+ * @param target RTMP地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object rtmpPush(String streamPath, String target) {
|
|
|
+ String sync = getHttp(ApiInfo.RTMP_PUSH, new HashMap<>() {{
|
|
|
+ put("streamPath", streamPath);
|
|
|
+ put("target", encode(target));
|
|
|
+ }});
|
|
|
+ return JSONObject.parseObject(sync, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object rtmpPull(String streamPath, String target, String save) {
|
|
|
+ String sync = getHttp(ApiInfo.RTMP_PULL, new HashMap<>() {{
|
|
|
+ put("streamPath", streamPath);
|
|
|
+ put("target", encode(target));
|
|
|
+ put("save", save);
|
|
|
+ }});
|
|
|
+ return JSONObject.parseObject(sync, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有rtsp列表
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object rtspList() {
|
|
|
+ String sync = getHttp(ApiInfo.RTSP_LIST, new HashMap<>());
|
|
|
+ return JSONObject.parseObject(sync, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送rtsp流
|
|
|
+ *
|
|
|
+ * @param streamPath 流标识
|
|
|
+ * @param target RTSP地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object rtspPush(String streamPath, String target) {
|
|
|
+ String sync = getHttp(ApiInfo.RTSP_PUSH, new HashMap<>() {{
|
|
|
+ put("streamPath", streamPath);
|
|
|
+ put("target", encode(target));
|
|
|
+ }});
|
|
|
+ return JSONObject.parseObject(sync, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object rtspPull(String streamPath, String target, String save) {
|
|
|
+ String sync = getHttp(ApiInfo.RTSP_PULL, new HashMap<>() {{
|
|
|
+ put("streamPath", streamPath);
|
|
|
+ put("target", encode(target));
|
|
|
+ put("save", save);
|
|
|
+ }});
|
|
|
+ return JSONObject.parseObject(sync, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object recordListPage(String type, String pageSize, String pageNum, String streamPath) {
|
|
|
+ String sync = getHttp(ApiInfo.RECORD_LIST_PAGE, new HashMap<>() {{
|
|
|
+ put("type", type);
|
|
|
+ put("pageSize", pageSize);
|
|
|
+ put("pageNum", pageNum);
|
|
|
+ put("streamPath", streamPath);
|
|
|
+ }});
|
|
|
+ return JSONObject.parseObject(sync, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+}
|