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.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("ws") @ApiOperation(value="查询webSocket",protocols = "ws",notes = "接口:ws://localhost:8090/websocket,发送消息为testPlanId") public Response doc(){ return new Response().setMsg(1,"查看接口描述"); } @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 list = new ArrayList(); for (String deviceId: devIds) { List 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); } }