| | |
| | | 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; |
| | | |
| | |
| | | import static java.lang.Thread.sleep; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class ExperimentService { |
| | | |
| | | @Resource |
| | |
| | | private CommonMapper commonMapper; |
| | | |
| | | public Response exist() { |
| | | QueryWrapper<Experiment> wrapper = Wrappers.query(); |
| | | wrapper.eq("status",1); |
| | | List<Experiment> experiments = mapper.selectList(wrapper); |
| | | ExperimentConditionDTO condition = new ExperimentConditionDTO(); |
| | | condition.setStatus(1); |
| | | List<Experiment> experiments = mapper.getList(condition); |
| | | //不存在试验 |
| | | if (experiments.isEmpty()){ |
| | | return new Response().set(-1,"暂无试验"); |
| | | return new Response().setMsg(-1,"暂无试验"); |
| | | } |
| | | Experiment experiment = experiments.get(0); |
| | | String url = null; |
| | |
| | | }else{ |
| | | url ="不存在呀"; |
| | | } |
| | | return new Response().set(1,url); |
| | | return new Response().set(1,experiments.get(0),url); |
| | | } |
| | | |
| | | /** |
| | |
| | | //2种方式 |
| | | //m1 |
| | | QueryWrapper<ExperimentPrecondition> wrapper = Wrappers.query(); |
| | | wrapper.eq("type",type); |
| | | wrapper.eq("type",type).eq("flag",1); |
| | | List<ExperimentPrecondition> preconditions = preconditionMapper.selectList(wrapper); |
| | | preconditions.stream().forEach(precondition->{ |
| | | //获取对应的值 |
| | |
| | | /** |
| | | * 检查中置条件-当前功率和当前定子温度,如果符合标准,返回当前值 |
| | | */ |
| | | public Response checkPreconditionStep1(String type){ |
| | | public Response checkPreconditionStep1(String experimentId){ |
| | | String type = experimentId.split("_")[0].toLowerCase(); |
| | | QueryWrapper<ExperimentPrecondition> wrapper = Wrappers.query(); |
| | | wrapper.eq("type",type+"_1"); |
| | | List<ExperimentPrecondition> preconditions = preconditionMapper.selectList(wrapper); |
| | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //设置试验整体状态为1,启动时间更新 |
| | | mapper.updateStatusAndTime(experimentId,1,"start_time",new Date()); |
| | | return new Response().set(1,preconditions); |
| | | } |
| | | |
| | |
| | | QueryWrapper<ExperimentPoint> wrapper = Wrappers.query(); |
| | | wrapper.eq("experiment_id",experimentId); |
| | | List<ExperimentPoint> experimentPoints = pointMapper.selectList(wrapper); |
| | | /*//TODO 针对已完成的测试点,计算出这段时间内的平均功率并返回,需要考虑是否将功率字段持久化到表中 |
| | | experimentPoints.stream().forEach(point -> { |
| | | if(point.getStatus()==2){ |
| | | //计算出平均功率并赋值 |
| | | int power = (int) (100 + Math.random() * 10); |
| | | point.setAveragePower(power); |
| | | } |
| | | });*/ |
| | | return new Response<List<ExperimentPoint>>().set(1,experimentPoints); |
| | | } |
| | | |
| | |
| | | |
| | | /** |
| | | * 启动测试点,并在测试时长过后,返回测试结果 |
| | | * 测试点的结束,结束分两种情况: |
| | | * 情况一:经过预定的测试时长,停止 |
| | | * 情况二:更新了数据库的某个字段,代码循环判断这个字段,如果为2则停止实验并返回结果 |
| | | * @return |
| | | */ |
| | | public Response startExperimentPoint(ExperimentPoint point){ |
| | |
| | | //等待duration时间 |
| | | Integer duration = point.getDuration(); |
| | | Integer durationSecond = duration*60; |
| | | try { |
| | | final boolean[] threadStatus = {true}; |
| | | //采用线程的原因是非线程sleep后无法执行手动结束条件判断代码 |
| | | Thread t = new Thread(){ |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | sleep(durationSecond*1000); |
| | | 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()); |
| | | //返回这段时间的测试结果 |
| | | Map<String,String> resMap = new HashMap<>(); |
| | | //TODO 这段时间内的数据,需要进行动态计算 |
| | | int value = (int) (100 + Math.random() * 10); |
| | | resMap.put("平均功率",value+"kW"); |
| | | setExperimentState(point.getExperimentId(),1); |
| | | return new Response().set(1,resMap); |
| | | ExperimentPoint pointTemp = new ExperimentPoint(); |
| | | pointTemp.setId(point.getId()); |
| | | pointTemp.setAveragePower(value); |
| | | pointMapper.updateById(pointTemp); |
| | | return new Response().setMsg(1,"测试点结束"); |
| | | } |
| | | |
| | | 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 restartExperimentPoint(Integer id) { |
| | | ExperimentPoint point = new ExperimentPoint(); |
| | | point.setId(id); |
| | | point.setStatus(0); |
| | | point.setStartTime(null); |
| | | point.setEndTime(null); |
| | | pointMapper.updateById(point); |
| | | return new Response().setMsg(1,"重置测试点成功"); |
| | | } |
| | | |
| | | public Response setPrecondition(Integer id,Integer value) { |
| | | switch (id){ |
| | | case 1:{ |
| | | commonMapper.setPrecondition("`db_3.5mw_motor`.tb_central_monitor_sys_rt","10001","switch_close",value.toString()); |
| | | break; |
| | | } |
| | | case 2:{ |
| | | commonMapper.setPrecondition("`db_3.5mw_motor`.tb_rectifier_power_rt","30001","rectifier_vol",value.toString()); |
| | | break; |
| | | } |
| | | case 3:{ |
| | | commonMapper.setPrecondition("`db_3.5mw_motor`.tb_central_monitor_sys_st","10005","vol_a",value.toString()); |
| | | break; |
| | | } |
| | | case 4:{ |
| | | commonMapper.setPrecondition("`db_3.5mw_motor`.tb_central_monitor_sys_st","10005","vol_b",value.toString()); |
| | | break; |
| | | } |
| | | case 5:{ |
| | | commonMapper.setPrecondition("`db_3.5mw_motor`.tb_device_state","40001","comm_status",value.toString()); |
| | | break; |
| | | } |
| | | case 6:{ |
| | | commonMapper.setPrecondition("`db_3.5mw_motor`.tb_device_state","40002","comm_status",value.toString()); |
| | | break; |
| | | } |
| | | case 7:{ |
| | | commonMapper.setPrecondition("`db_3.5mw_motor`.tb_device_state","50001","comm_status",value.toString()); |
| | | break; |
| | | } |
| | | } |
| | | return new Response().setMsg(1,"设置完成"); |
| | | } |
| | | |
| | | /*======History======*/ |
| | | |
| | | public Response<PageInfo<Experiment>> getPage(Integer pageNum, Integer pageSize, ExperimentConditionDTO condition) { |
| | | condition.setStatus(2); |
| | | PageHelper.startPage(pageNum,pageSize); |
| | | List<Experiment> experimentList = mapper.getPage(condition); |
| | | List<Experiment> experimentList = mapper.getList(condition); |
| | | //查询结果的duration是分钟,转换为 x时x分格式 |
| | | experimentList.stream().forEach(experiment -> { |
| | | int duration = Integer.parseInt(experiment.getDuration()); |