package com.iailab.module.data.channel.http.collector.ihdb; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.iailab.framework.common.constant.CommonConstant; import com.iailab.module.data.channel.http.entity.HttpApiEntity; import com.iailab.module.data.channel.http.service.HttpApiService; import com.iailab.module.data.common.enums.DataSourceType; import com.iailab.module.data.common.utils.DateUtils; import com.iailab.module.data.common.utils.HttpRequest; import com.iailab.module.data.common.utils.TagUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.*; /** * iHyperDB采集 * * @author lirm * @Description * @createTime 2024年10月16日 */ @Slf4j @Component public class HttpCollectorForIhd { private Map apiMap = new HashMap<>(); @Autowired private HttpApiService httpApiService; private static final String STA_TRUE = "true"; private static final int GROUP_MAX_COUNT = 50; private HttpApiEntity getHttpApi(String id) { if (apiMap.containsKey(id)) { return apiMap.get(id); } HttpApiEntity httpApi = httpApiService.info(id); apiMap.put(id, httpApi); return httpApi; } public BigDecimal getTagValue(String sourceId, String tagNo, Integer dimension, String valueType) { BigDecimal value = CommonConstant.BAD_VALUE; HttpApiEntity httpApi = this.getHttpApi(sourceId); StringBuilder tagSb = new StringBuilder(); tagSb.append("["); Map queryParams = new HashMap<>(); queryParams.put("datatype", valueType); queryParams.put("dimension", dimension); queryParams.put("tagname", tagNo); String jsonString = JSON.toJSONString(queryParams); tagSb.append(jsonString); tagSb.append("]"); log.info("body=====" + tagSb.toString()); String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00"); String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString()); JSONObject responseObj = JSON.parseObject(responseStr); if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) { JSONArray tagValueList = responseObj.getJSONArray("data"); if (!CollectionUtils.isEmpty(tagValueList)) { for (int i = 0; i < tagValueList.size(); i++) { JSONObject item = tagValueList.getJSONObject(i); value = new BigDecimal(item.get("value").toString()); } } } return value; } public Map getTagValues(List params) { if (CollectionUtils.isEmpty(params)) { return new HashMap<>(); } Map> measurePointsCountGroup = new HashMap<>(); int pointListSize = params.size(); int groupCount = pointListSize / GROUP_MAX_COUNT + ((pointListSize % GROUP_MAX_COUNT) > 0 ? 1 : 0); log.info("groupCount=" + groupCount); for (int i = 0; i < groupCount; i++) { int end = (i + 1) * GROUP_MAX_COUNT; if (end > pointListSize) { end = pointListSize; } measurePointsCountGroup.put(i, params.subList(i * GROUP_MAX_COUNT, end)); } Map result = new HashMap<>(params.size()); for (Map.Entry> measurePointsItem : measurePointsCountGroup.entrySet()) { try { getByHtp(result, measurePointsItem.getValue()); } catch (Exception ex) { ex.printStackTrace(); } } return result; } private void getByHtp(Map result, List params) { HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString()); StringBuilder tagSb = new StringBuilder(); tagSb.append("["); for (int i = 0; i < params.size(); i++) { Map queryParams = new HashMap<>(); queryParams.put("tagname", params.get(i)[1]); queryParams.put("dimension", params.get(i)[2]); queryParams.put("datatype", params.get(i)[3]); String jsonString = JSON.toJSONString(queryParams); tagSb.append(jsonString); if (i < params.size() - 1) { tagSb.append(","); } } tagSb.append("]"); log.info("body=====" + tagSb.toString()); String currentDate = DateUtils.format(new Date(), "yyyyMMddHHmm00"); String responseStr = HttpRequest.sendPost(httpApi.getUrl() + "/" + currentDate, tagSb.toString()); JSONObject responseObj = JSON.parseObject(responseStr); log.info("responseObj=====" + responseObj.toJSONString()); if (STA_TRUE.equals(responseObj.get("isSuccess").toString())) { JSONArray tagValueList = responseObj.getJSONArray("data"); if (!CollectionUtils.isEmpty(tagValueList)) { for (int i = 0; i < tagValueList.size(); i++) { JSONObject item = tagValueList.getJSONObject(i); result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), httpApi.getCode(), item.get("tagname").toString()), item.get("value")); } } } } }