From db184afd0c5bf3359b44eb0251fa5b07386eb3ff Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期三, 06 十一月 2024 09:48:13 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java |   75 +++++++++++++++++++++++++++++--------
 1 files changed, 59 insertions(+), 16 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 9b0cd5f..3743b24 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
@@ -13,14 +13,21 @@
 import com.iailab.module.data.common.utils.DateUtils;
 import com.iailab.module.data.common.utils.HttpRequest;
 import com.iailab.module.data.common.utils.TagUtils;
+import com.iailab.module.data.influxdb.pojo.InfluxPointValueBoolPOJO;
+import com.iailab.module.data.influxdb.pojo.InfluxPointValueDigPOJO;
+import com.iailab.module.data.influxdb.pojo.InfluxPointValueSimPOJO;
+import com.iailab.module.data.influxdb.pojo.InfluxPointValueStrPOJO;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.BoundHashOperations;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 /**
  * iHyperDB采集
@@ -36,6 +43,9 @@
 
     @Autowired
     private HttpApiService httpApiService;
+
+    @Autowired
+    private RedisTemplate redisTemplate;
 
     @Autowired
     private HttpTagService httpTagService;
@@ -83,25 +93,58 @@
 
     public Map<String, Object> getLastValues(List<String> tagNames) {
         Map<String, Object> result = new HashMap<>();
-        Gson gson = new Gson();
-        String tagSb = gson.toJson(tagNames);
-        log.info("body=====" + tagSb);
-        String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
-        String responseStr = HttpRequest.sendPost("http://172.16.59.105:9082/api/IHD/getPointslast" + "/" + currentDate, tagSb);
-        JSONObject responseObj = JSON.parseObject(responseStr);
-        if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
-            JSONArray tagValueList = responseObj.getJSONArray("data");
-            if (!CollectionUtils.isEmpty(tagValueList)) {
-                for (int i = 0; i < tagValueList.size(); i++) {
-                    JSONObject item = tagValueList.getJSONObject(i);
-                    if(item.get("value")!=null){
-                        BigDecimal value = new BigDecimal(item.get("value").toString());
-                        result.put(item.get("tagname").toString(), value.setScale(3, RoundingMode.HALF_UP));
-                    }else{
-                        result.put(item.get("tagname").toString(), CommonConstant.BAD_VALUE);
+        try {
+            if (CollectionUtils.isEmpty(tagNames)) {
+                return result;
+            }
+            List<String> noCacheTagNames = new ArrayList<>();//未缓存的tag
+            for (int i = 0; i < tagNames.size(); i++) {
+                //先查缓存
+                BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps(tagNames.get(i));
+                if (ops.get("value") != null) {
+                    BigDecimal value = new BigDecimal(ops.get("value").toString());
+                    result.put(tagNames.get(i), value.setScale(3, RoundingMode.HALF_UP));
+                } else {
+                    noCacheTagNames.add(tagNames.get(i));
+                }
+            }
+            if (CollectionUtils.isEmpty(noCacheTagNames)) {
+                log.info("全部读取缓存");
+                return result;
+            }
+            log.info("查询未缓存的数据");
+            Gson gson = new Gson();
+            String tagSb = gson.toJson(noCacheTagNames);
+            log.info("body=====" + tagSb);
+            String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
+            String responseStr = "";
+            responseStr = HttpRequest.sendPost("http://172.16.59.105:9082/api/IHD/getPointslast" + "/" + currentDate, tagSb);
+            JSONObject responseObj = JSON.parseObject(responseStr);
+            if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
+                JSONArray tagValueList = responseObj.getJSONArray("data");
+                if (!CollectionUtils.isEmpty(tagValueList)) {
+                    for (int i = 0; i < tagValueList.size(); i++) {
+                        JSONObject item = tagValueList.getJSONObject(i);
+                        if (item.get("value") != null) {
+                            //存缓存
+                            BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps(item.get("tagname").toString());
+                            ops.put("value", item.get("value").toString());
+                            //设置过期时间
+                            redisTemplate.expire(item.get("tagname").toString(), 10, TimeUnit.SECONDS);
+                            //把查询到的数据插入结果集
+                            BigDecimal value = new BigDecimal(item.get("value").toString());
+                            result.put(item.get("tagname").toString(), value.setScale(3, RoundingMode.HALF_UP));
+                        } else {
+                            result.put(item.get("tagname").toString(), CommonConstant.BAD_VALUE);
+                        }
                     }
                 }
             }
+
+        } catch (Exception ex) {
+            log.info("getCurrentValue异常");
+            ex.printStackTrace();
+            throw ex;
         }
         return result;
     }

--
Gitblit v1.9.3