DirectiveBuffer.java
9.95 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
package com.bsth.vehicle.directive.buffer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.service.realcontrol.buffer.ScheduleBuffer;
import com.bsth.vehicle.common.CommonMapped;
import com.bsth.vehicle.directive.MsgIdGenerator;
import com.bsth.vehicle.directive.entity.Directive;
import com.bsth.vehicle.directive.entity.Directive60;
import com.bsth.vehicle.directive.entity.DirectiveReply;
import com.bsth.vehicle.directive.entity.Directive80;
import com.bsth.vehicle.directive.entity.DirectiveC0;
import com.bsth.vehicle.directive.entity.DirectiveC0.DirectiveC0Data;
import com.bsth.vehicle.directive.entity.Directive64;
import com.bsth.vehicle.directive.repository.Directive60Repository;
import com.bsth.vehicle.directive.repository.Directive80Repository;
import com.bsth.vehicle.directive.repository.LineChangeRepository;
import com.bsth.vehicle.directive.service.DirectiveService;
import com.bsth.websocket.handler.RealControlSocketHandler;
import com.google.common.collect.ArrayListMultimap;
/**
*
* @ClassName: DirectiveBuffer
* @Description: TODO(调度指令缓存)
* @author PanZhao
* @date 2016年6月7日 下午3:24:19
*
*/
@Component
public class DirectiveBuffer {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
Directive80Repository d80Repository;
@Autowired
LineChangeRepository d64Repository;
@Autowired
DirectiveService directiveService60;
/**
* 等待入库的调度指令
*/
public static LinkedList<Directive60> transientList;
/**
* 线路切换指令 64
*/
public static Map<String, Directive64> changeMap;
/**
* 当日60指令缓存
*/
private static Map<Integer, Directive60> directiveMap;
@Autowired
Directive60Repository d60Repository;
/**
* 驾驶员上报数据
* {K: 线路编码}
*/
private static ArrayListMultimap<Integer, Directive80> reportMultiMap;
@Autowired
RealControlSocketHandler socketHandler;
static{
clear();
}
public static void put(Directive60 directive){
directiveMap.put(directive.getMsgId(), directive);
}
/**
*
* @Title: reply
* @Description: TODO(指令 46,47 响应)
* @throws
*/
public void reply(DirectiveReply reply){
Integer msgId = reply.getMsgId();
if(msgId == null){
logger.error("reply error , msgId is null.");
return;
}
Directive60 directive = directiveMap.get(msgId);
if(null == directive){
//无效的响应
return;
}
switch (reply.getStatus()) {
case 0:
//失败
directive.setReply46((short)-1);
break;
case 1:
//发送成功
directive.setReply46((short)0);
break;
case 2:
//驾驶员阅读
directive.setReply47((short)0);
break;
}
ScheduleRealInfo sch = directive.getSch();
if(null == sch)
return;
//调度指令回复
if(directive.isDispatch()){
//更新班次状态
sch.setDirectiveState(reply.getStatus() * 100);
ScheduleBuffer.persistentList.add(sch);
//通知页面
directiveService60.sendDirectiveToPage(sch);
}
//运营状态指令回复
else if(directive.getData().getDispatchInstruct() == 0x03){
sch.setOpDirectiveState(reply.getStatus() * 100);
ScheduleBuffer.persistentList.add(sch);
}
transientList.add(directive);
}
/**
*
* @Title: reply64
* @Description: TODO(64 协议响应)
* @throws
*/
public void reply64(JSONObject json){
String key = json.getString("deviceId") + "_" + json.getString("timestamp");
Directive64 change = changeMap.get(key);
if(null == change)
logger.warn("64响应 -找不到请求源,json: " + json);
else if(change.getRespAck() != null)
logger.warn("64响应 -重复响应,json: " + json);
else{
JSONObject data = json.getJSONObject("data");
if(null == data)
logger.warn("64响应 data is null ,json: " + json);
else{
change.setRespAck(data.getShort("requestAck"));
//响应入库
d64Repository.save(change);
}
}
}
/**
*
* @Title: jsyReport
* @Description: TODO(80 驾驶员上报)
* @throws
*/
public void jsyReport(Directive80 report){
//将托管的线路自动处理掉
Integer lineCode = report.getData().getLineId();
//Integer status = ScheduleBuffer.trustMap.get(lineCode);
//请求代码
//Short requestCode = report.getData().getRequestCode();
//托管
/*if(null == status || status == 0){
//自动处理出场请求
if(requestCode == 0xA3){
DirectiveC0 c0 = createC0(report, (short)0x06);
HttpUtils.postJson(JSON.toJSONString(c0));
report.setC0(c0);
d80Repository.save(report);
}
}*/
//实时入库
d80Repository.save(report);
reportMultiMap.put(lineCode, report);
//推送到页面
report.getData().setNbbm(CommonMapped.vehicDeviceBiMap.get(report.getDeviceId()));
JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(report));
json.put("fn", "report80");
socketHandler.sendMessageToLine(lineCode, json.toJSONString());
}
/**
*
* @Title: createC0
* @Description: TODO(生成C0数据)
* @param @param ack
* @throws
*/
public DirectiveC0 createC0(Directive80 report, short ack){
DirectiveC0 c0 = new DirectiveC0();
c0.setDeviceId(report.getDeviceId());
c0.setTimestamp(report.getTimestamp());
c0.setOperCode((short)0xC0);
DirectiveC0Data data = new DirectiveC0Data();
data.setOperCode2((short)0x86);
data.setRequestAck(ack);
c0.setData(data);
return c0;
}
/**
*
* @Title: saveAll
* @Description: TODO(所有缓存里的指令全部入库)
*/
public void saveAll(){
//未回复的60指令入库
Collection<Directive60> ds = directiveMap.values();
List<Directive60> pList = new ArrayList<>();
for(Directive60 d : ds){
if(d.getReply47() == null)
pList.add(d);
}
d60Repository.save(directiveMap.values());
//未回复的64指令入库
Collection<Directive64> ds64 = changeMap.values();
List<Directive64> pList2 = new ArrayList<>();
for(Directive64 d : ds64){
if(d.getRespAck() == null)
pList2.add(d);
}
d64Repository.save(pList2);
}
/**
*
* @Title: clear
* @Description: TODO(清理缓存)
* @throws
*/
public static void clear(){
transientList = new LinkedList<>();
directiveMap = new HashMap<>();
reportMultiMap = ArrayListMultimap.create();
changeMap = new HashMap<>();
}
/**
*
* @Title: recovery
* @Description: TODO(从数据库恢复当天的指令数据到内存.)
* @throws
*/
public void recovery(){
clear();
//设置msgId
Integer maxMsgId = d60Repository.maxMsgId();
MsgIdGenerator.setMsgId(maxMsgId ++);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 2);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
//当天2点开始恢复
long st = cal.getTimeInMillis()
,ct = System.currentTimeMillis();
//如果还不到2点,从前一天2点开始恢复
if(ct < st){
st -= (1000 * 60 * 60 * 24);
}
List<Directive60> d60List = d60Repository.findByGtTime(st);
//恢复60下发指令
for(Directive60 d : d60List){
directiveMap.put(d.getMsgId(), d);
}
System.out.println(directiveMap.values());
//恢复80驾驶员上报
List<Directive80> d80List = d80Repository.findByGtTime(st);
for(Directive80 d : d80List){
d.getData().setNbbm(CommonMapped.vehicDeviceBiMap.get(d.getDeviceId()));
reportMultiMap.put(d.getData().getLineId(), d);
}
}
public static List<Directive80> findNoCofm80(Integer lineCode){
List<Directive80> list = reportMultiMap.get(lineCode)
,rs = new ArrayList<>();
for(Directive80 d : list){
if(!d.isConfirm())
rs.add(d);
}
return rs;
}
public static Directive80 findById80(int id){
Collection<Directive80> ds = reportMultiMap.values();
Directive80 rs = null;
for(Directive80 d : ds){
if(d.getId() == id){
rs = d;
break;
}
}
return rs;
}
/**
*
* @Title: findAll
* @Description: TODO(获取缓存里的全部指令)
* @throws
*/
public static List<Directive> findAll(){
List<Directive> all = new ArrayList<>();
//60
all.addAll(directiveMap.values());
//64
all.addAll(changeMap.values());
return all;
}
/**
*
* @Title: findDispatch
* @Description: TODO(获取缓存里的调度指令)
* @throws
*/
public static List<Directive> findDispatch(){
List<Directive> rs = new ArrayList<>();
for(Directive60 d60 : directiveMap.values()){
if(d60.isDispatch())
rs.add(d60);
}
return rs;
}
/**
*
* @Title: findDispatch
* @Description: TODO(根据dispatchInstruct获取非调度指令)
* @throws
*/
public static List<Directive> findByDispatchInstruct(short dispatchInstruct){
List<Directive> rs = new ArrayList<>();
for(Directive60 d60 : directiveMap.values()){
if(d60.getData() != null && d60.getData().getDispatchInstruct() == dispatchInstruct
&& !d60.isDispatch())
rs.add(d60);
}
return rs;
}
public static class DComparator implements Comparator<Directive>{
@Override
public int compare(Directive o1, Directive o2) {
return (int) (o2.getTimestamp() - o1.getTimestamp());
}
}
public static List<Directive> findLineChange() {
List<Directive> rs = new ArrayList<>();
rs.addAll(changeMap.values());
return rs;
}
}