
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 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;
							insert_trafficplanexeclog(operateLog);
							break;
						}
						var runType = data[x].createAppName;
						console.log("=====pushStatMsg======requestState===="+$("#requestState").val()+"===tmpKey="+tmpKey+"=data[x].execState="+data[x].execState);
						selectPlanNextStep(planDeviceModel,i,runType);
						/*
						//最后一个设备-特殊处理
						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;
						*/
					}
				}
			}
		}
	}
}
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");	
			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;
			insert_trafficplanexeclog(operateLog);
			selectPlanNextStep(planDeviceModel,i,runType);
			/*
			//最后一个设备-特殊处理
			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(){
	var currentExecStep = planExecRecord.length;
	$('#currentExecStep').val(currentExecStep);
	$(".block").removeClass("active");
	$("#block"+ currentExecStep +"").addClass("active");
	$("#planState").val("0");
	console.log("=====execPlanStopStep=======");
}
function selectPlanNextStep(planDeviceModel,deviceNum,runType){
	if(deviceNum==planDeviceRecord.length-1){
		//最后一个设备
		execPlanNextStep(runType);
	}else{
		var planDeviceModel_next = planDeviceRecord[deviceNum+1];
		if(planDeviceModel.linkageID==planDeviceModel_next.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);
		}
	}
}
//延时执行
function planDelayTime(){
	var delayTime = $('#delayTime').val();
	var currentExecStep = $('#currentExecStep').val();
	if(parseInt(delayTime)<2){
		$('#delayTime').val("0");
		planExecStep(currentExecStep,"5");
		$('#plan_execModel_state_'+currentExecStep).html("延");
	}else{
		delayTime = parseInt(delayTime)-1;
		$('#delayTime').val(delayTime);
		$('#plan_execModel_state_'+currentExecStep).html(delayTime);
		setTimeout('planDelayTime()',1000);
	}
}