123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- package com.fdkankan.extend.service.impl;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.exceptions.ExceptionUtil;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.core.util.ZipUtil;
- import cn.hutool.http.HttpUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.fdkankan.common.constant.CommonSuccessStatus;
- import com.fdkankan.common.util.DateExtUtil;
- import com.fdkankan.extend.TowerSceneBean;
- import com.fdkankan.extend.entity.Camera;
- import com.fdkankan.extend.entity.CameraDetail;
- import com.fdkankan.extend.entity.ScenePlus;
- import com.fdkankan.extend.service.ICameraDetailService;
- import com.fdkankan.extend.service.ICameraService;
- import com.fdkankan.extend.service.IScenePlusService;
- import com.fdkankan.extend.service.ITowerService;
- import com.fdkankan.extend.util.RsaCryptTools;
- import com.fdkankan.fyun.face.FYunFileServiceInterface;
- import com.fdkankan.redis.constant.RedisKey;
- import com.fdkankan.redis.util.RedisUtil;
- import com.fdkankan.web.response.ResultData;
- import lombok.Data;
- 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 com.fdkankan.model.constants.UploadFilePath;
- import javax.crypto.BadPaddingException;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import java.io.IOException;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import java.security.spec.InvalidKeySpecException;
- import java.util.*;
- import java.util.stream.Collectors;
- @RefreshScope
- @Slf4j
- @Service
- public class TowerServiceImpl implements ITowerService {
- private static String parentPath = "/home/backend/4dkankan_v4/modeling-extend/download/";
- private static String ossParentPath = "customer-projects/china-tower/";
- private static String ossScenePoolFilePath = ossParentPath.concat("scene.txt");
- private static String ossCameraPoolFilePath = ossParentPath.concat("camera.txt");
- private static String pushLogPath = parentPath.concat("log.txt");
- @Value("${tower.manufact}")
- private String manufact;
- @Value("${tower.private-key}")
- private String privateKey;
- @Value("${tower.url}")
- private String url;
- @Autowired
- private FYunFileServiceInterface fYunFileService;
- @Autowired
- private RedisUtil redisUtil;
- @Autowired
- private ICameraDetailService cameraDetailService;
- @Autowired
- private IScenePlusService scenePlusService;
- @Autowired
- private ITowerService towerService;
- @Autowired
- private ICameraService cameraService;
- @Override
- public String packSceneDataHandler(String num, String title) throws Exception {
- //校验场景名称是否正确
- String sceneStr = fYunFileService.getFileContent(ossScenePoolFilePath);
- List<TowerSceneBean> towerSceneBeans = JSON.parseArray(sceneStr, TowerSceneBean.class);
- if(CollUtil.isEmpty(towerSceneBeans)){
- return null;
- }
- TowerSceneBean towerSceneBean = towerSceneBeans.stream().filter(bean -> title.equals(bean.getName())).findFirst().get();
- if(Objects.isNull(towerSceneBean)){
- return null;
- }
- log.info("开始下载:{}", num);
- //场景本地数据父目录
- String scenePath = parentPath.concat(num).concat("/");
- String sceneSourcePath = scenePath.concat(towerSceneBean.getSceneId()).concat("_").concat(towerSceneBean.getRoomId());
- String zipPath = sceneSourcePath.concat(".zip");
- //data数据下载
- String dataPath = sceneSourcePath + "/data/";
- String ossDataPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
- //下载floorplan.json
- fYunFileService.downloadFile(ossDataPath.concat("floorplan.json"), dataPath.concat("floorplan.json"));
- //下载scene.json
- String sceneJsonPath = dataPath.concat("scene.json");
- String sceneJson = redisUtil.get(String.format(RedisKey.SCENE_JSON, num));
- if(StrUtil.isEmpty(sceneJson)){
- sceneJson = fYunFileService.getFileContent(ossDataPath.concat("scene.json"));
- }
- JSONObject sceneJsonObj = JSON.parseObject(sceneJson);
- sceneJsonObj.put("sceneKind", "pano");
- FileUtil.writeUtf8String(sceneJsonObj.toJSONString(), sceneJsonPath);
- //下载mesh
- FileUtil.mkdir(dataPath.concat("mesh/"));
- List<String> meshList = fYunFileService.listRemoteFiles(ossDataPath.concat("mesh"));
- meshList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, dataPath.concat("mesh/")));
- //images数据下载
- String imagesPath = sceneSourcePath + "/images/";
- String ossImagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
- //下载high图
- List<String> highList = fYunFileService.listRemoteFiles(ossImagesPath.concat("pan/high/"));
- FileUtil.mkdir(imagesPath.concat("4k/"));
- highList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, imagesPath.concat("4k/")));
- //下载low图
- FileUtil.mkdir(imagesPath.concat("512/"));
- List<String> lowList = fYunFileService.listRemoteFiles(ossImagesPath.concat("pan/low/"));
- lowList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, imagesPath.concat("512/")));
- //下载vision.txt
- fYunFileService.downloadFile(ossImagesPath.concat("vision.txt"), imagesPath.concat("vision.txt"));
- //打包
- ZipUtil.zip(sceneSourcePath, zipPath, true);
- log.info("结束下载,开始推送:{}", num);
- try {
- // this.dataPush(num, title, towerSceneBean.getSceneId(), towerSceneBean.getRoomId(), zipPath);
- }finally {
- // FileUtil.del(scenePath);
- // FileUtil.del(sceneSourcePath);
- }
- // FileUtil.del(scenePath);
- FileUtil.copy(zipPath, parentPath, true);
- FileUtil.del(scenePath);
- return zipPath;
- }
- @Override
- public void packSceneData(Long companyId, List<String> nums) {
- String key = "tower:scene:data:download";
- List<ScenePlus> scenePlusList = null;
- if(Objects.nonNull(companyId)){
- //根据客户企业id查出所有相机
- List<CameraDetail> cameraDetailList = cameraDetailService.getByCompanyId(companyId);
- if(CollUtil.isEmpty(cameraDetailList)){
- return;
- }
- Set<Long> cameraIds = cameraDetailList.stream().map(detail -> detail.getCameraId()).collect(Collectors.toSet());
- //根据相机id查出所有待下载场景
- scenePlusList = scenePlusService.getByCameraIds(cameraIds);
- }else if(CollUtil.isNotEmpty(nums)){
- scenePlusList = scenePlusService.list(new LambdaQueryWrapper<ScenePlus>().in(ScenePlus::getNum, nums));
- }else{
- String snCodeStr = fYunFileService.getFileContent(ossCameraPoolFilePath);
- List<String> snCodeList = Arrays.asList(snCodeStr.split(","));
- List<Camera> cameraList = cameraService.listBySnCodes(snCodeList);
- List<Long> cameraIdList = cameraList.stream().map(Camera::getId).collect(Collectors.toList());
- String sceneStr = fYunFileService.getFileContent(ossScenePoolFilePath);
- List<TowerSceneBean> towerSceneBeans = JSON.parseArray(sceneStr, TowerSceneBean.class);
- List<String> titleList = towerSceneBeans.stream().map(TowerSceneBean::getName).collect(Collectors.toList());
- scenePlusList = scenePlusService.list(new LambdaQueryWrapper<ScenePlus>().in(ScenePlus::getCameraId, cameraIdList).in(ScenePlus::getTitle, titleList));
- List<String> dbTitleList = scenePlusList.stream().map(ScenePlus::getTitle).collect(Collectors.toList());
- List<String> notFindList = titleList.stream().filter(title -> {
- if (dbTitleList.contains(title)) {
- return false;
- }else{
- return true;
- }
- }).collect(Collectors.toList());
- if(CollUtil.isNotEmpty(notFindList)){
- fYunFileService.uploadFile(JSON.toJSONString(notFindList).getBytes(),
- ossParentPath.concat("notFind")
- .concat("-")
- .concat(DateExtUtil.format(new Date(), DateExtUtil.dateStyle7))
- .concat(".txt"));
- }
- }
- if(CollUtil.isEmpty(scenePlusList)){
- return;
- }
- //遍历场景列表开始下载
- scenePlusList.stream().forEach(plus -> {
- JSONObject jsonObject = new JSONObject();
- try {
- jsonObject = new JSONObject();
- jsonObject.put("title", plus.getTitle());
- jsonObject.put("num", plus.getNum());
- String zipPath = towerService.packSceneDataHandler(plus.getNum(), plus.getTitle());
- jsonObject.put("status", CommonSuccessStatus.SUCCESS.code());
- jsonObject.put("zipPath", zipPath);
- redisUtil.hset(key, plus.getNum(), JSON.toJSONString(jsonObject));
- }catch (Exception e){
- log.error("推送失败,num:{},title:{}", plus.getNum(), plus.getTitle(), e);
- jsonObject.put("status", CommonSuccessStatus.FAIL.code());
- jsonObject.put("error", ExceptionUtil.stacktraceToString(e, 3000));
- redisUtil.hset(key, plus.getNum(), JSON.toJSONString(jsonObject));
- }
- });
- }
- @Override
- public void dataPush(String num, String title, String sceneId, String roomId, String zipPath) throws Exception {
- log.info("开始组装参数");
- String fileMd5 = com.fdkankan.extend.util.FileUtil.getMulFileMD5(FileUtil.getInputStream(zipPath));
- String compose = sceneId.concat("+").concat(roomId).concat("+").concat(fileMd5);
- String encrypt = RsaCryptTools.encryptData(compose, privateKey);
- Map<String, Object> params = new HashMap<>();
- params.put("stationCode", sceneId);
- params.put("roomEntityID", roomId);
- params.put("file", FileUtil.file(zipPath));
- params.put("manufact", manufact);
- params.put("encrypt", encrypt);
- params.put("upUserAcct", "xxx");
- log.info("encrypt:{}", encrypt);
- log.info("开始推送");
- String post = HttpUtil.post(url, params);
- JSONObject jsonObject = JSON.parseObject(post);
- this.dataPushLog(num, title, jsonObject.getString("resultCode"), jsonObject.getString("resultMsg"));
- }
- public static void main(String[] args) {
- Map<String, Object> params = new HashMap<>();
- params.put("stationCode", "410192010000001484");
- params.put("roomEntityID", "000102050000000003085104");
- params.put("file", FileUtil.file("D:\\test\\410192010000001484_000102050000000003085104.zip"));
- params.put("manufact", "3");
- params.put("upUserAcct", "111");
- params.put("encrypt", "oNFZonGRrum3hldhU9AcMwP0JsXoxZocaTSTgj1h1Ag9pJzro9us0udinPr058y9KcwSPtkO58TKVE4csnasTZT00jghJQqsk0ATiNzX8QZh+F1iH33j6I3NHZu3vzjAV4lVHSo23WcEMo4s9G5dxymagYl8FKd7ua0/CYNysrs=");
- String post = HttpUtil.post("http://120.52.136.13:2808/fileManager/uploadRsaFileZipView", params);
- JSONObject jsonObject = JSON.parseObject(post);
- System.out.println(jsonObject.toJSONString());
- }
- private void dataPushLog(String num, String title, String resultCode, String resultMsg){
- String content = num.concat("-").concat(title).concat(" : ").concat(resultCode);
- if(StrUtil.isNotEmpty(resultMsg)){
- content = content.concat("-").concat(resultMsg);
- }
- content = content.concat("\r\n");
- FileUtil.writeUtf8String(content, parentPath.concat("download.log"));
- }
- }
|