潘志宝
2 天以前 34ba735bfa0d272e063054c7ba0ef6fde65880f4
提交 | 用户 | 时间
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) {
07890e 41         return daPointService.getSimpleInfoById(pointId);
D 42     }
43
44     @Override
50084d 45     public List<ApiPointDTO> getInfoByIds(Set<String> pointIds) {
07890e 46         List<ApiPointDTO> result = new ArrayList<>(pointIds.size());
D 47         for (String pointId : pointIds) {
48             result.add(daPointService.getSimpleInfoById(pointId));
49         }
50         return result;
b8b8cb 51     }
52
53     @Override
54     public ApiPointDTO getInfoByNo(String pointNo) {
55         return ConvertUtils.sourceToTarget(daPointService.getSimpleInfoByNo(pointNo), ApiPointDTO.class);
60201e 56     }
57
58     @Override
73ed35 59     public Map<String, Object> queryPointsRealValue(List<String> pointNos) {
b8b8cb 60         return pointCollector.getCurrentValue(pointNos);
60201e 61     }
62
63     @Override
73ed35 64     public Map<String, List<Map<String, Object>>> queryPointsHistoryValue(ApiPointsValueQueryDTO queryDto) {
b8b8cb 65         Map<String, List<Map<String, Object>>> data = new HashMap<>();
34ba73 66         Calendar calendar = Calendar.getInstance();
67         calendar.set(Calendar.MILLISECOND, 0);
b8b8cb 68         if (queryDto.getEnd() == null) {
34ba73 69             queryDto.setEnd(calendar.getTime());
b8b8cb 70         }
34ba73 71         if (queryDto.getStart() == null) {
72             calendar.add(Calendar.HOUR_OF_DAY, -1);
73             queryDto.setStart(calendar.getTime());
74         }
75
b8b8cb 76         Map<String, Object> params = new HashMap<>(1);
77         params.put("pointNos", queryDto.getPointNos());
78         List<DaPointDTO> pointList = daPointService.list(params);
79         if (CollectionUtils.isEmpty(pointList)) {
80             return data;
81         }
82         List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
83             InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
84             pojo.setPoint(item.getPointNo());
85             pojo.setType(item.getDataType());
86             return pojo;
87         }).collect(Collectors.toList());
88         data = influxDBService.queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd());
89         return data;
60201e 90     }
91
92     @Override
73ed35 93     public List<ApiPointValueDTO> queryPointHistoryValue(ApiPointValueQueryDTO queryDto) {
b8b8cb 94         DaPointDTO daPointDTO = daPointService.getByNo(queryDto.getPointNo());
95         InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
96         pojo.setPoint(queryDto.getPointNo());
97         pojo.setType(daPointDTO.getDataType());
98         Date startTime = queryDto.getStart();
99         Date endTime = queryDto.getEnd();
34ba73 100         Calendar calendar = Calendar.getInstance();
101         calendar.set(Calendar.MILLISECOND, 0);
102         if (endTime == null) {
103             endTime = calendar.getTime();
104         }
105         if (startTime == null) {
106             calendar.add(Calendar.HOUR_OF_DAY, -1);
107             startTime = calendar.getTime();
108         }
b8b8cb 109         List<Map<String, Object>> list = influxDBService.queryPointValues(pojo, startTime, endTime);
110         List<ApiPointValueDTO> pointValueList = new ArrayList<>();
111         for (int i = 0; list.size() - i >= 1; i++) {
112             ApiPointValueDTO pointValue = new ApiPointValueDTO();
9df837 113             pointValue.setV(Double.parseDouble(list.get(i).get(InfluxDBServiceImpl.VALUE).toString()));
114             pointValue.setT(DateUtils.parse(list.get(i).get(InfluxDBServiceImpl.TIME).toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
b8b8cb 115             pointValueList.add(pointValue);
116         }
117         return pointValueList;
60201e 118     }
119
120     @Override
b8b8cb 121     public Boolean writePointRealValue(ApiPointValueWriteDTO writeDTO) {
122         try {
123             DaPointWriteValueDTO wr = new DaPointWriteValueDTO();
124             wr.setPointNo(writeDTO.getPointNo());
125             wr.setPointValue(writeDTO.getValue());
126             pointCollector.setValue(wr);
127             return true;
128         } catch (Exception ex) {
2780e6 129             ex.printStackTrace();
b8b8cb 130             return false;
131         }
60201e 132     }
133 }