潘志宝
2024-10-10 9100b0c14dca46366cfe79336eb85d5f0ca793af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.iailab.module.data.api.controller;
 
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.util.date.DateUtils;
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.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.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
import static com.iailab.framework.common.pojo.CommonResult.success;
 
/**
 * @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;
 
    @PermitAll
    @PostMapping("/point/history")
    @Operation(summary = "point历史数据")
    public CommonResult<Map<String, List<Map<String, Object>>>> pointHistory(HttpServletResponse response, HttpServletRequest
            request, @RequestBody ApiPointValueQueryDTO queryDto) {
        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());
            return success(data);
 
        } catch (Exception ex) {
            return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
        }
    }
 
    @PostMapping("/point/current")
    @Operation(summary = "point当前实时数据")
    public CommonResult<Map<String, Object>> pointCurrent(HttpServletResponse response, HttpServletRequest
            request, @RequestBody List<String> pointNos) {
        try {
            // apiSecurityUtils.validate(request);
            Map<String, Object> data = pointCollector.getCurrentValue(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) {
        BarLineDTO CommonResult = new BarLineDTO();
        try {
            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());
            if (CollectionUtils.isEmpty(dto.getCodes())) {
                return new CommonResult<BarLineDTO>().setData(CommonResult);
            }
            List<DaPointDTO> 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<Map<String, Object>> list = influxDBService.queryPointValues(pojo, startDate, endDate);
                List<Object[]> 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<BarLineDTO>().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 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<>();
        if (params.get("pointNos") == null) {
            return CommonResult;
        }
        List<String> pointNos = Arrays.asList(params.get("pointNos").toString().split(","));
        Map<String, Object> 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;
    }
}