ClientFileUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.jiayue.ipfcst.client.utils;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.http.HttpUtil;
  4. import cn.hutool.json.JSONObject;
  5. import cn.hutool.json.JSONUtil;
  6. import com.jiayue.ipfcst.client.domain.entity.ClientConfig;
  7. import com.jiayue.ipfcst.client.domain.entity.ClientResponse;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.stereotype.Component;
  10. import java.io.*;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. @Slf4j
  15. @Component
  16. public class ClientFileUtils {
  17. public static ClientConfig loadLocalConfigFile() {
  18. ClientConfig clientConfig = new ClientConfig();
  19. try {
  20. File file=new File("./clientConfig.properties");
  21. String content = org.apache.commons.io.FileUtils.readFileToString(file,"UTF-8");
  22. clientConfig = JSONUtil.toBean(content, ClientConfig.class);
  23. } catch (Exception e) {
  24. log.error("读取本地配置文件:{} 出现错误:{};尝试请求云预测获取配置并重写本地配置。","clientConfig.properties",e.getLocalizedMessage());
  25. }
  26. return clientConfig;
  27. }
  28. public static ClientConfig loadDefaultLocalConfigFile() throws Exception {
  29. ClientConfig clientConfig;
  30. try {
  31. File file=new File("./default.properties");
  32. String content = org.apache.commons.io.FileUtils.readFileToString(file,"UTF-8");
  33. clientConfig = JSONUtil.toBean(content, ClientConfig.class);
  34. } catch (Exception e) {
  35. log.error("读取本地default文件:{} 出现错误:{},请在client.jar包同级路径配置基本参数文件default.properties,模板找运维人员,设置云平台地址和该场站编码即可。","default.properties",e.getLocalizedMessage());
  36. throw new Exception("读取本地default文件出现错误" + e);
  37. }
  38. return clientConfig;
  39. }
  40. /**
  41. * 加载系统参数并获取云端client配置信息
  42. * @return
  43. */
  44. public static ClientConfig getCloudConfigFile() throws Exception {
  45. ClientConfig clientConfigDefault = loadDefaultLocalConfigFile();
  46. ClientConfig clientConfig = new ClientConfig();
  47. String body="";
  48. try {
  49. Map postParms = new HashMap<>(2);
  50. postParms.put("stationCode",clientConfigDefault.getStationCode());
  51. postParms.put("sign", Md5Util.makeMd5(clientConfigDefault.getStationCode()));
  52. body = HttpUtil.get(clientConfigDefault.getCloudAddr() + "getConfigInfo" ,postParms,10000);
  53. log.info("请求云预测获取气象服务器运行参数配置响应:{}",body);
  54. ClientResponse clientResponse = JSONUtil.toBean(body, ClientResponse.class);
  55. if (0 == clientResponse.getCode() && clientResponse.getData().length()>0) {
  56. clientConfig = JSONUtil.toBean(clientResponse.getData(), ClientConfig.class);
  57. clientConfig.setDqAndNwpFileTask(clientConfigDefault.getDqAndNwpFileTask());
  58. clientConfig.setFxglTask(clientConfigDefault.getFxglTask());
  59. clientConfig.setZxglTask(clientConfigDefault.getZxglTask());
  60. clientConfig.setCloudAddr(clientConfigDefault.getCloudAddr());
  61. }else {
  62. log.error("请求云预测获取client配置信息失败,响应报文:{}",body);
  63. }
  64. }catch (Exception e){
  65. throw new Exception("请求云预测获取client配置信息失败,响应报文:"+body);
  66. }
  67. return clientConfig;
  68. }
  69. /**
  70. * 将JSON数据格式化并保存到文件中
  71. * @param jsonData 需要输出的json数
  72. * @param filePath 输出的文件地址
  73. * @return
  74. */
  75. public static boolean createJsonFile(Object jsonData, String filePath) {
  76. String content = JSONUtil.toJsonStr(jsonData);
  77. // 标记文件生成是否成功
  78. boolean flag = true;
  79. // 生成json格式文件
  80. try {
  81. // 保证创建一个新文件
  82. File file = new File(filePath);
  83. if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
  84. file.getParentFile().mkdirs();
  85. }
  86. if (file.exists()) { // 如果已存在,删除旧文件
  87. file.delete();
  88. }
  89. file.createNewFile();
  90. // 将格式化后的字符串写入文件
  91. Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
  92. write.write(content);
  93. write.flush();
  94. write.close();
  95. } catch (Exception e) {
  96. flag = false;
  97. e.printStackTrace();
  98. }
  99. return flag;
  100. }
  101. /**
  102. * 获取同步配置信息
  103. * @return
  104. */
  105. public static ClientConfig syncClientConfig() throws Throwable {
  106. //加载云端配置,重新写入配置信息
  107. ClientConfig clientConfig = ClientFileUtils.getCloudConfigFile();
  108. ClientConfig clientConfigLocal = ClientFileUtils.loadLocalConfigFile();
  109. if(!clientConfig.getStationCode().equals(clientConfigLocal.getStationCode())){
  110. log.error("云端场站编码:{} 与本地:{} 配置不同,请检查默认配置中的场站编码 ",clientConfig.getStationCode(),clientConfigLocal.getStationCode());
  111. return clientConfigLocal;
  112. }
  113. List<String[]> strings = EntityUtil.checkDifference(clientConfig,clientConfigLocal);
  114. //参数未修改
  115. if(strings.size()==0){
  116. log.info("client云预测参数未修改,不需要更新本地配置文件。");
  117. return clientConfigLocal;
  118. }
  119. //替换本地参数为云端最新的配置
  120. clientConfigLocal.setFxglPath(clientConfig.getFxglPath());
  121. clientConfigLocal.setZxglPath(clientConfig.getZxglPath());
  122. clientConfigLocal.setStationCode(clientConfig.getStationCode());
  123. clientConfigLocal.setRenameFile(clientConfig.getRenameFile());
  124. clientConfigLocal.setDownLoadProtocol(clientConfig.getDownLoadProtocol());
  125. clientConfigLocal.setToRebootExternal(clientConfig.getToRebootExternal());
  126. clientConfigLocal.setStationStatus(clientConfig.getStationStatus());
  127. clientConfigLocal.setFxglType(clientConfig.getFxglType());
  128. clientConfigLocal.setDqAndNwpFileTask(clientConfig.getDqAndNwpFileTask());
  129. clientConfigLocal.setFxglTask(clientConfig.getFxglTask());
  130. clientConfigLocal.setZxglTask(clientConfig.getZxglTask());
  131. clientConfigLocal.setCloudAddr(clientConfig.getCloudAddr());
  132. clientConfigLocal.setDownLoadFileChoose(clientConfig.getDownLoadFileChoose());
  133. //更新后的参数写入本地配置文件
  134. ClientFileUtils.createJsonFile(JSONUtil.toJsonStr(clientConfigLocal), "./clientConfig.properties");
  135. log.info("client启动并加载云预测参数完成,更新本地配置文件。");
  136. return clientConfigLocal;
  137. }
  138. /**
  139. * 文件不存在则创建
  140. * @param filePath
  141. */
  142. public static void ifFileNotExistToCreate(String filePath){
  143. File file = new File(filePath);
  144. if (!file.exists() || file.length() ==0) {
  145. log.info("配置文件:{} 不存在,进行默认参数创建该文件",filePath);
  146. // String gang = "-";
  147. // String gangB = "-B";
  148. try {
  149. ClientFileUtils clientFileUtils = new ClientFileUtils();
  150. File settingFile ;
  151. String stationCode = "J01100";
  152. // //应对AB机情况的外网初始化场站编码配置
  153. // String externalUrl = "http://localhost:8082/api/forcast";
  154. // if(clientFileUtils.getPath().contains(gangB)){
  155. // externalUrl = "http://localhost:8083/api/forcast";
  156. // }
  157. // try{
  158. // String body = HttpUtil.get(externalUrl,60000);
  159. // JSONObject json = JSONUtil.parseObj(body);
  160. // stationCode = json.get("stationCode").toString();
  161. // }catch (Exception e){
  162. // log.error("请求external获取场站编码错误:{},请求地址:{} ;尝试读取external部署的application.yml文件中的场站编码",e.getLocalizedMessage(),externalUrl);
  163. // }
  164. //
  165. // //如果没请求到场站编码数据直接读取external配置文件中的场站编码
  166. // if(StrUtil.isBlankIfStr(stationCode)){
  167. // if(clientFileUtils.getPath().contains(gangB)){
  168. // settingFile = new File("/home/syjy/ipfcstV3-B/settings/application.yml");
  169. // }else{
  170. // settingFile = new File("/home/syjy/ipfcstV3/settings/application.yml");
  171. // }
  172. // if(settingFile.exists()) {
  173. // String content = org.apache.commons.io.FileUtils.readFileToString(settingFile, "UTF-8");
  174. // stationCode = content.substring(content.indexOf("stationCode") + 12, content.indexOf("stationCode") + 25).trim();
  175. // }else{
  176. // log.error("external配置文件:{} 不存在,未获取到有效的场站编码,无法启动client,退出本程序",settingFile);
  177. // System.exit(1);
  178. // return;
  179. // }
  180. // }
  181. // //过滤山东场站编码有AB机的情况
  182. // if(stationCode.contains(gang)){
  183. // stationCode = stationCode.substring(0,8);
  184. // }else {
  185. // stationCode = stationCode.substring(0,6);
  186. // }
  187. ClientConfig clientConfig = new ClientConfig();
  188. clientConfig.setStationCode(stationCode);
  189. clientConfig.setCloudAddr("https://117.78.19.70:9010/client/");
  190. clientConfig.setDqAndNwpFileTask("是");
  191. clientConfig.setFxglTask("是");
  192. clientConfig.setZxglTask("否");
  193. //更新后的参数写入本地配置文件
  194. file.createNewFile();
  195. ClientFileUtils.createJsonFile(JSONUtil.toJsonStr(clientConfig), filePath);
  196. } catch (IOException e) {
  197. log.error("初始化配置文件失败:{}",e);
  198. }
  199. }
  200. }
  201. public String getPath() {
  202. String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
  203. if (System.getProperty("os.name").contains("dows")) {
  204. path = path.substring(1, path.length());
  205. }
  206. if (path.contains("jar")) {
  207. path = path.substring(0, path.lastIndexOf("."));
  208. return path.substring(0, path.lastIndexOf("/"));
  209. }
  210. return path.replace("target/classes/", "");
  211. }
  212. }