xieb
2023-09-23 eeefddd9cb9759febda0d81488a490b3cc2650fa
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
package com.dji.sample.manage.service.impl;
 
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.model.dto.TopologyDTO;
import com.dji.sample.manage.model.dto.TopologyDeviceDTO;
import com.dji.sample.manage.model.enums.DeviceDomainEnum;
import com.dji.sample.manage.model.param.DeviceQueryParam;
import com.dji.sample.manage.service.IDeviceService;
import com.dji.sample.manage.service.ITopologyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
 
/**
 * @author sean
 * @version 0.2
 * @date 2021/12/8
 */
@Service
public class TopologyServiceImpl implements ITopologyService {
 
    @Autowired
    private IDeviceService deviceService;
 
    @Override
    public List<TopologyDTO> getDeviceTopology(String workspaceId) {
        // Query the information of all gateway devices in the workspace.
        List<DeviceDTO> gatewayList = deviceService.getDevicesByParams(
                DeviceQueryParam.builder()
                        .workspaceId(workspaceId)
                        .domains(List.of(DeviceDomainEnum.GATEWAY.getVal()))
                        .build());
 
        List<TopologyDTO> topologyList = new ArrayList<>();
 
        gatewayList.forEach(device -> this.getDeviceTopologyByGatewaySn(device.getDeviceSn())
                .ifPresent(topologyList::add));
 
        return topologyList;
    }
 
    public Optional<TopologyDTO> getDeviceTopologyByGatewaySn(String gatewaySn) {
        Optional<DeviceDTO> dtoOptional = deviceService.getDeviceBySn(gatewaySn);
        if (dtoOptional.isEmpty()) {
            return Optional.empty();
        }
        List<TopologyDeviceDTO> parents = new ArrayList<>();
        DeviceDTO device = dtoOptional.get();
        TopologyDeviceDTO gateway = deviceService.deviceConvertToTopologyDTO(device);
        parents.add(gateway);
 
        // Query the topology data of the drone based on the drone sn.
        Optional<TopologyDeviceDTO> deviceTopo = deviceService.getDeviceTopoForPilot(device.getChildDeviceSn());
        List<TopologyDeviceDTO> deviceTopoList = new ArrayList<>();
        deviceTopo.ifPresent(deviceTopoList::add);
 
        return Optional.ofNullable(TopologyDTO.builder().parents(parents).hosts(deviceTopoList).build());
    }
}