data.js 9.84 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/**
 * 数据处理模块
*/
var _data = (function(){
	var storage = window.localStorage;
	
	var gpsTimer;
	
	//实时GPS数据
	var allGps = {};
	//10秒刷新一次实时GPS
	var realGpsT = 1000 * 10;
	
	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.fcno - b.fcno) + (a.fcsjT - b.fcsjT);
	}
	
	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;
			
			var lpArray = lineLpMap[sch.xlBm][sch.lpName];
			$.each(lpArray, function(i){
				if(this.id == sch.id){
					lpArray.splice(i, 1, sch);
					return false;
				}
			});
		},
		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;
		},
		//根据线路编码获取应发未发班次数量
		getYfwfNumByLine: function(lineCode){
			var lpMap = lineLpMap[lineCode]
				,arr = [], t = new Date().valueOf(), num = 0;
			for(var lp in lpMap){
				arr = lpMap[lp];
				for(var i = 0, sch; sch=arr[i++];){
					if(sch.fcsjT > t)
						break;
					
					if(sch.fcsjActual == null && sch.fcsjActualTime == null)
						num ++;
				}
			}
			return num;
		},
		//根据线路编码获取站点路由
		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.dfsjT) / 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){
					addArray.push(gps);
				}
				else if(gps.timestamp > oldGps.timestamp){
					//更新
					upArray.push(gps);
				}
				allGps[gps.deviceId] = 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;
	}
	
	countDown('data.js');
	//queryStationRoute();
	return dataObject;
})();