dongyukun
2024-11-05 2e7f34ad38423471c27b3ccdc46a3c7c7fdfb9f6
缓存apitag当前值
已修改1个文件
75 ■■■■ 文件已修改
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java 75 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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;
    }