package com.iailab.module.data.api.controller; import com.iailab.api.IFeignModelApi; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.module.data.common.dto.IndexQueryDTO; import com.iailab.module.data.common.dto.echarts.BarLineDTO; import com.iailab.module.data.common.dto.echarts.SeriesItem; import com.iailab.module.data.common.utils.R; import com.iailab.module.data.api.dto.ApiPointValueQueryDTO; import com.iailab.module.data.point.collection.PointCollector; 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.ind.collection.IndItemCollector; 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 com.iailab.module.data.ind.dto.IndItemValueDTO; 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.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * @author PanZhibao * @Description * @createTime 2023年05月02日 10:58:00 */ @Slf4j @RestController @RequestMapping("/api/data") @Tag(name = "数据") public class DataController { @Resource private DaPointService daPointService; @Resource private ApiSecurityUtils apiSecurityUtils; @Resource private DaPointValueService daPointValueService; @Resource private InfluxDBService influxDBService; @Resource private PointCollector pointCollector; @Resource private IndItemCollector indItemCollector; @Resource private IFeignModelApi feignModelApi; @PostMapping("/point/history") @Operation(summary = "point历史数据") public R pointHistory(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiPointValueQueryDTO queryDto) { try { apiSecurityUtils.validate(request); Map>> data = new HashMap<>(); if (CollectionUtils.isEmpty(queryDto.getPointNos())) { return R.ok().put("data", data); } if (queryDto.getStart() == null) { queryDto.setStart(new Date()); } if (queryDto.getEnd() == null) { queryDto.setEnd(new Date()); } Map params = new HashMap<>(1); params.put("pointNos", queryDto.getPointNos()); List pointList = daPointService.list(params); if (CollectionUtils.isEmpty(pointList)) { return R.ok().put("data", data); } List 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()); return R.ok().put("data", data); } catch (Exception ex) { return R.error(ex.getMessage()); } } @PostMapping("/point/current") @Operation(summary = "point当前实时数据") public R pointCurrent(HttpServletResponse response, HttpServletRequest request, @RequestBody List pointNos) { try { // apiSecurityUtils.validate(request); Map data = pointCollector.getCurrentValue(pointNos); return R.ok().put("data", data); } catch (Exception ex) { return R.error(ex.getMessage()); } } @PostMapping("/point/chart") public CommonResult pointChart(@RequestBody IndexQueryDTO dto) { BarLineDTO CommonResult = new BarLineDTO(); try { List legend = new ArrayList<>(); List 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 categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 10 : dto.getGranularity()); if (CollectionUtils.isEmpty(dto.getCodes())) { return new CommonResult().setData(CommonResult); } List pointList = new ArrayList<>(); dto.getCodes().forEach(item -> { pointList.add(daPointService.getByNo(item)); }); pointList.forEach(item -> { 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> list = influxDBService.queryPointValues(pojo, startDate, endDate); List sData = list.stream().map(dataItem -> { Object[] valueArray = new Object[]{dataItem.get("time"), getFormatValue(item.getDataType(), dataItem.get("value"))}; return valueArray; }).collect(Collectors.toList()); seriesItem.setData(sData); series.add(seriesItem); }); CommonResult.setLegend(legend); CommonResult.setCategories(categories); CommonResult.setSeries(series); } catch (Exception ex) { ex.printStackTrace(); } return new CommonResult().setData(CommonResult); } private Object getFormatValue(String dataType, Object value) { if (!PointDataTypeEnum.BOOLEAN.getCode().equals(dataType)) { BigDecimal decValue = new BigDecimal(value.toString()); if (PointDataTypeEnum.FLOAT.getCode().equals(dataType)) { return decValue.setScale(2, BigDecimal.ROUND_HALF_UP); } else if (PointDataTypeEnum.INT.getCode().equals(dataType)) { decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP); } } return value; } @PostMapping("/pointRelation/history") @Operation(summary = "pointRelation历史数据") public R pointRelationHistory(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiPointValueQueryDTO queryDto) { try { Map>> data = new HashMap<>(); if (CollectionUtils.isEmpty(queryDto.getPointNos())) { return R.ok().put("data", 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 R.ok().put("data", data); } return R.ok().put("data", data); } catch (Exception ex) { return R.error(ex.getMessage()); } } @GetMapping("/device-value") public List getDeviceValue(@RequestParam Map params) { List CommonResult = new ArrayList<>(); if (params.get("pointNos") == null) { return CommonResult; } List pointNos = Arrays.asList(params.get("pointNos").toString().split(",")); Map data = pointCollector.getCurrentValue(pointNos); if (!CollectionUtils.isEmpty(data)) { data.forEach((k, v) -> { DeviceValueDTO dto = new DeviceValueDTO(); dto.setDataId(k); dto.setValue(new BigDecimal(v.toString())); CommonResult.add(dto); }); } return CommonResult; } @PostMapping("/ind-item/values") @Operation(summary = "point当前实时数据") public R indItemValues(HttpServletResponse response, HttpServletRequest request, @RequestBody List itemNos) { try { // apiSecurityUtils.validate(request); Map> data = indItemCollector.getValueList(itemNos); return R.ok().put("data", data); } catch (Exception ex) { return R.error(ex.getMessage()); } } @PostMapping("/ind-item/current-value") @Operation(summary = "point当前实时数据") public R indItemCurrentValue(HttpServletResponse response, HttpServletRequest request, @RequestBody List itemNos) { try { Map data = new HashMap<>(); // apiSecurityUtils.validate(request); Map> dataList = indItemCollector.getValueList(itemNos); dataList.forEach((k, v) -> { data.put(k, CollectionUtils.isEmpty(v) ? null : v.get(0).getDataValue()); }); return R.ok().put("data", data); } catch (Exception ex) { return R.error(ex.getMessage()); } } @PostMapping("/ind-item/values-trend") @Operation(summary = "point当前实时数据") public R indItemValuesTrend(HttpServletResponse response, HttpServletRequest request, @RequestBody List itemNos) { try { apiSecurityUtils.validate(request); Map CommonResult = new HashMap<>(); Map> values = indItemCollector.getValueList(itemNos); CommonResult.put("values", values); Map> trend = new HashMap<>(); Map params = new HashMap<>(1); params.put("modelCode", "trend_analysis"); values.forEach((k, v) -> { try { List sampleDataList = new ArrayList<>(); List nv = v.stream().filter(dto -> { return dto.getDataValue() != null; }).collect(Collectors.toList()); double[][] mix = new double[nv.size()][1]; for (int i = 0; i < nv.size(); i++) { mix[i][0] = nv.get(i).getDataValue().doubleValue(); } sampleDataList.add(mix); Map trendItem = feignModelApi.runModel(params, sampleDataList); trend.put(k, trendItem); } catch (Exception ex) { ex.printStackTrace(); } }); CommonResult.put("trend", trend); return R.ok().put("data", CommonResult); } catch (Exception ex) { ex.printStackTrace(); return R.error(ex.getMessage()); } } }