|
|
@@ -39,6 +39,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
import java.util.*;
|
|
|
@@ -71,9 +72,6 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
@Autowired
|
|
|
private IFlightTrackTaskService flightTaskService;
|
|
|
|
|
|
- // 定义一个静态变量来存储上次调用的时间戳
|
|
|
- private static long lastInvocationTimestamp = 0;
|
|
|
-
|
|
|
@Override
|
|
|
public TopicStatusResponse<MqttReply> updateTopoOnline(TopicStatusRequest<UpdateTopo> request, MessageHeaders headers) {
|
|
|
UpdateTopoSubDevice updateTopoSubDevice = request.getData().getSubDevices().get(0);
|
|
|
@@ -263,7 +261,6 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
private void toAddFilghtPoint(String from, DeviceDTO device,OsdRcDrone data) {
|
|
|
|
|
|
switch (data.getModeCode()) {
|
|
|
- //case IDLE:
|
|
|
case TAKEOFF_FINISHED:
|
|
|
case MANUAL:
|
|
|
case TAKEOFF_AUTO:
|
|
|
@@ -274,75 +271,87 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
case RETURN_AUTO:
|
|
|
case LIVE_FLIGHT_CONTROLS:
|
|
|
case LANDING_THREE_PROPELLER:
|
|
|
+ FlightTaskDTO flightTask = getFlightTask(device);
|
|
|
+ if(flightTask == null) {
|
|
|
+ Optional<String> lockOpt = deviceRedisService.getDroneFlightTaskLock(device.getDeviceSn());
|
|
|
+ if(lockOpt.isPresent()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("==============创建飞行任务====:" + data.toString());
|
|
|
+ deviceRedisService.setDroneFlightTaskLock(device.getDeviceSn());
|
|
|
+ flightTask = createFlightTask(device);
|
|
|
+ }
|
|
|
//添加飞行轨迹
|
|
|
- addFlightPoint(device,data);
|
|
|
+ addFlightPoint(flightTask, device,data);
|
|
|
//推送实时轨迹信息
|
|
|
- pushFlyTrackToWeb(from,device,data);
|
|
|
+ List<FlightTrackDTO> trackList = getRedisFlightPoint(flightTask,device,data);
|
|
|
+ pushFlyTrackToWeb(from,device,trackList);
|
|
|
|
|
|
break;
|
|
|
case LANDING_AUTO:
|
|
|
case LANDING_FORCED:
|
|
|
//飞行结束
|
|
|
+ log.info("==============飞机降落====:" + data.toString());
|
|
|
+ Optional<FlightTaskDTO> opt = deviceRedisService.getDroneFlightTask(device.getDeviceSn());
|
|
|
+ if(opt.isPresent()) {
|
|
|
+ //移除redis 任务
|
|
|
+ deviceRedisService.delDroneFlightTask(device.getDeviceSn());
|
|
|
+
|
|
|
+ flightTask = opt.get();
|
|
|
+ log.info("==============飞机降落 执行添加飞行轨迹====:" + flightTask.toString());
|
|
|
+ //添加飞行轨迹
|
|
|
+ FlightTaskDTO task = addFlightPoint(flightTask,device,data);
|
|
|
+ //更新飞行任务
|
|
|
+ updateFlightTask(task);
|
|
|
|
|
|
- //添加飞行轨迹
|
|
|
- FlightTaskDTO task = addFlightPoint(device,data);
|
|
|
- //推送实时轨迹信息
|
|
|
- pushFlyTrackToWeb(from,device,data);
|
|
|
+ //推送实时轨迹信息
|
|
|
+ trackList = getRedisFlightPoint(flightTask,device,data);
|
|
|
+ pushFlyTrackToWeb(from,device,trackList);
|
|
|
|
|
|
- //更新飞行任务
|
|
|
- if(task != null) {
|
|
|
- updateFlightTask(task);
|
|
|
+ //移除redis航线
|
|
|
+ deviceRedisService.delDroneFlyTrack(device.getDeviceSn());
|
|
|
}
|
|
|
- //移除redis
|
|
|
- deviceRedisService.delDroneFlightTask(device.getDeviceSn());
|
|
|
- deviceRedisService.delDroneFlyTrack(device.getDeviceSn());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void pushFlyTrackToWeb(String from, DeviceDTO device,OsdRcDrone data) {
|
|
|
+ private void pushFlyTrackToWeb(String from, DeviceDTO device,List<FlightTrackDTO> trackList) {
|
|
|
//推送实时轨迹信息
|
|
|
- List<FlightTrackDTO> trackList = getFlightLine(device);
|
|
|
- if (System.currentTimeMillis() - lastInvocationTimestamp >= 2000) {
|
|
|
- if(!CollectionUtils.isEmpty(trackList)) {
|
|
|
- lastInvocationTimestamp = System.currentTimeMillis();
|
|
|
- deviceService.pushOsdDataToWeb(device.getWorkspaceId(), BizCodeEnum.DRONE_FLY_TRACK, from, trackList);
|
|
|
- }
|
|
|
- //模拟第二条实时航线
|
|
|
- List<FlightTrackDTO> flightPoints = flightTaskService.getRTFlightTrackByTaskId(44);
|
|
|
+ if(!CollectionUtils.isEmpty(trackList)) {
|
|
|
deviceService.pushOsdDataToWeb(device.getWorkspaceId(), BizCodeEnum.DRONE_FLY_TRACK, from, trackList);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- private List<FlightTrackDTO> getFlightLine(DeviceDTO device) {
|
|
|
+ private List<FlightTrackDTO> getRedisFlightPoint(FlightTaskDTO task,DeviceDTO device,OsdRcDrone data) {
|
|
|
+ List<FlightTrackDTO> flightPoints = null;
|
|
|
Optional<List<FlightTrackDTO>> optList = deviceRedisService.getDroneFlyTrack(device.getDeviceSn());
|
|
|
- if(!optList.isPresent() || optList.get().isEmpty()) {
|
|
|
- //获取飞行任务(轨迹)
|
|
|
- FlightTaskDTO task = getFlightTask(device, false);
|
|
|
- if(task == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- List<FlightTrackDTO> flightPoints = flightTaskService.getRTFlightTrackByTaskId(task.getId());
|
|
|
- deviceRedisService.setDroneFlyTrack(device.getDeviceSn(),flightPoints);
|
|
|
- return flightPoints;
|
|
|
+ if(optList.isPresent()) {
|
|
|
+ flightPoints = optList.get();
|
|
|
} else {
|
|
|
- return optList.get();
|
|
|
+ flightPoints = new ArrayList<FlightTrackDTO>();
|
|
|
}
|
|
|
+ addRedisFlightPoint(task,flightPoints,device,data);
|
|
|
+ return flightPoints;
|
|
|
}
|
|
|
|
|
|
- private void addRedisFlightPoint(String sn,FlightTrackDTO point) {
|
|
|
- List<FlightTrackDTO> flightPoints = new ArrayList<FlightTrackDTO>();
|
|
|
- Optional<List<FlightTrackDTO>> optList = deviceRedisService.getDroneFlyTrack(sn);
|
|
|
- if(optList.isPresent()) {
|
|
|
- flightPoints = optList.get();
|
|
|
- }
|
|
|
+ private void addRedisFlightPoint(FlightTaskDTO task, List<FlightTrackDTO> flightPoints, DeviceDTO device,OsdRcDrone data) {
|
|
|
+ FlightTrackDTO point = FlightTrackDTO.builder()
|
|
|
+ .taskId(task.getId())
|
|
|
+ .modeCode(data.getModeCode().getCode())
|
|
|
+ .longitude(data.getLongitude().doubleValue())
|
|
|
+ .latitude(data.getLatitude().doubleValue())
|
|
|
+ .altitude(data.getHeight().doubleValue())
|
|
|
+ .elevation(data.getElevation().doubleValue())
|
|
|
+ .type(FlightPointTypeEnum.HAND.getType())
|
|
|
+ .createTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault())).build();
|
|
|
flightPoints.add(point);
|
|
|
- deviceRedisService.setDroneFlyTrack(sn,flightPoints);
|
|
|
+ deviceRedisService.setDroneFlyTrack(device.getDeviceSn(),flightPoints);
|
|
|
}
|
|
|
|
|
|
- private FlightTaskDTO addFlightPoint(DeviceDTO device,OsdRcDrone data) {
|
|
|
+ private FlightTaskDTO addFlightPoint(FlightTaskDTO task, DeviceDTO device,OsdRcDrone data) {
|
|
|
synchronized (device.getDeviceSn().intern()) {
|
|
|
Optional<FlightTrackEntity> lastPoint = deviceRedisService.getLastDroneTrack(device.getDeviceSn());
|
|
|
long curTime = System.currentTimeMillis();
|
|
|
@@ -371,8 +380,6 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
}
|
|
|
}
|
|
|
if(isAdd) {
|
|
|
- //获取飞行任务(轨迹)
|
|
|
- FlightTaskDTO task = getFlightTask(device, true);
|
|
|
log.info("获取飞行轨迹任务:" + task.toString());
|
|
|
//添加Home信息
|
|
|
if(!lastPoint.isPresent()) {
|
|
|
@@ -386,7 +393,6 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
.type(FlightPointTypeEnum.HOME.getType())
|
|
|
.createTime(System.currentTimeMillis()).build();
|
|
|
flightTaskService.addHomePoint(flightTrackEntity);
|
|
|
- addRedisFlightPoint(device.getDeviceSn(),flightTaskService.flightTrackEntityToDto(flightTrackEntity));
|
|
|
}
|
|
|
}
|
|
|
//添加轨迹信息
|
|
|
@@ -400,7 +406,6 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
.type(FlightPointTypeEnum.HAND.getType())
|
|
|
.createTime(System.currentTimeMillis()).build();
|
|
|
flightTaskService.addFlightPoint(flightTrackEntity);
|
|
|
- addRedisFlightPoint(device.getDeviceSn(),flightTaskService.flightTrackEntityToDto(flightTrackEntity));
|
|
|
log.info("添加轨迹完成: task:" + task.getTaskName());
|
|
|
//记录添加时间
|
|
|
deviceRedisService.setLastDroneTrack(device.getDeviceSn(), flightTrackEntity);
|
|
|
@@ -432,7 +437,7 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- private FlightTaskDTO getFlightTask(DeviceDTO device,boolean isCreate) {
|
|
|
+ private FlightTaskDTO getFlightTask(DeviceDTO device) {
|
|
|
//获取任务名称
|
|
|
FlightTaskDTO flightTaskDTO = null;
|
|
|
Optional<FlightTaskDTO> opt = deviceRedisService.getDroneFlightTask(device.getDeviceSn());
|
|
|
@@ -442,25 +447,27 @@ public class SDKDeviceService extends AbstractDeviceService {
|
|
|
//查询当天未完成的任务
|
|
|
flightTaskDTO = flightTaskService.getCurrentFlightTask(device.getWorkspaceId(),device.getDeviceSn());
|
|
|
}
|
|
|
- if(flightTaskDTO == null && isCreate) {
|
|
|
- Date curTime = new Date();
|
|
|
- String taskName = getTaskName(curTime, device.getDeviceSn());
|
|
|
- flightTaskDTO = flightTaskService.createFlightTask(FlightTaskEntity.builder()
|
|
|
- .taskName(taskName)
|
|
|
- .workspaceId(device.getWorkspaceId())
|
|
|
- .deviceSn(device.getDeviceSn())
|
|
|
- .deviceName(device.getDeviceName())
|
|
|
- .payload(device.getPayloadsList() == null ? null : device.getPayloadsList().get(0).getPayloadName())
|
|
|
- .beginTime(curTime.getTime())
|
|
|
- .createTime(curTime.getTime())
|
|
|
- .username(UserRequest.getCurrentUser() != null ? UserRequest.getCurrentUser().getUsername() : "system")
|
|
|
- .build());
|
|
|
-
|
|
|
- }
|
|
|
- deviceRedisService.setDroneFlightTask(device.getDeviceSn(),flightTaskDTO);
|
|
|
+ if(flightTaskDTO != null) {
|
|
|
+ deviceRedisService.setDroneFlightTask(device.getDeviceSn(), flightTaskDTO);
|
|
|
+ }
|
|
|
return flightTaskDTO;
|
|
|
}
|
|
|
|
|
|
+ private FlightTaskDTO createFlightTask(DeviceDTO device) {
|
|
|
+ Date curTime = new Date();
|
|
|
+ String taskName = getTaskName(curTime, device.getDeviceSn());
|
|
|
+ return flightTaskService.createFlightTask(FlightTaskEntity.builder()
|
|
|
+ .taskName(taskName)
|
|
|
+ .workspaceId(device.getWorkspaceId())
|
|
|
+ .deviceSn(device.getDeviceSn())
|
|
|
+ .deviceName(device.getDeviceName())
|
|
|
+ .payload(device.getPayloadsList() == null ? null : device.getPayloadsList().get(0).getPayloadName())
|
|
|
+ .beginTime(curTime.getTime())
|
|
|
+ .createTime(curTime.getTime())
|
|
|
+ .username(UserRequest.getCurrentUser() != null ? UserRequest.getCurrentUser().getUsername() : "system")
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
//飞行器sn yyyy-MM-dd HH:mm:ss
|
|
|
private String getTaskName(Date curTime,String droneSn) {
|
|
|
String format = DateUtil.format(curTime, "yyyy-MM-dd HH:mm:ss");
|