function PlanPushlet(){}
PlanPushlet.init = function (){
	PlanPushlet();
	PlanPushlet.initPushLet();
}
//初始PUSHLET
PlanPushlet.initPushLet = function(){
	try {
		PL._init();
		PL.join();
		PL.listen();
		PL.subscribe('/trafficPlanData');
	} catch(e) {
		$.showMsg('Pushlet监听初始化失败! ');
		setTimeout("PlanPushlet.initPushLet()",1000);
	}
}
//接收PUSHLET数据
function onData(data) {
	var event = data.get("p_subject");
	var sid = data.get("p_sid");
	var text = data.get("json");
	if(text==""||text==null||text==undefined){
		return;
	}
	if(event == "/trafficPlanData") {
		PlanPushlet.pushStatMsg(eval(text));
		return;
	}
}
PlanPushlet.pushStatMsg = function(data){
	var currentDeviceStep = $('#currentDeviceStep').val();
	console.log(data);
	if(data&&data.length>0){
		for(var x=0;x<data.length;x++){
			if(data[x].deviceId&&data[x].deviceType){
				for(var i=0;i<PlanExec.planDeviceRecord.length;i++){
					var planDeviceModel = PlanExec.planDeviceRecord[i];					
					//根据AMQ返回的设备编号，设备类型，返回预案节点下设备的执行状态
					console.log(planDeviceModel.planType+"=pushlet="+data[x].deviceType+"=="+planDeviceModel.linkageDeviceId+"=="+data[x].deviceId);
					if(planDeviceModel.planType==data[x].deviceType&&planDeviceModel.linkageDeviceId==data[x].deviceId){
						var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
						if(data[x].execState=="3"){
							tmpexecState = "执行成功";
							$("#plan_linkage_statu_"+tmpKey).removeClass("state-doing");
							$("#plan_linkage_statu_"+tmpKey).addClass("state-success");	
						}else if(data[x].execState=="4"){
							tmpexecState = "执行失败";
							$("#plan_linkage_statu_"+tmpKey).removeClass("state-doing");
							$("#plan_linkage_statu_"+tmpKey).addClass("state-fail");	
						}	
						$('#currentDeviceState').val(tmpexecState);
						//设备执行返回插入日志
						var operateLog = {};
						operateLog.operateCommand = "执行返回";		
						operateLog.operateModule = planDeviceModel.typeName;
						operateLog.operateContent = '['+planDeviceModel.linkageDeviceName+']['+planDeviceModel.typeName+']'+tmpexecState+'.';
						operateLog.operateState = '成功';
						operateLog.deviceType = '';
						operateLog.deviceId = planDeviceModel.linkageDeviceId;
						operateLog.deviceName = planDeviceModel.linkageDeviceName;
						PlanExec.insert_trafficplanexeclog(operateLog);
						//当前在预案节点设备与AMQ返回设备一致时执行下一个设备节点
						var runType = data[x].createAppName;
						if(currentDeviceStep==tmpKey){
							$("#requestState").val("0");
							$("#requestTime").val("0");
							PlanRun.selectPlanNextStep(planDeviceModel,i,runType);
						}
						break;
					}
				}
			}
		}
	}
}
setTimeout('PlanPushlet.refreshCurrentPlan()',1000);  
//刷新查询   
PlanPushlet.refreshCurrentPlan = function(){
	if(!$("#requestState").val()){
		$("#requestState").val("0");
		$("#requestTime").val("0");
	}
	//判断请求返回，超过5秒结束任务，判断为失败
	if($("#requestState").val()=="0"){
		$("#requestTime").val("0");
	}
	if($("#requestState").val()=="1"){
		var resuestTime = $("#requestTime").val();
		if(isNaN(resuestTime)){
			resuestTime = 0;
		}	
		var currentExecStep = $('#currentExecStep').val();
		var currentExecLinkage = $('#currentExecLinkage').val();
		var currentDeviceStep = $('#currentDeviceStep').val();
		console.log("=定时刷新=["+resuestTime+"]["+currentExecStep+"]["+currentExecLinkage+"]["+currentDeviceStep+"]");
		resuestTime = parseInt(resuestTime)+parseInt(1);
		if(parseInt(resuestTime)>2){
			$("#requestState").val("0");
			$("#requestTime").val("0");
			//执行异常任务
			PlanPushlet.execPlanException();
			setTimeout('PlanPushlet.refreshCurrentPlan()',1000);
		}else{
			$("#requestTime").val(resuestTime);
			setTimeout('PlanPushlet.refreshCurrentPlan()',1000);
		}
	}else{
		setTimeout('PlanPushlet.refreshCurrentPlan()',1000);
	}
}  
//根据currentExecStep-currentDeviceStep——判断异常结果，进入下一个流程
PlanPushlet.execPlanException = function(){
	var currentExecStep = $('#currentExecStep').val();
	var currentExecLinkage = $('#currentExecLinkage').val();
	var currentDeviceStep = $('#currentDeviceStep').val();
	$('#currentDeviceState').val("执行失败");
	var runType = $("#currentExecType").val();
	for(var i=0;i<PlanExec.planDeviceRecord.length;i++){
		var planDeviceModel = PlanExec.planDeviceRecord[i];
		var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
		if(currentExecLinkage==planDeviceModel.linkageID&&(tmpKey)==currentDeviceStep){
			$("#plan_linkage_statu_"+tmpKey).removeClass("state-doing");
			$("#plan_linkage_statu_"+tmpKey).addClass("state-fail");	
			var operateLog = {};
			operateLog.operateCommand = "执行返回";		
			operateLog.operateModule = planDeviceModel.typeName;
			operateLog.operateContent = '['+planDeviceModel.linkageDeviceName+']['+planDeviceModel.typeName+']执行失败.';
			operateLog.operateState = '执行完成';
			operateLog.deviceType = '';
			operateLog.deviceId = planDeviceModel.linkageDeviceId;
			operateLog.deviceName = planDeviceModel.linkageDeviceName;
			PlanExec.insert_trafficplanexeclog(operateLog);
			PlanRun.selectPlanNextStep(planDeviceModel,i,runType);
			break;
		}
	}
}
//延时执行
PlanPushlet.planDelayTime = function(){
	var delayTime = $('#delayTime').val();
	var currentExecStep = $('#currentExecStep').val();
	if(parseInt(delayTime)<2){
		$('#delayTime').val("0");
		PlanRun.planRunStep(currentExecStep,"5");
		$('#plan_execModel_state_'+currentExecStep).html("延");
	}else{
		delayTime = parseInt(delayTime)-1;
		$('#delayTime').val(delayTime);
		$('#plan_execModel_state_'+currentExecStep).html(delayTime);
		setTimeout('PlanPushlet.planDelayTime()',1000);
	}
}