nvsencoders.js 8.22 KB

var layer = null;
var g_hasRenderEncodersTable = false;


///渲染查询结果表格
function reRenderEncodersTable(dataArray) {
	var table = layui.table;
	var configObject = {
		elem: '#encodersTableData'
		,skin: 'row ' //行边框风格
		,size: 'bg' //小尺寸的表格
		,data:dataArray
		,title: '编码器设备表'
		,toolbar: '#toolbarDemo' //开启头部工具栏,并为其绑定左侧模板
		,defaultToolbar: ['filter', 'exports', 'print']
		,id:"encodersTableDataId"
		,text: {
			none: '暂无相关数据' //默认:无数据。注:该属性为 layui 2.2.5 开始新增	
		}
		,cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
		,cols: [[
		   {type:'radio'}
		  ,{field:'id', title:'ID',width:160,align:'center',sort: true}
		  ,{field:'name', title:'名字',align:'left',sort: true}
		  ,{field:'model', title:'型号',width:120,align:'center'}
		  ,{field:'ip', title:'IP',width:120,align:'center'}
		  ,{field:'port', title:'端口',width:80,align:'center'}
		  ,{field:'username', title:'用户名',width:190,align:'center'}
		  ,{field:'password', title:'密码',width:80,align:'center'}
		  ,{field:'nums', title:'通道数',width:80,align:'center'}
		  ,{field:'address', title:'别名ID',width:190,align:'center'}
		  ,{field:'status', title:'状态',sort: true,width:80,align:'center'}
		  ,{field:'desc2', title:'描述信息',width:190,align:'center'}
		]]
		,page: true
		,done: function (res, curr, count) {
            $('tr').css({'background-color': '#009688', 'color': '#fff'});//表头样式修改

			 var that = this.elem.next();
            
            res.data.forEach(function (item, index) {
                if (item.status === "离线") {
                  var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                  tr.css({ 'color': 'red'});
                } 
            });
        }
	};
		
		
	if(g_hasRenderEncodersTable) {
		table.reload('encodersTableDataId',{data:dataArray});
	} else {
		table.render(configObject);
		g_hasRenderEncodersTable = true;
	}
}



function handleEncoderArray(encoderList){
	
	var dataArray = new Array(); 
	for (var i = 0; i < encoderList.length; i++) {
		
		var obj = encoderList[i];
		
		var strstatus = "";
		if(obj.status == "1") {
			strstatus = "在线";
		} else {
			strstatus = "离线";
		}
					
		
		var item = {"id":obj.id,"name":obj.name,"model":obj.type,"ip":obj.ip,"port":obj.port,"username":obj.username,"password":obj.password,"nums":obj.channelnum,"address":obj.address,"status":strstatus,"desc2":obj.description};
		dataArray.push(item);
	}
	
	reRenderEncodersTable(dataArray);
}


function getEncoderDeviceList(){
	
	var xhr = $.ajax({
		url: "/nvsthird/getencoderlist",
		type: "post",
		async: false,
		dataType: "json",
		contentType: "application/json",
		data: JSON.stringify({
			token: "",
			fromindex:0,
			toindex:-1
		}),
		timeout: 10000,
		success: function(res) {
			if (res.group != null){
				var objToStr = JSON.stringify(res.group);
				//console.log('tempData:'+objToStr); 
				setKeyValueToCache("getencoderlist", objToStr);
				
				handleEncoderArray(res.group);
				
			} else {
				layer.msg('获取设备列表失败',{icon: 2, title:'获取列表',time: 2000,skin: 'layer-ext-moon' });
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			layer.msg('获取设备列表超时:' + errorThrown, {icon: 2, title:'获取列表',time: 2000,skin: 'layer-ext-moon' });  
			return false;
		},
		complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
    	if(status == 'timeout'){//超时,status还有success,error等值的情况
      	 	xhr.abort();
    	} else if(status == 'error') {
				xhr.abort();
				layer.msg('获取设备列表失败', {icon: 2, time: 2000,title:'获取列表',skin: 'layer-ext-moon' });
			}
  	}
	});
}



