
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 = "执行失败";
									//已失败，不处理
								}	
							}
						}		
						console.log("=====pushStatMsg======requestState===="+$("#requestState").val()+"===tmpKey="+tmpKey+"=data[x].execState="+data[x].execState);
						//最后一个设备-特殊处理
						if(i==planDeviceRecord.length-1){
							execPlanStopStep(planDeviceModel);
							break;
						}
						var planDeviceModel_next = planDeviceRecord[i+1];
						execPlanNextStep(planDeviceModel,planDeviceModel_next);
						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("执行失败");
	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);
				break;
			}
			var planDeviceModel_next = planDeviceRecord[i+1];
			execPlanNextStep(planDeviceModel,planDeviceModel_next);
			break;
		}
	}
}
