|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.shkpr.service.customgateway.zhscada.components;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.global.base.log.LogLevelFlag;
|
|
|
+import com.global.base.log.LogPrintMgr;
|
|
|
+import com.shkpr.service.customgateway.core.components.DeviceIdGenerator;
|
|
|
+import com.shkpr.service.customgateway.core.components.DeviceRegistry;
|
|
|
+import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
|
|
|
+import com.shkpr.service.customgateway.core.domain.Device;
|
|
|
+import com.shkpr.service.customgateway.core.properties.CallingProperties;
|
|
|
+import com.shkpr.service.customgateway.core.utils.CallingUtil;
|
|
|
+import com.shkpr.service.customgateway.core.utils.InfluxDbUtil;
|
|
|
+import com.shkpr.service.customgateway.zhscada.constants.ScadaPlatformMetadata;
|
|
|
+import com.shkpr.service.customgateway.zhscada.domain.ScadaPlatformData;
|
|
|
+import com.shkpr.service.customgateway.zhscada.domain.ScadaPlatformResult;
|
|
|
+import org.influxdb.dto.Point;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据采集器
|
|
|
+ *
|
|
|
+ * @author 欧阳劲驰
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class DataCollector {
|
|
|
+ /**
|
|
|
+ * log
|
|
|
+ */
|
|
|
+ private static final String CLASS_NAME = "DataCollector";
|
|
|
+ private static final String BIZ_TYPE = LogFlagBusiType.BUSI_ALL.toStrValue();
|
|
|
+
|
|
|
+ final
|
|
|
+ CallingProperties callingProperties;
|
|
|
+ final
|
|
|
+ DeviceRegistry deviceRegistry;
|
|
|
+ final
|
|
|
+ DeviceIdGenerator deviceIdGenerator;
|
|
|
+ final
|
|
|
+ InfluxDbUtil influxDbUtil;
|
|
|
+ final
|
|
|
+ CallingUtil callingUtil;
|
|
|
+
|
|
|
+ public DataCollector(CallingProperties callingProperties, DeviceRegistry deviceRegistry, DeviceIdGenerator deviceIdGenerator, InfluxDbUtil influxDbUtil, CallingUtil callingUtil) {
|
|
|
+ this.callingProperties = callingProperties;
|
|
|
+ this.deviceRegistry = deviceRegistry;
|
|
|
+ this.deviceIdGenerator = deviceIdGenerator;
|
|
|
+ this.influxDbUtil = influxDbUtil;
|
|
|
+ this.callingUtil = callingUtil;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 采集scada
|
|
|
+ */
|
|
|
+ public void collectScada() {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
|
|
|
+ , "开始采集Scada数据,开始拉取数据");
|
|
|
+ long begin = System.currentTimeMillis();
|
|
|
+
|
|
|
+ //平台对接点
|
|
|
+ CallingProperties.CallingEndpoint endpoint = callingProperties.getEndpoints().get(ScadaPlatformMetadata.NAME);
|
|
|
+ //请求地址
|
|
|
+ String url = endpoint.getUrl() + ScadaPlatformMetadata.Uri.REAL_TIME_DATA;
|
|
|
+
|
|
|
+ //设备
|
|
|
+ List<Device> devices = deviceRegistry.findAll();
|
|
|
+
|
|
|
+ //参数
|
|
|
+ Map<String, Object> params = ScadaPlatformMetadata.getRealTimeDataParams(devices);
|
|
|
+ //请求结果项
|
|
|
+ List<ScadaPlatformData> items = callingUtil.request(url, HttpMethod.GET, params, Collections.emptyList(),
|
|
|
+ new TypeReference<ScadaPlatformResult<List<ScadaPlatformData>>>() {
|
|
|
+ });
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
|
|
|
+ , String.format("拉取数据成功,数据量:%d", items.size()));
|
|
|
+
|
|
|
+ //构建influxdb
|
|
|
+ List<Point> points = items.parallelStream().map(d -> d.toPoint(devices)).collect(Collectors.toList());
|
|
|
+ //写入influxDb
|
|
|
+ influxDbUtil.write(points);
|
|
|
+
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
|
|
|
+ , String.format("结束采集Scada数据 用时(毫秒):%d", (end - begin))
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|