CmdServerImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package com.fd.server.impl;
  2. import com.fd.constant.MsgCode;
  3. import com.fd.entity.FileEntity;
  4. import com.fd.entity.OutputFileEntity;
  5. import com.fd.repository.FileRepository;
  6. import com.fd.repository.OutputFileRepository;
  7. import com.fd.server.CmdServer;
  8. import com.fd.util.R;
  9. import lombok.extern.log4j.Log4j2;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import java.io.BufferedReader;
  15. import java.io.InputStreamReader;
  16. import java.util.Date;
  17. /**
  18. * Created by Owen on 2019/11/14 0014 15:33
  19. */
  20. @Log4j2
  21. @Service
  22. public class CmdServerImpl implements CmdServer {
  23. // @Value("${output.file.path}")
  24. // private String OUTPUT_FILE_PATH;
  25. @Autowired
  26. private FileRepository fileRepository;
  27. @Autowired
  28. private OutputFileRepository outputFileRepository;
  29. @Override
  30. public R exeCmd(String commandStr) {
  31. Integer isCmd = exeCmdSingle(commandStr);
  32. if (isCmd == 0) {
  33. return new R(200, MsgCode.SUCCESS);
  34. } else {
  35. return new R(200, MsgCode.E50005);
  36. }
  37. }
  38. // 复杂坐标转换
  39. @Override
  40. public Integer exeCmd(String cmd1, String cmd2) {
  41. exeCmdSingle(cmd1);
  42. Integer isCmd = exeCmdSingle(cmd2);
  43. // if (isCmd == 0) {
  44. // return new R(200, MsgCode.SUCCESS);
  45. // } else {
  46. // return new R(200, MsgCode.E50005);
  47. // }
  48. return isCmd;
  49. }
  50. @Override
  51. public R exeCmd(String commandStr, Long id) {
  52. return null;
  53. }
  54. private Integer exeCmdSingle(String commandStr) {
  55. Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
  56. try {
  57. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  58. Process ps = Runtime.getRuntime().exec(cmd);
  59. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  60. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  61. StringBuffer sb = new StringBuffer();
  62. StringBuffer errorStr = new StringBuffer();
  63. // error : 坑, 控制台信息是从errorBuf这里出来的
  64. String errorLine;
  65. while ((errorLine = errorBuf.readLine()) != null) {
  66. errorStr.append(errorLine).append("\n");
  67. }
  68. // log.info("error result: {}", errorStr.toString());
  69. if (StringUtils.isNotEmpty(errorStr)){
  70. log.info("error result: {}", errorStr.toString());
  71. }
  72. // success ,没有获取到信息
  73. String line;
  74. while ((line = br.readLine()) != null) {
  75. // log.info("===== br.readLine: ======== {}", br.readLine());
  76. //执行结果加上回车
  77. sb.append(line).append("\n");
  78. }
  79. log.info("result: {}", sb.toString());
  80. // 结束命令行
  81. isCmd = ps.waitFor();
  82. // 关闭流
  83. br.close();
  84. errorBuf.close();
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. if (isCmd == 0) {
  89. log.info("end exeCmd : {}", isCmd);
  90. } else {
  91. log.info("wsitFore cmd run error : {}", isCmd);
  92. }
  93. return isCmd;
  94. }
  95. @Override
  96. public Integer exeCmdModelSlice(String cmd) {
  97. return exeCmdSingle(cmd);
  98. }
  99. @Override
  100. public Integer exeCmdInt(String cmd) {
  101. Integer isCmd = exeCmdSingle(cmd);
  102. return isCmd;
  103. }
  104. /**
  105. * 矢量数据判断坐标
  106. * @param
  107. * @return
  108. */
  109. @Override
  110. public Integer exeCmdJudgeCoord(String commandStr) {
  111. Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
  112. StringBuffer sb = new StringBuffer();
  113. StringBuffer errorStr = new StringBuffer();
  114. try {
  115. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  116. Process ps = Runtime.getRuntime().exec(cmd);
  117. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  118. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  119. // error : 坑, 控制台信息是从errorBuf这里出来的
  120. String errorLine;
  121. while ((errorLine = errorBuf.readLine()) != null) {
  122. errorStr.append(errorLine).append("\n");
  123. }
  124. // log.info("error result: {}", errorStr.toString());
  125. if (StringUtils.isNotEmpty(errorStr)){
  126. log.info("error result: {}", errorStr.toString());
  127. }
  128. // success ,没有获取到信息
  129. String line;
  130. while ((line = br.readLine()) != null) {
  131. // log.info("===== br.readLine: ======== {}", br.readLine());
  132. //执行结果加上回车
  133. sb.append(line).append("\n");
  134. }
  135. log.info("result: {}", sb.toString());
  136. // 结束命令行
  137. isCmd = ps.waitFor();
  138. // 关闭流
  139. br.close();
  140. errorBuf.close();
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }
  144. if (isCmd == 0) {
  145. log.info("end exeCmd : {}", isCmd);
  146. // 判断坐标
  147. if (sb.toString().contains("GEOGCS[\"China Geodetic Coordinate System 2000\"")) {
  148. // 需要普通坐标转换
  149. isCmd = 1000;
  150. log.info("需要坐标转换code:{}, GEOGCS[\"China Geodetic Coordinate System 2000\"", isCmd);
  151. }
  152. if (sb.toString().contains("GEOGCS[\"Xian 1980\"")) {
  153. // 需要严格坐标转换
  154. isCmd = 1001; // 2:
  155. log.info("需要严格坐标转换code:{}, GEOGCS[\"Xian 1980\"", isCmd);
  156. }
  157. if (sb.toString().contains("GEOGCS[\"WGS 84\"")) {
  158. // 不需要坐标转换
  159. isCmd = 0;
  160. log.info("不需要坐标转换code:{}, GEOGCS[\"WGS 84\"", isCmd);
  161. }
  162. } else {
  163. log.info("error exeCmd wsitFore: {}", isCmd);
  164. }
  165. return isCmd;
  166. }
  167. /**
  168. * 栅格数据坐标判断
  169. *
  170. * return :
  171. * 1000:需要转换
  172. * 1001:不需要转换
  173. */
  174. @Override
  175. public Integer exeCmdRasterJudgeCoord(String commandStr) {
  176. Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
  177. StringBuffer sb = new StringBuffer();
  178. StringBuffer errorStr = new StringBuffer();
  179. try {
  180. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  181. Process ps = Runtime.getRuntime().exec(cmd);
  182. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  183. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  184. // error : 坑, 控制台信息是从errorBuf这里出来的
  185. String errorLine;
  186. while ((errorLine = errorBuf.readLine()) != null) {
  187. errorStr.append(errorLine).append("\n");
  188. }
  189. if (StringUtils.isNotEmpty(errorStr)){
  190. log.info("error result: {}", errorStr.toString());
  191. }
  192. // success ,没有获取到信息
  193. String line;
  194. while ((line = br.readLine()) != null) {
  195. // log.info("===== br.readLine: ======== {}", br.readLine());
  196. //执行结果加上回车
  197. sb.append(line).append("\n");
  198. }
  199. log.info("result: {}", sb.toString());
  200. // 结束命令行
  201. isCmd = ps.waitFor();
  202. // 关闭流
  203. br.close();
  204. errorBuf.close();
  205. } catch (Exception e) {
  206. e.printStackTrace();
  207. }
  208. if (isCmd == 0) {
  209. log.info("end exeCmd : {}", isCmd);
  210. // 判断坐标
  211. if (sb.toString().contains("+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=38500000")) {
  212. // 需要坐标转换
  213. isCmd = 1000; // 1000: 需要转换
  214. }
  215. if (sb.toString().contains("+proj=longlat +datum=WGS84")) {
  216. // 不需要坐标转换
  217. isCmd = 0;
  218. }
  219. } else {
  220. log.info("error cmd wsitFore: {}", isCmd);
  221. }
  222. return isCmd;
  223. }
  224. /**
  225. * 栅格数据切片
  226. *
  227. * 需要处理进度条
  228. * @param
  229. * @param entity
  230. * @return
  231. */
  232. @Override
  233. public Integer exeCmdRasterSlice(String commandStr, OutputFileEntity entity) {
  234. log.warn("run exeCmdRasterSlice");
  235. Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
  236. try {
  237. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  238. Process ps = Runtime.getRuntime().exec(cmd);
  239. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  240. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  241. StringBuffer sb = new StringBuffer();
  242. // 进度
  243. int pre = 0;
  244. int num;
  245. String str;
  246. while ((num = br.read()) != -1) {
  247. str = String.valueOf((char) num);
  248. // 判断数字
  249. if (numRegex(str)) {
  250. if (!str.equals("0")) {
  251. // 非0开头的
  252. sb.append(",").append(str);
  253. } else {
  254. // 以0开头的
  255. sb.append(str);
  256. }
  257. // 截取进度数字
  258. str = StringUtils.substringAfterLast(sb.toString(), ",");
  259. // 100,1000都会当一次
  260. if (str.length() == 2) {
  261. pre = pre + 5;
  262. // System.out.println("pre: " + pre);
  263. log.warn("pre: {}", pre);
  264. // 能被2整除的存一下db
  265. if (pre % 2 == 0) {
  266. entity.setProgress(pre);
  267. entity.setUpdateTime(new Date());
  268. outputFileRepository.save(entity);
  269. }
  270. }
  271. }
  272. }
  273. log.info("cmd console: {}", sb.toString());
  274. StringBuffer errorStr = new StringBuffer();
  275. String errorLine;
  276. while ((errorLine = errorBuf.readLine()) != null) {
  277. errorStr.append(errorLine).append("\n");
  278. }
  279. if (StringUtils.isNotEmpty(errorStr)){
  280. log.info("error result: {}", errorStr.toString());
  281. }
  282. // 结束命令行
  283. isCmd = ps.waitFor();
  284. // 关闭流
  285. br.close();
  286. errorBuf.close();
  287. } catch (Exception e) {
  288. e.printStackTrace();
  289. }
  290. if (isCmd == 0) {
  291. log.info("end exeCmd : {}", isCmd);
  292. } else {
  293. log.info("wsitFore cmd run error : {}", isCmd);
  294. }
  295. log.warn("end exeCmdRasterSlice");
  296. return isCmd;
  297. }
  298. // 匹配数字
  299. private boolean numRegex(String str) {
  300. String reg = "\\d";
  301. if (StringUtils.isEmpty(str)) {
  302. return false;
  303. }
  304. return str.matches(reg);
  305. }
  306. }