潘志宝
9 小时以前 c3e9ef1922656471b934aba4034a8f91a5ba9555
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.iailab.module.data.channel.http.collector.asdb;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iailab.framework.common.constant.CommonConstant;
import com.iailab.framework.common.util.http.HttpUtils;
import com.iailab.module.data.channel.http.collector.asdb.vo.HttpAsdbRespDataVO;
import com.iailab.module.data.channel.http.entity.HttpApiEntity;
import com.iailab.module.data.channel.http.service.HttpApiService;
import com.iailab.module.data.common.enums.DataSourceType;
import com.iailab.module.data.common.utils.TagUtils;
import com.iailab.module.data.point.common.MeasurePointValueType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2025年03月26日
 */
@Slf4j
@Component
public class HttpCollectorForAsMediaType {
 
    @Autowired
    private HttpApiService httpApiService;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Autowired
    private HttpCollectorForAsag httpCollectorForAsag;
 
    public static final long offset = 10;
 
    private HttpApiEntity getHttpApi(String id) {
        return httpApiService.getFromCatch(id);
    }
 
    public BigDecimal getTagValue(String sourceId, String tagNo) {
        BigDecimal value = CommonConstant.BAD_VALUE;
        HttpApiEntity httpApi = this.getHttpApi(sourceId);
        //先查缓存
        String catchKey = "IailabData:" + httpApi.getCode() + ":" + tagNo;
        if (redisTemplate.hasKey(catchKey)) {
            log.info("查找IailabData缓存: " + catchKey);
            return new BigDecimal(redisTemplate.opsForValue().get(catchKey).toString());
        }
        String responseStr = HttpUtils.sendGet(httpApi.getUrl(), null, "");
        List<HttpAsdbRespDataVO> dataList = JSON.parseArray(responseStr, HttpAsdbRespDataVO.class);
        log.info("存入IailabData缓存: " + catchKey);
        dataList.forEach(item -> {
            redisTemplate.opsForValue().set("IailabData:" + httpApi.getCode() + ":" + item.getPoint(), item.getValue().toString(), offset, TimeUnit.SECONDS);
        });
        for (HttpAsdbRespDataVO data : dataList){
            if (tagNo.equals(data.getPoint())){
                value = data.getValue();
                break;
            }
        }
        return value;
    }
 
    public Map<String, Object> getTagValues(String sourceId, List<String> tagNames) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (CollectionUtils.isEmpty(tagNames)) {
                return result;
            }
            for (String tagName : tagNames) {
                result.put(tagName, getTagValue(sourceId, tagName));
            }
        } catch (Exception ex) {
            log.info("getCurrentValue异常");
            ex.printStackTrace();
            throw ex;
        }
        return result;
    }
 
    public Map<String, Object> getTagValues(List<Object[]> params, Date collectTime) {
        Map<String, Object> result = new HashMap<>();
        if (CollectionUtils.isEmpty(params)) {
            return new HashMap<>();
        }
        try {
            List<Object[]> paramsDig = new ArrayList<>();
            List<Object[]> paramsSim = new ArrayList<>();
            for (Object[] param : params) {
                if (((String) param[3]).equals(MeasurePointValueType.DIGITAL.getCode()) || (Integer) param[2] == 0) {
                    paramsDig.add(param);
                } else {
                    paramsSim.add(param);
                }
            }
            if (!CollectionUtils.isEmpty(paramsDig)) {
                log.info("查询数字量,paramsDig.size(): " + paramsDig.size());
                HttpApiEntity httpApi = this.getHttpApi(paramsDig.get(0)[0].toString());
                this.getByHtp(httpApi.getUrl(), httpApi.getCode(), result, paramsDig);
            }
            if (!CollectionUtils.isEmpty(paramsSim)) {
                log.info("查询模拟量,paramsSim.size(): " + paramsSim.size());
                httpCollectorForAsag.getTagValues(paramsSim, collectTime, result);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
 
    private void getByHtp(String url, String sourceName, Map<String, Object> result, List<Object[]> params) {
        log.info("HttpCollectorForAsMediaType.getByHtp:url=" + url);
        log.info("HttpCollectorForAsMediaType.getByHtp:params=" + JSONArray.toJSONString(params));
        String responseStr = HttpUtils.sendGet(url, null, "");
        log.info("HttpCollectorForAsMediaType.getByHtp:responseStr=" + responseStr);
        List<HttpAsdbRespDataVO> dataList = JSON.parseArray(responseStr, HttpAsdbRespDataVO.class);
        Map<String, HttpAsdbRespDataVO> valueGroup = new HashMap<>();
        for (HttpAsdbRespDataVO data : dataList) {
            valueGroup.put(data.getPoint(), data);
        }
        for (Object[] item : params) {
            if (valueGroup.containsKey(item[1].toString())) {
                HttpAsdbRespDataVO data = valueGroup.get(item[1].toString());
                result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, data.getPoint()), data.getValue());
            }
        }
    }
}