TowerServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package com.fdkankan.extend.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.exceptions.ExceptionUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.core.util.ZipUtil;
  7. import cn.hutool.http.HttpUtil;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.fdkankan.common.constant.CommonSuccessStatus;
  13. import com.fdkankan.common.util.DateExtUtil;
  14. import com.fdkankan.extend.TowerSceneBean;
  15. import com.fdkankan.extend.entity.Camera;
  16. import com.fdkankan.extend.entity.CameraDetail;
  17. import com.fdkankan.extend.entity.ScenePlus;
  18. import com.fdkankan.extend.service.ICameraDetailService;
  19. import com.fdkankan.extend.service.ICameraService;
  20. import com.fdkankan.extend.service.IScenePlusService;
  21. import com.fdkankan.extend.service.ITowerService;
  22. import com.fdkankan.extend.util.RsaCryptTools;
  23. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  24. import com.fdkankan.redis.constant.RedisKey;
  25. import com.fdkankan.redis.util.RedisUtil;
  26. import com.fdkankan.web.response.ResultData;
  27. import lombok.Data;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.beans.factory.annotation.Value;
  31. import org.springframework.cloud.context.config.annotation.RefreshScope;
  32. import org.springframework.stereotype.Service;
  33. import com.fdkankan.model.constants.UploadFilePath;
  34. import javax.crypto.BadPaddingException;
  35. import javax.crypto.IllegalBlockSizeException;
  36. import javax.crypto.NoSuchPaddingException;
  37. import java.io.IOException;
  38. import java.security.InvalidKeyException;
  39. import java.security.NoSuchAlgorithmException;
  40. import java.security.spec.InvalidKeySpecException;
  41. import java.util.*;
  42. import java.util.stream.Collectors;
  43. @RefreshScope
  44. @Slf4j
  45. @Service
  46. public class TowerServiceImpl implements ITowerService {
  47. private static String parentPath = "/home/backend/4dkankan_v4/modeling-extend/download/";
  48. private static String ossParentPath = "customer-projects/china-tower/";
  49. private static String ossScenePoolFilePath = ossParentPath.concat("scene.txt");
  50. private static String ossCameraPoolFilePath = ossParentPath.concat("camera.txt");
  51. private static String pushLogPath = parentPath.concat("log.txt");
  52. @Value("${tower.manufact}")
  53. private String manufact;
  54. @Value("${tower.private-key}")
  55. private String privateKey;
  56. @Value("${tower.url}")
  57. private String url;
  58. @Autowired
  59. private FYunFileServiceInterface fYunFileService;
  60. @Autowired
  61. private RedisUtil redisUtil;
  62. @Autowired
  63. private ICameraDetailService cameraDetailService;
  64. @Autowired
  65. private IScenePlusService scenePlusService;
  66. @Autowired
  67. private ITowerService towerService;
  68. @Autowired
  69. private ICameraService cameraService;
  70. @Override
  71. public String packSceneDataHandler(String num, String title) throws Exception {
  72. //校验场景名称是否正确
  73. String sceneStr = fYunFileService.getFileContent(ossScenePoolFilePath);
  74. List<TowerSceneBean> towerSceneBeans = JSON.parseArray(sceneStr, TowerSceneBean.class);
  75. if(CollUtil.isEmpty(towerSceneBeans)){
  76. return null;
  77. }
  78. TowerSceneBean towerSceneBean = towerSceneBeans.stream().filter(bean -> title.equals(bean.getName())).findFirst().get();
  79. if(Objects.isNull(towerSceneBean)){
  80. return null;
  81. }
  82. log.info("开始下载:{}", num);
  83. //场景本地数据父目录
  84. String scenePath = parentPath.concat(num).concat("/");
  85. String sceneSourcePath = scenePath.concat(towerSceneBean.getSceneId()).concat("_").concat(towerSceneBean.getRoomId());
  86. String zipPath = sceneSourcePath.concat(".zip");
  87. //data数据下载
  88. String dataPath = sceneSourcePath + "/data/";
  89. String ossDataPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  90. //下载floorplan.json
  91. fYunFileService.downloadFile(ossDataPath.concat("floorplan.json"), dataPath.concat("floorplan.json"));
  92. //下载scene.json
  93. String sceneJsonPath = dataPath.concat("scene.json");
  94. String sceneJson = redisUtil.get(String.format(RedisKey.SCENE_JSON, num));
  95. if(StrUtil.isEmpty(sceneJson)){
  96. sceneJson = fYunFileService.getFileContent(ossDataPath.concat("scene.json"));
  97. }
  98. JSONObject sceneJsonObj = JSON.parseObject(sceneJson);
  99. sceneJsonObj.put("sceneKind", "pano");
  100. FileUtil.writeUtf8String(sceneJsonObj.toJSONString(), sceneJsonPath);
  101. //下载mesh
  102. FileUtil.mkdir(dataPath.concat("mesh/"));
  103. List<String> meshList = fYunFileService.listRemoteFiles(ossDataPath.concat("mesh"));
  104. meshList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, dataPath.concat("mesh/")));
  105. //images数据下载
  106. String imagesPath = sceneSourcePath + "/images/";
  107. String ossImagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  108. //下载high图
  109. List<String> highList = fYunFileService.listRemoteFiles(ossImagesPath.concat("pan/high/"));
  110. FileUtil.mkdir(imagesPath.concat("4k/"));
  111. highList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, imagesPath.concat("4k/")));
  112. //下载low图
  113. FileUtil.mkdir(imagesPath.concat("512/"));
  114. List<String> lowList = fYunFileService.listRemoteFiles(ossImagesPath.concat("pan/low/"));
  115. lowList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, imagesPath.concat("512/")));
  116. //下载vision.txt
  117. fYunFileService.downloadFile(ossImagesPath.concat("vision.txt"), imagesPath.concat("vision.txt"));
  118. //打包
  119. ZipUtil.zip(sceneSourcePath, zipPath, true);
  120. log.info("结束下载,开始推送:{}", num);
  121. try {
  122. // this.dataPush(num, title, towerSceneBean.getSceneId(), towerSceneBean.getRoomId(), zipPath);
  123. }finally {
  124. // FileUtil.del(scenePath);
  125. // FileUtil.del(sceneSourcePath);
  126. }
  127. // FileUtil.del(scenePath);
  128. FileUtil.copy(zipPath, parentPath, true);
  129. FileUtil.del(scenePath);
  130. return zipPath;
  131. }
  132. @Override
  133. public void packSceneData(Long companyId, List<String> nums) {
  134. String key = "tower:scene:data:download";
  135. List<ScenePlus> scenePlusList = null;
  136. if(Objects.nonNull(companyId)){
  137. //根据客户企业id查出所有相机
  138. List<CameraDetail> cameraDetailList = cameraDetailService.getByCompanyId(companyId);
  139. if(CollUtil.isEmpty(cameraDetailList)){
  140. return;
  141. }
  142. Set<Long> cameraIds = cameraDetailList.stream().map(detail -> detail.getCameraId()).collect(Collectors.toSet());
  143. //根据相机id查出所有待下载场景
  144. scenePlusList = scenePlusService.getByCameraIds(cameraIds);
  145. }else if(CollUtil.isNotEmpty(nums)){
  146. scenePlusList = scenePlusService.list(new LambdaQueryWrapper<ScenePlus>().in(ScenePlus::getNum, nums));
  147. }else{
  148. String snCodeStr = fYunFileService.getFileContent(ossCameraPoolFilePath);
  149. List<String> snCodeList = Arrays.asList(snCodeStr.split(","));
  150. List<Camera> cameraList = cameraService.listBySnCodes(snCodeList);
  151. List<Long> cameraIdList = cameraList.stream().map(Camera::getId).collect(Collectors.toList());
  152. String sceneStr = fYunFileService.getFileContent(ossScenePoolFilePath);
  153. List<TowerSceneBean> towerSceneBeans = JSON.parseArray(sceneStr, TowerSceneBean.class);
  154. List<String> titleList = towerSceneBeans.stream().map(TowerSceneBean::getName).collect(Collectors.toList());
  155. scenePlusList = scenePlusService.list(new LambdaQueryWrapper<ScenePlus>().in(ScenePlus::getCameraId, cameraIdList).in(ScenePlus::getTitle, titleList));
  156. List<String> dbTitleList = scenePlusList.stream().map(ScenePlus::getTitle).collect(Collectors.toList());
  157. List<String> notFindList = titleList.stream().filter(title -> {
  158. if (dbTitleList.contains(title)) {
  159. return false;
  160. }else{
  161. return true;
  162. }
  163. }).collect(Collectors.toList());
  164. if(CollUtil.isNotEmpty(notFindList)){
  165. fYunFileService.uploadFile(JSON.toJSONString(notFindList).getBytes(),
  166. ossParentPath.concat("notFind")
  167. .concat("-")
  168. .concat(DateExtUtil.format(new Date(), DateExtUtil.dateStyle7))
  169. .concat(".txt"));
  170. }
  171. }
  172. if(CollUtil.isEmpty(scenePlusList)){
  173. return;
  174. }
  175. //遍历场景列表开始下载
  176. scenePlusList.stream().forEach(plus -> {
  177. JSONObject jsonObject = new JSONObject();
  178. try {
  179. jsonObject = new JSONObject();
  180. jsonObject.put("title", plus.getTitle());
  181. jsonObject.put("num", plus.getNum());
  182. String zipPath = towerService.packSceneDataHandler(plus.getNum(), plus.getTitle());
  183. jsonObject.put("status", CommonSuccessStatus.SUCCESS.code());
  184. jsonObject.put("zipPath", zipPath);
  185. redisUtil.hset(key, plus.getNum(), JSON.toJSONString(jsonObject));
  186. }catch (Exception e){
  187. log.error("推送失败,num:{},title:{}", plus.getNum(), plus.getTitle(), e);
  188. jsonObject.put("status", CommonSuccessStatus.FAIL.code());
  189. jsonObject.put("error", ExceptionUtil.stacktraceToString(e, 3000));
  190. redisUtil.hset(key, plus.getNum(), JSON.toJSONString(jsonObject));
  191. }
  192. });
  193. }
  194. @Override
  195. public void dataPush(String num, String title, String sceneId, String roomId, String zipPath) throws Exception {
  196. log.info("开始组装参数");
  197. String fileMd5 = com.fdkankan.extend.util.FileUtil.getMulFileMD5(FileUtil.getInputStream(zipPath));
  198. String compose = sceneId.concat("+").concat(roomId).concat("+").concat(fileMd5);
  199. String encrypt = RsaCryptTools.encryptData(compose, privateKey);
  200. Map<String, Object> params = new HashMap<>();
  201. params.put("stationCode", sceneId);
  202. params.put("roomEntityID", roomId);
  203. params.put("file", FileUtil.file(zipPath));
  204. params.put("manufact", manufact);
  205. params.put("encrypt", encrypt);
  206. params.put("upUserAcct", "xxx");
  207. log.info("encrypt:{}", encrypt);
  208. log.info("开始推送");
  209. String post = HttpUtil.post(url, params);
  210. JSONObject jsonObject = JSON.parseObject(post);
  211. this.dataPushLog(num, title, jsonObject.getString("resultCode"), jsonObject.getString("resultMsg"));
  212. }
  213. public static void main(String[] args) {
  214. Map<String, Object> params = new HashMap<>();
  215. params.put("stationCode", "410192010000001484");
  216. params.put("roomEntityID", "000102050000000003085104");
  217. params.put("file", FileUtil.file("D:\\test\\410192010000001484_000102050000000003085104.zip"));
  218. params.put("manufact", "3");
  219. params.put("upUserAcct", "111");
  220. params.put("encrypt", "oNFZonGRrum3hldhU9AcMwP0JsXoxZocaTSTgj1h1Ag9pJzro9us0udinPr058y9KcwSPtkO58TKVE4csnasTZT00jghJQqsk0ATiNzX8QZh+F1iH33j6I3NHZu3vzjAV4lVHSo23WcEMo4s9G5dxymagYl8FKd7ua0/CYNysrs=");
  221. String post = HttpUtil.post("http://120.52.136.13:2808/fileManager/uploadRsaFileZipView", params);
  222. JSONObject jsonObject = JSON.parseObject(post);
  223. System.out.println(jsonObject.toJSONString());
  224. }
  225. private void dataPushLog(String num, String title, String resultCode, String resultMsg){
  226. String content = num.concat("-").concat(title).concat(" : ").concat(resultCode);
  227. if(StrUtil.isNotEmpty(resultMsg)){
  228. content = content.concat("-").concat(resultMsg);
  229. }
  230. content = content.concat("\r\n");
  231. FileUtil.writeUtf8String(content, parentPath.concat("download.log"));
  232. }
  233. }