function  handleSetEncoderChannelNums(nvsurl){
	var timeoutDefaultValue = 60 * 1000;
	
	var xhr = $.ajax({
		url: nvsurl,
		type: "get",
		async: true,
		dataType: "json",
		contentType: "application/json",
		timeout: timeoutDefaultValue,
		success: function(res) {	
		
		   if(res.errormsg === "OK") {
			   layer.msg('设置成功',{icon: 1, title:'修改设备通道数',time: 2000,skin: 'layer-ext-moon' });
		   } else {
			  layer.msg('设置失败', {icon: 2, time: 2000,title:'修改设备通道数记',skin: 'layer-ext-moon' });
		   }
		   
			getEncoderDeviceList();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			layer.msg('设置超时:' + errorThrown, {icon: 2, title:'修改设备通道数',time: 2000,skin: 'layer-ext-moon' });  
			return false;
		},
		complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
    	if(status == 'timeout'){//超时,status还有success,error等值的情况
      	 	xhr.abort();
    	} else if(status == 'error') {
				xhr.abort();
				layer.msg('设置失败', {icon: 2, time: 2000,title:'修改设备通道数记',skin: 'layer-ext-moon' });
			}
  	}
	});
}

function doSetEncoderChannelNums(obj){
	var data = obj.data;
	if(isEmpty(data)) {
		layer.msg('没有选择数据', {icon: 2, time: 2000,title:'修改设备通道数',skin: 'layer-ext-moon' });
		return;
	}
	
	layer.prompt({
		formType: 2
		,title:"修改设备通道数"
		,value: "" + data[0].nums
	}, function(value, index){
		
		if(value === data.nums){
			layer.msg('没有修改通道数量', {icon: 2, time: 2000,title:'修改设备通道数',skin: 'layer-ext-moon' });
			
		} else {
			var urls = "/nvsthird/setEncoderChannels?id=" + data[0].id + "&nums=" + value;
			handleSetEncoderChannelNums(urls);	
		}
		
		layer.close(index);
	});
}



function  handleSyncCatalog(nvsurl){
	var timeoutDefaultValue = 20 * 1000;
	
	var xhr = $.ajax({
		url: nvsurl,
		type: "get",
		async: true,
		dataType: "json",
		contentType: "application/json",
		timeout: timeoutDefaultValue,
		success: function(res) {	
			
		   if(res.errormsg === "OK") {
			   layer.msg('同步成功',{icon: 1, title:'同步资源',time: 2000,skin: 'layer-ext-moon' });
		   } else {
			   layer.msg('同步失败', {icon: 2, time: 2000,title:'同步资源',skin: 'layer-ext-moon' });
		   }
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			layer.msg('同步超时:' + errorThrown, {icon: 2, title:'同步资源',time: 2000,skin: 'layer-ext-moon' });  
			return false;
		},
		complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
    	if(status == 'timeout'){//超时,status还有success,error等值的情况
      	 	xhr.abort();
    	} else if(status == 'error') {
				xhr.abort();
				layer.msg('同步失败', {icon: 2, time: 2000,title:'同步资源',skin: 'layer-ext-moon' });
			}
  	}
	});
}

function doSyncCatalogData(obj){
	var data = obj.data;
	if(isEmpty(data)) {
		layer.msg('没有选择数据', {icon: 2, time: 2000,title:'同步资源',skin: 'layer-ext-moon' });
		return;
	}

	layer.confirm('同步资源将要删除之前编码对应关系,确定要操作吗?', {
		 btn: ['确定','放弃'] 
		,title:"提示"
		}, function(){
			var urls = "/nvsthird/SyncCatalog?id=" + data[0].id ;
			handleSyncCatalog(urls);
		}, function(){
		}
	);
}

  
$(function(){
	
	///动态设置页面高度
	var all_height = $(window).height();
    var div_height = all_height -  $("#headFrames").height() ;
    $("#encoders_control").css("height",div_height+"px");
	
	 
	//console.log("div_height:" + div_height + " treebox_height:" +treebox_height + " all_height:"+all_height);
	
	///弹窗初始化
	layui.use('layer', function(){
		layer = layui.layer; 
    });
	

	
	///查询结果表格监听
	layui.use('table', function(){
		var table = layui.table;
		
		reRenderEncodersTable([]);
		
		
		//头工具栏事件
		table.on('toolbar(encodersTableData)', function(obj){
			var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
			switch(obj.event){
				case 'editChannels':
					var data = checkStatus.data;  //获取选中行数据
					doSetEncoderChannelNums(checkStatus);
					break;
					
				case 'syncData':
					var data = checkStatus.data;  //获取选中行数据
					doSyncCatalogData(checkStatus);
					break;
				
			};
		});
		
	});
	
	
	///等待加载
	setTimeout(function(){
		getEncoderDeviceList();
    },100);
  
	
});