Jay
2025-02-27 bc8fecdda4b446d71c9bd141e7184de6ee5b5677
提交 | 用户 | 时间
e30eca 1 package com.iailab.module.data.channel.http.collector.asdb;
J 2
3 import com.alibaba.fastjson.JSON;
4 import com.iailab.framework.common.constant.CommonConstant;
5 import com.iailab.framework.common.util.http.HttpUtils;
6 import com.iailab.module.data.channel.http.collector.asdb.vo.HttpAsdbRespDataVO;
7 import com.iailab.module.data.channel.http.entity.HttpApiEntity;
8 import com.iailab.module.data.channel.http.service.HttpApiService;
9 import com.iailab.module.data.common.enums.DataSourceType;
10 import com.iailab.module.data.common.utils.TagUtils;
11 import lombok.extern.slf4j.Slf4j;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.data.redis.core.RedisTemplate;
14 import org.springframework.stereotype.Component;
15 import org.springframework.util.CollectionUtils;
16
17 import java.math.BigDecimal;
18 import java.util.*;
19 import java.util.concurrent.*;
20
21 /**
22  * AnSteelDB采集
23  *
24  * @author Jay
25  */
26 @Slf4j
27 @Component
28 public class HttpCollectorForAsdb {
29     private static Map<String, HttpApiEntity> apiMap = new HashMap<>();
30
31     @Autowired
32     private HttpApiService httpApiService;
33
34     @Autowired
35     private RedisTemplate redisTemplate;
36
460f91 37     public static final long offset = 10;
e30eca 38
J 39     private HttpApiEntity getHttpApi(String id) {
40         if (apiMap.containsKey(id)) {
41             return apiMap.get(id);
42         }
43         HttpApiEntity httpApi = httpApiService.info(id);
44         apiMap.put(id, httpApi);
45         return httpApi;
46     }
47
460f91 48     public BigDecimal getTagValue(String sourceId, String tagNo) {
e30eca 49         BigDecimal value = CommonConstant.BAD_VALUE;
J 50         HttpApiEntity httpApi = this.getHttpApi(sourceId);
460f91 51         //先查缓存
52         String catchKey = "IailabData:" + httpApi.getCode() + ":" + tagNo;
53         if (redisTemplate.hasKey(catchKey)) {
54             log.info("查找IailabData缓存: " + catchKey);
55             return new BigDecimal(redisTemplate.opsForValue().get(catchKey).toString());
e30eca 56         }
460f91 57         String responseStr = HttpUtils.sendGet(httpApi.getUrl(), null, "");
e30eca 58         List<HttpAsdbRespDataVO> dataList = JSON.parseArray(responseStr, HttpAsdbRespDataVO.class);
460f91 59         log.info("存入IailabData缓存: " + catchKey);
60         dataList.forEach(item -> {
61             redisTemplate.opsForValue().set(catchKey, item.getValue().toString(), offset, TimeUnit.SECONDS);
62         });
bc8fec 63         for (HttpAsdbRespDataVO data : dataList){
J 64             if (tagNo.equals(data.getPoint())){
65                 value = data.getValue();
66                 break;
67             }
68         }
e30eca 69         return value;
J 70     }
71
460f91 72     public Map<String, Object> getTagValues(String sourceId, List<String> tagNames) {
e30eca 73         Map<String, Object> result = new HashMap<>();
J 74         try {
75             if (CollectionUtils.isEmpty(tagNames)) {
76                 return result;
77             }
460f91 78             for (String tagName : tagNames) {
79                 result.put(tagName, getTagValue(sourceId, tagName));
e30eca 80             }
J 81         } catch (Exception ex) {
82             log.info("getCurrentValue异常");
83             ex.printStackTrace();
84             throw ex;
85         }
86         return result;
87     }
88
89     public Map<String, Object> getTagValues(List<Object[]> params, Date collectTime) {
90         Map<String, Object> result = new HashMap<>();
91         if (CollectionUtils.isEmpty(params)) {
92             return new HashMap<>();
93         }
94         try {
460f91 95             HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString());
96             this.getByHtp(httpApi.getUrl(), httpApi.getCode(), result, params);
e30eca 97         } catch (Exception ex) {
J 98             ex.printStackTrace();
99         }
100         return result;
101     }
102
460f91 103     private void getByHtp(String url, String sourceName, Map<String, Object> result, List<Object[]> params) {
104         String responseStr = HttpUtils.sendGet(url, null, "");
105         List<HttpAsdbRespDataVO> dataList = JSON.parseArray(responseStr, HttpAsdbRespDataVO.class);
106         Map<String, HttpAsdbRespDataVO> valueGroup = new HashMap<>();
107         for (HttpAsdbRespDataVO data : dataList) {
108             valueGroup.put(data.getPoint(), data);
e30eca 109         }
460f91 110         for (Object[] item : params) {
111             if (valueGroup.containsKey(item[1].toString())) {
112                 HttpAsdbRespDataVO data = valueGroup.get(item[1].toString());
e30eca 113                 result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, data.getPoint()), data.getValue());
J 114             }
115         }
116     }
117 }