DirectiveServiceImpl.java
20.6 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package com.bsth.service.directive;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.data.BasicData;
import com.bsth.data.directive.DayOfDirectives;
import com.bsth.data.directive.DirectiveCreator;
import com.bsth.data.directive.GatewayHttpUtils;
import com.bsth.data.gpsdata_v2.GpsRealData;
import com.bsth.data.gpsdata_v2.entity.GpsEntity;
import com.bsth.data.pilot80.PilotReport;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.data.utils.ListFilterUtils;
import com.bsth.data.utils.ListPageQueryUtils;
import com.bsth.entity.directive.*;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.directive.D60Repository;
import com.bsth.repository.directive.D64Repository;
import com.bsth.repository.directive.D80Repository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.directive.dto.DeviceConfigDto;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.websocket.handler.RealControlSocketHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class DirectiveServiceImpl extends BaseServiceImpl<D60, Integer> implements DirectiveService {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
D60Repository d60Repository;
@Autowired
GpsRealData gpsRealDataBuffer;
@Autowired
D64Repository d64Repository;
@Autowired
RealControlSocketHandler socketHandler;
@Autowired
D80Repository d80Repository;
@Autowired
DayOfDirectives dayOfDirectives;
@Autowired
PilotReport pilotReport;
@Autowired
DayOfSchedule dayOfSchedule;
@Autowired
JdbcTemplate jdbcTemplate;
//static Long schDiff = 1000 * 60 * 60L;
private static DateTimeFormatter fmtHHmm = DateTimeFormat.forPattern("HH:mm"), fmtHHmm_CN = DateTimeFormat.forPattern("HH点mm分");
@Override
public int send60Phrase(String nbbm, String text, String sender) {
D60 d60 = null;
try {
text = StringEscapeUtils.unescapeHtml4(text);
text = text.replaceAll("#", "");
d60 = create60Data(nbbm, text, (short) 0x00, null);
} catch (Exception e) {
logger.error("发送消息短语出现异常", e);
return -1;
}
if (null == d60)
return -1;
// 发送指令
int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
if (null != sender)
d60.setSender(sender);
d60.setHttpCode(code);
if (code != 0)
d60.setErrorText("网关通讯失败, code: " + code);
dayOfDirectives.put60(d60);
return code;
}
@Override
public int send60Dispatch(ScheduleRealInfo sch, int finish, String sender, String txtPrefix) {
D60 d60 = null;
try {
if (sch.isDestroy()) {
logger.warn("烂班不允许发送调度指令....");
return -1;
}
//待发应到时间
String dfsj = fmtHHmm.print(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
String text = "您已完成" + finish + "个班次,下一发车时间" + fmtHHmm_CN.print(sch.getDfsjT()) + ",由"
+ sch.getQdzName() + "发往" + sch.getZdzName() + ";应到 " + dfsj+";停车之后,请拉手刹!";
if(sch.getBcType().equals("venting")){
text += " (直放)";
}
else if(sch.getBcType().equals("major")){
text += " (放站到"+sch.getMajorStationName()+"带客)";
}
//下发0x02指令 调度指令(闹钟有效)
long t = System.currentTimeMillis() + 1000 * 30,
alarmTime = sch.getDfsjT() < t?t:sch.getDfsjT();
if(StringUtils.isNotEmpty(txtPrefix)){
text = txtPrefix + text;
}
text = StringEscapeUtils.unescapeHtml4(text);
int state = 0;//营运状态
if(dayOfSchedule.emptyService(sch))
state = 1;
d60 = new DirectiveCreator().createD60_02(sch.getClZbh(), text, Integer.parseInt(sch.getXlDir())
, state, new Date(alarmTime));
d60.setLineCode(sch.getXlBm());
} catch (Exception e) {
logger.error("生成调度指令时出现异常", e);
return -1;
}
if (null == d60)
return -1;
d60.setSender(sender);
JSONObject jObj = JSON.parseObject(JSON.toJSONString(d60));
//进场或者出场班次时,附加lock 标识
if (null != sch.getBcType()
&& (sch.getBcType().equals("out") || sch.getBcType().equals("in"))) {
jObj.put("lock", 1);
}
// 发送指令
int code = GatewayHttpUtils.postJson(jObj.toJSONString());
d60.setDispatch(true);
d60.setSch(sch);
d60.setHttpCode(code);
if (code == 0) {
sch.setDirectiveState(60);
// 通知页面
sendD60ToPage(sch);
} else {
d60.setErrorText("网关通讯失败, code: " + code);
}
dayOfDirectives.put60(d60);
return code;
}
/**
* @Title: sendDirectiveState @Description: TODO(向页面推送班次指令状态) @throws
*/
@Override
public void sendD60ToPage(ScheduleRealInfo sch) {
Map<String, Object> map = new HashMap<>();
map.put("fn", "directive");
map.put("t", sch);
ObjectMapper mapper = new ObjectMapper();
try {
socketHandler.sendMessageToLine(sch.getXlBm(), mapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
logger.error("", e);
}
}
@Override
public int send60Dispatch(Long id, String sender) {
ScheduleRealInfo sch = dayOfSchedule.get(id);
// 车辆已完成班次
int finish = dayOfSchedule.doneSum(sch.getClZbh());
return send60Dispatch(sch, finish, sender, "");
}
@Override
public int send60Operation(String nbbm, int state, int upDown, String sender) {
logger.info("切换运营状态, nbbm: " + nbbm + " ,state: " + state + " ,upDown:" + upDown);
String text = "切换为 " + (upDown == 0 ? "上行" : "下行") + (state == 0 ? "营运" : "未营运");
D60 d60 = new DirectiveCreator().createD60(nbbm, text, (short) 0x03, upDown, state, null);
if (null == d60)
return -1;
if (null != sender)
d60.setSender(sender);
else
d60.setSender("系统");
// 发送指令
int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
// 添加到缓存,等待入库
d60.setHttpCode(code);
GpsEntity gps = gpsRealDataBuffer.getByNbbm(nbbm);
if(null != gps)
d60.setLineCode(gps.getLineId());
if (code != 0)
d60.setErrorText("网关通讯失败, code: " + code);
dayOfDirectives.put60(d60);
return code;
}
/**
* 线路切换
*/
@Override
public int lineChange(String nbbm, String lineCode, String sender) {
return lineChangeByDeviceId(BasicData.deviceId2NbbmMap.inverse().get(nbbm), lineCode, sender);
}
@Override
public int lineChangeByDeviceId(String deviceId, String lineCode, String sender) {
DirectiveCreator crt = new DirectiveCreator();
Long t = System.currentTimeMillis();
//生成64数据包
D64 d64 = crt.create64(deviceId, lineCode, t);
if (null != sender)
d64.setSender(sender);
else
d64.setSender("系统");
//String deviceId = d64.getDeviceId();
int code = GatewayHttpUtils.postJson(JSON.toJSONString(d64));
// 入库
d64.setHttpCode(code);
d64.getData().setTxtContent("切换线路[" + BasicData.lineCode2NameMap.get(lineCode) + "]");
// 通知设备刷新线路文件,忽略结果
if (code == 0)
GatewayHttpUtils.postJson(crt.createDeviceRefreshData(deviceId, lineCode));
else
d64.setErrorText(" 网关通讯失败, code: " + code);
dayOfDirectives.put64(d64);
return code;
}
public D60 create60Data(String nbbm, String text, Short dispatchInstruct, ScheduleRealInfo sch) {
String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
String lineCode = null;
if (null == deviceId) {
logger.error("没有设备号对照的车辆:" + nbbm);
return null;
}
// 根据当前GPS确定 上下行和营运状态
Byte upDown = null;
Integer state = null;
if (null == sch) {
GpsEntity gpsData = gpsRealDataBuffer.get(deviceId);
if (null == gpsData) {
/*
* 短语指令不会变更设备状态,所以在没有gps状态对照的情况下可以下发
* 其他指令在不确定状态的情况下,一律不允许
*/
if (dispatchInstruct == 0) {
upDown = 0;
state = 0;
} else {
logger.error("没有找到gps对照,无法确认营运状态和上下行:" + nbbm);
return null;
}
} else {
upDown = gpsData.getUpDown();
state = gpsData.getState();
lineCode = gpsData.getLineId();
}
} else {
upDown = Byte.parseByte(sch.getXlDir());
state = 0;
}
return new DirectiveCreator().createD60(nbbm, text, dispatchInstruct, upDown, state, lineCode);
}
/* @Override
public int upDownChange(String nbbm, Integer upDown, String sender) {
return send60Operation(nbbm, 0, upDown, sender);
}*/
@Override
public Map<String, List<D80>> findNoCofm80(String lineCodes) {
List<String> lineList = Splitter.on(",").trimResults().splitToList(lineCodes);
Map<String, List<D80>> rs = new HashMap<>();
for (String code : lineList)
rs.put(code, pilotReport.unconfirmed80(code));
return rs;
}
@Override
public Map<String, Object> reply80(int id, int reply) {
Map<String, Object> rs = new HashMap<>();
D80 d80 = pilotReport.findById(id);
try {
if (null == d80) {
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "服务器没有找到对应数据!");
} else if (d80.isConfirm()) {
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "该数据已经被处理了!");
} else {
SysUser user = SecurityUtils.getCurrentUser();
d80.setC0(reply, user.getUserName());
// 入库
d80Repository.save(d80);
//回复网关
int code = GatewayHttpUtils.postJson(JSON.toJSONString(d80.getC0()));
rs.put("status", ResponseCode.SUCCESS);
if (code != 0)
rs.put("msg", "发送C0响应指令到车载设备失败,但该操作已经被系统记录!");
pilotReport.reply(d80);
// 通知页面
Map<String, Object> sockMap = new HashMap<>();
sockMap.put("fn", "d80Confirm");
sockMap.put("id", d80.getId());
sockMap.put("lineId", d80.getData().getLineId());
socketHandler.sendMessageToLine(d80.getData().getLineId().toString(), JSON.toJSONString(sockMap));
}
} catch (Exception e) {
logger.error("80响应出现异常...", e);
rs.put("status", ResponseCode.SUCCESS);
}
return rs;
}
@Override
public Map<String, Object> findDirective(String nbbms, int dType, int page, int size) {
Map<String, Object> rsMap = new HashMap<>();
try{
List<String> carArray = new ArrayList<>(), deviceArray=new ArrayList<>();
if(StringUtils.isNotEmpty(nbbms)){
carArray = Splitter.on(",").splitToList(nbbms);
//转换成设备号
Map<String, String> nbbm2deviceMap = BasicData.deviceId2NbbmMap.inverse();
for(int i = 0, len=carArray.size(); i < len; i++){
deviceArray.add(nbbm2deviceMap.get(carArray.get(i)));
}
}
List<Directive> list = new ArrayList<>();
switch (dType) {
case -1:
//所有指令
list = new ArrayList<>(dayOfDirectives.all());
break;
case 0:
//调度指令
Collection<D60> dptArray = dayOfDirectives.all60();
for (D60 d60 : dptArray) {
if (d60.isDispatch())
list.add(d60);
}
break;
case 1:
//运营指令
Collection<D60> yyArray = dayOfDirectives.all60();
for (D60 d60 : yyArray) {
if (d60 != null && d60.getData().getDispatchInstruct() == (short) 0x03
&& !d60.isDispatch())
list.add(d60);
}
break;
case 2:
//线路切换指令
list.addAll(dayOfDirectives.all64());
break;
case 3:
//消息短语
Collection<D60> dyArray = dayOfDirectives.all60();
for (D60 d60 : dyArray) {
if (d60 != null && d60.getData().getDispatchInstruct() == (short) 0x00
&& !d60.isDispatch())
list.add(d60);
}
break;
}
// 时间倒序
Collections.sort(list, new DayOfDirectives.DComparator());
if(deviceArray.size() > 0){
//按设备号过滤
List<Directive> subList = new ArrayList<>();
for (Directive d : list) {
if (deviceArray.contains(d.getDeviceId())) {
subList.add(d);
}
}
list = subList;
}
for(Directive d : list){
d.setTimeHHmm(fmtHHmm.print(d.getTimestamp()));
d.setNbbm(BasicData.deviceId2NbbmMap.get(d.getDeviceId()));
}
int count = list.size();
// 分页
int s = page * size, e = s + size;
if (e > count)
e = count;
List<Directive> rs = list.subList(s, e);
rsMap.put("list", rs);
rsMap.put("totalPages", count % size == 0 ? (count / size - 1) : count / size);
rsMap.put("page", page);
}catch (Exception e){
logger.error("", e);
throw e;
}
return rsMap;
}
@Override
public Map<String, Object> findAll80(Map<String, Object> map, int page, int size) {
List all = ListFilterUtils.filter(pilotReport.findAll(), map, D80.class);
//排序
Collections.sort(all, new Comparator<D80>() {
@Override
public int compare(D80 o1, D80 o2) {
return (int) (o2.getTimestamp() - o1.getTimestamp());
}
});
List<D80> d80s = ListPageQueryUtils.paging(all, page, size);
//时间格式化
for (D80 d80 : d80s) {
d80.setTimeStr(fmtHHmm.print(d80.getTimestamp()));
}
Map<String, Object> rsMap = new HashMap<>();
rsMap.put("list", d80s);
rsMap.put("totalPages", all.size() % size == 0 ? all.size() / size - 1: all.size() / size);
rsMap.put("page", page);
return rsMap;
}
@Override
public D64 save64(D64 d64) {
return d64Repository.save(d64);
}
@Override
public Map<String, Object> sendC0A4(String nbbm) {
Map<String, Object> rs = new HashMap<>();
String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
GpsEntity gps = gpsRealDataBuffer.get(deviceId);
if(gps == null || gps.isOffline()){
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "下发指令失败,设备离线");
return rs;
}
Map<String, Object> c0a4 = new HashMap<>();
c0a4.put("deviceId", deviceId);
c0a4.put("timestamp", System.currentTimeMillis());
c0a4.put("operCode", (short) 0xC0);
Map<String, Object> data = new HashMap<>();
data.put("deviceId", deviceId);
data.put("operCode2", (short) 0xA4);
c0a4.put("data", data);
int code = GatewayHttpUtils.postJson(JSON.toJSONString(c0a4));
if(code != 0){
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "和网关通讯失败, code " + code);
}
else{
rs.put("status", ResponseCode.SUCCESS);
}
return rs;
}
@Override
public int sendC0A3(DC0_A3 c0a3) {
GatewayHttpUtils.postJson(JSON.toJSONString(c0a3));
return 0;
}
@Override
public int sendC0A5(String json) {
return GatewayHttpUtils.postJson(json);
}
@Override
public int refreshLineFile(String deviceId) {
GpsEntity gps = gpsRealDataBuffer.get(deviceId);
if (gps == null)
return -1;
return GatewayHttpUtils.postJson(new DirectiveCreator().createDeviceRefreshData(deviceId, gps.getLineId()));
}
/* @Override
public int stateChange(String nbbm, Integer upDown, Integer state, String userName) {
return send60Operation(nbbm, state, upDown, userName);
}*/
@Override
public Map<String, Object> deviceCofigList(Map<String, String> map, int page, int size) {
Map<String, Object> rs = new HashMap<>();
try {
String conts="";
if(StringUtils.isNotBlank(map.get("lineId"))){
conts+=" AND xl='" + map.get("lineId")+"'";
}
if(StringUtils.isNotBlank(map.get("nbbm"))){
conts+=" AND inside_code like '%" + map.get("nbbm")+"%'";
}
if(StringUtils.isNotBlank(map.get("deviceId"))){
conts+=" AND equipment_code like '%" + map.get("deviceId")+"%'";
}
String sql = "select * from (SELECT * FROM (SELECT id AS cl_id,inside_code,equipment_code FROM bsth_c_cars ) t1 LEFT JOIN bsth_v_c0_a4 t2 ON t1.equipment_code = t2.id LEFT JOIN (SELECT xl,cl FROM bsth_c_s_ccinfo WHERE id IN (SELECT max(id) FROM bsth_c_s_ccinfo GROUP BY cl)) t3 ON t1.cl_id = t3.cl ORDER BY t2.`timestamp` DESC) t4 where 1=1 "+conts;
int count = jdbcTemplate.queryForObject("select count(*) from ("+sql+") c1", Integer.class);
int totalPages = count % size == 0 ? count / size : count / size + 1;
int s = page * size;
List<DeviceConfigDto> list = jdbcTemplate.query(sql + " LIMIT " + s + "," + size, new BeanPropertyRowMapper<>(DeviceConfigDto.class));
rs.put("status", ResponseCode.SUCCESS);
rs.put("list", list);
rs.put("totalPages", totalPages);
rs.put("page", page);
} catch (Exception e) {
e.printStackTrace();
logger.error("", e);
rs.put("status", ResponseCode.ERROR);
}
return rs;
}
}