houzhongjian
2024-10-11 6434fc445f48cffd49df9738576d5e340163ed34
提交 | 用户 | 时间
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
9100b0 25 import javax.annotation.security.PermitAll;
a6de49 26 import javax.servlet.http.HttpServletRequest;
H 27 import javax.servlet.http.HttpServletResponse;
28 import java.math.BigDecimal;
29 import java.util.*;
30 import java.util.stream.Collectors;
c7f709 31
L 32 import static com.iailab.framework.common.pojo.CommonResult.success;
a6de49 33
H 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 = "数据")
9961c7 43 public class ApiDataController {
a6de49 44
H 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
9100b0 60     @PermitAll
a6de49 61     @PostMapping("/point/history")
H 62     @Operation(summary = "point历史数据")
c7f709 63     public CommonResult<Map<String, List<Map<String, Object>>>> pointHistory(HttpServletResponse response, HttpServletRequest
a6de49 64             request, @RequestBody ApiPointValueQueryDTO queryDto) {
H 65         try {
66             apiSecurityUtils.validate(request);
67             Map<String, List<Map<String, Object>>> data = new HashMap<>();
68             if (CollectionUtils.isEmpty(queryDto.getPointNos())) {
c7f709 69                 return success(data);
a6de49 70             }
H 71             if (queryDto.getStart() == null) {
72                 queryDto.setStart(new Date());
73             }
74             if (queryDto.getEnd() == null) {
75                 queryDto.setEnd(new Date());
76             }
77             Map<String, Object> params = new HashMap<>(1);
78             params.put("pointNos", queryDto.getPointNos());
79             List<DaPointDTO> pointList = daPointService.list(params);
80             if (CollectionUtils.isEmpty(pointList)) {
c7f709 81                 return success(data);
a6de49 82             }
H 83             List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
84                 InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
85                 pojo.setPoint(item.getPointNo());
86                 pojo.setType(item.getDataType());
87                 return pojo;
88             }).collect(Collectors.toList());
89             data = influxDBService.queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd());
c7f709 90             return success(data);
a6de49 91
H 92         } catch (Exception ex) {
c7f709 93             return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
a6de49 94         }
H 95     }
96
97     @PostMapping("/point/current")
98     @Operation(summary = "point当前实时数据")
c7f709 99     public CommonResult<Map<String, Object>> pointCurrent(HttpServletResponse response, HttpServletRequest
a6de49 100             request, @RequestBody List<String> pointNos) {
H 101         try {
102             // apiSecurityUtils.validate(request);
103             Map<String, Object> data = pointCollector.getCurrentValue(pointNos);
c7f709 104             return success(data);
a6de49 105         } catch (Exception ex) {
c7f709 106             return new CommonResult<Map<String, Object>>().setMsg(ex.getMessage());
a6de49 107         }
H 108     }
109
110     @PostMapping("/point/chart")
111     public CommonResult<BarLineDTO> pointChart(@RequestBody IndexQueryDTO dto) {
112         BarLineDTO CommonResult = new BarLineDTO();
113         try {
114             List<String> legend = new ArrayList<>();
115             List<SeriesItem> series = new ArrayList<>();
116             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");
117             Date endDate = DateUtils.parse(endDateStr, "yyyy-MM-dd HH:mm");
118             Date startDate = dto.getStartDate() == null ? DateUtils.addDateHours(endDate, -2) : dto.getStartDate();
119             List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 10 : dto.getGranularity());
120             if (CollectionUtils.isEmpty(dto.getCodes())) {
121                 return new CommonResult<BarLineDTO>().setData(CommonResult);
122             }
123             List<DaPointDTO> pointList = new ArrayList<>();
124             dto.getCodes().forEach(item -> {
125                 pointList.add(daPointService.getByNo(item));
126             });
127             pointList.forEach(item -> {
128                 legend.add(item.getPointName());
129                 SeriesItem seriesItem = new SeriesItem();
130                 seriesItem.setName(item.getPointName());
131                 InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
132                 pojo.setPoint(item.getPointNo());
133                 pojo.setType(item.getDataType());
134                 List<Map<String, Object>> list = influxDBService.queryPointValues(pojo, startDate, endDate);
135                 List<Object[]> sData = list.stream().map(dataItem -> {
136                     Object[] valueArray = new Object[]{dataItem.get("time"),
137                             getFormatValue(item.getDataType(), dataItem.get("value"))};
138                     return valueArray;
139                 }).collect(Collectors.toList());
140                 seriesItem.setData(sData);
141                 series.add(seriesItem);
142             });
143             CommonResult.setLegend(legend);
144             CommonResult.setCategories(categories);
145             CommonResult.setSeries(series);
146         } catch (Exception ex) {
147             ex.printStackTrace();
148         }
149         return new CommonResult<BarLineDTO>().setData(CommonResult);
150     }
151
152     private Object getFormatValue(String dataType, Object value) {
153         if (!PointDataTypeEnum.BOOLEAN.getCode().equals(dataType)) {
154             BigDecimal decValue = new BigDecimal(value.toString());
155             if (PointDataTypeEnum.FLOAT.getCode().equals(dataType)) {
156                 return decValue.setScale(2, BigDecimal.ROUND_HALF_UP);
157             } else if (PointDataTypeEnum.INT.getCode().equals(dataType)) {
158                 decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP);
159             }
160         }
161         return value;
162     }
163
164
165     @PostMapping("/pointRelation/history")
166     @Operation(summary = "pointRelation历史数据")
c7f709 167     public CommonResult<Map<String, List<Map<String, Object>>>> pointRelationHistory(HttpServletResponse response, HttpServletRequest
a6de49 168             request, @RequestBody ApiPointValueQueryDTO queryDto) {
H 169         try {
170             Map<String, List<Map<String, Object>>> data = new HashMap<>();
171             if (CollectionUtils.isEmpty(queryDto.getPointNos())) {
c7f709 172                 return success(data);
a6de49 173             }
H 174             if (queryDto.getStart() == null) {
175                 queryDto.setStart(new Date());
176             }
177             if (queryDto.getEnd() == null) {
178                 queryDto.setEnd(new Date());
179             }
180             data = daPointValueService.getHistoryList(queryDto);
181             if (CollectionUtils.isEmpty(data)) {
c7f709 182                 return success(data);
a6de49 183             }
c7f709 184             return success(data);
a6de49 185         } catch (Exception ex) {
c7f709 186             return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
a6de49 187         }
H 188     }
189
190     @GetMapping("/device-value")
191     public List<DeviceValueDTO> getDeviceValue(@RequestParam Map<String, Object> params) {
192         List<DeviceValueDTO> CommonResult = new ArrayList<>();
193         if (params.get("pointNos") == null) {
194             return CommonResult;
195         }
196         List<String> pointNos = Arrays.asList(params.get("pointNos").toString().split(","));
197         Map<String, Object> data = pointCollector.getCurrentValue(pointNos);
198         if (!CollectionUtils.isEmpty(data)) {
199             data.forEach((k, v) -> {
200                 DeviceValueDTO dto = new DeviceValueDTO();
201                 dto.setDataId(k);
202                 dto.setValue(new BigDecimal(v.toString()));
203                 CommonResult.add(dto);
204             });
205         }
206         return CommonResult;
207     }
208 }