提交 | 用户 | 时间
|
52487d
|
1 |
package com.iailab.module.data.channel.http.collector.ihdb; |
L |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.alibaba.fastjson.JSONObject; |
|
6 |
import com.iailab.framework.common.constant.CommonConstant; |
|
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.DateUtils; |
d41f14
|
11 |
import com.iailab.module.data.common.utils.HttpRequest; |
52487d
|
12 |
import com.iailab.module.data.common.utils.TagUtils; |
L |
13 |
import lombok.extern.slf4j.Slf4j; |
|
14 |
import org.springframework.beans.factory.annotation.Autowired; |
|
15 |
import org.springframework.stereotype.Component; |
|
16 |
import org.springframework.util.CollectionUtils; |
|
17 |
|
|
18 |
import java.math.BigDecimal; |
|
19 |
import java.util.*; |
|
20 |
|
|
21 |
/** |
|
22 |
* iHyperDB采集 |
d41f14
|
23 |
* |
52487d
|
24 |
* @author lirm |
L |
25 |
* @Description |
|
26 |
* @createTime 2024年10月16日 |
|
27 |
*/ |
|
28 |
@Slf4j |
|
29 |
@Component |
d41f14
|
30 |
public class HttpCollectorForIhd { |
52487d
|
31 |
private Map<String, HttpApiEntity> apiMap = new HashMap<>(); |
L |
32 |
|
|
33 |
@Autowired |
|
34 |
private HttpApiService httpApiService; |
|
35 |
|
d41f14
|
36 |
private static final String STA_TRUE = "true"; |
52487d
|
37 |
|
d41f14
|
38 |
private static final int GROUP_MAX_COUNT = 50; |
52487d
|
39 |
|
L |
40 |
private HttpApiEntity getHttpApi(String id) { |
|
41 |
if (apiMap.containsKey(id)) { |
|
42 |
return apiMap.get(id); |
|
43 |
} |
|
44 |
HttpApiEntity httpApi = httpApiService.info(id); |
|
45 |
apiMap.put(id, httpApi); |
|
46 |
return httpApi; |
|
47 |
} |
|
48 |
|
|
49 |
public BigDecimal getTagValue(String sourceId, String tagNo, Integer dimension, String valueType) { |
|
50 |
BigDecimal value = CommonConstant.BAD_VALUE; |
|
51 |
HttpApiEntity httpApi = this.getHttpApi(sourceId); |
|
52 |
StringBuilder tagSb = new StringBuilder(); |
|
53 |
tagSb.append("["); |
|
54 |
Map<String, Object> queryParams = new HashMap<>(); |
|
55 |
queryParams.put("datatype", valueType); |
|
56 |
queryParams.put("dimension", dimension); |
|
57 |
queryParams.put("tagname", tagNo); |
d41f14
|
58 |
String jsonString = JSON.toJSONString(queryParams); |
52487d
|
59 |
tagSb.append(jsonString); |
L |
60 |
tagSb.append("]"); |
|
61 |
log.info("body=====" + tagSb.toString()); |
d41f14
|
62 |
String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00"); |
潘 |
63 |
String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString()); |
52487d
|
64 |
JSONObject responseObj = JSON.parseObject(responseStr); |
L |
65 |
if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) { |
|
66 |
JSONArray tagValueList = responseObj.getJSONArray("data"); |
|
67 |
if (!CollectionUtils.isEmpty(tagValueList)) { |
|
68 |
for (int i = 0; i < tagValueList.size(); i++) { |
|
69 |
JSONObject item = tagValueList.getJSONObject(i); |
|
70 |
value = new BigDecimal(item.get("value").toString()); |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
return value; |
|
75 |
} |
|
76 |
|
|
77 |
public Map<String, Object> getTagValues(List<Object[]> params) { |
|
78 |
if (CollectionUtils.isEmpty(params)) { |
|
79 |
return new HashMap<>(); |
|
80 |
} |
|
81 |
|
|
82 |
Map<Integer, List<Object[]>> measurePointsCountGroup = new HashMap<>(); |
|
83 |
int pointListSize = params.size(); |
d41f14
|
84 |
int groupCount = pointListSize / GROUP_MAX_COUNT + ((pointListSize % GROUP_MAX_COUNT) > 0 ? 1 : 0); |
52487d
|
85 |
log.info("groupCount=" + groupCount); |
L |
86 |
for (int i = 0; i < groupCount; i++) { |
|
87 |
int end = (i + 1) * GROUP_MAX_COUNT; |
|
88 |
if (end > pointListSize) { |
|
89 |
end = pointListSize; |
|
90 |
} |
|
91 |
measurePointsCountGroup.put(i, params.subList(i * GROUP_MAX_COUNT, end)); |
|
92 |
} |
|
93 |
Map<String, Object> result = new HashMap<>(params.size()); |
d41f14
|
94 |
for (Map.Entry<Integer, List<Object[]>> measurePointsItem : measurePointsCountGroup.entrySet()) { |
52487d
|
95 |
try { |
L |
96 |
getByHtp(result, measurePointsItem.getValue()); |
|
97 |
} catch (Exception ex) { |
|
98 |
ex.printStackTrace(); |
|
99 |
} |
|
100 |
} |
|
101 |
return result; |
|
102 |
} |
|
103 |
|
|
104 |
private void getByHtp(Map<String, Object> result, List<Object[]> params) { |
|
105 |
HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString()); |
|
106 |
StringBuilder tagSb = new StringBuilder(); |
|
107 |
tagSb.append("["); |
d41f14
|
108 |
for (int i = 0; i < params.size(); i++) { |
52487d
|
109 |
Map<String, Object> queryParams = new HashMap<>(); |
L |
110 |
queryParams.put("tagname", params.get(i)[1]); |
|
111 |
queryParams.put("dimension", params.get(i)[2]); |
|
112 |
queryParams.put("datatype", params.get(i)[3]); |
d41f14
|
113 |
String jsonString = JSON.toJSONString(queryParams); |
52487d
|
114 |
tagSb.append(jsonString); |
L |
115 |
if (i < params.size() - 1) { |
|
116 |
tagSb.append(","); |
|
117 |
} |
|
118 |
} |
|
119 |
tagSb.append("]"); |
|
120 |
log.info("body=====" + tagSb.toString()); |
d41f14
|
121 |
String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00"); |
潘 |
122 |
String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString()); |
52487d
|
123 |
JSONObject responseObj = JSON.parseObject(responseStr); |
L |
124 |
log.info("responseObj=====" + responseObj.toJSONString()); |
|
125 |
if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) { |
|
126 |
JSONArray tagValueList = responseObj.getJSONArray("data"); |
|
127 |
if (!CollectionUtils.isEmpty(tagValueList)) { |
|
128 |
for (int i = 0; i < tagValueList.size(); i++) { |
|
129 |
JSONObject item = tagValueList.getJSONObject(i); |
|
130 |
result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), httpApi.getCode(), item.get("tagname").toString()), item.get("value")); |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
} |