1. 程式人生 > >給原有介面新增一個停止訓練功能

給原有介面新增一個停止訓練功能

在介面增加一個“停止訓練”按鈕

<td  class="center">
  <a href='/trainlog/setlist?eq_id=${entity.setId}&eq_orgId=${entity.orgId}'>檢視日誌</a>
  <a  class="btn btn-minier" href="#" onclick="stop('${entity.id}')"  >停止訓練</a>
</td>

點選“停止訓練”,進到stop方法裡,傳遞id作為引數

function stop(id){
                     $.ajax({
                         type: 
"POST", url: '/traintask/stoptask', data: "id="+id, async: false, success: function(msg) { layer.msg(msg.msg, { icon:
1, time: 1000 //2秒關閉(如果不配置,預設是3秒) }, function(){ $("#listForm").submit(); }); } }); }
在對應路徑下建方法 url: '/traintask/stoptask'
@Controller
@RequestMapping("/traintask")
public class TrainTaskController extends BaseController{
    @Autowired
    private TrainTaskService trainTaskService;
 
  /**
     * 停止訓練任務
     * @param id
     * @param request
     * @return
     */
    @ResponseBody 
    @RequestMapping(value="/stoptask",produces = "application/json; charset=utf-8",method={RequestMethod.POST,RequestMethod.GET})
    public Object stopTask(String id,HttpServletRequest request){   
        int flag = trainTaskService.stopTask(id);
        return  genRetCommonMsg(flag,0,request);
    }
}

controller建立stopTask方法實現停止訓練,將方法放進service裡,便於其他controller呼叫

public interface TrainTaskService extends BaseService<TrainTask,TrainTaskFilter> {
        ...
    public int stopTask (String id);
}

方法寫進實現類裡

@Service
public class TrainTaskServiceImpl extends BaseServiceImpl<TrainTask,TrainTaskFilter> implements TrainTaskService {
    @Autowired
    TagTypeService tagTypeService;
    @Autowired
    TrainLogService trainLogService;
    @Autowired
    TrainTaskService trainTaskService;


...

    //重構後清空日誌
                TrainLogFilter filter = new TrainLogFilter();
                filter.setEq_setId(setId);
                List<TrainLog> trainLogs = trainLogService.selectAll(filter);
                for(TrainLog trainLog : trainLogs){
                    trainLog.setDelFlag(0);
                    trainLogService.save(trainLog);
                }

...
       @Transactional
       public int stopTask(String id){
           TrainTask task = trainTaskService.getById(id);
           int flag=0;
           //檢視是否處於訓練中,
           //處於訓練中,停掉客戶端,clientState記為1,訓練停止後開啟客戶端
           //未訓練中,clientState仍為0,不需停止客戶端
           int clientState =0;//      
           String clientId="";

           //停掉客戶端
           if(task != null && task.getDelFlag() != 0 && task.getStates() == 1){
               clientState=1;
               TrainClientFilter filter = new TrainClientFilter();
               filter.setLk_ip(task.getTrainNum().split("-")[0]);
               filter.setEq_number(task.getTrainNum().split("-")[1]);
               filter.setEq_delFlag(1);
               TrainClient client = trainClientService.selectAll(filter).get(0);
               clientId = client.getId();
               
               trainClientService.addtask(clientId, "shutdown");
           }           
           //TagType表取到setid後,改completestates為0
           String setId = task.getSetId();
           TagType set = tagTypeService.getById(setId);
           if(set != null && set.getCompleteStates() != 0){
               set.setCompleteStates(0);                   
           }else {
            System.out.println("已是停止訓練狀態");
           }
           task.setDelFlag(0);
       
           tagTypeService.save(set);
           trainTaskService.save(task);

           if(set.getCompleteStates()==0 && task.getDelFlag()==0){
               flag=1;
           }
           
           return flag;
       }
}

還有重構時再加上清空日誌