123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- 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);
- }
- }
|