iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/ApiDataController.java
@@ -5,20 +5,22 @@ import com.iailab.module.data.api.dto.IndexQueryDTO; import com.iailab.module.data.api.dto.echarts.BarLineDTO; import com.iailab.module.data.api.dto.echarts.SeriesItem; import com.iailab.module.data.api.dto.ApiPointValueQueryDTO; import com.iailab.module.data.point.collection.PointCollector; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.data.api.point.dto.ApiPointValueDTO; import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; import com.iailab.module.data.api.point.dto.ApiPointsValueQueryDTO; import com.iailab.module.data.point.common.PointDataTypeEnum; import com.iailab.module.data.point.dto.DaPointDTO; import com.iailab.module.data.point.service.DaPointService; import com.iailab.module.data.point.service.DaPointValueService; import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; import com.iailab.module.data.influxdb.service.InfluxDBService; import com.iailab.module.data.api.dto.DeviceValueDTO; import com.iailab.module.data.api.utils.ApiSecurityUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; @@ -38,7 +40,7 @@ */ @Slf4j @RestController @RequestMapping("/api/data") @RequestMapping("/admin-api/data/api") @Tag(name = "数据") public class ApiDataController { @@ -48,45 +50,18 @@ @Resource private ApiSecurityUtils apiSecurityUtils; @Resource private DaPointValueService daPointValueService; @Resource private InfluxDBService influxDBService; @Resource private PointCollector pointCollector; @Autowired private DataPointApi dataPointApi; @PermitAll @PostMapping("/point/history") @Operation(summary = "point历史数据") public CommonResult<Map<String, List<Map<String, Object>>>> pointHistory(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiPointValueQueryDTO queryDto) { @PostMapping("/query-points/history-value") @Operation(summary = "查询多个测点历史值") public CommonResult<Map<String, List<Map<String, Object>>>> queryPointsRealValue(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiPointsValueQueryDTO queryDto) { Map<String, List<Map<String, Object>>> data = new HashMap<>(); try { apiSecurityUtils.validate(request); Map<String, List<Map<String, Object>>> data = new HashMap<>(); if (CollectionUtils.isEmpty(queryDto.getPointNos())) { return success(data); } if (queryDto.getStart() == null) { queryDto.setStart(new Date()); } if (queryDto.getEnd() == null) { queryDto.setEnd(new Date()); } Map<String, Object> params = new HashMap<>(1); params.put("pointNos", queryDto.getPointNos()); List<DaPointDTO> pointList = daPointService.list(params); if (CollectionUtils.isEmpty(pointList)) { return success(data); } List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> { InfluxPointValuePOJO pojo = new InfluxPointValuePOJO(); pojo.setPoint(item.getPointNo()); pojo.setType(item.getDataType()); return pojo; }).collect(Collectors.toList()); data = influxDBService.queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd()); data = dataPointApi.queryPointsHistoryValue(queryDto); return success(data); } catch (Exception ex) { @@ -94,29 +69,48 @@ } } @PostMapping("/point/current") @Operation(summary = "point当前实时数据") public CommonResult<Map<String, Object>> pointCurrent(HttpServletResponse response, HttpServletRequest request, @RequestBody List<String> pointNos) { @PermitAll @PostMapping("/query-point/history-value") @Operation(summary = "查询单个测点历史值") public CommonResult<List<ApiPointValueDTO>> queryPointHistoryValue(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiPointValueQueryDTO queryDto) { List<ApiPointValueDTO> pointValueList = new ArrayList<>(); try { // apiSecurityUtils.validate(request); Map<String, Object> data = pointCollector.getCurrentValue(pointNos); apiSecurityUtils.validate(request); pointValueList = dataPointApi.queryPointHistoryValue(queryDto); return success(pointValueList); } catch (Exception ex) { return new CommonResult<List<ApiPointValueDTO>>().setMsg(ex.getMessage()); } } @PostMapping("/query-points/real-value") @Operation(summary = "查询多个测点当前值") public CommonResult<Map<String, Object>> queryPointsRealValue(HttpServletResponse response, HttpServletRequest request, @RequestBody List<String> pointNos) { Map<String, Object> data = new HashMap<>(); try { apiSecurityUtils.validate(request); data = dataPointApi.queryPointsRealValue(pointNos); return success(data); } catch (Exception ex) { return new CommonResult<Map<String, Object>>().setMsg(ex.getMessage()); } } @PostMapping("/point/chart") public CommonResult<BarLineDTO> pointChart(@RequestBody IndexQueryDTO dto) { @PostMapping("/query-points/chart") public CommonResult<BarLineDTO> queryPointsChart(HttpServletResponse response, HttpServletRequest request, @RequestBody IndexQueryDTO dto) { BarLineDTO CommonResult = new BarLineDTO(); try { apiSecurityUtils.validate(request); List<String> legend = new ArrayList<>(); List<SeriesItem> series = new ArrayList<>(); String endDateStr = dto.getEndDate() == null ? DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") : DateUtils.format(dto.getEndDate(), "yyyy-MM-dd HH:mm:ss"); Date endDate = DateUtils.parse(endDateStr, "yyyy-MM-dd HH:mm"); Date startDate = dto.getStartDate() == null ? DateUtils.addDateHours(endDate, -2) : dto.getStartDate(); List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 10 : dto.getGranularity()); List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 60 : dto.getGranularity()); if (CollectionUtils.isEmpty(dto.getCodes())) { return new CommonResult<BarLineDTO>().setData(CommonResult); } @@ -128,13 +122,14 @@ legend.add(item.getPointName()); SeriesItem seriesItem = new SeriesItem(); seriesItem.setName(item.getPointName()); InfluxPointValuePOJO pojo = new InfluxPointValuePOJO(); pojo.setPoint(item.getPointNo()); pojo.setType(item.getDataType()); List<Map<String, Object>> list = influxDBService.queryPointValues(pojo, startDate, endDate); ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); queryDto.setStart(startDate); queryDto.setEnd(endDate); queryDto.setPointNo(item.getPointNo()); List<ApiPointValueDTO> list = dataPointApi.queryPointHistoryValue(queryDto); List<Object[]> sData = list.stream().map(dataItem -> { Object[] valueArray = new Object[]{dataItem.get("time"), getFormatValue(item.getDataType(), dataItem.get("value"))}; Object[] valueArray = new Object[]{DateUtils.format(dataItem.getDataTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), getFormatValue(item.getDataType(), dataItem.getDataValue())}; return valueArray; }).collect(Collectors.toList()); seriesItem.setData(sData); @@ -162,31 +157,6 @@ } @PostMapping("/pointRelation/history") @Operation(summary = "pointRelation历史数据") public CommonResult<Map<String, List<Map<String, Object>>>> pointRelationHistory(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiPointValueQueryDTO queryDto) { try { Map<String, List<Map<String, Object>>> data = new HashMap<>(); if (CollectionUtils.isEmpty(queryDto.getPointNos())) { return success(data); } if (queryDto.getStart() == null) { queryDto.setStart(new Date()); } if (queryDto.getEnd() == null) { queryDto.setEnd(new Date()); } data = daPointValueService.getHistoryList(queryDto); if (CollectionUtils.isEmpty(data)) { return success(data); } return success(data); } catch (Exception ex) { return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage()); } } @GetMapping("/device-value") public List<DeviceValueDTO> getDeviceValue(@RequestParam Map<String, Object> params) { List<DeviceValueDTO> CommonResult = new ArrayList<>(); @@ -194,7 +164,7 @@ return CommonResult; } List<String> pointNos = Arrays.asList(params.get("pointNos").toString().split(",")); Map<String, Object> data = pointCollector.getCurrentValue(pointNos); Map<String, Object> data = dataPointApi.queryPointsRealValue(pointNos); if (!CollectionUtils.isEmpty(data)) { data.forEach((k, v) -> { DeviceValueDTO dto = new DeviceValueDTO(); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexQueryDTO.java
@@ -1,5 +1,6 @@ package com.iailab.module.data.api.dto; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.Date; @@ -8,8 +9,10 @@ @Data public class IndexQueryDTO { @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date startDate; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date endDate; private String code; iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/utils/ApiSecurityUtils.java
@@ -5,10 +5,13 @@ import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.security.core.LoginUser; import com.iailab.framework.security.core.util.SecurityFrameworkUtils; import com.iailab.framework.tenant.core.context.TenantContextHolder; import com.iailab.module.system.api.user.AdminUserApi; import com.iailab.module.system.api.user.dto.AdminUserRespDTO; import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; @@ -43,9 +46,18 @@ return token; } private void setTenantId(HttpServletRequest httpRequest) { String tenantId = httpRequest.getHeader(Constant.HEAD_TENANT_ID); if (StringUtils.isBlank(tenantId)) { TenantContextHolder.setTenantId(Long.parseLong(tenantId)); } } public void validate(HttpServletRequest httpRequest) throws Exception { String token = getRequestToken(httpRequest); setTenantId(httpRequest); /*String token = getRequestToken(httpRequest); if (StringUtils.isBlank(token)) { throw new Exception("token 不能为空!"); } @@ -58,7 +70,7 @@ throw new RuntimeException("用户不存在"); } AdminUserRespDTO userData = user.getData(); String username = userData.getUsername(); String username = userData.getUsername();*/ /*ApiAppEntity appInfo = apiAppService.getInfoByAppKey(username); if (appInfo == null) { throw new RuntimeException("应用未授权"); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForIhd.java
文件名从 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/http/collector/ihdb/HttpCollectorForSS.java 修改 @@ -1,6 +1,5 @@ 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; @@ -9,7 +8,7 @@ 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.HttpRequest; import com.iailab.module.data.common.utils.TagUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -17,30 +16,26 @@ 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 { public class HttpCollectorForIhd { private Map<String, HttpApiEntity> apiMap = new HashMap<>(); @Autowired private HttpApiService httpApiService; @Autowired HttpsRequest httpsRequest; private static final String STA_TRUE = "true"; private final String STA_TRUE = "true"; private final int GROUP_MAX_COUNT = 50; private static final int GROUP_MAX_COUNT = 50; private HttpApiEntity getHttpApi(String id) { if (apiMap.containsKey(id)) { @@ -64,8 +59,8 @@ 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"); 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"); @@ -123,8 +118,8 @@ } 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"); 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())) { @@ -136,16 +131,5 @@ } } } } 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/tag/controller/admin/TagController.java
@@ -157,7 +157,7 @@ List<TagOptionDTO> op2 = new ArrayList<>(); tags.forEach(item1 -> { TagOptionDTO op3 = new TagOptionDTO(); op3.setValue(item1.getTagDesc()); op3.setValue(item1.getTagName()); op3.setLabel(item1.getTagName()); op2.add(op3); }); 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_SHASTEEL_IH = "SHASTEEL_IH"; String iHyperDB = "iHyperDB"; } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/MeasureValueType.java
对比新文件 @@ -0,0 +1,36 @@ package com.iailab.module.data.common.enums; import lombok.AllArgsConstructor; import lombok.Getter; /** * 测量值类型 * * @author PanZhibao * @Description * @createTime 2024年10月28日 */ @Getter @AllArgsConstructor public enum MeasureValueType { SIMULATE("SIMULATE", "模拟量"), DIGITAL("DIGITAL", "数字量"); private String code; private String desc; public static MeasureValueType getEumByCode(String code) { if (code == null) { return null; } for (MeasureValueType statusEnum : MeasureValueType.values()) { if (statusEnum.getCode().equals(code)) { return statusEnum; } } return null; } } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/utils/HttpRequest.java
@@ -142,6 +142,7 @@ // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("content-type", "application/json"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java
@@ -19,8 +19,8 @@ @Value("${influx-db.org}") public String org; @Value("${influx-db.bucket}") public String bucket; /*@Value("${influx-db.bucket}") public String bucket;*/ @Value("${influx-db.token}") public String token; @@ -28,7 +28,7 @@ @Value("${influx-db.url}") public String url; private final static String BUCKET_NAME = "tenant_"; private final static String BUCKET_NAME = "bucket_"; private Logger logger = LoggerFactory.getLogger(getClass()); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java
@@ -100,7 +100,8 @@ } if (!CollectionUtils.isEmpty(pointValues)) { pointValues.forEach(item -> { writeApi.writeMeasurement(influxDBInstance.bucket, influxDBInstance.org, WritePrecision.MS, item); String bucket = influxDBInstance.getBucket(); writeApi.writeMeasurement(bucket, influxDBInstance.org, WritePrecision.MS, item); }); } writeApi.flush(); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/job/task/PointCollectTaskNet60.java
@@ -3,7 +3,9 @@ import com.iailab.module.data.point.collection.PointCollector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Resource; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @@ -19,9 +21,9 @@ */ @Component("pointCollectTaskNet60") public class PointCollectTaskNet60 implements ITask { private Logger logger = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass()); private final String NET = "NET60"; private static final String NET = "1min"; @Resource private PointCollector pointCollector; iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java
@@ -66,22 +66,18 @@ Map<String, Object> dataMap = new HashMap<>(); List<InfluxPointValuePOJO> pointValues = new ArrayList<>(); // 读取常量点 log.info("读取常量点"); List<DaPointDTO> pointConstantList = daPointService.getConstantPoint(minfreq); pointValues.addAll(constantHandle.handle(collectTime, pointConstantList, dataMap)); // 读取测量点 log.info("读取测量点"); List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(minfreq); pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap)); // 读取计算点 log.info("读取计算点"); List<DaPointDTO> pointCalculateList = daPointService.getMathPoint(minfreq); pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap)); // 存入数据库 log.info("存入数据库"); influxDBService.asyncWritePointValues(pointValues); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
@@ -39,7 +39,7 @@ @Resource private JavaScriptHandler javaScriptHandler; public static final String regex = "[+\\-\\*\\/\\(\\)\\&\\|\\>\\<]"; public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) { List<InfluxPointValuePOJO> result = new ArrayList<>(); iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
@@ -1,6 +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.http.collector.ihdb.HttpCollectorForIhd; 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; @@ -52,7 +52,7 @@ private OpcDACollector opcDACollector; @Autowired private HttpCollectorForSS httpCollectorForSS; private HttpCollectorForIhd httpCollectorForIhd; @Resource private DaPointService daPointService; @@ -68,7 +68,7 @@ List<String[]> opcDaTagIds = new ArrayList<>(); List<String[]> modbusTagIds = new ArrayList<>(); List<String[]> kioTagIds = new ArrayList<>(); List<Object[]> httpTagSS = new ArrayList<>(); List<Object[]> httpTagIhd = new ArrayList<>(); dtos.stream().forEach(item -> { @@ -81,8 +81,8 @@ } 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()}); if (CommonConstant.iHyperDB.equals(item.getSourceName())) { httpTagIhd.add(new Object[]{item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()}); } } }); @@ -100,8 +100,8 @@ if (!CollectionUtils.isEmpty(kioTagIds)) { tagValues.putAll(kingIOCollector.getTagValues(kioTagIds)); } if (!CollectionUtils.isEmpty(httpTagSS)) { tagValues.putAll(httpCollectorForSS.getTagValues(httpTagSS)); if (!CollectionUtils.isEmpty(httpTagIhd)) { tagValues.putAll(httpCollectorForIhd.getTagValues(httpTagIhd)); } this.toCommonResult(collectTime, dtos, tagValues, dataMap, result); log.info("测量点处理结束"); @@ -178,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()); value = httpCollectorForIhd.getTagValue(item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()); } else { log.info("没有匹配的TagNo=" + item.getTagNo()); }