data.js 9.28 KB
/**
 * 数据处理模块
*/
var _data = (function(){
	var storage = window.localStorage;
	
	var gpsTimer;
	
	//实时GPS数据
	var allGps = {};
	//300秒刷新一次实时GPS
	var realGpsT = 1000 * 300;
	
	var dateStr = moment().format('YYYY-MM-DD');
	//实际排班
	var schedules = {};
	
	//站点路由缓存
	var stationRoute = {};
	
	//线路 ——> 路牌 ——> 班次 3层映射
	var lineLpMap = {};
	//线路标准信息
	var lineInformations = {};
	//车辆和班次数组映射
	//var clSchMap = {};
	
	//车辆自编号和设备号对照
	var carDeviceIdMapp = {};
	
	var fcsjSort = function(a, b){
		//return a.fcsjT - b.fcsjT;
		return a.fcno - b.fcno;
	}
	
	var dataObject = {
		/*//班次发车
		setFcsj: function(schId, fcsj, fcsjT){
			schedules[schId].fcsjActual = fcsj;
			schedules[schId].fcsjActualTime = fcsjT;
		},*/
		//根据线路编码获取上行和下行班次
		/*findByLineAll: function(xlbm){
			var array = [],sch;
			for(var id in schedules){
				sch = schedules[id];
				if(sch.xlBm == xlbm)
					array.push(e);
			}
			//排序
			array.sort(function(a, b){
				return a.fcsjT - b.fcsjT;
			});
			
			return array;
		},*/
		//根据线路和上下行获取计划排班
		findSchByLine: function(xlbm, upDown){
			var array = [];
			var sch;
			for(var id in schedules){
				sch = schedules[id];
				if(sch.xlBm == xlbm
						&& sch.xlDir == upDown){
					array.push(sch);
				}
			}
			//排序
			array.sort(fcsjSort);
			
			return array;
		},
		//根据车辆获取班次数组
		findByCl: function(nbbm){
			var array = [];
			for(var id in schedules){
				sch = schedules[id];
				if(sch.clZbh == nbbm)
					array.push(sch);
			}
			
			//排序
			array.sort(fcsjSort);
			return array;
		},
		//添加一个班次
		pushSchedule: function(sch){
			//附加信息
			attachInfo(sch);
			schedules[sch.id] = sch;
			lineLpMap[sch.xlBm][sch.lpName].push(sch);
		},
		//更新班次信息
		updateSchedule: function(sch){
			//附加信息
			attachInfo(sch);
			schedules[sch.id] = sch;
			lineLpMap[sch.xlBm][sch.lpName].push(sch);
		},
		getLines: function(){
			return JSON.parse(storage.getItem('lineControlItems'));
		},
		getDeviceIdByCar: function(car){
			return carDeviceIdMapp[car];
		},
		//获取线路标准信息
		getLineInformation: function(lineCode){
			return lineInformations[lineCode];
		},
		getLineIds: function(){
			return JSON.parse(storage.getItem('lineIds'));
		},
		//获取线路路牌对照数据
		getLineLpMap: function(){
			return lineLpMap;
		},
		//根据线路编码获取站点路由
		getStationRouteByLine: function(lineCode){
			return stationRoute[lineCode];
		},
		//为GPS点附加班次信息
		attachSchedulInfo: function(gpsArray){
			$.each(gpsArray, function(){
				gps = this;
				if(gps.currSchId)
					gps.currSch = schedules[gps.currSchId];
				if(gps.nextSchId)
					gps.nextSch = schedules[gps.nextSchId];
				
			});
			
			return gpsArray;
		}
		//查询站点路由
		,queryStationRoute : function(lineId,container, cb, width){
			$get('/stationroute/all', {'line.lineCode_eq': lineId, 'destroy_eq': 0}, function(routes){
				var svgData = analyData(routes);
				cb && cb(lineId, svgData, container, width);
			});
		},
		//实时GPS定时刷新
		startRefreshGpsTimer: function(){
			var f = arguments.callee;
			refreshGpsProxy();
			gpsTimer = setTimeout(f, realGpsT);
		},
		//查询实际排班计划
		queryRealSchedule: function(lineArrayStr, cb){
			$.get('/realSchedule/lines', {lines: lineArrayStr} ,function(rs){
				var list, sm, em, lineName;
				for(var lineCode in rs){
					list = rs[lineCode];
					lineLpMap[lineCode] = {};
					$.each(list, function(){
						attachInfo(this);
						//缓存排班计划
						schedules[this.id] = this;
						//构造 线路 ——> 路牌 ——> 班次 3层映射
						if(!lineLpMap[lineCode][this.lpName])
							lineLpMap[lineCode][this.lpName] = [];
						lineLpMap[lineCode][this.lpName].push(this);
						//车辆 ——> 班次数组
						/*if(!clSchMap[this.clZbh])
							clSchMap[this.clZbh] = [];
						clSchMap[this.clZbh].push(this);*/
					});
					
					//按发车时间排序
					list.sort(fcsjSort);
				}
				cb && cb(rs);
			});
		},
		//根据排班ID获取排班
		getSchedulById: function(id){
			return schedules[id];
		},
		//根据车辆内部编码获取排班数组
		getSchedulByVeh: function(nbbm){
			var array = [];
			var sch;
			for(var id in schedules){
				sch = schedules[id];
				if(sch.clZbh == nbbm)
					array.push(sch);
			}
			
			//排序
			array.sort(fcsjSort);
			return array;
		},
		//根据设备号获取GPS
		getGpsByDeviceId: function(deviceId){
			return allGps[deviceId];
		},
		//获取所有GPS
		findAllGps: function(){
			return allGps;
		}
	};
	
	//初始化carDeviceIdMapp
	$.get('/realSchedule/carDeviceMapp', function(rs){
		carDeviceIdMapp = rs;
	});
	
	//初始化lineCodes
	$.each(dataObject.getLines(), function(i, obj){
		lineCodes += (obj.lineCode + ',');
		lineMap[obj.lineCode] = obj;
	});
	lineCodes = lineCodes.substr(0, lineCodes.length - 1);
	
	//地图tab页显示时 注入gps数据
	$('a[href=#tab_map]').on('shown.bs.tab', function(){
		$('#tab_map #mapContainer').trigger('gps_refresh', [allGps]);
	});
	
	//获取线路标准信息
	$.get('/lineInformation/line/multi', {lineCodes: lineCodes}
		,function(rs){
			$.each(rs, function(){
				lineInformations[this.line.lineCode] = this;
				delete this['line'];
			});
	});
	
	function attachInfo(sch){
		//实际发车误差值
		if(sch.fcsjActualTime){
			var diff = parseInt((sch.fcsjActualTime - sch.fcsjT) / 1000 / 60);
			if(diff > 0)
				sch.fcsj_diff = '( +' + diff + ' )';
			else if(diff < 0)
				sch.fcsj_diff = '( ' + diff + ' )';
			else
				sch.fcsj_diff = '';
		}
	}
	
	function refreshGpsProxy(){
		refreshGps(function(add, up){
			$('#tab_home,#tab_map #mapContainer').trigger('gps_refresh', [add, up]);
		});
	}
	var upSort = function(a, b){
		return a.stationRouteCode - b.stationRouteCode;
	}
	
	var downSort = function(a, b){
		return b.stationRouteCode - a.stationRouteCode;
	}
	
	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});
			
			//停止gps刷新
			clearTimeout(gpsTimer);
		}
	}
	
	/**
	 * 解析数据成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);
		//缓存路由
		if(routes.length > 0){
			stationRoute[routes[0].lineCode] = [up.slice(0), down.slice(0).sort(upSort)];
		}
		
		//合并
		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;
})();