package com.jiayue.ipfcst.client.utils; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.jiayue.ipfcst.client.domain.entity.ClientConfig; import com.jiayue.ipfcst.client.domain.entity.ClientResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.io.*; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j @Component public class ClientFileUtils { public static ClientConfig loadLocalConfigFile() { ClientConfig clientConfig = new ClientConfig(); try { File file=new File("./clientConfig.properties"); String content = org.apache.commons.io.FileUtils.readFileToString(file,"UTF-8"); clientConfig = JSONUtil.toBean(content, ClientConfig.class); } catch (Exception e) { log.error("读取本地配置文件:{} 出现错误:{};尝试请求云预测获取配置并重写本地配置。","clientConfig.properties",e.getLocalizedMessage()); } return clientConfig; } public static ClientConfig loadDefaultLocalConfigFile() throws Exception { ClientConfig clientConfig; try { File file=new File("./default.properties"); String content = org.apache.commons.io.FileUtils.readFileToString(file,"UTF-8"); clientConfig = JSONUtil.toBean(content, ClientConfig.class); } catch (Exception e) { log.error("读取本地default文件:{} 出现错误:{},请在client.jar包同级路径配置基本参数文件default.properties,模板找运维人员,设置云平台地址和该场站编码即可。","default.properties",e.getLocalizedMessage()); throw new Exception("读取本地default文件出现错误" + e); } return clientConfig; } /** * 加载系统参数并获取云端client配置信息 * @return */ public static ClientConfig getCloudConfigFile() throws Exception { ClientConfig clientConfigDefault = loadDefaultLocalConfigFile(); ClientConfig clientConfig = new ClientConfig(); String body=""; try { Map postParms = new HashMap<>(2); postParms.put("stationCode",clientConfigDefault.getStationCode()); postParms.put("sign", Md5Util.makeMd5(clientConfigDefault.getStationCode())); body = HttpUtil.get(clientConfigDefault.getCloudAddr() + "getConfigInfo" ,postParms,10000); log.info("请求云预测获取气象服务器运行参数配置响应:{}",body); ClientResponse clientResponse = JSONUtil.toBean(body, ClientResponse.class); if (0 == clientResponse.getCode() && clientResponse.getData().length()>0) { clientConfig = JSONUtil.toBean(clientResponse.getData(), ClientConfig.class); clientConfig.setDqAndNwpFileTask(clientConfigDefault.getDqAndNwpFileTask()); clientConfig.setFxglTask(clientConfigDefault.getFxglTask()); clientConfig.setZxglTask(clientConfigDefault.getZxglTask()); clientConfig.setCloudAddr(clientConfigDefault.getCloudAddr()); }else { log.error("请求云预测获取client配置信息失败,响应报文:{}",body); } }catch (Exception e){ throw new Exception("请求云预测获取client配置信息失败,响应报文:"+body); } return clientConfig; } /** * 将JSON数据格式化并保存到文件中 * @param jsonData 需要输出的json数 * @param filePath 输出的文件地址 * @return */ public static boolean createJsonFile(Object jsonData, String filePath) { String content = JSONUtil.toJsonStr(jsonData); // 标记文件生成是否成功 boolean flag = true; // 生成json格式文件 try { // 保证创建一个新文件 File file = new File(filePath); if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录 file.getParentFile().mkdirs(); } if (file.exists()) { // 如果已存在,删除旧文件 file.delete(); } file.createNewFile(); // 将格式化后的字符串写入文件 Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); write.write(content); write.flush(); write.close(); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } /** * 获取同步配置信息 * @return */ public static ClientConfig syncClientConfig() throws Throwable { //加载云端配置,重新写入配置信息 ClientConfig clientConfig = ClientFileUtils.getCloudConfigFile(); ClientConfig clientConfigLocal = ClientFileUtils.loadLocalConfigFile(); if(!clientConfig.getStationCode().equals(clientConfigLocal.getStationCode())){ log.error("云端场站编码:{} 与本地:{} 配置不同,请检查默认配置中的场站编码 ",clientConfig.getStationCode(),clientConfigLocal.getStationCode()); return clientConfigLocal; } List strings = EntityUtil.checkDifference(clientConfig,clientConfigLocal); //参数未修改 if(strings.size()==0){ log.info("client云预测参数未修改,不需要更新本地配置文件。"); return clientConfigLocal; } //替换本地参数为云端最新的配置 clientConfigLocal.setFxglPath(clientConfig.getFxglPath()); clientConfigLocal.setZxglPath(clientConfig.getZxglPath()); clientConfigLocal.setStationCode(clientConfig.getStationCode()); clientConfigLocal.setRenameFile(clientConfig.getRenameFile()); clientConfigLocal.setDownLoadProtocol(clientConfig.getDownLoadProtocol()); clientConfigLocal.setToRebootExternal(clientConfig.getToRebootExternal()); clientConfigLocal.setStationStatus(clientConfig.getStationStatus()); clientConfigLocal.setFxglType(clientConfig.getFxglType()); clientConfigLocal.setDqAndNwpFileTask(clientConfig.getDqAndNwpFileTask()); clientConfigLocal.setFxglTask(clientConfig.getFxglTask()); clientConfigLocal.setZxglTask(clientConfig.getZxglTask()); clientConfigLocal.setCloudAddr(clientConfig.getCloudAddr()); clientConfigLocal.setDownLoadFileChoose(clientConfig.getDownLoadFileChoose()); //更新后的参数写入本地配置文件 ClientFileUtils.createJsonFile(JSONUtil.toJsonStr(clientConfigLocal), "./clientConfig.properties"); log.info("client启动并加载云预测参数完成,更新本地配置文件。"); return clientConfigLocal; } /** * 文件不存在则创建 * @param filePath */ public static void ifFileNotExistToCreate(String filePath){ File file = new File(filePath); if (!file.exists() || file.length() ==0) { log.info("配置文件:{} 不存在,进行默认参数创建该文件",filePath); // String gang = "-"; // String gangB = "-B"; try { ClientFileUtils clientFileUtils = new ClientFileUtils(); File settingFile ; String stationCode = "J01100"; // //应对AB机情况的外网初始化场站编码配置 // String externalUrl = "http://localhost:8082/api/forcast"; // if(clientFileUtils.getPath().contains(gangB)){ // externalUrl = "http://localhost:8083/api/forcast"; // } // try{ // String body = HttpUtil.get(externalUrl,60000); // JSONObject json = JSONUtil.parseObj(body); // stationCode = json.get("stationCode").toString(); // }catch (Exception e){ // log.error("请求external获取场站编码错误:{},请求地址:{} ;尝试读取external部署的application.yml文件中的场站编码",e.getLocalizedMessage(),externalUrl); // } // // //如果没请求到场站编码数据直接读取external配置文件中的场站编码 // if(StrUtil.isBlankIfStr(stationCode)){ // if(clientFileUtils.getPath().contains(gangB)){ // settingFile = new File("/home/syjy/ipfcstV3-B/settings/application.yml"); // }else{ // settingFile = new File("/home/syjy/ipfcstV3/settings/application.yml"); // } // if(settingFile.exists()) { // String content = org.apache.commons.io.FileUtils.readFileToString(settingFile, "UTF-8"); // stationCode = content.substring(content.indexOf("stationCode") + 12, content.indexOf("stationCode") + 25).trim(); // }else{ // log.error("external配置文件:{} 不存在,未获取到有效的场站编码,无法启动client,退出本程序",settingFile); // System.exit(1); // return; // } // } // //过滤山东场站编码有AB机的情况 // if(stationCode.contains(gang)){ // stationCode = stationCode.substring(0,8); // }else { // stationCode = stationCode.substring(0,6); // } ClientConfig clientConfig = new ClientConfig(); clientConfig.setStationCode(stationCode); clientConfig.setCloudAddr("https://117.78.19.70:9010/client/"); clientConfig.setDqAndNwpFileTask("是"); clientConfig.setFxglTask("是"); clientConfig.setZxglTask("否"); //更新后的参数写入本地配置文件 file.createNewFile(); ClientFileUtils.createJsonFile(JSONUtil.toJsonStr(clientConfig), filePath); } catch (IOException e) { log.error("初始化配置文件失败:{}",e); } } } public String getPath() { String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); if (System.getProperty("os.name").contains("dows")) { path = path.substring(1, path.length()); } if (path.contains("jar")) { path = path.substring(0, path.lastIndexOf(".")); return path.substring(0, path.lastIndexOf("/")); } return path.replace("target/classes/", ""); } }