whycxzp
2021-05-25 65807b1a334e55b0f30dd53df8bdac81d2dc4dbd
更新试验接口
3个文件已修改
103 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/ExperimentController.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/mapper/CommonMapper.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ExperimentService.java 83 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ExperimentController.java
@@ -59,8 +59,14 @@
            "振动:zd,\n" +
            "耐压:ny,\n" +
            "转动惯量:zdgl,\n")
    public Response checkPrecondition(String type){
    public Response checkPrecondition(@RequestParam String type){
        return service.checkPrecondition(type);
    }
    @PutMapping("precondition")
    @ApiOperation(value = "设置前置条件",notes = "进线屏开关状态-1,大功率整流电源-2,...")
    public Response setPrecondition(@RequestParam Integer id){
        return service.setPrecondition(id);
    }
    /**
@@ -94,6 +100,12 @@
        return service.finishExperiment(experimentId);
    }
    @PostMapping("finishExperimentPoint")
    @ApiOperation(value = "结束测试点")
    public Response finishExperimentPoint(@RequestParam Integer id){
        return service.finishExperimentPoint(id);
    }
    /*======History======*/
    @PostMapping("page")
src/main/java/com/whyc/mapper/CommonMapper.java
@@ -3,6 +3,7 @@
import com.whyc.pojo.ExperimentPrecondition;
import net.sf.jsqlparser.expression.JdbcNamedParameter;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@@ -11,6 +12,9 @@
    @Select("select TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA= #{dBName} and TABLE_NAME like concat(#{tableLike},'%')")
    List<String> getTableName(String dBName, String tableLike);
    @Select("select ${field} as actualValue from ${tableName} where dev_id = #{deviceId}")
    @Select("select ${field} as actualValue from ${tableName} where dev_id in (#{deviceId})")
    Object getPreconditionStatus(ExperimentPrecondition precondition);
    @Update("update ${tableName} set ${field} = #{value} where dev_id in (${deviceId})")
    int setPrecondition(String tableName,String deviceId,String field,String value);
}
src/main/java/com/whyc/service/ExperimentService.java
@@ -13,6 +13,7 @@
import com.whyc.pojo.ExperimentBaseDataKZ;
import com.whyc.pojo.ExperimentPoint;
import com.whyc.pojo.ExperimentPrecondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -28,6 +29,7 @@
import static java.lang.Thread.sleep;
@Service
@Slf4j
public class ExperimentService {
    @Resource
@@ -270,6 +272,9 @@
    /**
     * 启动测试点,并在测试时长过后,返回测试结果
     * 测试点的结束,结束分两种情况:
     *  情况一:经过预定的测试时长,停止
     *  情况二:更新了数据库的某个字段,代码循环判断这个字段,如果为2则停止实验并返回结果
     * @return
     */
    public Response startExperimentPoint(ExperimentPoint point){
@@ -280,34 +285,98 @@
        //等待duration时间
        Integer duration = point.getDuration();
        Integer durationSecond = duration*60;
        final boolean[] threadStatus = {true};
        //采用线程的原因是非线程sleep后无法执行手动结束条件判断代码
        Thread t = new Thread(){
            @Override
            public void run() {
        try {
                    sleep(10000);
                    threadStatus[0] = false;
                } catch (InterruptedException e) {
                    interrupt();
                    log.warn("t1线程被打断");
                    //e.printStackTrace();
                }
            };
        };
        t.start();
        boolean statusFlag =true;
        //手动停止和测试点预设时间,1个条件成立就终止测试
        while(statusFlag && threadStatus[0]){
            //查询测试点状态是否被打断
            QueryWrapper<ExperimentPoint> wrapper = Wrappers.query();
            wrapper.select("status").eq("id",point.getId());
            ExperimentPoint currentPoint = pointMapper.selectOne(wrapper);
            if(currentPoint.getStatus()==3){
                statusFlag = false;
            }
        }
        log.warn("测试点结束");
        if (t.isAlive()) {
            t.interrupt();
        }
        /*try {
            //sleep(durationSecond);
            //演示需要,使用5s
            sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }*/
        pointMapper.updateEndTime(point.getId(),new Date());
        //返回这段时间的测试结果
        //TODO 这段时间内的数据,需要进行动态计算
        Map<String,String> resMap = new HashMap<>();
        int value = (int) (100 + Math.random() * 10);
        resMap.put("平均功率",value+"kW");
        setExperimentState(point.getExperimentId(),1);
        return new Response().set(1,resMap);
    }
    public void setExperimentState(String id,int status){
        Experiment experiment = mapper.selectById(id);
        experiment.setStatus(status);
        mapper.updateById(experiment);
    }
    public Response finishExperiment(String experimentId) {
        mapper.updateStatusAndTime(experimentId,2,"end_time",new Date());
        return new Response().setMsg(1,"状态更新成功");
    }
    public Response finishExperimentPoint(Integer id) {
        ExperimentPoint point = new ExperimentPoint();
        point.setId(id);
        //中断信号
        point.setStatus(3);
        pointMapper.updateById(point);
        return new Response().setMsg(1,"测试点中断信号更新成功");
    }
    public Response setPrecondition(Integer id) {
        switch (id){
            case 1:{
                commonMapper.setPrecondition("`db_3.5mw_motor`.tb_central_monitor_sys_rt","10001","switch_close","0");
            }
            case 2:{
            }
            case 3:{
            }
            case 4:{
            }
            case 5:{
            }
            case 6:{
            }
            case 7:{
            }
        }
        return new Response().setMsg(1,"设置完成");
    }
    /*======History======*/
    public Response<PageInfo<Experiment>> getPage(Integer pageNum, Integer pageSize, ExperimentConditionDTO condition) {