package com.iailab.module.data.channel.http.collector; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.iailab.module.data.common.enums.CommonConstant; import com.iailab.module.data.common.enums.DataSourceType; import com.iailab.module.data.common.utils.HttpsRequest; import com.iailab.module.data.common.utils.TagUtils; import com.iailab.module.data.channel.http.entity.HttpApiEntity; import com.iailab.module.data.channel.http.service.HttpApiService; import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 中选时序数据采集 * http://192.168.55.122/api/Data/CurrentData?domain=NttRslinxLogic&tagstr=[ZX1]D_113.State.KM,[ZX1]D_114.State.KM * domain:限定数据域,可空,为空时不限定数据域。 * tagstr:限定标签集合,可空,为空时不限定标签。格式为英文逗号分隔的一个或多个标签名,如tag1,tag2,由于标签中可能包含特殊字符,get参数需要对此参数进行encode处理。 * { * "sta": true, * "msg": null, * "res": [ * { * "Tag": "[ZX1]D_110.State.KM",//标签 * "Value": 0//当前值 * }, * * @author PanZhibao * @Description * @createTime 2024年05月16日 */ @Slf4j @Component public class HttpCollectorForZxzk { private Map apiMap = new HashMap<>(); @Resource private HttpApiService httpApiService; @Resource HttpsRequest httpsRequest; private final String STA_TRUE = "true"; private 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) { BigDecimal value = CommonConstant.BAD_VALUE; HttpApiEntity httpApi = this.getHttpApi(sourceId); Map queryParams = new HashMap<>(); queryParams.put("tagstr", tagNo); String responseStr = httpsRequest.doGet(httpApi.getUrl(), queryParams, "utf-8", ""); JSONObject responseObj = JSON.parseObject(responseStr); if (STA_TRUE.equals(responseObj.get("sta").toString())) { JSONArray tagValueList = responseObj.getJSONArray("res"); 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]); Map queryParams = new HashMap<>(); StringBuilder tagSb = new StringBuilder(); for (int i = 0; i < params.size(); i ++) { tagSb.append(params.get(i)[1]); if (i < params.size() - 1) { tagSb.append(","); } } queryParams.put("tagstr", tagSb.toString()); String responseStr = httpsRequest.doGet(httpApi.getUrl(), queryParams, "utf-8", ""); JSONObject responseObj = JSON.parseObject(responseStr); if (STA_TRUE.equals(responseObj.get("sta").toString())) { JSONArray tagValueList = responseObj.getJSONArray("res"); 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("Tag").toString()), item.get("Value")); } } } } }