HaixinServiceImpl.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.fdkankan.contro.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.fdkankan.common.util.CmdUtils;
  7. import com.fdkankan.contro.dto.UploadSceneDTO;
  8. import com.fdkankan.contro.entity.SceneOrigBd;
  9. import com.fdkankan.contro.entity.ScenePlus;
  10. import com.fdkankan.contro.httpclient.HaixinClient;
  11. import com.fdkankan.contro.service.IHaixinService;
  12. import com.fdkankan.contro.service.ISceneOrigBdService;
  13. import com.fdkankan.contro.service.IScenePlusService;
  14. import com.fdkankan.fyun.config.FYunFileConfig;
  15. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  16. import com.fdkankan.model.constants.ConstantFilePath;
  17. import com.fdkankan.model.constants.UploadFilePath;
  18. import com.fdkankan.web.response.ResultData;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.stereotype.Service;
  23. import javax.annotation.Resource;
  24. import java.io.File;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. @Slf4j
  30. @Service
  31. public class HaixinServiceImpl implements IHaixinService {
  32. private static final String API_FDFS_UPLOAD = "/fdfs/api/file/upload";
  33. private static final String API_SUBMIT_RENDER_RESULT = "/ecs/api/panoramicImageService/submitRenderResult";
  34. private static final String API_SYNC_RENDER_STATUS = "/ecs/api/panoramicImageService/syncRenderStatus";
  35. private static final int WAIT_TIME = 10 * 60;//推送等待时间
  36. private static final int PER_SHOOT_TIME = 5 * 60;//推送等待时间
  37. @Value("${haixin.host:#{null}}")
  38. private String haixinHost;
  39. @Value("${haixin.syncRenderStatus:false}")
  40. private boolean haixinSyncRenderStatus;
  41. @Autowired
  42. private ISceneOrigBdService sceneOrigBdService;
  43. @Resource
  44. private FYunFileConfig fYunFileConfig;
  45. @Resource
  46. private FYunFileServiceInterface fileServiceInterface;
  47. @Autowired
  48. private IScenePlusService scenePlusService;
  49. @Resource
  50. private HaixinClient haixinClient;
  51. @Autowired
  52. private IHaixinService haixinService;
  53. @Override
  54. public ResultData uploadScene(UploadSceneDTO dto) {
  55. SceneOrigBd sceneOrigBd = new SceneOrigBd();
  56. sceneOrigBd.setFileUrl(dto.getFileUrl());
  57. sceneOrigBd.setTaskId(dto.getTaskId());
  58. sceneOrigBd.setKNo(dto.getkNo());
  59. sceneOrigBd.setRyId(dto.getRyId());
  60. sceneOrigBd.setRyNo(dto.getRyNo());
  61. sceneOrigBd.setPassword(dto.getPassword());
  62. sceneOrigBd.setRyPhone(dto.getRyPhone());
  63. sceneOrigBd.setRyCid(dto.getRyCid());
  64. sceneOrigBd.setRyName(dto.getRyName());
  65. sceneOrigBd.setShootCount(dto.getShootCount());
  66. sceneOrigBd.setExpectedTime(this.getExpectedTime(dto.getShootCount()));
  67. sceneOrigBd.setTaskNo(dto.getTaskNo());
  68. if(StrUtil.isNotEmpty(dto.getPushAddress()) && dto.getPushAddress().endsWith(File.separator)){
  69. dto.setPushAddress(dto.getPushAddress().substring(0, dto.getPushAddress().length() - 1));
  70. }
  71. sceneOrigBd.setPushAddress(dto.getPushAddress());
  72. sceneOrigBd.setDistrictCode(dto.getDistrictCode());
  73. sceneOrigBd.setDistrictName(dto.getDistrictName());
  74. sceneOrigBdService.save(sceneOrigBd);
  75. //推送进度
  76. haixinService.syncRenderStatus(sceneOrigBd.getId(), 0, this.getExpectedTime(sceneOrigBd), null);
  77. return ResultData.ok();
  78. }
  79. private Integer getExpectedTime(Integer shootCount){
  80. if(shootCount == null){
  81. return null;
  82. }
  83. Integer waitTime = 10 * 60;//等待推送时间
  84. Integer callTime = shootCount * PER_SHOOT_TIME;//算法预估耗时
  85. Integer preTime = callTime/10;//前置处理耗时,约是算法时间的10%
  86. Integer postTime = (callTime/10) * 2;//后置处理耗时,约是算法时间的20%
  87. return waitTime + callTime + preTime + postTime;
  88. }
  89. private Integer getExpectedTime(SceneOrigBd sceneOrigBd){
  90. if(sceneOrigBd.getExpectedTime() == null){
  91. return null;
  92. }
  93. List<SceneOrigBd> list = sceneOrigBdService.list(
  94. new LambdaQueryWrapper<SceneOrigBd>()
  95. .in(SceneOrigBd::getStatus, 0, 1, 2)
  96. .ne(SceneOrigBd::getId, sceneOrigBd.getId()).isNotNull(SceneOrigBd::getExpectedTime));
  97. int sum = list.stream().mapToInt(v -> v.getExpectedTime()).sum() + sceneOrigBd.getExpectedTime();
  98. return sum;
  99. }
  100. @Override
  101. public void noticHaixin(String num, boolean buildSuccess) throws Exception {
  102. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  103. String taskId = scenePlus.getTaskId();
  104. String zipDir = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + "haixin/";
  105. String zipPath = zipDir + num + ".zip";
  106. try {
  107. if(buildSuccess){//计算成功,把原始资源打压缩包
  108. //复制scene_edit_data/num/data/mapping目录到view目录
  109. String editMappingPath = "/oss/4dkankan/" + String.format(UploadFilePath.DATA_EDIT_PATH, num) + "mapping";
  110. String viewMappingPath = "/oss/4dkankan/" + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "mapping";
  111. if(FileUtil.exist(editMappingPath)){
  112. FileUtil.copyContent(new File(editMappingPath), new File(viewMappingPath), true);
  113. }
  114. String viewPath = "/oss/4dkankan/" + String.format(UploadFilePath.VIEW_PATH, num);
  115. //打压缩包
  116. this.zip(viewPath, zipPath);
  117. //上传到fastdf
  118. String url = haixinHost.concat(API_FDFS_UPLOAD);
  119. List<File> files = FileUtil.loopFiles(zipDir);
  120. List<String> fileNameRemoteList = new ArrayList<>();
  121. String finalUrl = url;
  122. files.stream().forEach(file->{
  123. JSONObject jsonObject = haixinClient.uploadToFdfs(finalUrl, file.getAbsolutePath());
  124. JSONObject data = jsonObject.getJSONObject("data");
  125. fileNameRemoteList.add(data.getString("fileNameRemote"));
  126. });
  127. //通知计算结果
  128. StringBuilder sb = new StringBuilder();
  129. for (String fileNameRemote : fileNameRemoteList) {
  130. sb.append(",").append(fileNameRemote);
  131. }
  132. this.sendResult(taskId, sb.substring(1));
  133. }else{
  134. this.sendResult(taskId, null);
  135. }
  136. }catch (Exception e){
  137. log.warn("推送计算结果失败,taskId:{}", taskId, e);
  138. this.sendResult(taskId, null);
  139. }finally {
  140. FileUtil.del(zipDir);
  141. }
  142. }
  143. private void zip(String sourcePath, String zipPath) throws Exception {
  144. FileUtil.mkParentDirs(zipPath);
  145. String cmd = "cd " + sourcePath + " && zip -r -s 2048M " + zipPath + " *";//&& mv -f " + zipPath + " " + target
  146. CmdUtils.callLineSh(cmd, 200);
  147. }
  148. @Override
  149. public void sendResult(String taskId, String packetPath){
  150. String url = haixinHost.concat(API_SUBMIT_RENDER_RESULT);
  151. Map<String, String> params = new HashMap<>();
  152. params.put("vendor","A0BF");
  153. params.put("projectId", taskId);
  154. params.put("packetPath", packetPath);
  155. haixinClient.postJson(url, params);
  156. }
  157. @Override
  158. public void syncRenderStatus(Long sceneOirgBdId, Integer status, Integer expectedTime, String failReason) {
  159. SceneOrigBd sceneOrigBd = sceneOrigBdService.getById(sceneOirgBdId);
  160. sceneOrigBd.setStatus(status);
  161. sceneOrigBd.setReason(failReason);
  162. sceneOrigBdService.updateById(sceneOrigBd);
  163. if(!haixinSyncRenderStatus){
  164. return;
  165. }
  166. String num = null;
  167. ScenePlus scenePlus = scenePlusService.getByTaskId(sceneOrigBd.getTaskId());
  168. if(scenePlus != null){
  169. num = scenePlus.getNum();
  170. }
  171. if(expectedTime == null && sceneOrigBd.getExpectedTime() != null){//如果指定预测时间是空,证明开始进入计算前置流程,可以计算预测时间
  172. if(status == 1){
  173. expectedTime = sceneOrigBd.getExpectedTime() - WAIT_TIME;
  174. }
  175. if(status == 2){
  176. expectedTime = sceneOrigBd.getExpectedTime() - WAIT_TIME - (sceneOrigBd.getShootCount() * PER_SHOOT_TIME);
  177. }
  178. if(status == -1 || status == 3 || status == 4){
  179. expectedTime = 0;
  180. }
  181. }
  182. String url = haixinHost.concat(API_SYNC_RENDER_STATUS);
  183. if(StrUtil.isNotEmpty(sceneOrigBd.getPushAddress())){
  184. url = sceneOrigBd.getPushAddress().concat(API_SYNC_RENDER_STATUS);
  185. }
  186. Map<String, Object> params = new HashMap<>();
  187. params.put("taskNo",sceneOrigBd.getTaskNo());
  188. params.put("projectId", sceneOrigBd.getTaskId());
  189. params.put("kNo", sceneOrigBd.getKNo());
  190. params.put("num", num);
  191. params.put("status", status);
  192. params.put("expectedTime", expectedTime == null ? 0 : expectedTime);
  193. haixinClient.postJson(url, params);
  194. }
  195. }