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