dengzedong
2024-11-06 db184afd0c5bf3359b44eb0251fa5b07386eb3ff
提交 | 用户 | 时间
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;
e8ad66 6 import com.google.gson.Gson;
52487d 7 import com.iailab.framework.common.constant.CommonConstant;
L 8 import com.iailab.module.data.channel.http.entity.HttpApiEntity;
e8ad66 9 import com.iailab.module.data.channel.http.entity.HttpTagEntity;
52487d 10 import com.iailab.module.data.channel.http.service.HttpApiService;
e8ad66 11 import com.iailab.module.data.channel.http.service.HttpTagService;
52487d 12 import com.iailab.module.data.common.enums.DataSourceType;
L 13 import com.iailab.module.data.common.utils.DateUtils;
d41f14 14 import com.iailab.module.data.common.utils.HttpRequest;
52487d 15 import com.iailab.module.data.common.utils.TagUtils;
2e7f34 16 import com.iailab.module.data.influxdb.pojo.InfluxPointValueBoolPOJO;
D 17 import com.iailab.module.data.influxdb.pojo.InfluxPointValueDigPOJO;
18 import com.iailab.module.data.influxdb.pojo.InfluxPointValueSimPOJO;
19 import com.iailab.module.data.influxdb.pojo.InfluxPointValueStrPOJO;
52487d 20 import lombok.extern.slf4j.Slf4j;
L 21 import org.springframework.beans.factory.annotation.Autowired;
2e7f34 22 import org.springframework.data.redis.core.BoundHashOperations;
D 23 import org.springframework.data.redis.core.RedisTemplate;
52487d 24 import org.springframework.stereotype.Component;
L 25 import org.springframework.util.CollectionUtils;
26
27 import java.math.BigDecimal;
e8ad66 28 import java.math.RoundingMode;
52487d 29 import java.util.*;
2e7f34 30 import java.util.concurrent.TimeUnit;
52487d 31
L 32 /**
33  * iHyperDB采集
d41f14 34  *
52487d 35  * @author lirm
L 36  * @Description
37  * @createTime 2024年10月16日
38  */
39 @Slf4j
40 @Component
d41f14 41 public class HttpCollectorForIhd {
52487d 42     private Map<String, HttpApiEntity> apiMap = new HashMap<>();
L 43
44     @Autowired
45     private HttpApiService httpApiService;
2e7f34 46
D 47     @Autowired
48     private RedisTemplate redisTemplate;
e8ad66 49
D 50     @Autowired
51     private HttpTagService httpTagService;
52487d 52
d41f14 53     private static final String STA_TRUE = "true";
52487d 54
d41f14 55     private static final int GROUP_MAX_COUNT = 50;
52487d 56
L 57     private HttpApiEntity getHttpApi(String id) {
58         if (apiMap.containsKey(id)) {
59             return apiMap.get(id);
60         }
61         HttpApiEntity httpApi = httpApiService.info(id);
62         apiMap.put(id, httpApi);
63         return httpApi;
64     }
65
66     public BigDecimal getTagValue(String sourceId, String tagNo, Integer dimension, String valueType) {
67         BigDecimal value = CommonConstant.BAD_VALUE;
68         HttpApiEntity httpApi = this.getHttpApi(sourceId);
69         StringBuilder tagSb = new StringBuilder();
70         tagSb.append("[");
71         Map<String, Object> queryParams = new HashMap<>();
72         queryParams.put("datatype", valueType);
73         queryParams.put("dimension", dimension);
74         queryParams.put("tagname", tagNo);
d41f14 75         String jsonString = JSON.toJSONString(queryParams);
52487d 76         tagSb.append(jsonString);
L 77         tagSb.append("]");
78         log.info("body=====" + tagSb.toString());
d41f14 79         String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
80         String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString());
52487d 81         JSONObject responseObj = JSON.parseObject(responseStr);
L 82         if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
83             JSONArray tagValueList = responseObj.getJSONArray("data");
84             if (!CollectionUtils.isEmpty(tagValueList)) {
85                 for (int i = 0; i < tagValueList.size(); i++) {
86                     JSONObject item = tagValueList.getJSONObject(i);
87                     value = new BigDecimal(item.get("value").toString());
88                 }
89             }
90         }
91         return value;
92     }
93
e8ad66 94     public Map<String, Object> getLastValues(List<String> tagNames) {
D 95         Map<String, Object> result = new HashMap<>();
2e7f34 96         try {
D 97             if (CollectionUtils.isEmpty(tagNames)) {
98                 return result;
99             }
100             List<String> noCacheTagNames = new ArrayList<>();//未缓存的tag
101             for (int i = 0; i < tagNames.size(); i++) {
102                 //先查缓存
103                 BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps(tagNames.get(i));
104                 if (ops.get("value") != null) {
105                     BigDecimal value = new BigDecimal(ops.get("value").toString());
106                     result.put(tagNames.get(i), value.setScale(3, RoundingMode.HALF_UP));
107                 } else {
108                     noCacheTagNames.add(tagNames.get(i));
109                 }
110             }
111             if (CollectionUtils.isEmpty(noCacheTagNames)) {
112                 log.info("全部读取缓存");
113                 return result;
114             }
115             log.info("查询未缓存的数据");
116             Gson gson = new Gson();
117             String tagSb = gson.toJson(noCacheTagNames);
118             log.info("body=====" + tagSb);
119             String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
120             String responseStr = "";
121             responseStr = HttpRequest.sendPost("http://172.16.59.105:9082/api/IHD/getPointslast" + "/" + currentDate, tagSb);
122             JSONObject responseObj = JSON.parseObject(responseStr);
123             if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
124                 JSONArray tagValueList = responseObj.getJSONArray("data");
125                 if (!CollectionUtils.isEmpty(tagValueList)) {
126                     for (int i = 0; i < tagValueList.size(); i++) {
127                         JSONObject item = tagValueList.getJSONObject(i);
128                         if (item.get("value") != null) {
129                             //存缓存
130                             BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps(item.get("tagname").toString());
131                             ops.put("value", item.get("value").toString());
132                             //设置过期时间
133                             redisTemplate.expire(item.get("tagname").toString(), 10, TimeUnit.SECONDS);
134                             //把查询到的数据插入结果集
135                             BigDecimal value = new BigDecimal(item.get("value").toString());
136                             result.put(item.get("tagname").toString(), value.setScale(3, RoundingMode.HALF_UP));
137                         } else {
138                             result.put(item.get("tagname").toString(), CommonConstant.BAD_VALUE);
139                         }
e8ad66 140                     }
D 141                 }
142             }
2e7f34 143
D 144         } catch (Exception ex) {
145             log.info("getCurrentValue异常");
146             ex.printStackTrace();
147             throw ex;
e8ad66 148         }
D 149         return result;
150     }
151
52487d 152     public Map<String, Object> getTagValues(List<Object[]> params) {
L 153         if (CollectionUtils.isEmpty(params)) {
154             return new HashMap<>();
155         }
156
157         Map<Integer, List<Object[]>> measurePointsCountGroup = new HashMap<>();
158         int pointListSize = params.size();
d41f14 159         int groupCount = pointListSize / GROUP_MAX_COUNT + ((pointListSize % GROUP_MAX_COUNT) > 0 ? 1 : 0);
52487d 160         log.info("groupCount=" + groupCount);
L 161         for (int i = 0; i < groupCount; i++) {
162             int end = (i + 1) * GROUP_MAX_COUNT;
163             if (end > pointListSize) {
164                 end = pointListSize;
165             }
166             measurePointsCountGroup.put(i, params.subList(i * GROUP_MAX_COUNT, end));
167         }
168         Map<String, Object> result = new HashMap<>(params.size());
d41f14 169         for (Map.Entry<Integer, List<Object[]>> measurePointsItem : measurePointsCountGroup.entrySet()) {
52487d 170             try {
L 171                 getByHtp(result, measurePointsItem.getValue());
172             } catch (Exception ex) {
173                 ex.printStackTrace();
174             }
175         }
176         return result;
177     }
178
179     private void getByHtp(Map<String, Object> result, List<Object[]> params) {
180         HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString());
181         StringBuilder tagSb = new StringBuilder();
182         tagSb.append("[");
d41f14 183         for (int i = 0; i < params.size(); i++) {
52487d 184             Map<String, Object> queryParams = new HashMap<>();
L 185             queryParams.put("tagname", params.get(i)[1]);
186             queryParams.put("dimension", params.get(i)[2]);
187             queryParams.put("datatype", params.get(i)[3]);
d41f14 188             String jsonString = JSON.toJSONString(queryParams);
52487d 189             tagSb.append(jsonString);
L 190             if (i < params.size() - 1) {
191                 tagSb.append(",");
192             }
193         }
194         tagSb.append("]");
195         log.info("body=====" + tagSb.toString());
d41f14 196         String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
197         String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString());
52487d 198         JSONObject responseObj = JSON.parseObject(responseStr);
L 199         log.info("responseObj=====" + responseObj.toJSONString());
200         if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
201             JSONArray tagValueList = responseObj.getJSONArray("data");
202             if (!CollectionUtils.isEmpty(tagValueList)) {
203                 for (int i = 0; i < tagValueList.size(); i++) {
204                     JSONObject item = tagValueList.getJSONObject(i);
205                     result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), httpApi.getCode(), item.get("tagname").toString()), item.get("value"));
206                 }
207             }
208         }
209     }
210 }