
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){
	//console.log(data[0]);
	if(data&&data.length>0){
		//console.log(data[0].deviceId);
		if(data[0].deviceId&&data[0].deviceType){
			for(var i=0;i<planDeviceRecord.length;i++){
				var planDeviceInfo = planDeviceRecord[i];
				//console.log(planDeviceInfo);
				if(planDeviceInfo.planType==data[0].deviceType&&planDeviceInfo.linkageDeviceId==data[0].deviceId){
					var tmpkey = "_"+planDeviceInfo.linkageID+"_"+planDeviceInfo.planType+"_"+planDeviceInfo.execOrder;
					if($("#plan_linkage_statu_"+tmpkey)){
						var tmpexecState = "";
						/*
						if(data[0].execState=="1"){
							tmpexecState = "未执行";
						}else if(data[0].execState=="2"){
							tmpexecState = "执行中";
						}else
						*/
						if(data[0].execState=="3"){
							tmpexecState = "执行成功";
						}else if(data[0].execState=="4"){
							tmpexecState = "执行失败";
							$('#currentDeviceState').val(tmpexecState);
						} 
						$("#plan_linkage_statu_"+tmpkey).html(tmpexecState);	
					}
					resuestTime = 0;
					$("#requestState").val("0");
					if(i==planDeviceRecord.length-2){
						break;
					}
					var tmp_planDeviceInfo = planDeviceRecord[i+1];
					execPlanNextStep(planDeviceInfo,tmp_planDeviceInfo);
					break;
				}
			}
		}
	}
	//
}
setInterval('refreshCurrentPlan()',1000);   
/* 刷新查询 */  
var resuestTime = 0;
function refreshCurrentPlan(){
	if(!$("#requestState").val()){
		$("#requestState").val("0");
	}
	//判断请求返回，超过5秒结束任务，判断为失败
	if($("#requestState").val()=="0"){
		resuestTime = 0;
	}
	if($("#requestState").val()=="1"){
		console.log("=====refreshCurrentPlan======resuestTime===="+resuestTime);
		resuestTime = parseInt(resuestTime)+parseInt(1);
		if(resuestTime>8){
			//执行异常任务
			execPlanRunException();
		}
	}
}  
//根据currentExecStep-currentDeviceStep——判断异常结果，进入下一个流程
function execPlanRunException(){
	var currentExecStep = $('#currentExecStep').val();
	var currentExecLinkage = $('#currentExecLinkage').val();
	var currentDeviceStep = $('#currentDeviceStep').val();
	//tmpexecState = "执行失败";
	$('#currentDeviceState').val("执行失败");
	for(var i=0;i<planDeviceRecord.length;i++){
		var planDeviceInfo = planDeviceRecord[i];
		//console.log(planDeviceInfo);
		if(i==planDeviceRecord.length-2){
			break;
		}
		if(currentExecLinkage==planDeviceInfo.linkageID&&(planDeviceInfo.linkageID+"_"+planDeviceInfo.execOrder)==currentDeviceStep){
			var tmpkey = "_"+planDeviceInfo.linkageID+"_"+planDeviceInfo.planType+"_"+planDeviceInfo.execOrder;
			console.log("===execPlanRunExceptionexecPlanRunException==refreshCurrentPlan====currentExecStep===="+currentExecStep+"=currentDeviceStep="+currentDeviceStep+"==tmpkey=="+tmpkey+"=currentExecLinkage="+currentExecLinkage);
			$("#plan_linkage_statu_"+tmpkey).html("执行失败");
			var tmp_planDeviceInfo = planDeviceRecord[i+1];
			//自动
			if(tmp_planDeviceInfo.execModel=="0"){
				execPlanNextStep(planDeviceInfo,tmp_planDeviceInfo);
			}
		}
	}
	resuestTime = 0;
	$("#requestState").val("0");
}
//执行下一个流程
function execPlanNextStep(planDeviceInfo,tmp_planDeviceInfo){
	console.log("===execPlanNextStep==start=tmp_planDeviceInfo.linkageID="+tmp_planDeviceInfo.linkageID);
	$('#currentExecLinkage').val(tmp_planDeviceInfo.linkageID);
	if(tmp_planDeviceInfo.linkageID==planDeviceInfo.linkageID){
		//本任务还有下一个
		$('#currentDeviceStep').val(tmp_planDeviceInfo.linkageID+"_"+tmp_planDeviceInfo.execOrder);
		planExec(tmp_planDeviceInfo.linkageID,"1");
	}else{
		//进入下一个STEP
		var currentExecStep = $('#currentExecStep').val();
		console.log("==$('#currentDeviceState').val()=="+$('#currentDeviceState').val());
		if($('#currentDeviceState').val()=="执行失败"){
			$("#plan_exec_zt_"+currentExecStep).html("执行失败");
		}
		currentExecStep = parseInt(1)+parseInt(currentExecStep);
		//console.log($('#currentExecStep').val()+"======currentExecStep==="+currentExecStep);
		$('#currentExecStep').val(currentExecStep);
		$('#currentDeviceState').val("");
		$('#currentDeviceStep').val(tmp_planDeviceInfo.linkageID+"_"+tmp_planDeviceInfo.execOrder);
		planExec(tmp_planDeviceInfo.linkageID,"1");
	}
}