Jay
2025-02-27 bc8fecdda4b446d71c9bd141e7184de6ee5b5677
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
package com.iailab.module.data.channel.http.collector.asdb;
 
import com.alibaba.fastjson.JSON;
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 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.*;
 
/**
 * AnSteelDB采集
 *
 * @author Jay
 */
@Slf4j
@Component
public class HttpCollectorForAsdb {
    private static Map<String, HttpApiEntity> apiMap = new HashMap<>();
 
    @Autowired
    private HttpApiService httpApiService;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    public static final long offset = 10;
 
    private HttpApiEntity getHttpApi(String id) {
        if (apiMap.containsKey(id)) {
            return apiMap.get(id);
        }
        HttpApiEntity httpApi = httpApiService.info(id);
        apiMap.put(id, httpApi);
        return httpApi;
    }
 
    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(catchKey, 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 {
            HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString());
            this.getByHtp(httpApi.getUrl(), httpApi.getCode(), result, params);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
 
    private void getByHtp(String url, String sourceName, Map<String, Object> result, List<Object[]> params) {
        String responseStr = HttpUtils.sendGet(url, null, "");
        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());
            }
        }
    }
}