| | |
| | | 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); |
| | | if (flag) { |
| | | // 测点信息设置 |
| | | flag = pointValueInfoSet(jsonObject, map, type,flag); |
| | | } |
| | | if (flag) { |
| | | map.put("batchId",UUID.randomUUID()); |
| | | map.put("serialNo",getSerialNo()); |
| | | log.info("发送数据:{}",map); |
| | | // 发送请求 |
| | | 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<>(); |
| | | private boolean waterInfoSet(JSONObject jsonObject, Map<String, Object> map) { |
| | | String rz = jsonObject.getString("rz"); |
| | | if(Strings.isBlank(rz)){ |
| | | return false; |
| | | } |
| | | // 需要过滤2022高标准水库才发送 |
| | | String res_cd = replaceSpecialStr(jsonObject.getString("res_cd")); |
| | | String flag = openApiMapper.getResInfoBy2022(res_cd); |
| | | if (Strings.isBlank(flag)){ |
| | | return false; |
| | | } |
| | | 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")); |
| | | 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); |
| | | map.put("waterLevelInfo", waterLevelInfoList); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 测点信息设置 |
| | | * @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 |
| | | */ |
| | | public JSONArray getZtData(String params, String url) { |
| | | log.info("实时测点请求中台接口:{}",url); |
| | | //设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.add(ZtConfigConstant.header_key, ZtConfigConstant.header_value); |
| | |
| | | formEntity, |
| | | String.class); |
| | | JSONObject jsonObject = JSON.parseObject(responseEntity.getBody()); |
| | | log.info("实时测点请求中台接口结果数据:{}",jsonObject); |
| | | // 返回 |
| | | return JSONArray.parseArray(jsonObject.get("data").toString()); |
| | | } catch (Exception e) { |
| | |
| | | for (int i = 0; i < dataJsonArray.size(); i++) { |
| | | DamAlarmRecord damAlarmRecord = new DamAlarmRecord(); |
| | | JSONObject alarmRecordJSONObject = dataJsonArray.getJSONObject(i); |
| | | damAlarmRecord.setPointId(alarmRecordJSONObject.getString("pointId")); |
| | | // 设置测点信息,类型、测点编号 |
| | | setPointInfo(damAlarmRecord,alarmRecordJSONObject.getString("pointId")); |
| | | // 设置其他信息 |
| | | damAlarmRecord.setResGuid(alarmRecordJSONObject.getString("RS_CODE")); |
| | | damAlarmRecord.setCollecTime(alarmRecordJSONObject.getDate("collecTime")); |
| | | damAlarmRecord.setPointValue(alarmRecordJSONObject.getString("ponitValue")); |
| | | JSONObject generalInfo = alarmRecordJSONObject.getJSONObject("generalInfo"); |
| | | damAlarmRecord.setPointWarnLevel(generalInfo.getInteger("pointWarnLevel")); |
| | | damAlarmRecord.setPointWarnInfo(generalInfo.getString("pointWarnInfo")); |
| | | damAlarmRecord.setCreateTime(new Date()); |
| | | // 保存 |
| | | // 获取回调类型:1:告警规则法 2: 预测法 3:待补充 |
| | | Integer callbackType = alarmRecordJSONObject.getInteger("callbackType"); |
| | | damAlarmRecord.setCallbackType(callbackType); |
| | | if (callbackType==1){ |
| | | JSONObject generalInfo = alarmRecordJSONObject.getJSONObject("generalInfo"); |
| | | damAlarmRecord.setPointWarnLevel(generalInfo.getInteger("pointWarnLevel")); |
| | | damAlarmRecord.setPointWarnInfo(generalInfo.getString("pointWarnInfo")); |
| | | } |
| | | if (callbackType==2){ |
| | | damAlarmRecord.setValue(alarmRecordJSONObject.getString("value")); |
| | | damAlarmRecord.setTimeValue(alarmRecordJSONObject.getString("timeValue")); |
| | | } |
| | | // 保存详情,预测没有详情 |
| | | flag = damAlarmRecordService.save(damAlarmRecord); |
| | | if (flag){ |
| | | if (callbackType==1 && flag){ |
| | | flag = saveDamAlarmRecordDetail(alarmRecordJSONObject.getJSONArray("detailInfo"),damAlarmRecord); |
| | | } |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * 设置测点信息 |
| | | * @param pointId |
| | | */ |
| | | private void setPointInfo(DamAlarmRecord damAlarmRecord,String pointId) { |
| | | damAlarmRecord.setPointId(pointId); |
| | | // 测点值解析赋值 |
| | | String[] point = pointId.split("_"); |
| | | // 渗压、渗流 |
| | | if (point.length==3) { |
| | | // 设置测点id |
| | | damAlarmRecord.setStationId(point[1]); |
| | | String typeName = point[2]; |
| | | // 设置 大坝安全监测类型 |
| | | damAlarmRecord.setType(Integer.parseInt(DamEnum.ofName(typeName).getKey())); |
| | | } |
| | | // 位移 |
| | | if (point.length==4) { |
| | | // 设置测点id |
| | | damAlarmRecord.setStationId(point[1]); |
| | | String typeName = point[2] + "_" + point[3]; |
| | | // 设置 大坝安全监测类型 |
| | | damAlarmRecord.setType(Integer.parseInt(DamEnum.ofName(typeName).getKey())); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean saveDamAlarmRecordDetail(JSONArray detailInfo,DamAlarmRecord damAlarmRecord) { |
| | | boolean flag = false; |
| | | List<DamAlarmRecordDetail> list = new ArrayList<>(); |
| | | for (int i = 0; i < detailInfo.size(); i++) { |
| | | DamAlarmRecordDetail damAlarmRecordDetail = new DamAlarmRecordDetail(); |
| | | JSONObject jsonObject = detailInfo.getJSONObject(i); |
| | | damAlarmRecordDetail.setDamAlarmRecordId(damAlarmRecord.getId()); |
| | | damAlarmRecordDetail.setAlgorithmName(jsonObject.getString("algorithmName")); |
| | | damAlarmRecordDetail.setAlgorithmWarnLevel(jsonObject.getInteger("algorithmWarnLevel")); |
| | | damAlarmRecordDetail.setAlgorithmWarnInfo(jsonObject.getString("algorithmWarnInfo")); |
| | | damAlarmRecordDetail.setAlgorithmWarnExplain(jsonObject.getString("algorithmWarnExplain")); |
| | | damAlarmRecordDetail.setCreateTime(new Date()); |
| | | // 加入集合 |
| | | list.add(damAlarmRecordDetail); |
| | | } |
| | | // 保存 |
| | | if (list.size()>0) { |
| | | flag = damAlarmRecordDetailService.saveBatch(list); |
| | | }else { |
| | | flag = true; |
| | | if (null!=detailInfo) { |
| | | List<DamAlarmRecordDetail> list = new ArrayList<>(); |
| | | for (int i = 0; i < detailInfo.size(); i++) { |
| | | DamAlarmRecordDetail damAlarmRecordDetail = new DamAlarmRecordDetail(); |
| | | JSONObject jsonObject = detailInfo.getJSONObject(i); |
| | | damAlarmRecordDetail.setDamAlarmRecordId(damAlarmRecord.getId()); |
| | | damAlarmRecordDetail.setAlgorithmName(jsonObject.getString("algorithmName")); |
| | | damAlarmRecordDetail.setAlgorithmWarnLevel(jsonObject.getInteger("algorithmWarnLevel")); |
| | | damAlarmRecordDetail.setAlgorithmWarnInfo(jsonObject.getString("algorithmWarnInfo")); |
| | | damAlarmRecordDetail.setAlgorithmWarnExplain(jsonObject.getString("algorithmWarnExplain")); |
| | | damAlarmRecordDetail.setCreateTime(new Date()); |
| | | // 加入集合 |
| | | list.add(damAlarmRecordDetail); |
| | | } |
| | | // 保存 |
| | | if (list.size() > 0) { |
| | | flag = damAlarmRecordDetailService.saveBatch(list); |
| | | } else { |
| | | flag = true; |
| | | } |
| | | } |
| | | 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; |
| | | } |
| | | } |