webSocketHandle.js
4.72 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
/** web socket */
var _socket = (function(){
//将当前用户和线路绑定到后台
setTimeout(function(){
// $.get('/realSchedule/registerLine', {lineCodes: lineCodes}
// ,function(rs){
// if(rs == 0){
// console.log('注册线路成功!');
initWebSocket();
// }
// });
}, 500);
//初始化webSocket
var msgSock;
var initWebSocket = function(){
msgSock = new SockJS('/sockjs/realcontrol');
msgSock.onopen = function(e) {
console.log('webSocket[realcontrol] onopen');
countDown('webSocketHandle.js');
//注册线路监听
var data = {
operCode: 'register_line',
idx: '63002'
}
msgSock.send(JSON.stringify(data));
};
//接收消息
msgSock.onmessage = function(e) {
try{
var jsonMsg = $.parseJSON(e.data);
msgHandle[jsonMsg.fn](jsonMsg);
}catch(e){
console.log(e);
}
};
//断开
msgSock.onclose = function(e) {
alert('和服务器连接断开....');
$('.portlet-fullscreen').addClass('offline');
//window.location.reload(true);
};
};
var msgHandle = {
//驾驶员80
report80: function(msg){
msg.dateStr = moment(msg.timestamp).format('HH:mm.ss');
msg.text = reqCodeMap[msg.data.requestCode];
appendLogItem('console_80_temp', {list: [msg]}, msg.data.lineId);
},
//驾驶员80被处理
d80Confirm: function(msg){
var $that = $('.log-item.handle[data-id='+msg.id+']');
_fadeOut($that);
var lineCode = $that.parents('.log-item-list').data('code');
_messenger.setUntreatedNum(lineCode);
},
//车辆发出
faChe: function(msg){
_alone.refreshSchedule(msg.t);
msg.jsTime = Date.parse(new Date()) / 1000;
//信使
appendLogItem('console_fache_temp', msg, msg.t.xlBm);
//重新计算应发未发
_messenger.setYFWFNum(msg.t.xlBm);
},
//到达终点
zhongDian: function(msg){
_alone.refreshSchedule(msg.t);
_alone.refreshSchedule(msg.nt);
msg.jsTime = Date.parse(new Date()) / 1000;
//信使
appendLogItem('console_zhongdian_temp', msg, msg.t.xlBm);
},
//指令状态改变
directive: function(msg){
var sch = msg.t;
if(!sch)return;
var tab = '#tab_line_' + sch.xlBm;
//找到行
var $tr = $('tr[data-id='+sch.id+']', tab);
//更新指令状态
var clazz = '';
switch (sch.directiveState) {
case 60:
clazz = 'tl-xxfc';
break;
case 100:
clazz = 'tl-xxsd';
break;
case 200:
clazz = 'tl-xxrd';
break;
case 0:
//发送调度指令失败
break;
}
$tr.find('td[data-name=clZbh]').attr('class', clazz);
},
refresh: function(msg){
//刷新
layer.msg('正在切换到 ' + msg.dateStr + '数据', {icon: 16, shade: 0.6, time: 0});
setTimeout(function(){
window.location.reload(true);
}, 1000);
},
//刷新班次
refreshSch: function(msg){
var array = msg.ts;
if(array && array.length > 0){
if(array.length == 1)
_alone.refreshSchedule(array[0]);
else
_alone.refreshScheduleArray(array);
//重新计算应发未发
_messenger.setYFWFNum(msg.ts[0].xlBm);
}
},
//系统通知
systemNotice: function(msg){
note(msg.text, msg.type);
}
};
function appendLogItem(tempId, json, lineCode){
var $item = $(template(tempId, json));
var logWrap = $('.console-log .log-item-list', '#tab_line_' + lineCode);
logWrap.prepend($item);
//地理解析
$.each($item.find('.nt-coord'), function(){
var lon = $(this).data('lon')
,lat = $(this).data('lat')
,that = this
,coord = TransGPS.wgsToBD(lat, lon);
var geoc = new BMap.Geocoder();
geoc.getLocation(new BMap.Point(coord.lng, coord.lat), function(rs){
$(that).text(rs.address.replace('上海市', '')).removeClass('nt-coord');
});
});
if(h5Speech.isEnable() == 1){
//语音播报
var text = $item.find('.log-item-text').text();
//线路名称
var lineName = _data.getLineIds()[lineCode];
h5Speech.speak(lineName + ' ' + text);
}
//重新计算未处理消息
_messenger.setUntreatedNum(lineCode);
}
var noteTimer, noteArray = [];
function note(text, type){
noteArray.push({text: text, type: type});
if(!noteTimer){
showNote();
}
}
function showNote(){
hideNote();
/*var len = noteArray.length;
if(len == 0){
noteTimer = null;
return;
}*/
var item = noteArray.pop();
if(!item){
noteTimer = null;
return;
}
$('.ctm-note .content').text(item.text);
$('.ctm-note').show();
noteTimer = setTimeout(showNote, 5000);
}
function hideNote(){
$('.ctm-note').hide().find('.content').text('');
}
countDown('webSocketHandle.js');
var socketObj = {
close: function(){
console.log(msgSock);
msgSock.close();
}
}
return socketObj;
})();