|
@@ -0,0 +1,296 @@
|
|
|
+package com.gis.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.gis.common.constant.ConfigConstant;
|
|
|
+import com.gis.common.constant.MsgCode;
|
|
|
+import com.gis.common.exception.BaseRuntimeException;
|
|
|
+import com.gis.common.proto.util.ConvertUtils;
|
|
|
+import com.gis.common.util.FileUtils;
|
|
|
+import com.gis.common.util.Result;
|
|
|
+import com.gis.domain.po.SceneEntity;
|
|
|
+import com.gis.service.AgeService;
|
|
|
+import com.gis.service.SceneService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.junit.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2021/6/22 0011 16:16
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AgeServiceImpl implements AgeService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ SceneService sceneService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ConfigConstant configConstant;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result uploadAge(MultipartFile file) {
|
|
|
+
|
|
|
+
|
|
|
+ String sceneCode = getSceneCode(file);
|
|
|
+
|
|
|
+ SceneEntity dbEntity = sceneService.findBySceneCode(sceneCode);
|
|
|
+ String newSceneCode;
|
|
|
+ if (dbEntity == null) {
|
|
|
+ //创建父级
|
|
|
+ dbEntity = new SceneEntity();
|
|
|
+ dbEntity.setSceneCode(sceneCode);
|
|
|
+ dbEntity.setVersion(1);
|
|
|
+ sceneService.save(dbEntity);
|
|
|
+ newSceneCode = sceneCode + "_" + dbEntity.getVersion();
|
|
|
+ } else {
|
|
|
+ int version = dbEntity.getVersion() + 1;
|
|
|
+ newSceneCode = sceneCode + "_" + version;
|
|
|
+ }
|
|
|
+
|
|
|
+ String basePath = configConstant.serverBasePath + "/age/" + newSceneCode;
|
|
|
+ // 把zip写入服务器,并用新的版本场景码命名
|
|
|
+ unZip(basePath, file);
|
|
|
+
|
|
|
+ // 检查 images+场景码目录是否存在
|
|
|
+ String ageBaseSceneCodePath = basePath + "/v3local/images/images" + sceneCode;
|
|
|
+
|
|
|
+ // vision.modeldata -> vision.json: visibles2、visibles3 删除 -> vision.modeldata
|
|
|
+ visionModelDataToVision(ageBaseSceneCodePath);
|
|
|
+ editVision(ageBaseSceneCodePath);
|
|
|
+ VisionToVisionModelData(ageBaseSceneCodePath);
|
|
|
+ createSomeDataJaon(ageBaseSceneCodePath, newSceneCode);
|
|
|
+ createData2Js(ageBaseSceneCodePath);
|
|
|
+ String scenePath = configConstant.serverBasePath + "/" + newSceneCode;
|
|
|
+ moveFile(ageBaseSceneCodePath, scenePath);
|
|
|
+
|
|
|
+ SceneEntity sceneEntity = new SceneEntity();
|
|
|
+ sceneEntity.setSceneCode(newSceneCode);
|
|
|
+ sceneEntity.setPath("/data/" + newSceneCode);
|
|
|
+ Long parentId = dbEntity.getId();
|
|
|
+ sceneEntity.setParentId(parentId);
|
|
|
+ sceneService.save(sceneEntity);
|
|
|
+ // 父级版本号加1
|
|
|
+ sceneService.addVersion(parentId);
|
|
|
+
|
|
|
+ return Result.success();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param filePath 文件全路径
|
|
|
+ */
|
|
|
+ private static void existFile(String filePath) {
|
|
|
+ if (!FileUtil.isFile(filePath)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3021, "文件不存在, path:" + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void existDir(String filePath) {
|
|
|
+ if (!FileUtil.isDirectory(filePath)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3021, "目录不存在, path:" + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void unZip(String basePath, MultipartFile file) {
|
|
|
+ String zipPath = basePath + ".zip";
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), zipPath);
|
|
|
+ log.info("zip写入完成,path: {}", zipPath);
|
|
|
+
|
|
|
+ existFile(zipPath);
|
|
|
+ // 解压文件
|
|
|
+ ZipUtil.unzip(zipPath);
|
|
|
+ log.info("zip解压完成, path: {}", basePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取场景码
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String getSceneCode(MultipartFile file) {
|
|
|
+ if (!FileUtils.checkFile(file)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3021, "非法文件,只能能上传zip文件");
|
|
|
+ }
|
|
|
+
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ if (!originalFilename.endsWith(".zip")) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3021, "非法文件,只能能上传zip文件");
|
|
|
+ }
|
|
|
+
|
|
|
+ String sceneCode = StringUtils.substringBeforeLast(originalFilename, ".");
|
|
|
+ return sceneCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void createData2Js(String basePath) {
|
|
|
+ String data2Path = basePath + "/data2.js";
|
|
|
+ FileUtil.writeUtf8String("{}", data2Path);
|
|
|
+ log.info("新的data2.js写入完成, path:{}", data2Path);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 处理场景码, 在原来的后缀加1
|
|
|
+ * @param sceneCode
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String getCode(String sceneCode) {
|
|
|
+ String s = StringUtils.substringAfterLast(sceneCode, "_");
|
|
|
+ sceneCode = StringUtils.substringBeforeLast(sceneCode, "_");
|
|
|
+ int i = Integer.valueOf(s) + 1;
|
|
|
+ return sceneCode + "_" + i;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param basePath 父级目录
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static void visionModelDataToVision(String basePath) {
|
|
|
+ String visionModelDataPath = basePath + "/vision.modeldata";
|
|
|
+ if (!FileUtil.isFile(visionModelDataPath)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3021, "vision.modeldata不存在,path:" + visionModelDataPath);
|
|
|
+ }
|
|
|
+ String visionJsonPath = basePath + "/vision.json";
|
|
|
+ log.info("visionModelDataPath: " + visionModelDataPath);
|
|
|
+ log.info("visionJsonPath: " + visionJsonPath);
|
|
|
+ try {
|
|
|
+ ConvertUtils.convertVisionModelDataToTxt(visionModelDataPath, visionJsonPath);
|
|
|
+ log.info("VisionModelDataToVisionJson转换完成");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param basePath 父级目录
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static void VisionToVisionModelData(String basePath) {
|
|
|
+
|
|
|
+ String visionJsonPath = basePath + "/vision.json";
|
|
|
+ if (!FileUtil.isFile(visionJsonPath)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3021, "vision.json不存在,path:" + visionJsonPath);
|
|
|
+ }
|
|
|
+ String visionModelDataPath = basePath + "/vision.modeldata";
|
|
|
+ log.info("visionModelDataPath: " + visionModelDataPath);
|
|
|
+ log.info("visionJsonPath: " + visionJsonPath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ ConvertUtils.convertTxtToVisionModelData(visionJsonPath, visionModelDataPath);
|
|
|
+ log.info("visionJsonToVisionModelData转换完成");
|
|
|
+ // 删除vision.json
|
|
|
+// FileUtil.del(visionJsonPath);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑vision.json 删除visibles2、visibles3 节点
|
|
|
+ *
|
|
|
+ * @param basePath 父级目录
|
|
|
+ */
|
|
|
+ private static void editVision(String basePath) {
|
|
|
+
|
|
|
+ String visionJsonPath = basePath + "/vision.json";
|
|
|
+ if (!FileUtil.isFile(visionJsonPath)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3021, "vision.json不存在,path:" + visionJsonPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ String in = FileUtil.readUtf8String(visionJsonPath);
|
|
|
+ JSONObject contentJson = JSONObject.parseObject(in);
|
|
|
+ JSONArray sweepLocations = contentJson.getJSONArray("sweepLocations");
|
|
|
+
|
|
|
+ // fori循环才能正确删除节点
|
|
|
+ for (int i = 0; i < sweepLocations.size(); i++) {
|
|
|
+ JSONObject indexJson = sweepLocations.getJSONObject(i);
|
|
|
+ indexJson.remove("visibles2");
|
|
|
+ indexJson.remove("visibles3");
|
|
|
+ }
|
|
|
+
|
|
|
+// log.info("vision: "+ contentJson.toJSONString());
|
|
|
+ FileUtil.writeUtf8String(contentJson.toJSONString(), visionJsonPath);
|
|
|
+ log.info("新的vision.json写入完成, path:{}", visionJsonPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建someData.json
|
|
|
+ *
|
|
|
+ * @param basePath
|
|
|
+ */
|
|
|
+ private static void createSomeDataJaon(String basePath, String sceneCode) {
|
|
|
+
|
|
|
+ // 获取基础someData.json
|
|
|
+ String content = FileUtils.getResourceContent("data/someData.json");
|
|
|
+ JSONObject contentJson = JSONObject.parseObject(content);
|
|
|
+ JSONObject modelJson = contentJson.getJSONObject("model");
|
|
|
+ // 更新新的场景码
|
|
|
+ modelJson.put("sid", sceneCode);
|
|
|
+ String someDataJsonPath = basePath + "/someData.json";
|
|
|
+
|
|
|
+ FileUtil.writeUtf8String(contentJson.toJSONString(), someDataJsonPath);
|
|
|
+ log.info("someData.json写入完成, path:{}", someDataJsonPath);
|
|
|
+ existFile(someDataJsonPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移动文件,并做相应的操作
|
|
|
+ *
|
|
|
+ * @param basePath
|
|
|
+ * @param outPath 移动位置
|
|
|
+ */
|
|
|
+ private static void moveFile(String basePath, String outPath) {
|
|
|
+ File file = new File(basePath);
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File every : files) {
|
|
|
+ String filePath = every.getAbsolutePath();
|
|
|
+ if (filePath.endsWith(".png")) {
|
|
|
+ FileUtil.del(filePath);
|
|
|
+ }
|
|
|
+ // 重命名文件
|
|
|
+ if (filePath.endsWith("_high1")) {
|
|
|
+ String highPath = filePath.substring(0, filePath.length() - 1);
|
|
|
+ if (FileUtil.exist(highPath)) {
|
|
|
+ FileUtil.del(highPath);
|
|
|
+ }
|
|
|
+ // isRetainExt: 原文件是否保留
|
|
|
+ FileUtil.rename(new File(filePath), highPath, false, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 删除无用文件 vision2.modeldata
|
|
|
+ FileUtil.del(basePath + "/vision2.modeldata");
|
|
|
+
|
|
|
+ FileUtil.copyContent(file, new File(outPath), true);
|
|
|
+ existDir(outPath);
|
|
|
+ log.info("大场景移动完成, path: {}", outPath);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void reName() {
|
|
|
+ String a = "F:\\work\\test\\fcb";
|
|
|
+ String b = "F:\\work\\test\\fcb11";
|
|
|
+// FileUtil.rename(new File(a), b, false, true);
|
|
|
+ System.out.println(b.substring(0, b.length() - 1));
|
|
|
+ }
|
|
|
+}
|