function PushletMonitorMovedData(){}
PushletMonitorMovedData.init = function(){
	PushletMonitorMovedData();
	PushletMonitorMovedData.initData();
	PushletMonitorMovedData.obj = {};
//	MonitorPresetPoint.init();
}

PushletMonitorMovedData.leave = function () {
	try {
		PL.leave(); 
	} catch(e) {
		console.log("Pushlet关闭失败!");
	}
}
PushletMonitorMovedData.initData = function (){
	try {
		PL._init();
		PL.join();
		PL.listen();
		PL.subscribe('/PresetPointData'); 
	} catch(e) {
		$.showMsg("Pushlet监听初始化失败!");
		setTimeout("PushletMonitorMovedData.initData()",10000);
	}
}
function onData(data) {
	var event = data.get("p_subject");
	var sid = data.get("p_sid");
	var text = data.get("json");
//	console.log("=========PresetPointData========="+text);
	if(!text){
		return;
	}
	if(event == "/PresetPointData") {
		PushletMonitorMovedData.pushStatMsg(eval(text));
		return;
		
		
	}
}

PushletMonitorMovedData.pushStatMsg = function (data){
	var len=data.length;
	console.log('===相机移位告警pushlet===',data);
	for(var i=0;i<len;i++){
		console.log(BaseDataYzd.monitorYzdMap.get(data[i].sbbh+"-"+(data[i].tdbh+1)))
		if(BaseDataYzd.monitorYzdMap.get(data[i].sbbh+"-"+(data[i].tdbh+1))){  //获取监控设备预置点位编号
			var sbbh = data[i].sbbh;
			var tdbh = data[i].tdbh+1;
			if(BaseDataYzd.monitorChooseObj.monitorMovedNotifyChoose=="1"){//amq接口发送相机移位告警信息
				if(data[i].zt==0){//复位
					$.ajax({
						type:'post',
						dataType : 'json',
						url:'sbtdspyzdAction!movedNotify.do',
						async:false,
						data:{
							"preset_index": BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).kz1,
						    "scene_is_valid": true,
						    "sbbh":sbbh,
						    "tdbh":tdbh
						    },
						success:function(res){
							console.log(res);
							if(res){
								$.ajax({
									type:'post',
									dataType : 'json',
									 async:false, 
									url:'sbtdspyzdAction!updateMonitorMovedCache.do',
									data:{sbbh:sbbh,tdbh:tdbh},
									success:function(res){
										$.showMsg(BaseDataYzd.monitorYzdMap.get(sbbh+"-"+(tdbh)).tdmc+"已复位！");
										$("#"+sbbh+"_"+(tdbh)).remove();
										if($("#monitorPreset .box ul:eq(0)").children().length<=0){
											$("#monitorPreset").hide();
										}
									}
								});
							}
						}
					});
				}else{
					var id = sbbh+"_"+tdbh;
					var flag = removeRepeatedMoved(id);
					if(flag){
						
						var li='<li id="'+sbbh+'_'+tdbh+'">'
						+'<span class="monitorPresetMsg">'+BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).tdmc+'移位已暂停告警</span>'
						+'<span class="monitorPresetRecovery" onclick="monitorPresetRecovery(\''+sbbh+'\',\''+tdbh+'\');">复位</span>'
						+'<span class="monitorPresetCancel" onclick="monitorPresetCancel(event,this);">取消</span></li>';
						$("#monitorPreset .box ul:eq(0)").append(li);
						
					}
					//通知算法
					$.ajax({
						type:'post',
						dataType : 'json',
						async:false, 
						url:'sbtdspyzdAction!movedNotify.do',
						data:{
							"preset_index": BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).kz1,
							"scene_is_valid": false,
							"sbbh":sbbh,
							"tdbh":tdbh
						},
						success:function(res){
							//alert(res);
						}
					});
					
				}
			}else{//算法推送相机告警移位信息
				if(data[i].zt==0){
					$.showMsg(BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).tdmc+"已复位！");
					$("#"+sbbh+"_"+parseInt(tdbh)).remove();
					if($("#monitorPreset .box ul:eq(0)").children().length<=0){
						$("#monitorPreset").hide();
					}
				}else{
					var id = sbbh+"_"+tdbh;
					var flag = removeRepeatedMoved(id);
					if(flag){
						
						var li='<li id="'+sbbh+'_'+tdbh+'">'
						+'<span class="monitorPresetMsg">'+BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).tdmc+'移位已暂停告警</span>'
						+'<span class="monitorPresetRecovery" onclick="monitorPresetRecovery(\''+sbbh+'\',\''+tdbh+'\');">复位</span>'
						+'<span class="monitorPresetCancel" onclick="monitorPresetCancel(event,this);">取消</span></li>';
						$("#monitorPreset .box ul:eq(0)").append(li);
					}
					//通知算法
					/*$.ajax({
							type:'post',
							dataType : 'json',
							url:'sbtdspyzdAction!movedNotify.do',
							data:{
								"preset_index": BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).kz1,
							    "scene_is_valid": false,
							    "sbbh":sbbh,
							    "tdbh":tdbh
							    },
							success:function(res){
								alert(res);
							}
						});*/
				}
			}
		}
	}
	$("#monitorPreset").show();
	if($("#monitorPreset .box ul:eq(0)").children().length<=0){
		$("#monitorPreset").hide();
	}
}
function onError(){
	setTimeout("PushletMonitorMovedData.initData()",1000);
}
function showPushletMsg(sbbh,tdbh){
	
}
var curPath = window.document.location.href;
var pathName = window.document.location.pathname;
var localhostPath = curPath.substring(0,curPath.indexOf(pathName));
var projectName = pathName.substring(0,pathName.substr(1).indexOf('/')+1);
var path = localhostPath+projectName+"/";


