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