iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForSS.java
对比新文件 @@ -0,0 +1,151 @@ package com.iailab.module.data.channel.http.collector.ihdb; import cn.hutool.core.util.ArrayUtil; 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.HttpsRequest; 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.sql.Array; import java.util.*; /** * iHyperDB采集 * @author lirm * @Description * @createTime 2024年10月16日 */ @Slf4j @Component public class HttpCollectorForSS { private Map<String, HttpApiEntity> apiMap = new HashMap<>(); @Autowired private HttpApiService httpApiService; @Autowired 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, Integer dimension, String valueType) { BigDecimal value = CommonConstant.BAD_VALUE; HttpApiEntity httpApi = this.getHttpApi(sourceId); StringBuilder tagSb = new StringBuilder(); tagSb.append("["); Map<String, Object> 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(),"yyyyMMddHHmmss"); String responseStr = httpsRequest.doPost(httpApi.getUrl(), tagSb.toString(), currentDate, "utf-8"); 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<String, Object> getTagValues(List<Object[]> params) { if (CollectionUtils.isEmpty(params)) { return new HashMap<>(); } Map<Integer, List<Object[]>> 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<String, Object> result = new HashMap<>(params.size()); for(Map.Entry<Integer, List<Object[]>> measurePointsItem : measurePointsCountGroup.entrySet()) { try { getByHtp(result, measurePointsItem.getValue()); } catch (Exception ex) { ex.printStackTrace(); } } return result; } private void getByHtp(Map<String, Object> result, List<Object[]> 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<String, Object> 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(),"yyyyMMddHHmmss"); String responseStr = httpsRequest.doPost(httpApi.getUrl(), tagSb.toString(),currentDate, "utf-8"); 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")); } } } } public static void main(String[] args) { Map<String, Object> queryParams = new HashMap<>(); queryParams.put("tagname", "11"); queryParams.put("dimension", 1); queryParams.put("datatype", "22"); String jsonString = JSONObject.toJSONString(queryParams); System.out.println(jsonString); System.out.println("[" + jsonString + "]"); } } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorIhDB.java
文件已删除 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/CommonConstant.java
@@ -14,5 +14,5 @@ BigDecimal ZERO_VALUE = new BigDecimal("0"); String HTTP_API_ZXZK_IH = "ZXZK_IH"; String HTTP_API_SHASTEEL_IH = "SHASTEEL_IH"; } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/utils/HttpsRequest.java
@@ -1,5 +1,6 @@ package com.iailab.module.data.common.utils; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.apache.http.Header; import org.apache.http.HttpEntity; @@ -122,13 +123,54 @@ * doPost * * @param url * @param jsonStr * @param date * @param charset * @return */ public String doPost(String url, String jsonStr, String date, String charset) { org.apache.http.client.HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try { httpClient = new SSLClient(); StringBuilder sb = new StringBuilder(); sb.append(url); sb.append("/"); sb.append(date); log.info("url=====" + sb.toString()); httpPost = new HttpPost(sb.toString()); //设置参数 httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); StringEntity stringEntity = new StringEntity(jsonStr); stringEntity.setContentEncoding("UTF-8"); stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity); HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception ex) { // ex.printStackTrace(); log.info("doPost失败,url=" + url); } return result; } /** * doPost * * @param url * @param map * @param json * @param charset * @param token * @return */ public String doPost(String url, Map<String, String> map, String json, String charset, String token) { public String doPostForToken(String url, Map<String, String> map, String json, String charset, String token) { org.apache.http.client.HttpClient httpClient = null; HttpPost httpPost = null; String result = null; iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java
@@ -19,6 +19,9 @@ @Value("${influx-db.org}") public String org; @Value("${influx-db.bucket}") public String bucket; @Value("${influx-db.token}") public String token; iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java
@@ -100,7 +100,7 @@ } if (!CollectionUtils.isEmpty(pointValues)) { pointValues.forEach(item -> { writeApi.writeMeasurement(influxDBInstance.getBucket(), influxDBInstance.org, WritePrecision.MS, item); writeApi.writeMeasurement(influxDBInstance.bucket, influxDBInstance.org, WritePrecision.MS, item); }); } writeApi.flush(); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
@@ -1,5 +1,6 @@ package com.iailab.module.data.point.collection.handler; import com.iailab.module.data.channel.http.collector.ihdb.HttpCollectorForSS; import com.iailab.module.data.channel.opcda.collector.OpcDACollector; import com.iailab.module.data.common.enums.CommonConstant; import com.iailab.module.data.common.enums.DataSourceType; @@ -50,6 +51,9 @@ @Autowired private OpcDACollector opcDACollector; @Autowired private HttpCollectorForSS httpCollectorForSS; @Resource private DaPointService daPointService; @@ -64,7 +68,7 @@ List<String[]> opcDaTagIds = new ArrayList<>(); List<String[]> modbusTagIds = new ArrayList<>(); List<String[]> kioTagIds = new ArrayList<>(); List<String[]> httpTagZxzk = new ArrayList<>(); List<Object[]> httpTagSS = new ArrayList<>(); dtos.stream().forEach(item -> { @@ -76,6 +80,10 @@ modbusTagIds.add(new String[]{item.getSourceId(), item.getTagNo()}); } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) { kioTagIds.add(new String[]{item.getSourceId(), item.getTagNo()}); } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) { if (CommonConstant.HTTP_API_SHASTEEL_IH.equals(item.getSourceName())) { httpTagSS.add(new Object[]{item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()}); } } }); @@ -91,6 +99,9 @@ } if (!CollectionUtils.isEmpty(kioTagIds)) { tagValues.putAll(kingIOCollector.getTagValues(kioTagIds)); } if (!CollectionUtils.isEmpty(httpTagSS)) { tagValues.putAll(httpCollectorForSS.getTagValues(httpTagSS)); } this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); log.info("测量点处理结束"); @@ -167,7 +178,7 @@ } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) { value = kingIOCollector.getTagValue(item.getSourceId(), item.getTagNo()); } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) { value = httpCollectorForSS.getTagValue(item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()); } else { log.info("没有匹配的TagNo=" + item.getTagNo()); } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/dto/DaPointDTO.java
@@ -101,6 +101,8 @@ @Schema(description = "测点Tag", required = true) private String tagNo; private Integer dimension; @Schema(description = "计算公式", required = true) private String expression; iailab-module-data/iailab-module-data-biz/src/main/resources/mapper/point/DaPointDao.xml
@@ -109,7 +109,9 @@ t2.source_type, t2.source_id, t3.source_name, t2.tag_no t2.tag_no, t2.dimension, t2.value_type FROM t_da_point t1 LEFT JOIN t_da_measure_point t2 ON t2.point_id = t1.id left join (