data.js 5.58 KB
/**
 * 数据处理模块
*/
var _data = (function(){
	
	var storage = window.localStorage;
	
	//实时GPS数据
	var allGps = {};
	//10秒刷新一次实时GPS
	var realGpsT = 1000 * 30;
	
	var dataObject = {
		getLines: function(){
			return JSON.parse(storage.getItem('lineControlItems'));
		},
		getLineIds: function(){
			return JSON.parse(storage.getItem('lineIds'));
		},
		getRealVehic: function(lineArray, cb){
			var tabList = [
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'},
						   {nbbm: 'W9H108', endDistance: '13.14', endTime: '82', instructions: '', speed: '16', roadSigns: '另1'}
			];
			var d = {
					'10232_0': tabList,
					'10232_1': tabList,
					'10566_0': tabList,
					'10566_1': tabList,
					'10904_0': tabList,
					'10904_1': tabList,
					'10069_0': tabList,
					'10069_1': tabList,
					'10474_0': tabList,
					'10474_1': tabList,
					'10507_0': tabList,
					'10507_1': tabList,
					'10702_0': tabList,
					'10702_1': tabList,
					'10220_0': tabList,
					'10220_1': tabList
			};
			cb && cb(d);
		}
		//查询站点路由
		,queryStationRoute : function(lineId,container,  cb){
			$get('/stationroute/all', {'line.lineCode_eq': lineId}, function(routes){
				var svgData = analyData(routes);
				
				cb && cb(lineId, svgData, container);
			});
		},
		/**
		 * 实时GPS定时刷新
		 */
		startRefreshGpsTimer: function(){
			var f = arguments.callee;
			refreshGpsProxy();
			setTimeout(f, realGpsT);
		}
	};
	
	//地图tab页显示时 注入gps数据
	$('a[href=#tab_map]').on('shown.bs.tab', function(){
		$('#tab_map #mapContainer').trigger('gps_refresh', [allGps]);
	});
	
	function refreshGpsProxy(){
		refreshGps(function(add, up){
			$('#tab_home,#tab_map #mapContainer').trigger('gps_refresh', [add, up]);
		});
	}

	
	//初始化lineCodes
	var lineCodes = '';
	$.each(dataObject.getLines(), function(i, obj){
		lineCodes += (obj.lineCode + ',');
	});
	lineCodes = lineCodes.substr(0, lineCodes.length - 1);
	
	var upSort = function(a, b){
		return a.outStationNmber - b.outStationNmber;
	}
	
	var downSort = function(a, b){
		return b.outStationNmber - a.outStationNmber;
	}
	
	var station_indexof = function(array, station , start){
		var res = -1
		
		for(var i = start, obj; obj = array[i++];){
			
			if(obj.stationName == station.stationName){
				res = i;
				break;
			}
		}
		
		return res;
	}
	
	
	/**
	 * 刷新GPS车辆信息
	 */
	function refreshGps(cb){
		$.ajax({
			url: '/gps/real/line',
			data: {lineCodes: lineCodes},
			timeout: 5000,//5秒超时
			success: getGpsSuccess,
			error: getGpsError
		});
		
		function getGpsSuccess(gpsList){
			if(!gpsList || gpsList.length == 0)
				return;
			
			var prve = allGps
				,addArray = []
				,upArray = []
				,oldGps;
			for(var i = 0, gps; gps=gpsList[i++];){
				oldGps = prve[gps.deviceId];
				if(!oldGps){
					//添加
					prve[gps.deviceId] = gps;
					addArray.push(gps);
				}
				else if(gps.timestamp > oldGps.timestamp){
					//更新
					upArray.push(gps);
				}
			}
			cb && cb(addArray, upArray);
		}
		
		function getGpsError(jqXHR, textStatus){
			if(textStatus === 'error')
				layer.alert('获取GPS数据时,服务器出现异常', {icon: 2});
			else if(textStatus === 'timeout')
				layer.alert('连接服务器超时', {icon: 2});
		}
	}
	
	/**
	 * 解析数据成svg想要的格式
	 */
	function analyData(routes){
		//按上下行拆分
		var up=[],down=[];
		for(var i = 0, route; route = routes[i++];){
			if(route.directions==0)
				up.push(route);
			else if(route.directions==1)
				down.push(route);
		}
		//排序
		up.sort(upSort);
		down.sort(downSort);
		
		//合并
		var data = [];
		for(var j = 0; j < up.length; j ++){
			var upS = up[j] == null?{}:up[j]
				,downS = down[j] == null?{}:down[j]
				,op = {name: [upS.stationName], id: [upS.stationCode, downS.stationCode], type: 2, stationMark: upS.stationMark};
			
			//编码相同
			if(upS.stationName != downS.stationName){
				var dIndex = station_indexof(down, upS, j);
				if(dIndex == -1){
					op.type = 0;
					op.id = [upS.stationCod, -1];
					//占位
					down.splice(j, 0, {});
				}else{
					for(var t = j; t < dIndex - 1; t++){
						var temp = down[t];
						data.push({name: [temp.stationName], type:1, id: [temp.stationCode]});
					}
					//delete
					down.splice(j, dIndex - 1 - j);
					j --;
					continue;
				}
			}
			data.push(op);
		}
		
		//将上下行挨着的独立站点合并
		var len = data.length - 1, first, sec;
		for(var s = 0; s < len; s ++){
			first = data[s];
			sec = data[s + 1];
			
			if(first.type == 0
					&& sec.type == 1){
				data.splice(s, 2, {name: [first['name'][0],sec['name'][0]], type:3, id: [first['id'][0],sec['id'][0]]});
				len --;
			}
			else if(first.type == 1 && sec.type == 0){
				data.splice(s, 2, {name: [first['name'][0],sec['name'][0]], type:3, id: [first['id'][0],sec['id'][0]]});
				len --;
			}
		}
		return data;
	}
	
	//queryStationRoute();
	return dataObject;
})();