From 52487d8938a59aba99e345b70c7a086e26eabb74 Mon Sep 17 00:00:00 2001
From: liriming <1343021927@qq.com>
Date: 星期三, 16 十月 2024 16:41:44 +0800
Subject: [PATCH] 沙钢数据采集

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/CommonConstant.java                    |    2 
 /dev/null                                                                                                                          |  119 -------------------
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/utils/HttpsRequest.java                      |   44 +++++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/dto/DaPointDTO.java                           |    2 
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java        |    3 
 iailab-module-data/iailab-module-data-biz/src/main/resources/mapper/point/DaPointDao.xml                                           |    4 
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForSS.java |  151 +++++++++++++++++++++++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java         |   15 ++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java      |    2 
 9 files changed, 217 insertions(+), 125 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForSS.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForSS.java
new file mode 100644
index 0000000..51c6bae
--- /dev/null
+++ b/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 + "]");
+    }
+}
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorIhDB.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorIhDB.java
deleted file mode 100644
index 434637e..0000000
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorIhDB.java
+++ /dev/null
@@ -1,119 +0,0 @@
-package com.iailab.module.data.channel.http.collector.ihdb;
-
-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 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.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- *
- * iHyperDB采集
- *
- * {
- *     "data": [
- *         {
- *             "tagname": "S10248500000002PS011",
- *             "value": 2701557.4736842105263157894737,
- *             "timeStamp": "2024-10-11T18:24:00"
- *         }
- *     ],
- *     "resultMessage": "请求成功...",
- *     "resultCode": 0,
- *     "isSuccess": true
- * }
- *
- * @author PanZhibao
- * @Description
- * @createTime 2024年10月14日
- */
-@Slf4j
-@Component
-public class HttpCollectorIhDB {
-    private Map<String, HttpApiEntity> apiMap = new HashMap<>();
-
-    @Autowired
-    private HttpApiService httpApiService;
-
-    private final boolean STA_TRUE = true;
-
-    private final String IS_SUCCESS = "isSuccess";
-
-    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);
-
-        //HttpRequest.sendPost()
-
-        return value;
-    }
-
-
-    public Map<String, Object> getTagValues(List<String[]> params) {
-        if (CollectionUtils.isEmpty(params)) {
-            return new HashMap<>();
-        }
-
-        /*Map<Integer, List<String[]>> 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<String[]>> measurePointsItem : measurePointsCountGroup.entrySet()) {
-            try {
-                getByHtp(result, measurePointsItem.getValue());
-            } catch (Exception ex) {
-                ex.printStackTrace();
-            }
-        }*/
-        return result;
-    }
-
-    private void getByHtp(Map<String, Object> result, List<String[]> params) {
-        HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0]);
-        Map<String, String> 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 = HttpRequest.sendGet(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"));
-                }
-            }
-        }*/
-
-    }
-}
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/CommonConstant.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/CommonConstant.java
index 2700d98..55b95f5 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/CommonConstant.java
+++ b/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";
 }
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/utils/HttpsRequest.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/utils/HttpsRequest.java
index 70901d9..3a48ae1 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/utils/HttpsRequest.java
+++ b/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;
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java
index a46e8af..b427907 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java
+++ b/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;
 
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java
index 5470286..17514d0 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java
+++ b/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();
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
index 455b836..cd4f668 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
+++ b/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());
                         }
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/dto/DaPointDTO.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/dto/DaPointDTO.java
index 840fc0f..685be47 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/dto/DaPointDTO.java
+++ b/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;
 
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/resources/mapper/point/DaPointDao.xml b/iailab-module-data/iailab-module-data-biz/src/main/resources/mapper/point/DaPointDao.xml
index de6b5a0..d53ecb3 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/resources/mapper/point/DaPointDao.xml
+++ b/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 (

--
Gitblit v1.9.3