提交 | 用户 | 时间
|
c7f709
|
1 |
package com.iailab.module.data.channel.http.collector; |
a6de49
|
2 |
|
H |
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.alibaba.fastjson.JSONObject; |
|
6 |
import com.iailab.module.data.common.enums.CommonConstant; |
|
7 |
import com.iailab.module.data.common.enums.DataSourceType; |
|
8 |
import com.iailab.module.data.common.utils.HttpsRequest; |
|
9 |
import com.iailab.module.data.common.utils.TagUtils; |
c7f709
|
10 |
import com.iailab.module.data.channel.http.entity.HttpApiEntity; |
L |
11 |
import com.iailab.module.data.channel.http.service.HttpApiService; |
a6de49
|
12 |
import lombok.extern.slf4j.Slf4j; |
H |
13 |
import javax.annotation.Resource; |
|
14 |
import org.springframework.stereotype.Component; |
|
15 |
import org.springframework.util.CollectionUtils; |
|
16 |
|
|
17 |
import java.math.BigDecimal; |
|
18 |
import java.util.HashMap; |
|
19 |
import java.util.List; |
|
20 |
import java.util.Map; |
|
21 |
|
|
22 |
/** |
|
23 |
* 中选时序数据采集 |
|
24 |
* http://192.168.55.122/api/Data/CurrentData?domain=NttRslinxLogic&tagstr=[ZX1]D_113.State.KM,[ZX1]D_114.State.KM |
|
25 |
* domain:限定数据域,可空,为空时不限定数据域。 |
|
26 |
* tagstr:限定标签集合,可空,为空时不限定标签。格式为英文逗号分隔的一个或多个标签名,如tag1,tag2,由于标签中可能包含特殊字符,get参数需要对此参数进行encode处理。 |
|
27 |
* { |
|
28 |
* "sta": true, |
|
29 |
* "msg": null, |
|
30 |
* "res": [ |
|
31 |
* { |
|
32 |
* "Tag": "[ZX1]D_110.State.KM",//标签 |
|
33 |
* "Value": 0//当前值 |
|
34 |
* }, |
|
35 |
* |
|
36 |
* @author PanZhibao |
|
37 |
* @Description |
|
38 |
* @createTime 2024年05月16日 |
|
39 |
*/ |
|
40 |
@Slf4j |
|
41 |
@Component |
|
42 |
public class HttpCollectorForZxzk { |
|
43 |
|
|
44 |
private Map<String, HttpApiEntity> apiMap = new HashMap<>(); |
|
45 |
|
|
46 |
@Resource |
|
47 |
private HttpApiService httpApiService; |
|
48 |
|
|
49 |
@Resource |
|
50 |
HttpsRequest httpsRequest; |
|
51 |
|
|
52 |
private final String STA_TRUE = "true"; |
|
53 |
|
|
54 |
private final int GROUP_MAX_COUNT = 50; |
|
55 |
|
|
56 |
private HttpApiEntity getHttpApi(String id) { |
|
57 |
if (apiMap.containsKey(id)) { |
|
58 |
return apiMap.get(id); |
|
59 |
} |
c7f709
|
60 |
HttpApiEntity httpApi = httpApiService.info(id); |
a6de49
|
61 |
apiMap.put(id, httpApi); |
H |
62 |
return httpApi; |
|
63 |
} |
|
64 |
|
|
65 |
public BigDecimal getTagValue(String sourceId, String tagNo) { |
|
66 |
BigDecimal value = CommonConstant.BAD_VALUE; |
|
67 |
HttpApiEntity httpApi = this.getHttpApi(sourceId); |
|
68 |
Map<String, String> queryParams = new HashMap<>(); |
|
69 |
queryParams.put("tagstr", tagNo); |
|
70 |
String responseStr = httpsRequest.doGet(httpApi.getUrl(), queryParams, "utf-8", ""); |
|
71 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
72 |
if (STA_TRUE.equals(responseObj.get("sta").toString())) { |
|
73 |
JSONArray tagValueList = responseObj.getJSONArray("res"); |
|
74 |
if (!CollectionUtils.isEmpty(tagValueList)) { |
|
75 |
for (int i = 0; i < tagValueList.size(); i++) { |
|
76 |
JSONObject item = tagValueList.getJSONObject(i); |
|
77 |
value = new BigDecimal(item.get("Value").toString()); |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
return value; |
|
82 |
} |
|
83 |
|
|
84 |
|
|
85 |
public Map<String, Object> getTagValues(List<String[]> params) { |
|
86 |
if (CollectionUtils.isEmpty(params)) { |
|
87 |
return new HashMap<>(); |
|
88 |
} |
|
89 |
|
|
90 |
Map<Integer, List<String[]>> measurePointsCountGroup = new HashMap<>(); |
|
91 |
int pointListSize = params.size(); |
|
92 |
int groupCount = pointListSize / GROUP_MAX_COUNT + ((pointListSize % GROUP_MAX_COUNT) > 0 ? 1 : 0); |
|
93 |
log.info("groupCount=" + groupCount); |
|
94 |
for (int i = 0; i < groupCount; i++) { |
|
95 |
int end = (i + 1) * GROUP_MAX_COUNT; |
|
96 |
if (end > pointListSize) { |
|
97 |
end = pointListSize; |
|
98 |
} |
|
99 |
measurePointsCountGroup.put(i, params.subList(i * GROUP_MAX_COUNT, end)); |
|
100 |
} |
|
101 |
Map<String, Object> result = new HashMap<>(params.size()); |
|
102 |
for(Map.Entry<Integer, List<String[]>> measurePointsItem : measurePointsCountGroup.entrySet()) { |
|
103 |
try { |
|
104 |
getByHtp(result, measurePointsItem.getValue()); |
|
105 |
} catch (Exception ex) { |
|
106 |
ex.printStackTrace(); |
|
107 |
} |
|
108 |
} |
|
109 |
return result; |
|
110 |
} |
|
111 |
|
|
112 |
private void getByHtp(Map<String, Object> result, List<String[]> params) { |
|
113 |
HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0]); |
|
114 |
Map<String, String> queryParams = new HashMap<>(); |
|
115 |
StringBuilder tagSb = new StringBuilder(); |
|
116 |
for (int i = 0; i < params.size(); i ++) { |
|
117 |
tagSb.append(params.get(i)[1]); |
|
118 |
if (i < params.size() - 1) { |
|
119 |
tagSb.append(","); |
|
120 |
} |
|
121 |
} |
|
122 |
queryParams.put("tagstr", tagSb.toString()); |
|
123 |
String responseStr = httpsRequest.doGet(httpApi.getUrl(), queryParams, "utf-8", ""); |
|
124 |
JSONObject responseObj = JSON.parseObject(responseStr); |
|
125 |
if (STA_TRUE.equals(responseObj.get("sta").toString())) { |
|
126 |
JSONArray tagValueList = responseObj.getJSONArray("res"); |
|
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("Tag").toString()), item.get("Value")); |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
} |
|
136 |
} |