Browse Source

rtmp url获取接口

S0025136190 1 year ago
parent
commit
5478df83c9

+ 68 - 0
Backend/sample/src/main/java/com/dji/sample/common/smsp/CallSmsp.java

@@ -0,0 +1,68 @@
+package com.dji.sample.common.smsp;
+
+import cn.hutool.crypto.digest.MD5;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+@Slf4j
+public class CallSmsp {
+    public static String getSmspToken(String username,String password) {
+        String pass = MD5.create().digestHex(password);
+        String url = "https://smsp.jkec.info:4443/api/v1/login?_t=1722393429&username="+username+"&password="+pass;
+        try {
+            String response = sendGetRequest(url);
+            log.debug("Response from API: " + response);
+            JSONObject json = new JSONObject(response);
+            JSONObject body = json.getJSONObject("EasyDarwin").getJSONObject("Body");
+            return body.getStr("Token");
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    public static String getRtmp(String token,String deviceSn) {
+        String url = "https://smsp.jkec.info:4443/api/v1/channelstream/all?token="+token;
+        try {
+            String response = sendGetRequest(url);
+            log.debug("Response from API: " + response);
+            JSONObject json = new JSONObject(response);
+            JSONArray dataArray = json.getJSONArray("data");
+            for (int i = 0; i < dataArray.size(); i++) {
+                JSONObject dataObject = dataArray.getJSONObject(i);
+                if (deviceSn.equals(dataObject.getStr("ChannelName"))) {
+                    return dataObject.getStr("RTMP");
+                }
+            }
+            return null;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    private static String sendGetRequest(String requestURL) throws Exception {
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        HttpGet httpGet = new HttpGet(requestURL);
+        CloseableHttpResponse response = httpClient.execute(httpGet);
+        try {
+            int statusCode = response.getStatusLine().getStatusCode();
+            if (statusCode == 200) {
+                return EntityUtils.toString(response.getEntity());
+            } else {
+                throw new RuntimeException("Failed : HTTP error code : " + statusCode);
+            }
+        } finally {
+            response.close();
+            httpClient.close();
+        }
+    }
+
+
+}

+ 8 - 0
Backend/sample/src/main/java/com/dji/sample/manage/dao/IManageDeviceLivestreamUrlMapper.java

@@ -0,0 +1,8 @@
+package com.dji.sample.manage.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.dji.sample.manage.model.entity.ManageDeviceLivestreamUrlEntity;
+
+public interface IManageDeviceLivestreamUrlMapper extends BaseMapper<ManageDeviceLivestreamUrlEntity> {
+
+}

+ 32 - 0
Backend/sample/src/main/java/com/dji/sample/manage/model/dto/ManageDeviceLivestreamUrlDTO.java

@@ -0,0 +1,32 @@
+package com.dji.sample.manage.model.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author huiqing.jiang
+ * @date 2024/7/11
+ * @version 0.1
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+public class ManageDeviceLivestreamUrlDTO {
+
+    private Integer id;
+
+    private String workspaceId;
+
+    private String deviceSn;
+
+    private String payload;
+
+    private Integer urlType;
+
+    private Integer subType;
+
+    private String url;
+}

+ 44 - 0
Backend/sample/src/main/java/com/dji/sample/manage/model/entity/ManageDeviceLivestreamUrlEntity.java

@@ -0,0 +1,44 @@
+package com.dji.sample.manage.model.entity;
+
+import com.baomidou.mybatisplus.annotation.*;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+@TableName(value = "manage_device_livestream_url")
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+public class ManageDeviceLivestreamUrlEntity implements Serializable {
+
+    @TableId(type = IdType.AUTO)
+    private Integer id;
+
+    @TableField(value = "workspace_id")
+    private String workspaceId;
+
+    @TableField(value = "device_sn")
+    private String deviceSn;
+
+    @TableField(value = "payload")
+    private String payload;
+
+    @TableField(value = "user_type")
+    private Integer urlType;
+
+    @TableField(value = "sub_type")
+    private Integer subType;
+
+    @TableField(value = "url")
+    private String url;
+
+    @TableField(value = "create_time", fill = FieldFill.INSERT)
+    private Long createTime;
+
+    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
+    private Long updateTime;
+}

+ 4 - 0
Backend/sample/src/main/java/com/dji/sample/manage/model/enums/LiveUrlTypeEnum.java

@@ -23,6 +23,10 @@ public enum LiveUrlTypeEnum {
         this.val = val;
     }
 
+    public int getVal() {
+        return this.val;
+    }
+
     public static LiveUrlTypeEnum find(Integer val) {
         if (AGORA.val == val) {
             return AGORA;

+ 17 - 0
Backend/sample/src/main/java/com/dji/sample/manage/service/IManageDeviceLivestreamUrlService.java

@@ -0,0 +1,17 @@
+package com.dji.sample.manage.service;
+
+
+import com.dji.sample.manage.model.dto.ManageDeviceLivestreamUrlDTO;
+import com.dji.sample.manage.model.dto.WorkspaceDTO;
+
+import java.util.List;
+import java.util.Optional;
+
+public interface IManageDeviceLivestreamUrlService {
+
+    /**
+     * 通过接口获取RTMP
+     * @return
+     */
+    String getRtmpUrl(ManageDeviceLivestreamUrlDTO deviceLivestreamUrlDTO);
+}

+ 66 - 0
Backend/sample/src/main/java/com/dji/sample/manage/service/impl/ManageDeviceLivestreamUrlServiceImpl.java

@@ -0,0 +1,66 @@
+package com.dji.sample.manage.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.dji.sample.common.smsp.CallSmsp;
+import com.dji.sample.common.util.DesUtil;
+import com.dji.sample.manage.dao.IManageDeviceLivestreamUrlMapper;
+import com.dji.sample.manage.dao.IUserMapper;
+import com.dji.sample.manage.model.dto.ManageDeviceLivestreamUrlDTO;
+import com.dji.sample.manage.model.entity.ManageDeviceLivestreamUrlEntity;
+import com.dji.sample.manage.model.entity.UserEntity;
+import com.dji.sample.manage.model.enums.LiveUrlTypeEnum;
+import com.dji.sample.manage.service.IManageDeviceLivestreamUrlService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+
+@Service
+@Transactional
+@Slf4j
+public class ManageDeviceLivestreamUrlServiceImpl implements IManageDeviceLivestreamUrlService {
+
+    @Autowired
+    private IUserMapper userMapper;
+
+    @Autowired
+    private IManageDeviceLivestreamUrlMapper mapper;
+
+    private final String clientId = "e534550a85d94faba73e8040e76514bc";
+
+    @Override
+    public String getRtmpUrl(ManageDeviceLivestreamUrlDTO deviceLivestreamUrlDTO) {
+
+        QueryWrapper<ManageDeviceLivestreamUrlEntity> wrapper = new QueryWrapper<>();
+        wrapper.lambda().eq(ManageDeviceLivestreamUrlEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn())
+                .eq(ManageDeviceLivestreamUrlEntity::getWorkspaceId,deviceLivestreamUrlDTO.getWorkspaceId())
+                .eq(ManageDeviceLivestreamUrlEntity::getPayload,deviceLivestreamUrlDTO.getPayload());
+        ManageDeviceLivestreamUrlEntity urlEntity = mapper.selectOne(wrapper);
+        if (urlEntity != null) {
+            return urlEntity.getUrl();
+        }
+
+        QueryWrapper<UserEntity> userWrapper = new QueryWrapper<>();
+        userWrapper.lambda().eq(UserEntity::getUserType,3).eq(UserEntity::getClientId,clientId);
+        UserEntity userEntity = userMapper.selectOne(userWrapper);
+        if (userEntity == null) {
+            log.debug("The user is already null.");
+            return null;
+        }
+        //密码解密
+        String password = DesUtil.getDecryptData(userEntity.getPassword(),userEntity.getSalt());
+        //获取token
+        String token = CallSmsp.getSmspToken(userEntity.getUsername(),password);
+        //获取rtmp地址
+        String rtmp = CallSmsp.getRtmp(token,deviceLivestreamUrlDTO.getDeviceSn());
+        deviceLivestreamUrlDTO.setUrlType(LiveUrlTypeEnum.RTMP.getVal());
+        deviceLivestreamUrlDTO.setSubType(2);
+        //dto转entity
+        BeanUtils.copyProperties(urlEntity,deviceLivestreamUrlDTO);
+        //存库
+        mapper.insert(urlEntity);
+        return rtmp;
+    }
+}