package com.nms.server.thread.mission;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.nms.server.bean.MissionResult;
import com.nms.server.bean.MissionStateTable;
import com.nms.server.bean.Task4;
import com.nms.server.util.ExceptionPrintUtils;
import com.nms.server.util.socket.SSLSocketCallable;

public class NMSClientTask4 extends SSLSocketCallable{
	private final Logger logger = Logger.getLogger(NMSClientTask4.class);
	private Task4 task4 ;
	private MissionStateTable mission;
	private MissionResult result;
	public NMSClientTask4(String ip,int port,MissionStateTable mission,Task4 object, MissionResult result)  throws Exception{
		super(ip,port);
		this.task4 = object;
		this.mission = mission;
		this.result = result;
	}

	@Override
	protected String toDo() throws Exception {
		Thread.currentThread().setName("Task 4:> "+mission.getMissionId()+" IP:> "+this.socket.getRemoteSocketAddress());
		try {
			
			//任务正常下发
			task4.setTaskId(mission.getMissionId());
			task4.setTaskType(mission.getMissionType());
			task4.setStartTime(mission.getStartTime()==null?null:mission.getStartTime().getTime());
			task4.setEndTime(mission.getEndTime()==null?null:mission.getEndTime().getTime());
			task4.setState(result.getResult());
			task4.setIsLoop(mission.getLoopFlag());
			task4.setLoopDelay(mission.getLoopDelay());
			task4.setMissionState(mission.getMissionState());
			JSONObject object = new JSONObject();
			object.put("typeInfo", mission.getMissionType());
			object.put("taskInfo", task4);
			logger.debug("JSONObject "+object.toString());
			this.sendMessage("char:task");
			this.receiveMessage();
			this.sendMessage(object.toString());
			String string =  this.receiveMessage();
			logger.debug("任务执行下发结果是 "+string);
			this.sendMessage(SUCCESS);
			return string;

		} catch (Exception e) {
			logger.error(ExceptionPrintUtils.printExceptionStack(e));
			return null;
		}
	}
	/*
	public static void main(String []args){
		CommonDao dao = new CommonDao();			//数据库链接
		UpgradeService service = new UpgradeService(dao);	//专属Service
		try {
			
			Task4 task4 = service.getTask4ById(1274540l);		//查询任务具体信息
			MissionStateTable table = service.getMessionById(1274540l);
			List<MissionResult> list =  service.getMissionResultsForTask(1274540l, 4);
			MissionResult result = list.get(0);
			result.setResult(3l);
			Common.service.submit(new NMSClientTask4("10.0.6.242",60701,table,task4, result));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}
