Task.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. package com.jiayue.ipfcst.client.Schedule;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.http.HttpRequest;
  6. import cn.hutool.http.HttpResponse;
  7. import cn.hutool.http.HttpUtil;
  8. import cn.hutool.json.JSONArray;
  9. import cn.hutool.json.JSONObject;
  10. import cn.hutool.json.JSONUtil;
  11. import com.jiayue.ipfcst.client.domain.entity.FileCreateLog;
  12. import com.jiayue.ipfcst.client.utils.*;
  13. import lombok.Data;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.io.FileUtils;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.context.annotation.Configuration;
  18. import org.springframework.scheduling.annotation.Async;
  19. import org.springframework.scheduling.annotation.EnableAsync;
  20. import org.springframework.scheduling.annotation.EnableScheduling;
  21. import org.springframework.scheduling.annotation.Scheduled;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.util.StringUtils;
  24. import java.io.*;
  25. import java.net.URLDecoder;
  26. import java.text.SimpleDateFormat;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. /**
  30. * 气象文件下载任务
  31. *
  32. * @version 1.0
  33. * @since 2019/4/8 11:24
  34. */
  35. @Component
  36. @Configuration
  37. @EnableScheduling
  38. @Slf4j
  39. @EnableAsync
  40. public class Task {
  41. @Value("${downLoadFile.stationCode}")
  42. String applicationStationCode;
  43. @Value("${zxglPath}")
  44. String zxglPath;
  45. /**
  46. * 功能:Java读取txt文件的内容
  47. * 步骤:1:先获得文件句柄
  48. * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
  49. * 3:读取到输入流后,需要读取生成字节流
  50. * 4:一行一行的输出。readline()。
  51. * 备注:需要考虑的是异常情况
  52. *
  53. * @param filePath
  54. */
  55. public static String readTxtFile(String filePath) {
  56. String val = "";
  57. try {
  58. String encoding = "GBK";
  59. File file = new File(filePath);
  60. if (file.isFile() && file.exists()) {
  61. InputStreamReader read = new InputStreamReader(
  62. new FileInputStream(file), encoding);//考虑到编码格式
  63. BufferedReader bufferedReader = new BufferedReader(read);
  64. String lineTxt = null;
  65. while ((lineTxt = bufferedReader.readLine()) != null) {
  66. val = val + lineTxt;
  67. }
  68. read.close();
  69. } else {
  70. log.error("找不到指定的文件");
  71. }
  72. } catch (Exception e) {
  73. log.error("读取文件内容出错", e);
  74. }
  75. return val;
  76. }
  77. /**
  78. * 根据反向隔离传输路径获取备份文件夹
  79. * 默认为new同级的bak路径
  80. *
  81. * @param path 反向隔离传输路径
  82. */
  83. public String getFxglModifyPath(String path, String targetPath) {
  84. String pathBak = "";
  85. String lastPathStr = "";
  86. if (StrUtil.endWithIgnoreCase(path, "/")) {
  87. String[] paths = path.split("/");
  88. lastPathStr = paths[paths.length - 1];
  89. } else {
  90. char separatorChar = '/';
  91. lastPathStr = StringUtils.unqualify(path, separatorChar);
  92. }
  93. if ("new".equals(lastPathStr)) {
  94. pathBak = path.replace("new", targetPath).replace("ipp/bak", "ipp/new");
  95. log.info("获取到反向隔离:{} 路径", targetPath);
  96. } else {
  97. try {
  98. throw new Exception("获取反向隔离备份路径失败,传输路径最后一层不为new");
  99. } catch (Exception e) {
  100. throw new RuntimeException(e);
  101. }
  102. }
  103. return pathBak;
  104. // }
  105. }
  106. /**
  107. * v3 http下载文件请求
  108. */
  109. @Async
  110. // @Scheduled(fixedRate = 6000)
  111. @Scheduled(initialDelay = 45 * 1000, fixedRateString = "60000")
  112. void v3DataFilesDownload() {
  113. if ("否".equals(Constant.cacheClientConfig.getDqAndNwpFileTask())) {
  114. log.info("气象文件下载任务已关闭");
  115. return;
  116. }
  117. log.info("气象文件下载任务开始执行");
  118. Constant.filedownLoadList = new ArrayList<>();
  119. String fxglFilePath = Constant.cacheClientConfig.getFxglPath();
  120. log.info("缓存配置中的反向隔离文件发送路径:{}", fxglFilePath);
  121. if (StrUtil.isBlank(fxglFilePath)) {
  122. log.warn("反向隔离路径未配置,不执行气象文件下载任务。");
  123. return;
  124. }
  125. String stationCode = Constant.cacheClientConfig.getStationCode();
  126. List<FileCreateLog> reqFilesList = new ArrayList<>();
  127. //当applicationStationCode不等于""时 下载所有配置的场站
  128. if ("".equals(applicationStationCode)) {
  129. reqFilesList = queryToDownfilesFromCloud(stationCode);
  130. if (stationCode.equals("J00192")) {
  131. reqFilesList.addAll(queryToDownfilesFromCloud("J00389"));
  132. reqFilesList.addAll(queryToDownfilesFromCloud("J00390"));
  133. }
  134. } else {
  135. String[] codes = applicationStationCode.split(",");
  136. for (String code : codes) {
  137. reqFilesList.addAll(queryToDownfilesFromCloud(code));
  138. }
  139. }
  140. // if (reqFilesList.size() == 0) {
  141. // log.info("未从云端获得今日未下载的文件信息");
  142. // return;
  143. // }
  144. //临时文件下载路径
  145. String tempFilePath = checkTempFilePath(fxglFilePath);
  146. //遍历下载所有文件
  147. try {
  148. for (FileCreateLog fileCreateLog : reqFilesList) {
  149. String curentFileName = fileCreateLog.getFileName();
  150. //判断该文件是否已下载
  151. if (!Constant.alreadyDownFilesList.contains(fileCreateLog.getStationCode() + curentFileName)) {
  152. //返回信息中有文件从minio中的下载路径,尝试直接下载,如果下载失败再从云端下载
  153. Long size = downloadFileByMinioOrCloud(tempFilePath + File.separatorChar + fileCreateLog.getStationCode(), curentFileName, fileCreateLog.getId(), fileCreateLog.getFileDownloadUrl());
  154. if (size > 0) {
  155. String filePath = tempFilePath + File.separatorChar + fileCreateLog.getStationCode() + File.separatorChar + curentFileName;
  156. log.info("成功下载文件到:{}", filePath);
  157. //文件下载成功缓存文件名称,避免再次下载 ,
  158. Constant.alreadyDownFilesList.add(fileCreateLog.getStationCode() + curentFileName);
  159. //缓存文件信息,做后续回传文件传入内网状态使用
  160. Constant.downLoadFilesStatusList.add(fileCreateLog);
  161. // if (curentFileName.contains("pointconfig")) {
  162. // //修唯外网处理气象数据配置,及重启client端
  163. // toDealProtocol(filePath, fileCreateLog.getId());
  164. // } else {
  165. //缓存文件路径,下载完成后移动文件到反向隔离发送路径
  166. Constant.filedownLoadList.add(filePath);
  167. // }
  168. } else {
  169. log.error("文件:【{}】下载失败", fileCreateLog.getFileName());
  170. }
  171. } else {
  172. log.info("文件:【{}】已经下载过了,不需要再下次,如有需要请手动下载或联系运维生成新文件。", fileCreateLog.getFileName());
  173. }
  174. }
  175. } catch (Exception e) {
  176. log.error("文件下载失败:{}", e);
  177. } finally {
  178. //移动文件到相应反向隔离目录,向内网传输
  179. if (Constant.filedownLoadList.size() > 0) {
  180. for (String fpath : Constant.filedownLoadList) {
  181. int startIndex = fpath.indexOf("J00");
  182. int endIndex = startIndex + 6;
  183. String result = fpath.substring(startIndex, endIndex);
  184. UtilTools.mvFile(fpath, fxglFilePath + File.separator + result + File.separator + "new");
  185. }
  186. }
  187. }
  188. log.info("气象文件下载任务执行结束");
  189. }
  190. /**
  191. * 请求云端,获取今日未下载文件列表
  192. *
  193. * @return
  194. */
  195. private List<FileCreateLog> queryToDownfilesFromCloud(String stationCode) {
  196. List<FileCreateLog> reqFilesList = new ArrayList<>();
  197. List<FileCreateLog> dqListNow = new ArrayList<>();
  198. List<FileCreateLog> nwpListNow = new ArrayList<>();
  199. List<FileCreateLog> finalList = new ArrayList<>();
  200. // Map postParms = new HashMap<>(2);
  201. // postParms.put("stationCode", stationCode);
  202. // postParms.put("sign", Md5Util.makeMd5(stationCode));
  203. // 请求minio
  204. String url = "https://117.78.19.70:9010/client/getFileLogsForAio/";
  205. // String body = HttpUtil.post(Constant.cacheClientConfig.getCloudAddr() + "getCurentDayUnDownLoadFile", postParms, 10000);
  206. HttpRequest httpRequest = HttpRequest.get(url + stationCode);
  207. httpRequest.setGlobalTimeout(20000);
  208. String body = httpRequest.execute().body();
  209. JSONObject json = JSONUtil.parseObj(body);
  210. String code = json.get("code").toString();
  211. String data = json.get("data").toString();
  212. if ("0".equals(code) && data.length() > 0) {
  213. JSONArray array = JSONUtil.parseArray(data);
  214. reqFilesList = array.toList(FileCreateLog.class);
  215. String dateNow = DateUtil.format(new Date(), "yyyyMMdd");
  216. if (CollectionUtil.isNotEmpty(reqFilesList)) {
  217. dqListNow = reqFilesList.stream().filter(f -> f.getFileName().contains("DQ_" + dateNow)).collect(Collectors.toList());
  218. nwpListNow = reqFilesList.stream().filter(f -> f.getFileName().contains("NWP_" + dateNow)).collect(Collectors.toList());
  219. if (CollectionUtil.isNotEmpty(dqListNow)) {
  220. // 降序
  221. dqListNow.sort(Comparator.comparing(FileCreateLog::getFileName).reversed());
  222. if (null != dqListNow && dqListNow.size() > 0) {
  223. // 取最新的一条数据
  224. FileCreateLog fileCreateLog = dqListNow.get(0);
  225. finalList.add(fileCreateLog);
  226. }
  227. } else {
  228. log.error("下载minio原始RB文件 --> {} 失败,失败原因:{}", stationCode, "文件为空!");
  229. }
  230. if (CollectionUtil.isNotEmpty(nwpListNow)) {
  231. // 降序
  232. nwpListNow.sort(Comparator.comparing(FileCreateLog::getFileName).reversed());
  233. if (null != dqListNow && dqListNow.size() > 0) {
  234. // 取最新的一条数据
  235. FileCreateLog fileCreateLog = nwpListNow.get(0);
  236. finalList.add(fileCreateLog);
  237. }
  238. } else {
  239. log.error("下载minio原始RB文件 --> {} 失败,失败原因:{}", stationCode, "文件为空!");
  240. }
  241. } else {
  242. log.error("下载minio原始RB文件 --> {} 失败,失败原因:{}", stationCode, "文件为空!");
  243. }
  244. // if (null != reqFilesList && reqFilesList.size() > 0) {
  245. // //对文件进行筛选
  246. // reqFilesList = screeningDownloadFils(reqFilesList);
  247. // }
  248. }
  249. return finalList;
  250. }
  251. /**
  252. * 下载文件,先从minio上下载,失败再从cloud下载
  253. * 下载限时20秒
  254. *
  255. * @param tempFilePath
  256. * @param curentFileName
  257. * @param id
  258. * @param fileDownloadUrl
  259. * @return
  260. */
  261. private Long downloadFileByMinioOrCloud(String tempFilePath, String curentFileName, String id, String fileDownloadUrl) {
  262. Long size = 0L;
  263. // if (StrUtil.isNotBlank(fileDownloadUrl)) {
  264. // size = HttpUtil.downloadFile(fileDownloadUrl, new File(tempFilePath + File.separatorChar
  265. // + curentFileName), 20000);
  266. // if (size > 0) {
  267. // log.info("直接从minio下载文件成功:{}", curentFileName);
  268. // callBackFileDownloadStatusToCloud(id,"S");
  269. // return size;
  270. // }
  271. // }
  272. String url = Constant.cacheClientConfig.getCloudAddr()
  273. + "downloadFileById?id=" + id + "&sign=" + Md5Util.makeMd5(id);
  274. size = HttpUtil.downloadFile(url, new File(tempFilePath + File.separatorChar
  275. + curentFileName), 20000);
  276. if (size > 0) {
  277. callBackFileDownloadStatusToCloud(id, "S");
  278. log.info("从minio下载文件失败,改从云端下载文件成功:{}", curentFileName);
  279. return size;
  280. }
  281. //最后都没有成功下载文件回传异常
  282. callBackFileDownloadStatusToCloud(id, "E");
  283. return size;
  284. }
  285. /**
  286. * 回传云端文件下载状态请求
  287. *
  288. * @param id
  289. * @param status
  290. */
  291. private void callBackFileDownloadStatusToCloud(String id, String status) {
  292. Map postParms = new HashMap<>(3);
  293. postParms.put("id", id);
  294. postParms.put("type", status);
  295. postParms.put("sign", Md5Util.makeMd5(id));
  296. String body = HttpUtil.post(Constant.cacheClientConfig.getCloudAddr() + "postBackDownFileStatus", postParms, 10000);
  297. log.info("文件下载成功,回传云端文件下载状态响应报文:{}", body);
  298. }
  299. /**
  300. * 回传云端文件下载文件传输到内网状态
  301. *
  302. * @param id
  303. * @param status
  304. */
  305. private void callBackTransInStatusToCloud(String id, String status) {
  306. Map postParms = new HashMap<>(3);
  307. postParms.put("id", id);
  308. postParms.put("type", status);
  309. postParms.put("sign", Md5Util.makeMd5(id));
  310. String body = HttpUtil.post(Constant.cacheClientConfig.getCloudAddr() + "postBackIntranetStatus", postParms, 10000);
  311. log.info("文件传输内网状态响应报文:{}", body);
  312. }
  313. /**
  314. * 移动外网气象配置文件到当前程序下,并回传接收文件正常状态给云端
  315. */
  316. private void toDealProtocol(String filePath, String id) {
  317. try {
  318. //如果是protocol2file的配置文件 不需要放到反向隔离目录下 而是放到程序同级目录
  319. //TODO 改变文件存放位置为上级目录 ../
  320. UtilTools.mvFile(filePath, "../");
  321. callBackTransInStatusToCloud(id, "S");
  322. log.info("回传protocol2file配置文件传入内网信息,重启client");
  323. Runtime.getRuntime().exec("service client restart");
  324. } catch (Exception e) {
  325. log.error("获取到protocol2file的配置文件,在移动时发生异常", e);
  326. }
  327. }
  328. /**
  329. * 筛选待下载文件,不需要下载的文件剔除
  330. *
  331. * @param list
  332. */
  333. private List<FileCreateLog> screeningDownloadFils(List<FileCreateLog> list) {
  334. List<FileCreateLog> resultList = new ArrayList<>();
  335. if (null == Constant.cacheClientConfig.getDownLoadFileChoose() || "0".equals(Constant.cacheClientConfig.getDownLoadFileChoose())) {
  336. return list;
  337. } else if ("1".equals(Constant.cacheClientConfig.getDownLoadFileChoose())) {//只下载短期文件
  338. log.debug("云端配置只下载短期文件");
  339. list.stream().forEach(fileCreateLog -> {
  340. if (fileCreateLog.getFileName().contains("DQ")) {
  341. resultList.add(fileCreateLog);
  342. }
  343. });
  344. } else if ("2".equals(Constant.cacheClientConfig.getDownLoadFileChoose())) { //只下载nwp文件
  345. log.debug("云端配置只下载NWP文件");
  346. list.stream().forEach(fileCreateLog -> {
  347. if (fileCreateLog.getFileName().contains("NWP")) {
  348. resultList.add(fileCreateLog);
  349. }
  350. });
  351. } else {
  352. resultList.addAll(list);
  353. }
  354. return resultList;
  355. }
  356. /**
  357. * 检查临时路径,不存在则创建
  358. *
  359. * @return
  360. */
  361. private String checkTempFilePath(String fxglFilePath) {
  362. //反向隔离文件发送地址
  363. fxglFilePath = UtilTools.judeDirExists(fxglFilePath);
  364. String dir = "";
  365. if (fxglFilePath.endsWith(String.valueOf(File.separatorChar))) {
  366. dir = fxglFilePath.substring(0, fxglFilePath.length() - 1) + "temp";
  367. } else {
  368. dir = fxglFilePath + "temp";
  369. }
  370. return dir;
  371. }
  372. /**
  373. * http扫描文件是否传入内网
  374. * 文件传送到内网成功后向云平台发送文件成功请求
  375. * //-----------------------//
  376. * 2022-08-18 加入判断反向隔离传输bak文件夹是否为空,
  377. * 当new 和bak文件夹都不存在已下载的文件时,判定文件已传输到内网并向云端回传
  378. */
  379. @Async
  380. @Scheduled(initialDelay = 30 * 1000, fixedRateString = "120000")
  381. void scanTasksFxgl() {
  382. if ("否".equals(Constant.cacheClientConfig.getFxglTask())) {
  383. log.info("反向隔离扫描任务已关闭");
  384. return;
  385. }
  386. log.info("开始执行定时任务:扫描反向隔离目录,查看文件是否传入内网");
  387. String filePath = Constant.cacheClientConfig.getFxglPath();
  388. if (null == filePath || filePath.length() == 0) {
  389. log.warn("反向隔离文件路径未配置,不执行扫描任务。");
  390. return;
  391. }
  392. //遍历已下载的文件list 筛选已成功传入内网服务器的文件,发送传入成功标识
  393. if (Constant.downLoadFilesStatusList == null || Constant.downLoadFilesStatusList.size() == 0) {
  394. log.warn("当前无下载文件,不需要扫描反向隔离路径");
  395. return;
  396. }
  397. for (String stationCode : applicationStationCode.split(",")) {
  398. Date date = new Date();
  399. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
  400. String today = simpleDateFormat.format(date);
  401. String newFilePath = filePath + "/" + stationCode + "/" + "new/";
  402. String analysisFilePath = filePath + "/" + stationCode + "/" + "success/" + today;
  403. //反向隔离new文件夹下文件列表
  404. List<String> newFilelist = getFxglFilesListByDirector(newFilePath);
  405. //反向隔离bak文件夹下文件列表
  406. String bakFilePath = getFxglModifyPath(newFilePath, "bak");
  407. List<String> bakFilelist = getFxglFilesListByDirector(bakFilePath);
  408. //反向隔离传输错误文件夹
  409. String errorFilePath = getFxglModifyPath(newFilePath, "error");
  410. List<String> errorFilelist = getFxglFilesListByDirector(errorFilePath);
  411. //获取解析成功文件夹下文件列表
  412. List<String> analysisFileList = getFxglFilesListByDirector(analysisFilePath);
  413. List<FileCreateLog> fileCreateLogs = new ArrayList<>();
  414. for (FileCreateLog fileCreateLog : Constant.downLoadFilesStatusList) {
  415. //判断不在新下载的文件夹中后:
  416. if (!newFilelist.contains(fileCreateLog.getFileName())) {
  417. //回传状态,默认失败
  418. String goBackFlag = "E";
  419. //文件传入bak目录说明文件下载成功,解析成功
  420. if (analysisFileList.contains(fileCreateLog.getFileName())) {
  421. goBackFlag = "S";
  422. log.info("文件:{} 默认传入内网成功,回传云平台此文件传输状态:{}", fileCreateLog.getFileName(), goBackFlag);
  423. } else if (errorFilelist.contains(fileCreateLog.getFileName())) {
  424. //在error文件夹下,向内网传输失败了
  425. // log.info("文件:{} 传入内网失败,回传云平台此文件传输状态:{}", fileCreateLog.getFileName(), goBackFlag);
  426. } else {
  427. //找不到文件,日志提示,不进行回传状态
  428. // log.warn("当前文件:{} 可能在移动中,不在new下 且未在bak或error文件夹下找到", fileCreateLog.getFileName());
  429. continue;
  430. }
  431. callBackTransInStatusToCloud(fileCreateLog.getId(), goBackFlag);
  432. //
  433. fileCreateLogs.add(fileCreateLog);
  434. } else {
  435. // log.warn("文件:{} 还在new文件夹下面,未被传输移动,2分钟后再次检查", fileCreateLog.getFileName());
  436. }
  437. }
  438. //清空下载文件列表中已经回传过文件传输状态的文件
  439. if (null != fileCreateLogs && fileCreateLogs.size() > 0) {
  440. for (FileCreateLog fileCreateLog : fileCreateLogs) {
  441. Constant.downLoadFilesStatusList.remove(fileCreateLog);
  442. }
  443. }
  444. }
  445. }
  446. /**
  447. * 获取反向隔离相关路径下的文件列表
  448. *
  449. * @param filePath
  450. * @return
  451. */
  452. private List<String> getFxglFilesListByDirector(String filePath) {
  453. //文件集合
  454. List<String> list = new ArrayList<>();
  455. //路径不存在则进行创建
  456. UtilTools.judeDirExists(filePath);
  457. File dirFile = new File(filePath);
  458. File[] files = dirFile.listFiles();
  459. if (null == files) {
  460. try {
  461. throw new Exception("文件路径:" + filePath + "不存在");
  462. } catch (Exception e) {
  463. throw new RuntimeException(e);
  464. }
  465. }
  466. for (File file : files) {
  467. list.add(file.getName());
  468. }
  469. return list;
  470. }
  471. /**
  472. * 正向隔离扫描
  473. */
  474. @Async
  475. // @Scheduled(initialDelay = 40 * 1000, fixedRateString = "120000")
  476. void scanTasksZxgl() {
  477. if ("否".equals(Constant.cacheClientConfig.getZxglTask())) {
  478. log.info("正向隔离扫描任务已关闭");
  479. return;
  480. }
  481. log.info("开始执行定时任务:扫描正向隔离目录");
  482. String filePath = Constant.cacheClientConfig.getZxglPath();
  483. if (null == filePath || filePath.length() == 0) {
  484. log.warn("正向隔离路径未配置,不执行定时任务");
  485. return;
  486. }
  487. try {
  488. filePath = URLDecoder.decode(filePath, "UTF-8");
  489. } catch (UnsupportedEncodingException e) {
  490. e.printStackTrace();
  491. }
  492. File dirFile = new File(filePath);
  493. //判断该目录是否存在,不存在时创建
  494. if (!dirFile.exists()) {
  495. dirFile.mkdirs();
  496. log.info("该【" + dirFile.getPath() + "】目录不存在,系统自动创建文件目录");
  497. }
  498. File[] files = dirFile.listFiles();
  499. String dayStr = new SimpleDateFormat("yyyyMMdd").format(new Date());//当前时间格式化为年月日
  500. if (files != null && files.length > 0) {
  501. for (File file : files) {
  502. if (!file.isDirectory()) {
  503. //如果不是今天的文件,则删除该文件
  504. if (!file.getName().substring(file.getName().indexOf("_") + 1, file.getName().indexOf("_") + 9).equals(dayStr)) {
  505. if (file.exists()) {
  506. file.delete();
  507. log.info(file.getName() + "文件不是当天文件不符合要求 、移除该文件");
  508. }
  509. continue;
  510. } else {
  511. //解析文件
  512. try {
  513. if (file.getName().startsWith("STATUS_") || file.getName().startsWith("RP_")) {
  514. String val = readTxtFile(filePath + file.getName());
  515. HttpUtil.get(val);
  516. moveFile(new File(filePath + file.getName()), filePath + File.separator + "bak" + File.separator);
  517. log.debug(file.getName() + "文件解析成功,移动到:" + filePath + File.separator + "bak" + File.separator);
  518. } else {
  519. file.delete();
  520. log.error(file.getName() + "文件名不符合要求,删除。");
  521. }
  522. } catch (Exception e) {
  523. log.error(file.getName() + "解析失败", e);
  524. }
  525. }
  526. } else {
  527. File[] filebaks = file.listFiles();
  528. for (File filebak : filebaks) {
  529. if (!filebak.getName().substring(filebak.getName().indexOf("_") + 1, filebak.getName().indexOf("_") + 9).equals(dayStr)) {
  530. if (filebak.exists()) {
  531. filebak.delete();
  532. log.info(filebak.getName() + "删除今天之前的历史文件");
  533. }
  534. }
  535. }
  536. }
  537. }
  538. } else {
  539. log.info("该【" + dirFile.getPath() + "】目录下没有文件");
  540. }
  541. }
  542. /**
  543. * 移动文件
  544. *
  545. * @param file 文件
  546. */
  547. private void moveFile(File file, String path) {
  548. File destFile = new File(path + file.getName());
  549. if (destFile.exists()) {
  550. destFile.delete();
  551. }
  552. try {
  553. FileUtils.moveFile(file, destFile);
  554. } catch (IOException e) {
  555. log.error(file.getName() + "文件移动失败", e);
  556. }
  557. }
  558. @Scheduled(fixedRate = 60000l)
  559. public void realPowerForQr() {
  560. File zxglPathFile = new File(zxglPath + File.separator + "new");
  561. log.info("开始扫描正向隔离回传目录{}", zxglPathFile.getPath());
  562. String bakPath = zxglPath + File.separator + "bak";
  563. if (zxglPathFile.exists()) {
  564. for (String s : zxglPathFile.list()) {
  565. List<String> list;
  566. try {
  567. File file = new File(zxglPathFile.getPath() + File.separator + s);
  568. list = FileUtil.getFileContent(file);
  569. TaskResultRequestVO taskResultRequestVO = new TaskResultRequestVO();
  570. taskResultRequestVO.setTaskResult(list.get(0));
  571. String url = "https://api.jiayuepowertech.com:8080/task/result";
  572. // 构造请求对象
  573. HttpRequest request = HttpRequest.put(url).body(JsonBeanUtil.beanToJson(taskResultRequestVO));
  574. // 发送请求并获取响应对象
  575. HttpResponse response = request.execute();
  576. FileUtils.moveFile(file, new File(bakPath + File.separator + file.getName()));
  577. log.info("回传实际功率状态=》{}", response.body());
  578. } catch (Exception e) {
  579. e.printStackTrace();
  580. }
  581. }
  582. }
  583. }
  584. @Data
  585. class TaskResultRequestVO {
  586. private static final long serialVersionUID = 1L;
  587. private String taskResult;
  588. }
  589. }