From 8b37a3da0aea0ba2857fbdd208cd4c655356e854 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期一, 17 三月 2025 10:43:26 +0800 Subject: [PATCH] getLastValues --- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java | 57 ++++++++++++++++++++++++++++++--------------------------- 1 files changed, 30 insertions(+), 27 deletions(-) diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java index da14d16..3eed380 100644 --- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java +++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java @@ -62,6 +62,8 @@ private static final String IS_SUCCESS = "isSuccess"; + private static final String RESP_DATA = "data"; + public static final String TIV = "TagIdValue:"; public static final long offset = 60 * 10L; @@ -121,7 +123,7 @@ String responseStr = HttpUtils.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString()); JSONObject responseObj = JSON.parseObject(responseStr); if (STA_TRUE.equals(responseObj.get(IS_SUCCESS).toString())) { - JSONArray tagValueList = responseObj.getJSONArray("data"); + JSONArray tagValueList = responseObj.getJSONArray(RESP_DATA); if (!CollectionUtils.isEmpty(tagValueList)) { for (int i = 0; i < tagValueList.size(); i++) { JSONObject item = tagValueList.getJSONObject(i); @@ -140,14 +142,14 @@ return result; } List<String> noCacheTagNames = new ArrayList<>();//未缓存的tag - for (int i = 0; i < tagNames.size(); i++) { + for (String tagName : tagNames) { //先查缓存 - BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps("IhdTag:" + tagNames.get(i)); + BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps("IhdTag:" + tagName); if (ops.get(V) != null) { BigDecimal value = new BigDecimal(ops.get(V).toString()); - result.put(tagNames.get(i), value.setScale(3, RoundingMode.HALF_UP)); + result.put(tagName, value.setScale(3, RoundingMode.HALF_UP)); } else { - noCacheTagNames.add(tagNames.get(i)); + noCacheTagNames.add(tagName); } } if (CollectionUtils.isEmpty(noCacheTagNames)) { @@ -161,25 +163,26 @@ String responseStr = ""; responseStr = HttpUtils.sendPost(httpApi.getUrl().replace("getPointdatasAvg", "getPointslast") + "/" + currentDate, tagSb); JSONObject responseObj = JSON.parseObject(responseStr); - if (STA_TRUE.equals(responseObj.get(IS_SUCCESS).toString())) { - JSONArray tagValueList = responseObj.getJSONArray("data"); - if (!CollectionUtils.isEmpty(tagValueList)) { - return result; + if (!STA_TRUE.equals(responseObj.get(IS_SUCCESS).toString())) { + return result; + } + JSONArray tagValueList = responseObj.getJSONArray(RESP_DATA); + if (CollectionUtils.isEmpty(tagValueList)) { + return result; + } + for (int i = 0; i < tagValueList.size(); i++) { + JSONObject item = tagValueList.getJSONObject(i); + if (item.get(V) == null) { + continue; } - for (int i = 0; i < tagValueList.size(); i++) { - JSONObject item = tagValueList.getJSONObject(i); - if (item.get(V) == null) { - continue; - } - //存缓存 - BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps("IhdTag:" + item.get(N).toString()); - ops.put(V, item.get(V).toString()); - //设置过期时间 - redisTemplate.expire(item.get(N).toString(), 10, TimeUnit.SECONDS); - //把查询到的数据插入结果集 - BigDecimal value = new BigDecimal(item.get(V).toString()); - result.put(item.get(N).toString(), value.setScale(3, RoundingMode.HALF_UP)); - } + //存缓存 + BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps("IhdTag:" + item.get(N).toString()); + ops.put(V, item.get(V).toString()); + //设置过期时间 + redisTemplate.expire(item.get(N).toString(), 10, TimeUnit.SECONDS); + //把查询到的数据插入结果集 + BigDecimal value = new BigDecimal(item.get(V).toString()); + result.put(item.get(N).toString(), value.setScale(3, RoundingMode.HALF_UP)); } } catch (Exception ex) { log.info("getCurrentValue异常"); @@ -312,7 +315,7 @@ JSONObject responseObj = JSON.parseObject(responseStr); log.info("responseObj=====" + responseObj.toJSONString()); if (STA_TRUE.equals(responseObj.get(IS_SUCCESS).toString())) { - JSONArray tagValueList = responseObj.getJSONArray("data"); + JSONArray tagValueList = responseObj.getJSONArray(RESP_DATA); if (!CollectionUtils.isEmpty(tagValueList)) { for (int i = 0; i < tagValueList.size(); i++) { JSONObject item = tagValueList.getJSONObject(i); @@ -346,7 +349,7 @@ JSONObject responseObj = JSON.parseObject(responseStr); log.info("responseObj=====" + responseObj.toJSONString()); if (STA_TRUE.equals(responseObj.get(IS_SUCCESS).toString())) { - JSONArray tagValueList = responseObj.getJSONArray("data"); + JSONArray tagValueList = responseObj.getJSONArray(RESP_DATA); if (!CollectionUtils.isEmpty(tagValueList)) { for (int i = 0; i < tagValueList.size(); i++) { JSONObject item = tagValueList.getJSONObject(i); @@ -358,8 +361,8 @@ private void setOut(String url, String sourceName, Map<String, Object> result, List<Object[]> params, Date collectTime) { for (int i = 0; i < params.size(); i++) { - String tagId = TIV + TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, params.get(i)[1].toString()); - if(redisTemplate.hasKey(tagId)) { + String tagId = TIV + TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, params.get(i)[1].toString()); + if (redisTemplate.hasKey(tagId)) { result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, params.get(i)[1].toString()), redisTemplate.opsForValue().get(tagId)); } } -- Gitblit v1.9.3