潘志宝
2024-09-10 f1162ee7b12a1e4fd45301f4d3fecb3bf155d006
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.api.controller;
H 2
3 import com.iailab.framework.common.pojo.CommonResult;
4 import com.iailab.framework.common.util.date.DateUtils;
585be5 5 import com.iailab.module.data.api.dto.IndexQueryDTO;
6 import com.iailab.module.data.api.dto.echarts.BarLineDTO;
7 import com.iailab.module.data.api.dto.echarts.SeriesItem;
a6de49 8 import com.iailab.module.data.api.dto.ApiPointValueQueryDTO;
H 9 import com.iailab.module.data.point.collection.PointCollector;
10 import com.iailab.module.data.point.common.PointDataTypeEnum;
11 import com.iailab.module.data.point.dto.DaPointDTO;
12 import com.iailab.module.data.point.service.DaPointService;
13 import com.iailab.module.data.point.service.DaPointValueService;
14 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
15 import com.iailab.module.data.influxdb.service.InfluxDBService;
16 import com.iailab.module.data.api.dto.DeviceValueDTO;
17 import com.iailab.module.data.api.utils.ApiSecurityUtils;
18 import io.swagger.v3.oas.annotations.Operation;
19 import io.swagger.v3.oas.annotations.tags.Tag;
20 import lombok.extern.slf4j.Slf4j;
21 import javax.annotation.Resource;
22 import org.springframework.util.CollectionUtils;
23 import org.springframework.web.bind.annotation.*;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import java.math.BigDecimal;
28 import java.util.*;
29 import java.util.stream.Collectors;
c7f709 30
L 31 import static com.iailab.framework.common.pojo.CommonResult.success;
a6de49 32
H 33 /**
34  * @author PanZhibao
35  * @Description
36  * @createTime 2023年05月02日 10:58:00
37  */
38 @Slf4j
39 @RestController
40 @RequestMapping("/api/data")
41 @Tag(name = "数据")
42 public class DataController {
43
44     @Resource
45     private DaPointService daPointService;
46
47     @Resource
48     private ApiSecurityUtils apiSecurityUtils;
49
50     @Resource
51     private DaPointValueService daPointValueService;
52
53     @Resource
54     private InfluxDBService influxDBService;
55
56     @Resource
57     private PointCollector pointCollector;
58
59     @PostMapping("/point/history")
60     @Operation(summary = "point历史数据")
c7f709 61     public CommonResult<Map<String, List<Map<String, Object>>>> pointHistory(HttpServletResponse response, HttpServletRequest
a6de49 62             request, @RequestBody ApiPointValueQueryDTO queryDto) {
H 63         try {
64             apiSecurityUtils.validate(request);
65             Map<String, List<Map<String, Object>>> data = new HashMap<>();
66             if (CollectionUtils.isEmpty(queryDto.getPointNos())) {
c7f709 67                 return success(data);
a6de49 68             }
H 69             if (queryDto.getStart() == null) {
70                 queryDto.setStart(new Date());
71             }
72             if (queryDto.getEnd() == null) {
73                 queryDto.setEnd(new Date());
74             }
75             Map<String, Object> params = new HashMap<>(1);
76             params.put("pointNos", queryDto.getPointNos());
77             List<DaPointDTO> pointList = daPointService.list(params);
78             if (CollectionUtils.isEmpty(pointList)) {
c7f709 79                 return success(data);
a6de49 80             }
H 81             List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
82                 InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
83                 pojo.setPoint(item.getPointNo());
84                 pojo.setType(item.getDataType());
85                 return pojo;
86             }).collect(Collectors.toList());
87             data = influxDBService.queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd());
c7f709 88             return success(data);
a6de49 89
H 90         } catch (Exception ex) {
c7f709 91             return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
a6de49 92         }
H 93     }
94
95     @PostMapping("/point/current")
96     @Operation(summary = "point当前实时数据")
c7f709 97     public CommonResult<Map<String, Object>> pointCurrent(HttpServletResponse response, HttpServletRequest
a6de49 98             request, @RequestBody List<String> pointNos) {
H 99         try {
100             // apiSecurityUtils.validate(request);
101             Map<String, Object> data = pointCollector.getCurrentValue(pointNos);
c7f709 102             return success(data);
a6de49 103         } catch (Exception ex) {
c7f709 104             return new CommonResult<Map<String, Object>>().setMsg(ex.getMessage());
a6de49 105         }
H 106     }
107
108     @PostMapping("/point/chart")
109     public CommonResult<BarLineDTO> pointChart(@RequestBody IndexQueryDTO dto) {
110         BarLineDTO CommonResult = new BarLineDTO();
111         try {
112             List<String> legend = new ArrayList<>();
113             List<SeriesItem> series = new ArrayList<>();
114             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");
115             Date endDate = DateUtils.parse(endDateStr, "yyyy-MM-dd HH:mm");
116             Date startDate = dto.getStartDate() == null ? DateUtils.addDateHours(endDate, -2) : dto.getStartDate();
117             List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 10 : dto.getGranularity());
118             if (CollectionUtils.isEmpty(dto.getCodes())) {
119                 return new CommonResult<BarLineDTO>().setData(CommonResult);
120             }
121             List<DaPointDTO> pointList = new ArrayList<>();
122             dto.getCodes().forEach(item -> {
123                 pointList.add(daPointService.getByNo(item));
124             });
125             pointList.forEach(item -> {
126                 legend.add(item.getPointName());
127                 SeriesItem seriesItem = new SeriesItem();
128                 seriesItem.setName(item.getPointName());
129                 InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
130                 pojo.setPoint(item.getPointNo());
131                 pojo.setType(item.getDataType());
132                 List<Map<String, Object>> list = influxDBService.queryPointValues(pojo, startDate, endDate);
133                 List<Object[]> sData = list.stream().map(dataItem -> {
134                     Object[] valueArray = new Object[]{dataItem.get("time"),
135                             getFormatValue(item.getDataType(), dataItem.get("value"))};
136                     return valueArray;
137                 }).collect(Collectors.toList());
138                 seriesItem.setData(sData);
139                 series.add(seriesItem);
140             });
141             CommonResult.setLegend(legend);
142             CommonResult.setCategories(categories);
143             CommonResult.setSeries(series);
144         } catch (Exception ex) {
145             ex.printStackTrace();
146         }
147         return new CommonResult<BarLineDTO>().setData(CommonResult);
148     }
149
150     private Object getFormatValue(String dataType, Object value) {
151         if (!PointDataTypeEnum.BOOLEAN.getCode().equals(dataType)) {
152             BigDecimal decValue = new BigDecimal(value.toString());
153             if (PointDataTypeEnum.FLOAT.getCode().equals(dataType)) {
154                 return decValue.setScale(2, BigDecimal.ROUND_HALF_UP);
155             } else if (PointDataTypeEnum.INT.getCode().equals(dataType)) {
156                 decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP);
157             }
158         }
159         return value;
160     }
161
162
163     @PostMapping("/pointRelation/history")
164     @Operation(summary = "pointRelation历史数据")
c7f709 165     public CommonResult<Map<String, List<Map<String, Object>>>> pointRelationHistory(HttpServletResponse response, HttpServletRequest
a6de49 166             request, @RequestBody ApiPointValueQueryDTO queryDto) {
H 167         try {
168             Map<String, List<Map<String, Object>>> data = new HashMap<>();
169             if (CollectionUtils.isEmpty(queryDto.getPointNos())) {
c7f709 170                 return success(data);
a6de49 171             }
H 172             if (queryDto.getStart() == null) {
173                 queryDto.setStart(new Date());
174             }
175             if (queryDto.getEnd() == null) {
176                 queryDto.setEnd(new Date());
177             }
178             data = daPointValueService.getHistoryList(queryDto);
179             if (CollectionUtils.isEmpty(data)) {
c7f709 180                 return success(data);
a6de49 181             }
c7f709 182             return success(data);
a6de49 183         } catch (Exception ex) {
c7f709 184             return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
a6de49 185         }
H 186     }
187
188     @GetMapping("/device-value")
189     public List<DeviceValueDTO> getDeviceValue(@RequestParam Map<String, Object> params) {
190         List<DeviceValueDTO> CommonResult = new ArrayList<>();
191         if (params.get("pointNos") == null) {
192             return CommonResult;
193         }
194         List<String> pointNos = Arrays.asList(params.get("pointNos").toString().split(","));
195         Map<String, Object> data = pointCollector.getCurrentValue(pointNos);
196         if (!CollectionUtils.isEmpty(data)) {
197             data.forEach((k, v) -> {
198                 DeviceValueDTO dto = new DeviceValueDTO();
199                 dto.setDataId(k);
200                 dto.setValue(new BigDecimal(v.toString()));
201                 CommonResult.add(dto);
202             });
203         }
204         return CommonResult;
205     }
206 }