diff --git a/src/main/java/com/corewing/app/dto/api/TrajectoryData.java b/src/main/java/com/corewing/app/dto/api/TrajectoryData.java new file mode 100644 index 0000000..091e2dc --- /dev/null +++ b/src/main/java/com/corewing/app/dto/api/TrajectoryData.java @@ -0,0 +1,47 @@ +package com.corewing.app.dto.api; + +import lombok.Data; + +@Data +public class TrajectoryData { + private Long id; + private Long session_id; + private Long timestamp; + + // 基础遥测数据 + private Long rc; + private Long gps; + private Long gps_status; + private double voltage; + private double cell_voltage; + private Long consumption; + private double current; + private double speed; + private double air_speed; + private double v_speed; + private double altitude; + private double distance; + private double range; + private Long throttle; + private Long wind_direction; + private double wind_speed; + private Long heading; + private double roll; + private double pitch; + private double yaw; + + // 扩展数据 + private double compass_heading; + private double drone_lat; + private double drone_lon; + private double home_lat; + private double home_lon; + private String map_type; + private Boolean show_trajectory; + private String flight_mode; + private Long vehicle_type; + private Long autopilot_type; + private Boolean is_armed; + + private String sync_id; +} diff --git a/src/main/java/com/corewing/app/dto/api/uploadReplaySessionRequest.java b/src/main/java/com/corewing/app/dto/api/uploadReplaySessionRequest.java index 9b182fc..2c91e1d 100644 --- a/src/main/java/com/corewing/app/dto/api/uploadReplaySessionRequest.java +++ b/src/main/java/com/corewing/app/dto/api/uploadReplaySessionRequest.java @@ -11,57 +11,16 @@ public class uploadReplaySessionRequest { private Long id; private String name; private String description; - private Long startTime; - private Long endTime; + private Long start_time; + private Long end_time; private Long duration; // 持续时间(秒) - private Long dataCount; // 数据点数量 - private Date createdAt; - private Date updatedAt; - + private Long data_count; // 数据点数量 + private Date created_at; + private Date updated_at; + private String sync_id; List trajectoryDataList; } -@Data -class TrajectoryData { - private Long id; - private Long sessionId; - private Long timestamp; - - // 基础遥测数据 - private Long rc; - private Long gps; - private Long gpsStatus; - private double voltage; - private double cellVoltage; - private Long consumption; - private double current; - private double speed; - private double airSpeed; - private double vSpeed; - private double altitude; - private double distance; - private double range; - private Long throttle; - private Long windDirection; - private double windSpeed; - private Long heading; - private double roll; - private double pitch; - private double yaw; - - // 扩展数据 - private double compassHeading; - private double droneLat; - private double droneLon; - private double homeLat; - private double homeLon; - private String mapType; - private Boolean showTrajectory; - private String flightMode; - private Long vehicleType; - private Long autopilotType; - private Boolean isArmed; -} diff --git a/src/main/java/com/corewing/app/entity/ReplaySession.java b/src/main/java/com/corewing/app/entity/ReplaySession.java index 9f59e5a..6201be4 100644 --- a/src/main/java/com/corewing/app/entity/ReplaySession.java +++ b/src/main/java/com/corewing/app/entity/ReplaySession.java @@ -1,11 +1,14 @@ package com.corewing.app.entity; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.corewing.app.dto.api.TrajectoryData; import lombok.Data; import java.util.Date; +import java.util.List; @Data @TableName("app_replay_session") @@ -14,9 +17,11 @@ public class ReplaySession { /** * id */ - @TableId(value = "id", type = IdType.INPUT) + @TableId(value = "id", type = IdType.AUTO) private Long id; + private String syncId; + /** * 用户id */ @@ -66,4 +71,10 @@ public class ReplaySession { * 更新时间 */ private Date updatedAt; + + /** + * 轨迹数据点 + */ + @TableField(exist = false) + List trajectoryDataList; } diff --git a/src/main/java/com/corewing/app/modules/app/AppReplaySessionController.java b/src/main/java/com/corewing/app/modules/app/AppReplaySessionController.java index 3844b53..a2b5311 100644 --- a/src/main/java/com/corewing/app/modules/app/AppReplaySessionController.java +++ b/src/main/java/com/corewing/app/modules/app/AppReplaySessionController.java @@ -36,4 +36,14 @@ public class AppReplaySessionController { return Result.isBool(replaySessionService.uploadReplaySession(uploadReplaySessionRequests)); } + /** + * 根据同步id删除数据 + * @param syncId + * @return + */ + @DeleteMapping("/deleteReplaySession/{syncId}") + public Result deleteReplaySession(@PathVariable String syncId) { + return Result.isBool(replaySessionService.deleteReplaySession(syncId)); + } + } diff --git a/src/main/java/com/corewing/app/service/ReplaySessionService.java b/src/main/java/com/corewing/app/service/ReplaySessionService.java index 64c0043..f70e47c 100644 --- a/src/main/java/com/corewing/app/service/ReplaySessionService.java +++ b/src/main/java/com/corewing/app/service/ReplaySessionService.java @@ -8,7 +8,10 @@ import java.util.List; public interface ReplaySessionService extends IService { + List getReplayList(Object loginId); boolean uploadReplaySession(List uploadReplaySessionRequests); + + boolean deleteReplaySession(String syncId); } diff --git a/src/main/java/com/corewing/app/service/impl/ReplaySessionServiceImpl.java b/src/main/java/com/corewing/app/service/impl/ReplaySessionServiceImpl.java index baa1e1c..7260e8a 100644 --- a/src/main/java/com/corewing/app/service/impl/ReplaySessionServiceImpl.java +++ b/src/main/java/com/corewing/app/service/impl/ReplaySessionServiceImpl.java @@ -1,55 +1,121 @@ package com.corewing.app.service.impl; +import cn.dev33.satoken.stp.StpUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.corewing.app.dto.api.TrajectoryData; import com.corewing.app.dto.api.uploadReplaySessionRequest; import com.corewing.app.entity.ReplaySession; import com.corewing.app.mapper.ReplaySessionMapper; import com.corewing.app.service.ReplaySessionService; -import com.fasterxml.jackson.core.JsonProcessingException; +import com.corewing.app.util.OSSUploadUtil; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; -import com.google.gson.JsonObject; -import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONObject; +import com.google.gson.reflect.TypeToken; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import javax.annotation.Resource; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.List; @Service public class ReplaySessionServiceImpl extends ServiceImpl implements ReplaySessionService { - private final Gson gson; - public ReplaySessionServiceImpl(Gson gson) { - this.gson = gson; - } + @Resource + private Gson gson; + /** + * 获取飞行记录 + * @param loginId + * @return + */ @Override public List getReplayList(Object loginId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ReplaySession::getUserId, loginId); - return this.list(queryWrapper); + // 去oss里面下载对应的json文件流,解析到文件里面 + List list = list(queryWrapper); + list.forEach(m -> { + try { + InputStream inputStream = OSSUploadUtil.downloadFileToStream(m.getOssPath()); + List dataList = new Gson().fromJson( + new InputStreamReader(inputStream, StandardCharsets.UTF_8), + new TypeToken>() {}.getType() // 增加 .getType() + ); + m.setTrajectoryDataList(dataList); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + + return list; } + /** + * 同步飞行记录 + * @param uploadReplaySessionRequests + * @return + */ @Transactional @Override public boolean uploadReplaySession(List uploadReplaySessionRequests) { uploadReplaySessionRequests.forEach(replaySessionRequest -> { + + // 校验是否重复 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(ReplaySession::getSyncId, replaySessionRequest.getSync_id()); + if (count(queryWrapper) > 0) { + return; + } + ReplaySession replaySession = new ReplaySession(); - replaySession.setId(replaySessionRequest.getId()); + replaySession.setSyncId(replaySessionRequest.getSync_id()); replaySession.setName(replaySessionRequest.getName()); replaySession.setDuration(replaySessionRequest.getDuration()); - replaySession.setStartTime(replaySessionRequest.getStartTime()); - replaySession.setEndTime(replaySessionRequest.getEndTime()); + replaySession.setStartTime(replaySessionRequest.getStart_time()); + replaySession.setEndTime(replaySessionRequest.getEnd_time()); replaySession.setDescription(replaySessionRequest.getDescription()); - replaySession.setDataCount(replaySessionRequest.getDataCount()); - replaySession.setCreatedAt(replaySessionRequest.getCreatedAt()); - replaySession.setUpdatedAt(replaySessionRequest.getUpdatedAt()); - saveOrUpdate(replaySession); + replaySession.setDataCount(replaySessionRequest.getData_count()); + replaySession.setCreatedAt(replaySessionRequest.getCreated_at()); + replaySession.setUpdatedAt(replaySessionRequest.getUpdated_at()); + replaySession.setUserId(Long.valueOf(StpUtil.getLoginId().toString())); + String json = gson.toJson(replaySessionRequest.getTrajectoryDataList()); - System.out.println(json); + // 必须指定编码,避免平台默认编码问题 + InputStream inputStream = new ByteArrayInputStream( + json.getBytes(StandardCharsets.UTF_8) + ); + String ossPath = "trajectory/" + replaySessionRequest.getSync_id() + ".json"; + OSSUploadUtil.uploadFile(inputStream, ossPath); + + replaySession.setOssPath(ossPath); + save(replaySession); }); return true; } + + /** + * 根据syncId删除数据 + * @param syncId + * @return + */ + @Override + public boolean deleteReplaySession(String syncId) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(ReplaySession::getSyncId, syncId); + ReplaySession replaySession = this.getOne(queryWrapper); + if (replaySession == null) { + return false; + } + OSSUploadUtil.deleteFile("trajectory/" + syncId + ".json"); + return removeById(replaySession.getId()); + } + + }