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