Jay
2024-11-08 02722a3f9eca857ce7fffea352e9f7ee692a1b71
提交 | 用户 | 时间
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("]");
d41f14 78         String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
79         String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString());
52487d 80         JSONObject responseObj = JSON.parseObject(responseStr);
L 81         if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
82             JSONArray tagValueList = responseObj.getJSONArray("data");
83             if (!CollectionUtils.isEmpty(tagValueList)) {
84                 for (int i = 0; i < tagValueList.size(); i++) {
85                     JSONObject item = tagValueList.getJSONObject(i);
86                     value = new BigDecimal(item.get("value").toString());
87                 }
88             }
89         }
90         return value;
91     }
92
e8ad66 93     public Map<String, Object> getLastValues(List<String> tagNames) {
D 94         Map<String, Object> result = new HashMap<>();
2e7f34 95         try {
D 96             if (CollectionUtils.isEmpty(tagNames)) {
97                 return result;
98             }
99             List<String> noCacheTagNames = new ArrayList<>();//未缓存的tag
100             for (int i = 0; i < tagNames.size(); i++) {
101                 //先查缓存
102                 BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps(tagNames.get(i));
103                 if (ops.get("value") != null) {
104                     BigDecimal value = new BigDecimal(ops.get("value").toString());
105                     result.put(tagNames.get(i), value.setScale(3, RoundingMode.HALF_UP));
106                 } else {
107                     noCacheTagNames.add(tagNames.get(i));
108                 }
109             }
110             if (CollectionUtils.isEmpty(noCacheTagNames)) {
111                 log.info("全部读取缓存");
112                 return result;
113             }
114             log.info("查询未缓存的数据");
115             Gson gson = new Gson();
116             String tagSb = gson.toJson(noCacheTagNames);
117             log.info("body=====" + tagSb);
118             String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
119             String responseStr = "";
120             responseStr = HttpRequest.sendPost("http://172.16.59.105:9082/api/IHD/getPointslast" + "/" + currentDate, tagSb);
121             JSONObject responseObj = JSON.parseObject(responseStr);
122             if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
123                 JSONArray tagValueList = responseObj.getJSONArray("data");
124                 if (!CollectionUtils.isEmpty(tagValueList)) {
125                     for (int i = 0; i < tagValueList.size(); i++) {
126                         JSONObject item = tagValueList.getJSONObject(i);
127                         if (item.get("value") != null) {
128                             //存缓存
129                             BoundHashOperations<String, String, Object> ops = redisTemplate.boundHashOps(item.get("tagname").toString());
130                             ops.put("value", item.get("value").toString());
131                             //设置过期时间
132                             redisTemplate.expire(item.get("tagname").toString(), 10, TimeUnit.SECONDS);
133                             //把查询到的数据插入结果集
134                             BigDecimal value = new BigDecimal(item.get("value").toString());
135                             result.put(item.get("tagname").toString(), value.setScale(3, RoundingMode.HALF_UP));
136                         } else {
137                             result.put(item.get("tagname").toString(), CommonConstant.BAD_VALUE);
138                         }
e8ad66 139                     }
D 140                 }
141             }
2e7f34 142
D 143         } catch (Exception ex) {
144             log.info("getCurrentValue异常");
145             ex.printStackTrace();
146             throw ex;
e8ad66 147         }
D 148         return result;
149     }
150
52487d 151     public Map<String, Object> getTagValues(List<Object[]> params) {
L 152         if (CollectionUtils.isEmpty(params)) {
153             return new HashMap<>();
154         }
155
156         Map<Integer, List<Object[]>> measurePointsCountGroup = new HashMap<>();
157         int pointListSize = params.size();
d41f14 158         int groupCount = pointListSize / GROUP_MAX_COUNT + ((pointListSize % GROUP_MAX_COUNT) > 0 ? 1 : 0);
52487d 159         log.info("groupCount=" + groupCount);
L 160         for (int i = 0; i < groupCount; i++) {
161             int end = (i + 1) * GROUP_MAX_COUNT;
162             if (end > pointListSize) {
163                 end = pointListSize;
164             }
165             measurePointsCountGroup.put(i, params.subList(i * GROUP_MAX_COUNT, end));
166         }
167         Map<String, Object> result = new HashMap<>(params.size());
d41f14 168         for (Map.Entry<Integer, List<Object[]>> measurePointsItem : measurePointsCountGroup.entrySet()) {
52487d 169             try {
L 170                 getByHtp(result, measurePointsItem.getValue());
171             } catch (Exception ex) {
172                 ex.printStackTrace();
173             }
174         }
175         return result;
176     }
177
178     private void getByHtp(Map<String, Object> result, List<Object[]> params) {
179         HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString());
180         StringBuilder tagSb = new StringBuilder();
181         tagSb.append("[");
d41f14 182         for (int i = 0; i < params.size(); i++) {
52487d 183             Map<String, Object> queryParams = new HashMap<>();
L 184             queryParams.put("tagname", params.get(i)[1]);
185             queryParams.put("dimension", params.get(i)[2]);
186             queryParams.put("datatype", params.get(i)[3]);
d41f14 187             String jsonString = JSON.toJSONString(queryParams);
52487d 188             tagSb.append(jsonString);
L 189             if (i < params.size() - 1) {
190                 tagSb.append(",");
191             }
192         }
193         tagSb.append("]");
194         log.info("body=====" + tagSb.toString());
d41f14 195         String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00");
196         String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString());
52487d 197         JSONObject responseObj = JSON.parseObject(responseStr);
L 198         log.info("responseObj=====" + responseObj.toJSONString());
199         if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) {
200             JSONArray tagValueList = responseObj.getJSONArray("data");
201             if (!CollectionUtils.isEmpty(tagValueList)) {
202                 for (int i = 0; i < tagValueList.size(); i++) {
203                     JSONObject item = tagValueList.getJSONObject(i);
204                     result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), httpApi.getCode(), item.get("tagname").toString()), item.get("value"));
205                 }
206             }
207         }
208     }
209 }