工业互联网平台2.0版本后端代码
TAG
潘志宝
2024-11-20 2f03e2eccd313f06b172c3becf9ab73665b10d1b
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java
@@ -6,21 +6,22 @@
import com.google.gson.Gson;
import com.iailab.framework.common.constant.CommonConstant;
import com.iailab.module.data.channel.http.entity.HttpApiEntity;
import com.iailab.module.data.channel.http.entity.HttpTagEntity;
import com.iailab.module.data.channel.http.service.HttpApiService;
import com.iailab.module.data.channel.http.service.HttpTagService;
import com.iailab.module.data.common.enums.DataSourceType;
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 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采集
@@ -38,11 +39,40 @@
    private HttpApiService httpApiService;
    @Autowired
    private HttpTagService httpTagService;
    private RedisTemplate redisTemplate;
    private static final String STA_TRUE = "true";
    private static final int GROUP_MAX_COUNT = 50;
    private static final String pattern = "yyyyMMddHHmm00";
    private static final String IS_SUCCESS = "isSuccess";
    /**
     * tagName
     */
    private static final String N = "n";
    /**
     * dimension
     */
    private static final String D = "d";
    /**
     * 类型
     */
    private static final String P = "p";
    /**
     * dataValue
     */
    private static final String V = "v";
    /**
     * dataTime
     */
    private static final String T = "t";
    private HttpApiEntity getHttpApi(String id) {
        if (apiMap.containsKey(id)) {
@@ -59,49 +89,82 @@
        StringBuilder tagSb = new StringBuilder();
        tagSb.append("[");
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("datatype", valueType);
        queryParams.put("dimension", dimension);
        queryParams.put("tagname", tagNo);
        queryParams.put(P, valueType);
        queryParams.put(D, dimension);
        queryParams.put(N, tagNo);
        String jsonString = JSON.toJSONString(queryParams);
        tagSb.append(jsonString);
        tagSb.append("]");
        log.info("body=====" + tagSb.toString());
        String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
        String currentDate = DateUtils.format(new Date(), pattern);
        String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString());
        JSONObject responseObj = JSON.parseObject(responseStr);
        if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
        if (STA_TRUE.equals(responseObj.get(IS_SUCCESS).toString())) {
            JSONArray tagValueList = responseObj.getJSONArray("data");
            if (!CollectionUtils.isEmpty(tagValueList)) {
                for (int i = 0; i < tagValueList.size(); i++) {
                    JSONObject item = tagValueList.getJSONObject(i);
                    value = new BigDecimal(item.get("value").toString());
                    value = new BigDecimal(item.get(V).toString());
                }
            }
        }
        return value;
    }
    public Map<String, Object> getLastValues(List<String> tagNames) {
    public Map<String, Object> getLastValues(String sourceId, 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);
        HttpApiEntity httpApi = this.getHttpApi(sourceId);
        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(V) != null) {
                    BigDecimal value = new BigDecimal(ops.get(V).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(), pattern);
            String responseStr = "";
            responseStr = HttpRequest.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)) {
                    for (int i = 0; i < tagValueList.size(); i++) {
                        JSONObject item = tagValueList.getJSONObject(i);
                        if (item.get(V) != null) {
                            //存缓存
                            BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps(item.get(V).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));
                        } else {
                            result.put(item.get(N).toString(), CommonConstant.BAD_VALUE);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            log.info("getCurrentValue异常");
            ex.printStackTrace();
            throw ex;
        }
        return result;
    }
@@ -139,9 +202,9 @@
        tagSb.append("[");
        for (int i = 0; i < params.size(); i++) {
            Map<String, Object> queryParams = new HashMap<>();
            queryParams.put("tagname", params.get(i)[1]);
            queryParams.put("dimension", params.get(i)[2]);
            queryParams.put("datatype", params.get(i)[3]);
            queryParams.put(N, params.get(i)[1]);
            queryParams.put(D, params.get(i)[2]);
            queryParams.put(P, params.get(i)[3]);
            String jsonString = JSON.toJSONString(queryParams);
            tagSb.append(jsonString);
            if (i < params.size() - 1) {
@@ -150,16 +213,16 @@
        }
        tagSb.append("]");
        log.info("body=====" + tagSb.toString());
        String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
        String currentDate = DateUtils.format(new Date(), pattern);
        String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString());
        JSONObject responseObj = JSON.parseObject(responseStr);
        log.info("responseObj=====" + responseObj.toJSONString());
        if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
        if (STA_TRUE.equals(responseObj.get(IS_SUCCESS).toString())) {
            JSONArray tagValueList = responseObj.getJSONArray("data");
            if (!CollectionUtils.isEmpty(tagValueList)) {
                for (int i = 0; i < tagValueList.size(); i++) {
                    JSONObject item = tagValueList.getJSONObject(i);
                    result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), httpApi.getCode(), item.get("tagname").toString()), item.get("value"));
                    result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), httpApi.getCode(), item.get(N).toString()), item.get(V));
                }
            }
        }