潘志宝
2024-10-29 d41f14d2986b46da9dd7742f6df63d9725cd29f3
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();