dengzedong
3 天以前 07890ec0dcb75572740f25a29c80f6d7bddb39e0
提交 | 用户 | 时间
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
45     public List<ApiPointDTO> getInfoByIds(List<String> pointIds) {
46         List<ApiPointDTO> result = new ArrayList<>(pointIds.size());
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<>();
66         if (queryDto.getStart() == null) {
67             queryDto.setStart(new Date());
68         }
69         if (queryDto.getEnd() == null) {
70             queryDto.setEnd(new Date());
71         }
72         Map<String, Object> params = new HashMap<>(1);
73         params.put("pointNos", queryDto.getPointNos());
74         List<DaPointDTO> pointList = daPointService.list(params);
75         if (CollectionUtils.isEmpty(pointList)) {
76             return data;
77         }
78         List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
79             InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
80             pojo.setPoint(item.getPointNo());
81             pojo.setType(item.getDataType());
82             return pojo;
83         }).collect(Collectors.toList());
84         data = influxDBService.queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd());
85         return data;
60201e 86     }
87
88     @Override
73ed35 89     public List<ApiPointValueDTO> queryPointHistoryValue(ApiPointValueQueryDTO queryDto) {
b8b8cb 90         DaPointDTO daPointDTO = daPointService.getByNo(queryDto.getPointNo());
91         InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
92         pojo.setPoint(queryDto.getPointNo());
93         pojo.setType(daPointDTO.getDataType());
94         Date startTime = queryDto.getStart();
95         Date endTime = queryDto.getEnd();
96         List<Map<String, Object>> list = influxDBService.queryPointValues(pojo, startTime, endTime);
97         List<ApiPointValueDTO> pointValueList = new ArrayList<>();
98         for (int i = 0; list.size() - i >= 1; i++) {
99             ApiPointValueDTO pointValue = new ApiPointValueDTO();
9df837 100             pointValue.setV(Double.parseDouble(list.get(i).get(InfluxDBServiceImpl.VALUE).toString()));
101             pointValue.setT(DateUtils.parse(list.get(i).get(InfluxDBServiceImpl.TIME).toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
b8b8cb 102             pointValueList.add(pointValue);
103         }
104         return pointValueList;
60201e 105     }
106
107     @Override
b8b8cb 108     public Boolean writePointRealValue(ApiPointValueWriteDTO writeDTO) {
109         try {
110             DaPointWriteValueDTO wr = new DaPointWriteValueDTO();
111             wr.setPointNo(writeDTO.getPointNo());
112             wr.setPointValue(writeDTO.getValue());
113             pointCollector.setValue(wr);
114             return true;
115         } catch (Exception ex) {
116             return false;
117         }
60201e 118     }
119 }