提交 | 用户 | 时间
|
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 |
}); |
e30eca
|
63 |
value = Objects.requireNonNull(dataList.stream().filter(data -> tagNo.equals(data.getPoint())).findFirst().orElse(null)).getValue(); |
J |
64 |
return value; |
|
65 |
} |
|
66 |
|
460f91
|
67 |
public Map<String, Object> getTagValues(String sourceId, List<String> tagNames) { |
e30eca
|
68 |
Map<String, Object> result = new HashMap<>(); |
J |
69 |
try { |
|
70 |
if (CollectionUtils.isEmpty(tagNames)) { |
|
71 |
return result; |
|
72 |
} |
460f91
|
73 |
for (String tagName : tagNames) { |
潘 |
74 |
result.put(tagName, getTagValue(sourceId, tagName)); |
e30eca
|
75 |
} |
J |
76 |
} catch (Exception ex) { |
|
77 |
log.info("getCurrentValue异常"); |
|
78 |
ex.printStackTrace(); |
|
79 |
throw ex; |
|
80 |
} |
|
81 |
return result; |
|
82 |
} |
|
83 |
|
|
84 |
public Map<String, Object> getTagValues(List<Object[]> params, Date collectTime) { |
|
85 |
Map<String, Object> result = new HashMap<>(); |
|
86 |
if (CollectionUtils.isEmpty(params)) { |
|
87 |
return new HashMap<>(); |
|
88 |
} |
|
89 |
try { |
460f91
|
90 |
HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString()); |
潘 |
91 |
this.getByHtp(httpApi.getUrl(), httpApi.getCode(), result, params); |
e30eca
|
92 |
} catch (Exception ex) { |
J |
93 |
ex.printStackTrace(); |
|
94 |
} |
|
95 |
return result; |
|
96 |
} |
|
97 |
|
460f91
|
98 |
private void getByHtp(String url, String sourceName, Map<String, Object> result, List<Object[]> params) { |
潘 |
99 |
String responseStr = HttpUtils.sendGet(url, null, ""); |
|
100 |
List<HttpAsdbRespDataVO> dataList = JSON.parseArray(responseStr, HttpAsdbRespDataVO.class); |
|
101 |
Map<String, HttpAsdbRespDataVO> valueGroup = new HashMap<>(); |
|
102 |
for (HttpAsdbRespDataVO data : dataList) { |
|
103 |
valueGroup.put(data.getPoint(), data); |
e30eca
|
104 |
} |
460f91
|
105 |
for (Object[] item : params) { |
潘 |
106 |
if (valueGroup.containsKey(item[1].toString())) { |
|
107 |
HttpAsdbRespDataVO data = valueGroup.get(item[1].toString()); |
e30eca
|
108 |
result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, data.getPoint()), data.getValue()); |
J |
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
} |