data.js
6.67 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
/**
* 数据处理模块
*/
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 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();
gpsTimer = setTimeout(f, realGpsT);
},
//查询实际排班计划
queryRealSchedule: function(lineArrayStr, cb){
$.get('/realSchedule/lines', {lines: lineArrayStr} ,function(scheduls){
var list, sm, em;
for(var lineCode in scheduls){
list = scheduls[lineCode];
$.each(list, function(){
//发车时间 + 历时 = 终点时间
if(this.bcsj){
sm = moment(dateStr + ' ' + this.fcsj, 'YYYY-MM-DD HH:mm');
em = sm.add(this.bcsj, 'minutes');
//发车时间戳
this.fcsjT = sm.valueOf();
this.zdsj = em.format('HH:mm');
//终点时间戳
this.zdsjT = em.valueOf();
//缓存实际排班计划
schedules[this.id] = this;
}
});
//按发车时间排序
list.sort(function(a, b){
return a.fcsjT - b.fcsjT;
});
}
cb && cb(scheduls);
});
},
//根据排班ID获取排班
getSchedulById: function(id){
return schedules[id];
}
};
//初始化lineCodes
$.each(dataObject.getLines(), function(i, obj){
lineCodes += (obj.lineCode + ',');
});
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]);
});
function refreshGpsProxy(){
refreshGps(function(add, up){
$('#tab_home,#tab_map #mapContainer').trigger('gps_refresh', [add, up]);
});
}
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});
//停止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);
//合并
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;
})();