潘志宝
2024-11-08 e7812194c9cb2c6e38af05524b55f687905fb5f6
提交 | 用户 | 时间
60201e 1 package com.iailab.module.data.api.point;
2
a2aa90 3 import com.iailab.framework.common.util.date.DateUtils;
b8b8cb 4 import com.iailab.framework.common.util.object.ConvertUtils;
60201e 5 import com.iailab.module.data.api.point.dto.*;
b8b8cb 6 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
7 import com.iailab.module.data.influxdb.service.InfluxDBService;
a2aa90 8 import com.iailab.module.data.influxdb.service.impl.InfluxDBServiceImpl;
b8b8cb 9 import com.iailab.module.data.point.collection.PointCollector;
10 import com.iailab.module.data.point.dto.DaPointDTO;
11 import com.iailab.module.data.point.dto.DaPointWriteValueDTO;
1c3121 12 import com.iailab.module.data.point.service.DaPointService;
13 import org.springframework.beans.factory.annotation.Autowired;
b8b8cb 14 import org.springframework.util.CollectionUtils;
60201e 15 import org.springframework.validation.annotation.Validated;
16 import org.springframework.web.bind.annotation.RestController;
17
b8b8cb 18 import java.util.*;
19 import java.util.stream.Collectors;
60201e 20
21 /**
22  * @author PanZhibao
23  * @Description
24  * @createTime 2024年09月02日
25  */
26 @RestController // 提供 RESTful API 接口,给 Feign 调用
27 @Validated
28 public class DataPointApiImpl implements DataPointApi {
29
1c3121 30     @Autowired
31     private DaPointService daPointService;
32
b8b8cb 33     @Autowired
34     private PointCollector pointCollector;
35
36     @Autowired
37     private InfluxDBService influxDBService;
60201e 38
39     @Override
73ed35 40     public ApiPointDTO getInfoById(String pointId) {
b8b8cb 41         return ConvertUtils.sourceToTarget(daPointService.getSimpleInfoById(pointId), ApiPointDTO.class);
42     }
43
44     @Override
45     public ApiPointDTO getInfoByNo(String pointNo) {
46         return ConvertUtils.sourceToTarget(daPointService.getSimpleInfoByNo(pointNo), ApiPointDTO.class);
60201e 47     }
48
49     @Override
73ed35 50     public Map<String, Object> queryPointsRealValue(List<String> pointNos) {
b8b8cb 51         return pointCollector.getCurrentValue(pointNos);
60201e 52     }
53
54     @Override
73ed35 55     public Map<String, List<Map<String, Object>>> queryPointsHistoryValue(ApiPointsValueQueryDTO queryDto) {
b8b8cb 56         Map<String, List<Map<String, Object>>> data = new HashMap<>();
57         if (queryDto.getStart() == null) {
58             queryDto.setStart(new Date());
59         }
60         if (queryDto.getEnd() == null) {
61             queryDto.setEnd(new Date());
62         }
63         Map<String, Object> params = new HashMap<>(1);
64         params.put("pointNos", queryDto.getPointNos());
65         List<DaPointDTO> pointList = daPointService.list(params);
66         if (CollectionUtils.isEmpty(pointList)) {
67             return data;
68         }
69         List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
70             InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
71             pojo.setPoint(item.getPointNo());
72             pojo.setType(item.getDataType());
73             return pojo;
74         }).collect(Collectors.toList());
75         data = influxDBService.queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd());
76         return data;
60201e 77     }
78
79     @Override
73ed35 80     public List<ApiPointValueDTO> queryPointHistoryValue(ApiPointValueQueryDTO queryDto) {
b8b8cb 81         DaPointDTO daPointDTO = daPointService.getByNo(queryDto.getPointNo());
82         InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
83         pojo.setPoint(queryDto.getPointNo());
84         pojo.setType(daPointDTO.getDataType());
85         Date startTime = queryDto.getStart();
86         Date endTime = queryDto.getEnd();
87         List<Map<String, Object>> list = influxDBService.queryPointValues(pojo, startTime, endTime);
88         List<ApiPointValueDTO> pointValueList = new ArrayList<>();
89         for (int i = 0; list.size() - i >= 1; i++) {
90             ApiPointValueDTO pointValue = new ApiPointValueDTO();
a2aa90 91             pointValue.setDataValue(Double.parseDouble(list.get(i).get(InfluxDBServiceImpl.VALUE).toString()));
92             pointValue.setDataTime(DateUtils.parse(list.get(i).get(InfluxDBServiceImpl.TIME).toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
b8b8cb 93             pointValueList.add(pointValue);
94         }
95         return pointValueList;
60201e 96     }
97
98     @Override
b8b8cb 99     public Boolean writePointRealValue(ApiPointValueWriteDTO writeDTO) {
100         try {
101             DaPointWriteValueDTO wr = new DaPointWriteValueDTO();
102             wr.setPointNo(writeDTO.getPointNo());
103             wr.setPointValue(writeDTO.getValue());
104             pointCollector.setValue(wr);
105             return true;
106         } catch (Exception ex) {
107             return false;
108         }
60201e 109     }
110 }