Browse Source

流媒体修改

S0025136190 1 year ago
parent
commit
18dbc068f0

+ 41 - 28
Backend/sample/src/main/java/com/dji/sample/common/smsp/CallSmsp.java

@@ -16,22 +16,28 @@ import org.apache.http.util.EntityUtils;
 
 
 import java.time.Instant;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 
 
 
 
 @Slf4j
 @Slf4j
 public class CallSmsp {
 public class CallSmsp {
-    public static String getSmspToken(String username,String password) {
+    public static Map<String,String> getSmspToken(String username,String password) {
 //        String pass = MD5.create().digestHex(password);
 //        String pass = MD5.create().digestHex(password);
 //        Instant now = Instant.now();
 //        Instant now = Instant.now();
 //        long currentTimeMillis = now.toEpochMilli();
 //        long currentTimeMillis = now.toEpochMilli();
         String url = CustomConfiguration.smspTokenUrl+"?username="+username+"&password="+password;
         String url = CustomConfiguration.smspTokenUrl+"?username="+username+"&password="+password;
         try {
         try {
-            String response = sendGetRequest(url);
+            Map<String,String> response = sendGetRequest(url);
             log.debug("Response from API: " + response);
             log.debug("Response from API: " + response);
-            JSONObject json = new JSONObject(response);
-            JSONObject body = json.getJSONObject("EasyDarwin").getJSONObject("Body");
-            return body.getStr("Token");
+            if(response.get("data") != null) {
+                JSONObject json = new JSONObject(response.get("data"));
+                JSONObject body = json.getJSONObject("EasyDarwin").getJSONObject("Body");
+                response.put("Token",body.getStr("Token"));
+            }
+
+            return response;
         } catch (Exception e) {
         } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
             return null;
             return null;
@@ -46,28 +52,30 @@ public class CallSmsp {
         String url = CustomConfiguration.smspRtmpUrl+"?token="+token;
         String url = CustomConfiguration.smspRtmpUrl+"?token="+token;
         ObjectMapper objectMapper = new ObjectMapper();
         ObjectMapper objectMapper = new ObjectMapper();
         try {
         try {
-            String response = sendGetRequest(url);
+            Map<String,String> response = sendGetRequest(url);
             log.debug("Response from API: " + response);
             log.debug("Response from API: " + response);
-            JsonNode rootNode = objectMapper.readTree(response);
-            // 遍历顶层的设备列表
-            for (JsonNode deviceNode : rootNode.get("data")) {
-                String deviceName = deviceNode.get("DeviceName").asText();
-                // 查找特定设备名
-                if (deviceSn.equals(deviceName)) {
-                    // 获取该设备的子节点
-                    JsonNode childrenNode = deviceNode.get("Children");
-                    // 遍历子节点
-                    for (JsonNode childNode : childrenNode) {
-                        ManageDeviceLivestreamUrlDTO livestreamUrlDTO = new ManageDeviceLivestreamUrlDTO();
-                        // 获取RTMP和WEBRTC数据
-                        String rtmpUrl = childNode.get("RTMP").asText();
-                        String webRtcUrl = childNode.get("WEBRTC").asText();
-                        Integer channelID = childNode.get("ChannelID").asInt();
-                        livestreamUrlDTO.setChannelId(channelID);
-                        livestreamUrlDTO.setRtmpUrl(rtmpUrl);
-                        livestreamUrlDTO.setWebRtcUrl(webRtcUrl);
+            if(response.get("data") != null ){
+                JsonNode rootNode = objectMapper.readTree(response.get("data"));
+                // 遍历顶层的设备列表
+                for (JsonNode deviceNode : rootNode.get("data")) {
+                    String deviceName = deviceNode.get("DeviceName").asText();
+                    // 查找特定设备名
+                    if (deviceSn.equals(deviceName)) {
+                        // 获取该设备的子节点
+                        JsonNode childrenNode = deviceNode.get("Children");
+                        // 遍历子节点
+                        for (JsonNode childNode : childrenNode) {
+                            ManageDeviceLivestreamUrlDTO livestreamUrlDTO = new ManageDeviceLivestreamUrlDTO();
+                            // 获取RTMP和WEBRTC数据
+                            String rtmpUrl = childNode.get("RTMP").asText();
+                            String webRtcUrl = childNode.get("WEBRTC").asText();
+                            Integer channelID = childNode.get("ChannelID").asInt();
+                            livestreamUrlDTO.setChannelId(channelID);
+                            livestreamUrlDTO.setRtmpUrl(rtmpUrl);
+                            livestreamUrlDTO.setWebRtcUrl(webRtcUrl);
 
 
-                        resultList.add(livestreamUrlDTO);
+                            resultList.add(livestreamUrlDTO);
+                        }
                     }
                     }
                 }
                 }
             }
             }
@@ -78,16 +86,21 @@ public class CallSmsp {
         }
         }
     }
     }
 
 
-    private static String sendGetRequest(String requestURL) throws Exception {
+    private static Map<String,String> sendGetRequest(String requestURL) throws Exception {
         CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpGet httpGet = new HttpGet(requestURL);
         HttpGet httpGet = new HttpGet(requestURL);
         CloseableHttpResponse response = httpClient.execute(httpGet);
         CloseableHttpResponse response = httpClient.execute(httpGet);
+        Map<String,String> resultMap = new HashMap<>();
         try {
         try {
             int statusCode = response.getStatusLine().getStatusCode();
             int statusCode = response.getStatusLine().getStatusCode();
             if (statusCode == 200) {
             if (statusCode == 200) {
-                return EntityUtils.toString(response.getEntity());
+                resultMap.put("data",EntityUtils.toString(response.getEntity()));
+                return resultMap;
             } else {
             } else {
-                throw new RuntimeException("Failed : 流媒体平台错误 : " + statusCode);
+                log.debug("Failed : 流媒体平台错误 : " + statusCode +":"+response.getStatusLine().getReasonPhrase());
+                resultMap.put("code",String.valueOf(statusCode));
+                resultMap.put("message","Failed : 流媒体平台错误 : " + statusCode +":"+response.getStatusLine().getReasonPhrase());
+                return resultMap;
             }
             }
         } finally {
         } finally {
             response.close();
             response.close();

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

@@ -24,7 +24,7 @@ public class ManageDeviceLivestreamUrlDTO {
 
 
     private String payload;
     private String payload;
 
 
-    private Integer payloadIndex;
+    private String payloadIndex;
 
 
     private Integer urlType;
     private Integer urlType;
 
 

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

@@ -28,7 +28,7 @@ public class ManageDeviceLivestreamUrlEntity implements Serializable {
     private String payload;
     private String payload;
 
 
     @TableField(value = "payload_index")
     @TableField(value = "payload_index")
-    private Integer payloadIndex;
+    private String payloadIndex;
 
 
     @TableField(value = "url_type")
     @TableField(value = "url_type")
     private Integer urlType;
     private Integer urlType;

+ 23 - 2
Backend/sample/src/main/java/com/dji/sample/manage/service/impl/LiveStreamServiceImpl.java

@@ -4,6 +4,8 @@ import com.dji.sample.manage.model.dto.*;
 import com.dji.sample.manage.model.param.DeviceQueryParam;
 import com.dji.sample.manage.model.param.DeviceQueryParam;
 import com.dji.sample.manage.service.*;
 import com.dji.sample.manage.service.*;
 import com.dji.sdk.cloudapi.device.DeviceDomainEnum;
 import com.dji.sdk.cloudapi.device.DeviceDomainEnum;
+import com.dji.sdk.cloudapi.device.DeviceEnum;
+import com.dji.sdk.cloudapi.device.PayloadIndex;
 import com.dji.sdk.cloudapi.device.VideoId;
 import com.dji.sdk.cloudapi.device.VideoId;
 import com.dji.sdk.cloudapi.livestream.*;
 import com.dji.sdk.cloudapi.livestream.*;
 import com.dji.sdk.cloudapi.livestream.api.AbstractLivestreamService;
 import com.dji.sdk.cloudapi.livestream.api.AbstractLivestreamService;
@@ -47,6 +49,9 @@ public class LiveStreamServiceImpl implements ILiveStreamService {
     @Autowired
     @Autowired
     private IManageDeviceLivestreamUrlService deviceLivestreamUrlService;
     private IManageDeviceLivestreamUrlService deviceLivestreamUrlService;
 
 
+    @Autowired
+    private IDeviceDictionaryService deviceDictionaryService;
+
     @Override
     @Override
     public List<CapacityDeviceDTO> getLiveCapacity(String workspaceId) {
     public List<CapacityDeviceDTO> getLiveCapacity(String workspaceId) {
 
 
@@ -75,10 +80,15 @@ public class LiveStreamServiceImpl implements ILiveStreamService {
         if (HttpResultResponse.CODE_SUCCESS != responseResult.getCode()) {
         if (HttpResultResponse.CODE_SUCCESS != responseResult.getCode()) {
             return responseResult;
             return responseResult;
         }
         }
+
         ManageDeviceLivestreamUrlDTO livestreamUrlDTO = new ManageDeviceLivestreamUrlDTO();
         ManageDeviceLivestreamUrlDTO livestreamUrlDTO = new ManageDeviceLivestreamUrlDTO();
         livestreamUrlDTO.setWorkspaceId(responseResult.getData().getWorkspaceId());
         livestreamUrlDTO.setWorkspaceId(responseResult.getData().getWorkspaceId());
-        livestreamUrlDTO.setPayload(liveParam.getVideoId().getPayloadIndex().toString());
-        livestreamUrlDTO.setDeviceSn(responseResult.getData().getDeviceSn());
+        liveParam.getVideoId().getPayloadIndex().getSubType();
+        liveParam.getVideoId().getPayloadIndex().getType();
+
+        livestreamUrlDTO.setPayload(getDeviceNameByModelKey(liveParam.getVideoId().getPayloadIndex()));
+        livestreamUrlDTO.setPayloadIndex(liveParam.getVideoId().getPayloadIndex().toString());
+        livestreamUrlDTO.setDeviceSn(liveParam.getVideoId().getDroneSn());
         livestreamUrlDTO = deviceLivestreamUrlService.getUrl(livestreamUrlDTO);
         livestreamUrlDTO = deviceLivestreamUrlService.getUrl(livestreamUrlDTO);
 
 
         ILivestreamUrl url = LiveStreamProperty.get(liveParam.getUrlType());
         ILivestreamUrl url = LiveStreamProperty.get(liveParam.getUrlType());
@@ -245,4 +255,15 @@ public class LiveStreamServiceImpl implements ILiveStreamService {
         }
         }
         return url;
         return url;
     }
     }
+
+    private String getDeviceNameByModelKey(PayloadIndex modelKey) {
+        Optional<DeviceDictionaryDTO> payloadDict = deviceDictionaryService
+                .getOneDictionaryInfoByTypeSubType(DeviceDomainEnum.PAYLOAD.getDomain(),
+                        modelKey.getType().getType(), modelKey.getSubType().getSubType());
+        if(payloadDict.isPresent()) {
+            return payloadDict.get().getDeviceName();
+        }
+        return  "";
+
+    }
 }
 }

+ 40 - 31
Backend/sample/src/main/java/com/dji/sample/manage/service/impl/ManageDeviceLivestreamUrlServiceImpl.java

@@ -14,13 +14,16 @@ import com.dji.sample.manage.model.enums.LiveUrlTypeEnum;
 import com.dji.sample.manage.model.enums.UserTypeEnum;
 import com.dji.sample.manage.model.enums.UserTypeEnum;
 import com.dji.sample.manage.service.IManageDeviceLivestreamUrlService;
 import com.dji.sample.manage.service.IManageDeviceLivestreamUrlService;
 import com.dji.sdk.cloudapi.device.DeviceSubTypeEnum;
 import com.dji.sdk.cloudapi.device.DeviceSubTypeEnum;
+import com.dji.sdk.common.HttpResultResponse;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 
 
@@ -54,35 +57,26 @@ public class ManageDeviceLivestreamUrlServiceImpl implements IManageDeviceLivest
         //密码解密
         //密码解密
         String password = DesUtil.getDecryptData(userEntity.getPassword(),userEntity.getSalt());
         String password = DesUtil.getDecryptData(userEntity.getPassword(),userEntity.getSalt());
         //获取token
         //获取token
-        String token = CallSmsp.getSmspToken(userEntity.getUsername(),password);
-        if(token == null) {
+        Map<String,String> tokenMap = CallSmsp.getSmspToken(userEntity.getUsername(),password);
+        if(tokenMap.get("Token") == null) {
             log.debug("The smsp token is null.");
             log.debug("The smsp token is null.");
         }
         }
 
 
         //获取rtmp地址
         //获取rtmp地址
-        List<ManageDeviceLivestreamUrlDTO> rtmpList = CallSmsp.getRtmp(token,deviceLivestreamUrlDTO.getDeviceSn(),deviceLivestreamUrlDTO.getPayload());
+        List<ManageDeviceLivestreamUrlDTO> rtmpList = CallSmsp.getRtmp(tokenMap.get("Token"),deviceLivestreamUrlDTO.getDeviceSn(),deviceLivestreamUrlDTO.getPayload());
         ManageDeviceLivestreamUrlDTO rtmpDto = new ManageDeviceLivestreamUrlDTO();
         ManageDeviceLivestreamUrlDTO rtmpDto = new ManageDeviceLivestreamUrlDTO();
-        //获取payload信息
-        QueryWrapper<DevicePayloadEntity> payloadWrapper = new QueryWrapper<>();
-        payloadWrapper.lambda().eq(DevicePayloadEntity::getPayloadIndex,deviceLivestreamUrlDTO.getPayload())
-                .eq(DevicePayloadEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn());
-        DevicePayloadEntity payloadEntity = payloadMapper.selectOne(payloadWrapper);
-
-        deviceLivestreamUrlDTO.setPayload(payloadEntity.getPayloadName());
-        deviceLivestreamUrlDTO.setPayloadIndex(payloadEntity.getPayloadIndex());
-
 
 
         //获取数据库中deviceSn对应的url信息
         //获取数据库中deviceSn对应的url信息
         QueryWrapper<ManageDeviceLivestreamUrlEntity> wrapper = new QueryWrapper<>();
         QueryWrapper<ManageDeviceLivestreamUrlEntity> wrapper = new QueryWrapper<>();
         wrapper.lambda().eq(ManageDeviceLivestreamUrlEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn())
         wrapper.lambda().eq(ManageDeviceLivestreamUrlEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn())
                 .eq(ManageDeviceLivestreamUrlEntity::getWorkspaceId,deviceLivestreamUrlDTO.getWorkspaceId());
                 .eq(ManageDeviceLivestreamUrlEntity::getWorkspaceId,deviceLivestreamUrlDTO.getWorkspaceId());
         List<ManageDeviceLivestreamUrlEntity> urlList = mapper.selectList(wrapper);
         List<ManageDeviceLivestreamUrlEntity> urlList = mapper.selectList(wrapper);
-        if(urlList.size() < 0 && rtmpList.size() < 0 ){
+        if(urlList.size() <= 0 && rtmpList.size() <= 0 ){
             log.debug("The result is null.");
             log.debug("The result is null.");
-            return null;
+            throw new RuntimeException (tokenMap.get("message"));
         }
         }
         //过滤payloadIndex
         //过滤payloadIndex
-        List<ManageDeviceLivestreamUrlEntity> filterIndexList = urlList.stream().filter(channelData -> payloadEntity.getPayloadIndex().equals(channelData.getPayloadIndex()))
+        List<ManageDeviceLivestreamUrlEntity> filterIndexList = urlList.stream().filter(channelData -> deviceLivestreamUrlDTO.getPayloadIndex().equals(channelData.getPayloadIndex()))
                 .collect(Collectors.toList());
                 .collect(Collectors.toList());
         if (filterIndexList.size() > 0) {
         if (filterIndexList.size() > 0) {
             //过滤channelId
             //过滤channelId
@@ -105,27 +99,40 @@ public class ManageDeviceLivestreamUrlServiceImpl implements IManageDeviceLivest
                 rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
                 rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
                 rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
                 rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
             } else {
             } else {
-                //过滤channelId
-                channelList = rtmpList.stream().filter(channelData -> !urlList.contains(channelData.getChannelId()))
-                        .collect(Collectors.toList());
-                for(ManageDeviceLivestreamUrlEntity url : filterIndexList) {
-                    url.setChannelId(channelList.get(0).getChannelId());
-
-                    if (url.getUrlType().equals(LiveUrlTypeEnum.RTMP.getVal())) {
-                        url.setUrl(channelList.get(0).getRtmpUrl());
-                        mapper.updateById(url);
-                    } else if(url.getUrlType().equals(LiveUrlTypeEnum.WEBRTC.getVal())) {
-                        url.setUrl(channelList.get(0).getWebRtcUrl());
-                        mapper.updateById(url);
+                if(rtmpList.size() > 0) {
+                    //过滤channelId
+                    channelList = rtmpList.stream().filter(channelData -> !urlList.contains(channelData.getChannelId()))
+                            .collect(Collectors.toList());
+                    for(ManageDeviceLivestreamUrlEntity url : filterIndexList) {
+                        url.setChannelId(channelList.get(0).getChannelId());
+
+                        if (url.getUrlType().equals(LiveUrlTypeEnum.RTMP.getVal())) {
+                            url.setUrl(channelList.get(0).getRtmpUrl());
+                            mapper.updateById(url);
+                        } else if(url.getUrlType().equals(LiveUrlTypeEnum.WEBRTC.getVal())) {
+                            url.setUrl(channelList.get(0).getWebRtcUrl());
+                            mapper.updateById(url);
+                        }
+                    }
+                    rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
+                    rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
+                } else {
+                    for(ManageDeviceLivestreamUrlEntity url : filterIndexList) {
+                        if (url.getUrlType().equals(LiveUrlTypeEnum.RTMP.getVal())) {
+                            rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
+                        } else if(url.getUrlType().equals(LiveUrlTypeEnum.WEBRTC.getVal())) {
+                            rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
+                        }
                     }
                     }
                 }
                 }
-                rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
-                rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
             }
             }
         } else {
         } else {
             //过滤channelId
             //过滤channelId
-            List<ManageDeviceLivestreamUrlDTO> channelList = rtmpList.stream().filter(channelData -> !urlList.contains(channelData.getChannelId()))
-                    .collect(Collectors.toList());
+            List<ManageDeviceLivestreamUrlDTO> channelList = rtmpList;
+            if(urlList.size() > 0) {
+                channelList = rtmpList.stream().filter(channelData -> urlList.contains(channelData.getChannelId()))
+                        .collect(Collectors.toList());
+            }
             ManageDeviceLivestreamUrlEntity urlEntity = new ManageDeviceLivestreamUrlEntity();
             ManageDeviceLivestreamUrlEntity urlEntity = new ManageDeviceLivestreamUrlEntity();
             if(channelList.size() > 0) {
             if(channelList.size() > 0) {
                 //dto转entity
                 //dto转entity
@@ -144,6 +151,8 @@ public class ManageDeviceLivestreamUrlServiceImpl implements IManageDeviceLivest
                 mapper.insert(urlEntity);
                 mapper.insert(urlEntity);
                 rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
                 rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
                 rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
                 rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
+            } else {
+                throw new RuntimeException("the url is null.");
             }
             }
         }
         }
         return rtmpDto;
         return rtmpDto;