
function initPushLet(){
	try {
		PL._init();
		PL.join();
		PL.listen();
		PL.subscribe('/trafficPlanData');
	} catch(e) {
		$.showMsg('Pushlet监听初始化失败! ');
		setTimeout("initPushLet()",1000);
	}
}
//接收数据
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") {
		pushStatMsg(eval(text));
		return;
	}
}
function pushStatMsg(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<planDeviceRecord.length;i++){
					var planDeviceModel = planDeviceRecord[i];					
					//找到设备ID的信息
					if(planDeviceModel.planType==data[x].deviceType&&planDeviceModel.linkageDeviceId==data[x].deviceId){
						var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
						if($("#plan_linkage_statu_"+tmpKey)){
							var tmpexecState = "";
							/*
							if(data[0].execState=="1"){
								tmpexecState = "未执行";
							}else if(data[0].execState=="2"){
								tmpexecState = "执行中";
							}else
							*/
							var plan_linkage_statu_class = $("#plan_linkage_statu_"+tmpKey).attr('class');
							if(plan_linkage_statu_class.indexOf("state-doing")!=-1){
								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);
								}	
								$("#requestState").val("0");
								$("#requestTime").val("0");
							}else if(plan_linkage_statu_class.indexOf("state-fail")!=-1){
								//
								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 = "执行失败";
									//已失败，不处理
								}	
							}
						}		
						var runType = data[x].createAppName;
						console.log("=====pushStatMsg======requestState===="+$("#requestState").val()+"===tmpKey="+tmpKey+"=data[x].execState="+data[x].execState);
						//最后一个设备-特殊处理
						if(i==planDeviceRecord.length-1){
							execPlanStopStep(planDeviceModel,runType);
							break;
						}
						var planDeviceModel_next = planDeviceRecord[i+1];
						//selectPlanNextStep(planDeviceModel,planDeviceModel_next,runType);
						//execPlanNextStep(planDeviceModel,planDeviceModel_next,data[x].createAppName);
						if(planDeviceModel_next.linkageID==planDeviceModel.linkageID){
							var tmpKey = "_"+planDeviceModel_next.linkageID+"_"+planDeviceModel_next.planType+"_"+planDeviceModel_next.execOrder;
							$('#currentExecLinkage').val(planDeviceModel_next.linkageID);
							$('#currentDeviceStep').val(tmpKey);
							planDeviceStep(planDeviceModel_next,runType);
						}else{
							execPlanNextStep(runType);
						}	
						break;
					}
				}
			}
		}
	}
}
//setInterval('refreshCurrentPlan()',1000);  
setTimeout('refreshCurrentPlan()',1000);  
/* 刷新查询 */  
function refreshCurrentPlan(){
	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();
		console.log("=====refreshCurrentPlan======resuestTime===="+resuestTime);
		resuestTime = parseInt(resuestTime)+parseInt(1);
		if(parseInt(resuestTime)>8){
			$("#requestState").val("0");
			$("#requestTime").val("0");
			//执行异常任务
			execPlanException();
			setTimeout('refreshCurrentPlan()',1000);
		}else{
			$("#requestTime").val(resuestTime);
			setTimeout('refreshCurrentPlan()',1000);
		}
	}else{
		setTimeout('refreshCurrentPlan()',1000);
	}
}  
//根据currentExecStep-currentDeviceStep——判断异常结果，进入下一个流程
function execPlanException(){
	var currentExecStep = $('#currentExecStep').val();
	var currentExecLinkage = $('#currentExecLinkage').val();
	var currentDeviceStep = $('#currentDeviceStep').val();
	$('#currentDeviceState').val("执行失败");
	var runType = $("#currentExecType").val();
	for(var i=0;i<planDeviceRecord.length;i++){
		var planDeviceModel = planDeviceRecord[i];
		var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
		if(currentExecLinkage==planDeviceModel.linkageID&&(tmpKey)==currentDeviceStep){
			//$("#plan_linkage_statu_"+tmpKey).html("执行失败");
			$("#plan_linkage_statu_"+tmpKey).removeClass("state-doing");
			$("#plan_linkage_statu_"+tmpKey).addClass("state-fail");	
			//最后一个设备-特殊处理
			if(i==planDeviceRecord.length-1){
				execPlanStopStep(planDeviceModel,runType);
				break;
			}
			var planDeviceModel_next = planDeviceRecord[i+1];
			//var runType = "1";//确认执行
			if(planDeviceModel_next.linkageID==planDeviceModel.linkageID){
				var tmpKey = "_"+planDeviceModel_next.linkageID+"_"+planDeviceModel_next.planType+"_"+planDeviceModel_next.execOrder;
				$('#currentExecLinkage').val(planDeviceModel_next.linkageID);
				$('#currentDeviceStep').val(tmpKey);
				planDeviceStep(planDeviceModel_next,runType);
			}else{
				execPlanNextStep(runType);
			}			
			break;
		}
	}
}
//执行最后一个
function execPlanStopStep(planDeviceModel,runType){
	var currentDeviceState = $('#currentDeviceState').val();
	var currentExecStep = $('#currentExecStep').val();
	if(currentDeviceState=="执行失败"){
		$("#plan_exec_zt_"+currentExecStep).html("<span class='icon-state icon-fail' ></span>执行失败");
		$('#currentDeviceState').val("");
	}else{
		$("#plan_exec_zt_"+currentExecStep).html("<span class='icon-state icon-success' ></span>执行成功");
	}
	//到最后，先判断一下，是否还有待执行任务
	var hasPlan = false;
	for(var zz=0;zz<planExecRecord.length;zz++){
		var tmp_step_num = parseInt(zz)+1;
		var tmp_html = $('#plan_exec_zt_'+tmp_step_num).html();
		if($("#btn_confirm_step_"+tmp_step_num)&&tmp_html.indexOf("等待中")!=-1){
			//
			$('#currentExecStep').val(tmp_step_num);
			for(var i=0;i<planDeviceRecord.length;i++){
				var planDeviceModel = planDeviceRecord[i];
				//同一节点下
				if(planDeviceModel.linkageID==planExecRecord[tmp_step_num-1].linkageID){
					var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
					$('#currentExecLinkage').val(planDeviceModel.linkageID);
					$('#currentDeviceStep').val(tmpKey);
					break;
				}
			}
			hasPlan = true;
			planExecStep(tmp_step_num,runType);
			break;
		}
	}
	if(!hasPlan){
		var currentExecStep = planExecRecord.length;
		$('#currentExecStep').val(currentExecStep);
		$(".block").removeClass("active");
		$("#block"+ currentExecStep +"").addClass("active");
	}
	console.log("=====execPlanStopStep=======");
}
