package com.fd.server.impl; import com.fd.constant.MsgCode; import com.fd.entity.FileEntity; import com.fd.entity.OutputFileEntity; import com.fd.repository.FileRepository; import com.fd.repository.OutputFileRepository; import com.fd.server.CmdServer; import com.fd.util.R; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; /** * Created by Owen on 2019/11/14 0014 15:33 */ @Log4j2 @Service public class CmdServerImpl implements CmdServer { // @Value("${output.file.path}") // private String OUTPUT_FILE_PATH; @Autowired private FileRepository fileRepository; @Autowired private OutputFileRepository outputFileRepository; @Override public R exeCmd(String commandStr) { Integer isCmd = exeCmdSingle(commandStr); if (isCmd == 0) { return new R(200, MsgCode.SUCCESS); } else { return new R(200, MsgCode.E50005); } } // 复杂坐标转换 @Override public Integer exeCmd(String cmd1, String cmd2) { exeCmdSingle(cmd1); Integer isCmd = exeCmdSingle(cmd2); // if (isCmd == 0) { // return new R(200, MsgCode.SUCCESS); // } else { // return new R(200, MsgCode.E50005); // } return isCmd; } @Override public R exeCmd(String commandStr, Long id) { return null; } private Integer exeCmdSingle(String commandStr) { Integer isCmd = null; // 命令运行结果 1:失败, 0:成功 try { String[] cmd = new String[]{"/bin/sh", "-c", commandStr}; Process ps = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream())); StringBuffer sb = new StringBuffer(); StringBuffer errorStr = new StringBuffer(); // error : 坑, 控制台信息是从errorBuf这里出来的 String errorLine; while ((errorLine = errorBuf.readLine()) != null) { errorStr.append(errorLine).append("\n"); } // log.info("error result: {}", errorStr.toString()); if (StringUtils.isNotEmpty(errorStr)){ log.info("error result: {}", errorStr.toString()); } // success ,没有获取到信息 String line; while ((line = br.readLine()) != null) { // log.info("===== br.readLine: ======== {}", br.readLine()); //执行结果加上回车 sb.append(line).append("\n"); } log.info("result: {}", sb.toString()); // 结束命令行 isCmd = ps.waitFor(); // 关闭流 br.close(); errorBuf.close(); } catch (Exception e) { e.printStackTrace(); } if (isCmd == 0) { log.info("end exeCmd : {}", isCmd); } else { log.info("wsitFore cmd run error : {}", isCmd); } return isCmd; } @Override public Integer exeCmdModelSlice(String cmd) { return exeCmdSingle(cmd); } @Override public Integer exeCmdInt(String cmd) { Integer isCmd = exeCmdSingle(cmd); return isCmd; } /** * 矢量数据判断坐标 * @param * @return */ @Override public Integer exeCmdJudgeCoord(String commandStr) { Integer isCmd = null; // 命令运行结果 1:失败, 0:成功 StringBuffer sb = new StringBuffer(); StringBuffer errorStr = new StringBuffer(); try { String[] cmd = new String[]{"/bin/sh", "-c", commandStr}; Process ps = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream())); // error : 坑, 控制台信息是从errorBuf这里出来的 String errorLine; while ((errorLine = errorBuf.readLine()) != null) { errorStr.append(errorLine).append("\n"); } // log.info("error result: {}", errorStr.toString()); if (StringUtils.isNotEmpty(errorStr)){ log.info("error result: {}", errorStr.toString()); } // success ,没有获取到信息 String line; while ((line = br.readLine()) != null) { // log.info("===== br.readLine: ======== {}", br.readLine()); //执行结果加上回车 sb.append(line).append("\n"); } log.info("result: {}", sb.toString()); // 结束命令行 isCmd = ps.waitFor(); // 关闭流 br.close(); errorBuf.close(); } catch (Exception e) { e.printStackTrace(); } if (isCmd == 0) { log.info("end exeCmd : {}", isCmd); // 判断坐标 if (sb.toString().contains("GEOGCS[\"China Geodetic Coordinate System 2000\"")) { // 需要普通坐标转换 isCmd = 1000; log.info("需要坐标转换code:{}, GEOGCS[\"China Geodetic Coordinate System 2000\"", isCmd); } if (sb.toString().contains("GEOGCS[\"Xian 1980\"")) { // 需要严格坐标转换 isCmd = 1001; // 2: log.info("需要严格坐标转换code:{}, GEOGCS[\"Xian 1980\"", isCmd); } if (sb.toString().contains("GEOGCS[\"WGS 84\"")) { // 不需要坐标转换 isCmd = 0; log.info("不需要坐标转换code:{}, GEOGCS[\"WGS 84\"", isCmd); } } else { log.info("error exeCmd wsitFore: {}", isCmd); } return isCmd; } /** * 栅格数据坐标判断 * * return : * 1000:需要转换 * 1001:不需要转换 */ @Override public Integer exeCmdRasterJudgeCoord(String commandStr) { Integer isCmd = null; // 命令运行结果 1:失败, 0:成功 StringBuffer sb = new StringBuffer(); StringBuffer errorStr = new StringBuffer(); try { String[] cmd = new String[]{"/bin/sh", "-c", commandStr}; Process ps = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream())); // error : 坑, 控制台信息是从errorBuf这里出来的 String errorLine; while ((errorLine = errorBuf.readLine()) != null) { errorStr.append(errorLine).append("\n"); } if (StringUtils.isNotEmpty(errorStr)){ log.info("error result: {}", errorStr.toString()); } // success ,没有获取到信息 String line; while ((line = br.readLine()) != null) { // log.info("===== br.readLine: ======== {}", br.readLine()); //执行结果加上回车 sb.append(line).append("\n"); } log.info("result: {}", sb.toString()); // 结束命令行 isCmd = ps.waitFor(); // 关闭流 br.close(); errorBuf.close(); } catch (Exception e) { e.printStackTrace(); } if (isCmd == 0) { log.info("end exeCmd : {}", isCmd); // 判断坐标 if (sb.toString().contains("+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=38500000")) { // 需要坐标转换 isCmd = 1000; // 1000: 需要转换 } if (sb.toString().contains("+proj=longlat +datum=WGS84")) { // 不需要坐标转换 isCmd = 0; } } else { log.info("error cmd wsitFore: {}", isCmd); } return isCmd; } /** * 栅格数据切片 * * 需要处理进度条 * @param * @param entity * @return */ @Override public Integer exeCmdRasterSlice(String commandStr, OutputFileEntity entity) { log.warn("run exeCmdRasterSlice"); Integer isCmd = null; // 命令运行结果 1:失败, 0:成功 try { String[] cmd = new String[]{"/bin/sh", "-c", commandStr}; Process ps = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream())); StringBuffer sb = new StringBuffer(); // 进度 int pre = 0; int num; String str; while ((num = br.read()) != -1) { str = String.valueOf((char) num); // 判断数字 if (numRegex(str)) { if (!str.equals("0")) { // 非0开头的 sb.append(",").append(str); } else { // 以0开头的 sb.append(str); } // 截取进度数字 str = StringUtils.substringAfterLast(sb.toString(), ","); // 100,1000都会当一次 if (str.length() == 2) { pre = pre + 5; // System.out.println("pre: " + pre); log.warn("pre: {}", pre); // 能被2整除的存一下db if (pre % 2 == 0) { entity.setProgress(pre); entity.setUpdateTime(new Date()); outputFileRepository.save(entity); } } } } log.info("cmd console: {}", sb.toString()); StringBuffer errorStr = new StringBuffer(); String errorLine; while ((errorLine = errorBuf.readLine()) != null) { errorStr.append(errorLine).append("\n"); } if (StringUtils.isNotEmpty(errorStr)){ log.info("error result: {}", errorStr.toString()); } // 结束命令行 isCmd = ps.waitFor(); // 关闭流 br.close(); errorBuf.close(); } catch (Exception e) { e.printStackTrace(); } if (isCmd == 0) { log.info("end exeCmd : {}", isCmd); } else { log.info("wsitFore cmd run error : {}", isCmd); } log.warn("end exeCmdRasterSlice"); return isCmd; } // 匹配数字 private boolean numRegex(String str) { String reg = "\\d"; if (StringUtils.isEmpty(str)) { return false; } return str.matches(reg); } }