package com.fdkankan.contro.mq.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.constant.*;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.contro.entity.ScenePlus;
import com.fdkankan.contro.entity.ScenePlusExt;
import com.fdkankan.contro.mq.service.IBuildSceneService;
import com.fdkankan.contro.service.*;
import com.fdkankan.fyun.config.FYunFileConfig;
import com.fdkankan.fyun.face.FYunFileServiceInterface;
import com.fdkankan.model.constants.ConstantFilePath;
import com.fdkankan.model.constants.UploadFilePath;
import com.fdkankan.model.enums.ModelTypeEnums;
import com.fdkankan.model.utils.CreateObjUtil;
import com.fdkankan.model.utils.SceneUtil;
import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
import com.fdkankan.rabbitmq.util.RabbitMqProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
*
* TODO
*
*
* @author dengsixing
* @since 2022/4/20
**/
@Slf4j
@Service
@RefreshScope
public class BuildSxTestServiceImpl implements IBuildSceneService {
@Value("${queue.modeling.modeling-call}")
private String queueModelingCall;
@Value("${queue.modeling.sx.modeling-sx-post:modeling-sx-post}")
private String queueModelingPost;
@Value("${model.type:#{null}}")
private String modelType;
@Value("${env:gn}")
private String env;
@Value("#{'${build.scene.post.not-delete-nas-nums:}'.split(',')}")
private List notDeleteNasNumList;
@Autowired
private RabbitMqProducer mqProducer;
@Resource
private FYunFileServiceInterface fYunFileService;
@Autowired
private FYunFileConfig fYunFileConfig;
@Autowired
private IScenePlusService scenePlusService;
@Autowired
private IScenePlusExtService scenePlusExtService;
@Autowired
private IBuildSceneDTService buildSceneDTService;
@Autowired
private ICommonService commonService;
@Autowired
private ISceneColdStorageService sceneColdStorageService;
@Autowired
private IntermitSceneService intermitSceneService;
@Autowired
private IBuildService buildService;
@Override
public void buildScenePre(BuildSceneCallMessage message) throws Exception{
String num = message.getSceneNum();
try {
//重新计算时需要删除文件夹,否知使用缓存
if(new File(message.getPath() + File.separator + "results").exists()){
FileUtils.deleteDirectory(message.getPath() + File.separator + "results");
}
//由于刘强说caches会影响计算结果,所以这里删除caches
if(new File(message.getPath() + File.separator + "caches").exists()){
FileUtils.deleteDirectory(message.getPath() + File.separator + "caches");
}
//下载文件
// //删除点位校准数据
// if (Objects.nonNull(message.getExt())
// && message.getExt().containsKey("deleteExtras")
// && (Boolean) message.getExt().get("deleteExtras")) {
// String extras = String.format(UploadFilePath.scene_result_data_path, num).concat("extras");
// if(CollUtil.isNotEmpty(fYunFileService.listRemoteFiles(extras))){
// fYunFileService.deleteFolder(extras);
// }
// }
//用户相机重新全量上传,需要解冻结
// sceneColdStorageService.unfreeze(num, "用户相机重新全量上传", message.getPath());
//根据相机类型,组装资源路径
//下载资源到本地
// this.downLoadSource(message, message.getPath());
//TODO 等算法文档出来 下载文件
//发送mq,就进行计算
message.setResultReceiverMqName(queueModelingPost);
JSONObject fdageData = commonService.getFdageData(message.getPath() + File.separator + "capture" + File.separator + "data.fdage");
//TODO 等算法文档出来 修改参数
buildService.writeDataJson(message, fdageData, null, null);
log.info("场景计算资源准备结束,场景码:{}", message.getSceneNum());
}catch (Exception e){
log.error("场景计算前置处理出错,num"+num, e);
buildSceneDTService.handBaseFail("场景计算资源准备异常!", message.getPath(), message.getSceneNum(), "计算控制服务器");
throw e;
}
}
private String getOssPath(String path) {
String ossPath = ConstantFilePath.OSS_PREFIX
+ path.replace(ConstantFilePath.BUILD_MODEL_PATH, "")
.replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "");
if (!ossPath.endsWith("/")) {
ossPath = ossPath.concat("/");
}
return ossPath;
}
@Override
public void downLoadSource(BuildSceneCallMessage buildSceneMqMessage,String path){
String ossPath = getOssPath(path);
fYunFileService.downloadFileByCommand(path + File.separator + "capture", ossPath);
}
@Override
public void buildScenePost(BuildSceneResultMqMessage message) throws Exception {
String sceneCode = message.getBuildContext().get("sceneNum").toString();
String path = message.getPath();
try {
// 上传计算日志
//如果是重复计算,没有走到计算逻辑,不需要上传日志文件
log.info("开始上传计算日志");
String buildLogPath = String.format(UploadFilePath.BUILD_LOG_PATH, sceneCode);
fYunFileService.uploadFile(path + File.separator + "console.log", buildLogPath + "console.log");
log.info("计算日志上传完成");
JSONObject fdageData = commonService.getFdageData(path + File.separator + "capture" +File.separator+"data.fdage");
if (!message.getBuildSuccess()) {
log.error("建模失败,修改状态为失败状态");
scenePlusService.update(new LambdaUpdateWrapper()
.set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
.eq(ScenePlus::getNum, sceneCode));
//推送到全景看看
intermitSceneService.sendMq(sceneCode, fdageData, CommonSuccessStatus.FAIL.code());
// 发送钉钉消息,计算失败
buildSceneDTService.handModelFail("计算失败", message.getPath(), sceneCode, message.getHostName());
return;
}
ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneCode);
Integer cameraType = Integer.parseInt(message.getBuildContext().get("cameraType").toString());
Map uploadFiles = getUploadFiles(scenePlus,path);
scenePlus.setPayStatus(PayStatus.PAY.code());
scenePlus.setUpdateTime(new Date());
scenePlus.setSceneStatus(SceneStatus.NO_DISPLAY.code());
Integer videoVersion = fdageData.getInteger("videoVersion");
//读取计算结果文件生成videosJson
JSONObject videosJson = commonService.getVideosJson(path, videoVersion, sceneCode, cameraType);
ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
log.info("开始上传场景计算结果数据,num:{}", sceneCode);
//上传文件
fYunFileService.uploadMulFiles(uploadFiles);
//容量统计
Long space = commonService.getSpace(sceneCode);
//写入数据库
this.updateDbPlus(scenePlus.getSceneSource(), space, videosJson.toJSONString(), message.getComputeTime(),false,scenePlusExt);
Object[] editInfoArr = commonService.updateEditInfo(scenePlus);
//统计原始资源大小
scenePlusExt.setOrigSpace(FileUtil.size(new File(path.concat(File.separator).concat("capture"))));
//删除计算目录
if(CollUtil.isEmpty(notDeleteNasNumList) || !notDeleteNasNumList.contains(sceneCode)){
CreateObjUtil.deleteFile(path.replace(ConstantFilePath.BUILD_MODEL_PATH, "/"));
}
//如果相机容量不足,需要把场景的paystatus改为容量不足状态
scenePlus.setPayStatus(commonService.getPayStatus(scenePlus.getCameraId(), space, fdageData));
this.uploadStatusJson(scenePlus, scenePlusExt);
scenePlusService.updateById(scenePlus);
scenePlusExtService.updateById(scenePlusExt);
//推送到全景看看
intermitSceneService.sendMq(sceneCode, fdageData, CommonSuccessStatus.SUCCESS.code());
log.info("场景计算结果处理结束,场景码:{}", sceneCode);
}catch (Exception e){
log.error("场景计算结果处理出错,num"+sceneCode, e);
buildSceneDTService.handBaseFail("场景计算结果处理出错!", message.getPath(), sceneCode, "计算控制服务器");
throw e;
}
}
private Map getUploadFiles(ScenePlus scenePlus,String path) throws Exception {
String projectNum = scenePlus.getNum();
String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, projectNum);
String imagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, projectNum);
String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, projectNum);
String resultsPath = path + File.separator + "results" + File.separator;
String uploadData = FileUtils.readFile(resultsPath + "upload.json");
JSONArray array = JSONObject.parseObject(uploadData).getJSONArray("upload");
JSONObject fileJson = null;
String fileName = "";
Map map = new HashMap();
for (int i = 0; i < array.size(); ++i) {
fileJson = array.getJSONObject(i);
fileName = fileJson.getString("file");
String filePath = resultsPath + fileName;
if (!(new File(filePath)).exists()) {
throw new Exception(filePath + "文件不存在");
}
if(fileJson.getIntValue("clazz") == 1 || fileJson.getIntValue("clazz") == 22 || fileJson.getIntValue("clazz") == 3 || fileJson.getIntValue("clazz") == 5){
map.put(filePath, imagesPath + fileName);
}
}
return map;
}
private void uploadStatusJson(ScenePlus scenePlus, ScenePlusExt scenePlusExt){
String num = scenePlus.getNum();
String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
Integer status = 1;
// 上传status JSON.
JSONObject statusJson = new JSONObject();
//临时将-2改成1,app还没完全更新
statusJson.put("status", status);
statusJson.put("webSite", scenePlusExt.getWebSite());
statusJson.put("sceneNum", num);
statusJson.put("thumb", scenePlusExt.getThumb());
statusJson.put("payStatus", scenePlus.getPayStatus());
statusJson.put("sceneScheme", scenePlusExt.getSceneScheme());
FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data/data" + num + File.separator + "status.json", statusJson.toString());
fYunFileService.uploadFile(statusJson.toJSONString().getBytes(StandardCharsets.UTF_8), dataViewPath + "status.json");
}
private void updateDbPlus(int sceneSource,Long space,String videosJson, Long computeTime,boolean isObj,ScenePlusExt scenePlusExt){
scenePlusExt.setSpace(space);
scenePlusExt.setComputeTime(computeTime);
scenePlusExt.setAlgorithmTime(new Date());
scenePlusExt.setVideos(videosJson);
scenePlusExt.setIsObj(isObj ? 1 : 0);
if(ModelTypeEnums.TILE_CODE.equals(modelType)){
scenePlusExt.setSceneScheme(3);
}
switch (SceneSource.get(sceneSource)){
case BM:
scenePlusExt.setSceneResolution(SceneResolution.two_K.code());
scenePlusExt.setSceneFrom(SceneFrom.PRO.code());
break;
case SM:
scenePlusExt.setSceneResolution(SceneResolution.one_k.code());
scenePlusExt.setSceneFrom(SceneFrom.LITE.code());
break;
case ZT:
scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
scenePlusExt.setSceneFrom(SceneFrom.MINION.code());
break;
case JG:
scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
break;
case SG:
scenePlusExt.setSceneResolution(SceneResolution.four_K.code());
scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
break;
}
String sceneKind = scenePlusExt.getSceneScheme() == 3 ? SceneKind.FACE.code():SceneKind.TILES.code();
scenePlusExt.setSceneKind(sceneKind);
// scenePlusExt.setModelKind(modelKind);
//统计点位数量
scenePlusExt.setShootCount(this.getShootCount(scenePlusExt));
scenePlusExtService.updateById(scenePlusExt);
}
private Integer getShootCount(ScenePlusExt scenePlusExt){
Integer shootCount = null;
String homePath = SceneUtil.getHomePath(scenePlusExt.getDataSource());
JSONObject dataFdageObj = JSON.parseObject(fYunFileService.getFileContent(homePath.concat("data.fdage")));
if(Objects.nonNull(dataFdageObj)){
JSONArray points = dataFdageObj.getJSONArray("points");
if(CollUtil.isNotEmpty(points)){
shootCount = points.size();
}
}
if(Objects.nonNull(shootCount) && shootCount > 0){
return shootCount;
}
String slamDataStr = fYunFileService.getFileContent(homePath.concat("slam_data.json"));
JSONObject slamDataObj = JSON.parseObject(slamDataStr);
if(Objects.nonNull(slamDataObj)){
JSONArray viewsInfo = slamDataObj.getJSONArray("views_info");
if(CollUtil.isNotEmpty(viewsInfo)){
shootCount = viewsInfo.stream().mapToInt(info -> {
return ((JSONObject) info).getJSONArray("list_pose").size();
}).sum();
}
}
return shootCount;
}
}