webSocketHandle.js
3.38 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
/** web 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');
};
//接收消息
msgSock.onmessage = function(e) {
try{
var jsonMsg = $.parseJSON(e.data);
msgHandle[jsonMsg.fn](jsonMsg);
}catch(e){
console.log(e);
}
};
//断开
msgSock.onclose = function(e) {
alert('和服务器连接断开....');
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);
}
}
};
function appendLogItem(tempId, json, lineCode){
var htmlStr = template(tempId, json);
var logWrap = $('.console-log .log-item-list', '#tab_line_' + lineCode);
logWrap.prepend(htmlStr);
if(h5Speech.isEnable() == 1){
//语音播报
var text = $(htmlStr).find('.log-item-text').text();
var lineName = _data.getLineIds()[lineCode];
h5Speech.speak(lineName.replace('闵行', '') + ' ' + text);
}
//重新计算未处理消息
_messenger.setUntreatedNum(lineCode);
}
countDown('webSocketHandle.js');
}();