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