| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.logging.log4j.util.Strings; |
| | | import org.springblade.core.redis.cache.BladeRedis; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.*; |
| | |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * 南科院水库预警算法api调用服务接口 |
| | |
| | | // 获取当前时间 |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | // 减去一个小时 |
| | | LocalDateTime oneHourAgo = now.minusHours(1); |
| | | LocalDateTime oneHourAgo = now.minusHours(8); |
| | | // 格式化时间 |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); |
| | | String oneHour = oneHourAgo.format(formatter) + ":00"; |
| | | String nowHour = now.format(formatter) + ":00"; |
| | | // 调用中台接口获取实时测点数据 |
| | | JSONArray ztData = getZtData("", |
| | | ZtConfigConstant.url_prefix + DamEnum.ofType(type).getApiUrl() + "?tm_list=" + "'" + oneHour + "'," + "'" + nowHour + "'"); |
| | | ZtConfigConstant.url_prefix + DamEnum.ofType(type).getApiUrl() + "?start_tm=" + oneHour + "&end_tm=" + nowHour + ""); |
| | | // 解析数据 |
| | | for (int i = 0; i < ztData.size(); i++) { |
| | | JSONObject jsonObject = ztData.getJSONObject(i); |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("batchId",UUID.randomUUID()); |
| | | map.put("serialNo",getSerialNo()); |
| | | // 水库信息设置 |
| | | waterInfoSet(jsonObject, map); |
| | | // 测点信息设置 |
| | | pointValueInfoSet(jsonObject,map, DamEnum.ofType(type).getName()); |
| | | // 发送请求 |
| | | // String response = sendPostJsonRequest(OpenApiConstant.POINT_VALUE_INLET_URL, map); |
| | | // JSONObject parseObject = JSON.parseObject(response); |
| | | // Integer status = parseObject.getInteger("status"); |
| | | // flag = status==200?true:false; |
| | | log.info("测点输入结果:" + flag); |
| | | flag = waterInfoSet(jsonObject, map,flag); |
| | | if (flag) { |
| | | // 测点信息设置 |
| | | flag = pointValueInfoSet(jsonObject, map, type,flag); |
| | | } |
| | | if (flag) { |
| | | map.put("batchId",UUID.randomUUID()); |
| | | map.put("serialNo",getSerialNo()); |
| | | // 发送请求 |
| | | String response = sendPostJsonRequest(OpenApiConstant.POINT_VALUE_INLET_URL, map); |
| | | JSONObject parseObject = JSON.parseObject(response); |
| | | Integer status = parseObject.getInteger("status"); |
| | | flag = status==200?true:false; |
| | | log.info("测点输入结果:" + flag); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | * @param jsonObject |
| | | * @param map |
| | | */ |
| | | private void waterInfoSet(JSONObject jsonObject, Map<String, Object> map) { |
| | | List<Map<String,Object>> waterLevelInfoList = new ArrayList<>(); |
| | | Map<String, Object> waterLevelInfoMap = new HashMap<>(); |
| | | waterLevelInfoMap.put("damCode",jsonObject.getString("res_cd")); |
| | | waterLevelInfoMap.put("waterLevel",jsonObject.getString("res_cd")); |
| | | waterLevelInfoMap.put("waterLevelTime",jsonObject.getString("res_cd")); |
| | | waterLevelInfoMap.put("rainCondition",jsonObject.getString("res_cd")); |
| | | waterLevelInfoMap.put("rainConditionTime",jsonObject.getString("res_cd")); |
| | | waterLevelInfoList.add(waterLevelInfoMap); |
| | | map.put("waterLevelInfo",waterLevelInfoList); |
| | | private boolean waterInfoSet(JSONObject jsonObject, Map<String, Object> map,boolean flag) { |
| | | String rz = jsonObject.getString("rz"); |
| | | flag = false; |
| | | if (!Strings.isBlank(rz)) { |
| | | flag = true; |
| | | String res_cd = replaceSpecialStr(jsonObject.getString("res_cd")); |
| | | List<Map<String, Object>> waterLevelInfoList = new ArrayList<>(); |
| | | Map<String, Object> waterLevelInfoMap = new HashMap<>(); |
| | | waterLevelInfoMap.put("damCode", res_cd); |
| | | waterLevelInfoMap.put("waterLevel", jsonObject.getString("rz")); |
| | | waterLevelInfoMap.put("waterLevelTime", jsonObject.getString("rz_tm")); |
| | | waterLevelInfoMap.put("rainCondition", jsonObject.getString("drp")); |
| | | waterLevelInfoMap.put("rainConditionTime", jsonObject.getString("drp_tm")); |
| | | waterLevelInfoList.add(waterLevelInfoMap); |
| | | map.put("waterLevelInfo", waterLevelInfoList); |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * 测点信息设置 |
| | | * @param map |
| | | */ |
| | | private void pointValueInfoSet(JSONObject jsonObject,Map<String, Object> map,String type) { |
| | | private boolean pointValueInfoSet(JSONObject jsonObject,Map<String, Object> map,String type,boolean flag) { |
| | | flag = false; |
| | | int num = 0; |
| | | List<Map<String,Object>> pointValueInfoList = new ArrayList<>(); |
| | | JSONArray childArray = jsonObject.getJSONArray("child"); |
| | | //遍历处理 |
| | | for (int i = 0; i < childArray.size(); i++) { |
| | | JSONObject childArrayJSONObject = childArray.getJSONObject(i); |
| | | String pointCodeTemp = jsonObject.getString("res_cd") + "_" |
| | | + childArrayJSONObject.getString("cd") |
| | | + "_" + type; |
| | | Map<String, Object> pointValueInfoMap = new HashMap<>(); |
| | | pointValueInfoMap.put("damCode",jsonObject.getString("res_cd")); |
| | | pointValueInfoMap.put("pointCode",pointCodeTemp); |
| | | pointValueInfoMap.put("type",type); |
| | | pointValueInfoMap.put("value",childArrayJSONObject.getString("sy_val")); |
| | | pointValueInfoMap.put("time",childArrayJSONObject.getString("tm")); |
| | | pointValueInfoList.add(pointValueInfoMap); |
| | | String sy_val = childArrayJSONObject.getString("sy_val"); |
| | | // type 为3 时为位移,位移返回数据格式不一致 |
| | | if (type.equals("3")){ |
| | | // 获取-位移_z 垂直 du是数值位移(垂直),de向东位移(上下游),dn向北位移(左右岸) |
| | | String du = childArrayJSONObject.getString("du"); |
| | | String de = childArrayJSONObject.getString("de"); |
| | | String dn = childArrayJSONObject.getString("dn"); |
| | | if (!Strings.isBlank(du)){ |
| | | num = setPointValue(jsonObject, DamEnum.ofType("5").getName(), num, pointValueInfoList, childArrayJSONObject, du); |
| | | } |
| | | if (!Strings.isBlank(dn)){ |
| | | num = setPointValue(jsonObject, DamEnum.ofType("4").getName(), num, pointValueInfoList, childArrayJSONObject, dn); |
| | | } |
| | | if (!Strings.isBlank(de)){ |
| | | num = setPointValue(jsonObject, DamEnum.ofType("3").getName(), num, pointValueInfoList, childArrayJSONObject, de); |
| | | } |
| | | }else { |
| | | // 有测点值才加入 |
| | | if (!Strings.isBlank(sy_val)) { |
| | | // 设置测点信息 |
| | | num = setPointValue(jsonObject, DamEnum.ofType(type).getName(), num, pointValueInfoList, childArrayJSONObject, sy_val); |
| | | } |
| | | } |
| | | } |
| | | map.put("pointValueInfo",pointValueInfoList); |
| | | // 只要有一个测点值设置 |
| | | if (num>0) { |
| | | flag = true; |
| | | map.put("pointValueInfo", pointValueInfoList); |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * 设置测点数据 |
| | | * @param jsonObject |
| | | * @param type |
| | | * @param num |
| | | * @param pointValueInfoList |
| | | * @param childArrayJSONObject |
| | | * @param sy_val |
| | | * @return |
| | | */ |
| | | private int setPointValue(JSONObject jsonObject, String type, int num, List<Map<String, Object>> pointValueInfoList, JSONObject childArrayJSONObject, String sy_val) { |
| | | num++; |
| | | String res_cd = replaceSpecialStr(jsonObject.getString("res_cd")); |
| | | // 测点编码:水库GUID+测点编码+类型 |
| | | String pointCodeTemp = res_cd + "_" |
| | | + childArrayJSONObject.getString("cd") |
| | | + "_" + type; |
| | | Map<String, Object> pointValueInfoMap = new HashMap<>(); |
| | | pointValueInfoMap.put("damCode", res_cd); |
| | | pointValueInfoMap.put("pointCode", pointCodeTemp); |
| | | pointValueInfoMap.put("type", type); |
| | | pointValueInfoMap.put("value", sy_val); |
| | | pointValueInfoMap.put("time", childArrayJSONObject.getString("tm")); |
| | | pointValueInfoList.add(pointValueInfoMap); |
| | | return num; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * 去除字符串中的空格、回车、换行符、制表符等 |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static String replaceSpecialStr(String str) { |
| | | String repl = ""; |
| | | if (str!=null) { |
| | | Pattern p = Pattern.compile("\\s*|\t|\r|\n"); |
| | | Matcher m = p.matcher(str); |
| | | repl = m.replaceAll(""); |
| | | } |
| | | return repl; |
| | | } |
| | | } |