MonitorPresetPoint.serverConnection.onmessage = function(evt){
	MonitorPresetPoint.handleResponse(evt);
	if(MonitorPresetPoint.isCallPrepoint){
		setTimeout(function(){
			console.log(BaseDataYzd.monitorChooseObj.urlAndPort+'/scene');
			$.ajax({
				type:'post',
				dataType : 'json',
				url:'sbtdspyzdAction!movedNotify.do',
				 async:false, 
				data:{
					"preset_index": PushletMonitorMovedData.obj.presetNum,
				    "scene_is_valid": true,
				    "sbbh":PushletMonitorMovedData.obj.sbbh,
				    "tdbh":PushletMonitorMovedData.obj.tdbh
				    },
				success:function(res){
					console.log(res);
					if(res){
						$.ajax({
							type:'post',
							dataType : 'json',
							 async:false, 
							url:'sbtdspyzdAction!updateMonitorMovedCache.do',
							data:{sbbh:PushletMonitorMovedData.obj.sbbh,tdbh:PushletMonitorMovedData.obj.tdbh},
							success:function(res){
								$("#"+PushletMonitorMovedData.obj.sbbh+"_"+parseInt(PushletMonitorMovedData.obj.tdbh)).remove();
								if($("#monitorPreset .box ul:eq(0)").children().length<=0){
									$("#monitorPreset").hide();
								}
								$.showMsg(BaseDataYzd.monitorYzdMap.get(PushletMonitorMovedData.obj.sbbh+"-"+PushletMonitorMovedData.obj.tdbh).tdmc+"已复位！");
							}
						});
					}
				}
			});
		},3000);
	}
}

function  monitorPresetCancel(event,obj){
	$(obj).parents("li").remove();
	if($("#monitorPreset .box ul:eq(0)").children().length<=0){
		$("#monitorPreset").hide();
	}
	event.cancelBubble=true;
}
//消息弹窗中，复位操作
function  monitorPresetRecovery(sbbh,tdbh){
	if(BaseDataYzd.monitorChooseObj.monitorMovedNotifyChoose=="1"){//amq推送移位告警信息
		if(sbbh && tdbh){
			$.ajax({
				type:'post',
				dataType : 'json',
				 async:false, 
				url:'sbtdspyzdAction!get_SbtdspyzdOne.do',
				data:{sbbh:sbbh,tdbh:tdbh},
				success:function(res){
					if(res){
						PushletMonitorMovedData.obj.sbbh=res.sbbh;
						PushletMonitorMovedData.obj.tdbh=res.tdbh;
						PushletMonitorMovedData.obj.presetNum=res.kz1;
						MonitorPresetPoint.callPrepoint(PushletMonitorMovedData.obj);
					}
				}
			});
		}
	}else{//算法推送移位告警信息
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'sbtdspyzdAction!movedNotify.do',//调用算法接口，通知算法相机已复位
			data:{
				"preset_index": BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).kz1,
			    "scene_is_valid": true,
			    "sbbh":sbbh,
			    "tdbh":tdbh
			    },
			success:function(res){
				console.log(res);
				if(res){
					$.ajax({
						type:'post',
						dataType : 'json',
						url:'sbtdspyzdAction!updateMonitorMovedCache.do',//操作缓存中相机状态信息
						data:{sbbh:sbbh,tdbh:tdbh},
						 async:false, 
						success:function(res){
							$.showMsg(BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).tdmc+"已复位！");
							$("#"+sbbh+"_"+tdbh).remove();
							if($("#monitorPreset .box ul:eq(0)").children().length<=0){
								$("#monitorPreset").hide();
							}
						}
					});
				}
			}
		});
		
		
		
		/*
		$.post(BaseDataYzd.initMonitorChoose().urlAndPort+'/scene1?video_id='+sbbh+"_"+(parseInt(tdbh)-1),{
		    "preset_index": BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).kz1,
		    "scene_is_valid": true
		},function(res,xhr){
			if(res=="200"){
				$.ajax({
					type:'post',
					dataType : 'json',
					url:'sbtdspyzdAction!updateMonitorMovedCache.do',
					data:{sbbh:sbbh,tdbh:tdbh},
					success:function(res){
					}
				});
				$.showMsg(BaseDataYzd.monitorYzdMap.get(sbbh+"-"+tdbh).tdmc+"已复位！");
				$("#"+sbbh+"_"+tdbh).remove();
			}
		});*/
	}
	
	
}
//判断是否有移位告警
function removeRepeatedMoved(id){
	var flag = true;
	$("#monitorPreset .box ul:eq(0) li" ).each(function(){
		var idO = $(this).attr("id");
		if(idO==id){
			flag = false;
			return flag;
		}
	});
	return flag;
}