whyczh
2021-03-31 8e560d924cd1d6918b969b6b0978b613da0bdfb1
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
package com.whyc.controller;
 
import com.google.gson.JsonObject;
import com.whyc.dto.Response;
import com.whyc.dto.TestDataDTO;
import com.whyc.pojo.MotorState;
import com.whyc.pojo.TestPlan;
import com.whyc.service.DeviceInfService;
import com.whyc.service.MotorStateService;
import com.whyc.service.TestPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
@RestController
@RequestMapping("testData")
@Api(tags = "实验数据")
@Slf4j
public class TestDataController {
    @Autowired
    private TestPlanService testPlanService;
    @Resource
    private MotorStateService motorStateService;
    @Autowired
    private DeviceInfService deviceInfService;
 
    @GetMapping()
    @ApiOperation("获取历史数据")
    public Response getHistoryData(@RequestParam Integer testPlanId){
        //获取试验计划数据
        TestPlan testPlan = testPlanService.getOneById(testPlanId);
        //获取试验参试设备
        String devices = testPlan.getDevices();
        String[] devIds = devices.split(",");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = dateFormat.format(testPlan.getStartTime());
        String endTime = dateFormat.format(testPlan.getEndTime());
        //通过设备和开始结束时间来获取对应的数据
        List<TestDataDTO> list = new ArrayList<TestDataDTO>();
        for (String deviceId: devIds) {
            List<MotorState> historyData =  motorStateService.getHistoryData(deviceId,startTime,endTime);
            String deviceName = deviceInfService.getOneByDeviceId(Integer.valueOf(deviceId)).getDeviceName();
            TestDataDTO testDataDTO = new TestDataDTO();
            testDataDTO.setDeviceId(deviceId);
            testDataDTO.setDeviceName(deviceName);
            testDataDTO.setData(historyData);
            list.add(testDataDTO);
        }
        return new Response().set(1,list);
    }
}