ServiceDataInterface.java
14.8 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
package com.bsth.controller.realcontrol;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.controller.realcontrol.dto.ChangePersonCar;
import com.bsth.controller.realcontrol.dto.DftzAndDestroy;
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.schedule.DayOfSchedule;
import com.bsth.data.schedule.external.TccExternalService;
import com.bsth.entity.Line;
import com.bsth.entity.directive.D60;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.repository.LineRepository;
import com.bsth.repository.directive.D60Repository;
import com.bsth.service.LineService;
import com.bsth.websocket.handler.SendUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.*;
/**
* 对外的营运数据接口
* Created by panzhao on 2017/3/15.
*/
@RestController
@RequestMapping("/companyService")
public class ServiceDataInterface {
private final static String SECRE_KEY = "dVPHJkWUt5FhMT7jrM2dLV7QvlHAmZFd42rs1P0usBx8A7HZki";
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
DayOfSchedule dayOfSchedule;
@Autowired
DayOfDirectives dayOfDirectives;
@Autowired
D60Repository d60Repository;
@Autowired
GpsRealData gpsRealData;
@Autowired
TccExternalService tccExternalService;
@Autowired
private SendUtils sendUtils;
@Autowired
LineRepository lineRepository;
@Autowired
private LineService service;
/**
* 获取车辆 和 当前执行班次对照信息
*
* @return
*/
@RequestMapping("/execSchList")
public List<Map<String, Object>> execSchList(@RequestParam String secretKey) {
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
List<Map<String, Object>> rs = new ArrayList<>();
Collection<ScheduleRealInfo> list = dayOfSchedule.execPlanMap().values();
Map<String, Object> map;
for (ScheduleRealInfo sch : list) {
if (null == sch)
continue;
map = new HashMap<>();
map.put("clZbh", sch.getClZbh());
map.put("jGh", sch.getjGh());
map.put("jName", sch.getjName());
map.put("sGh", sch.getsGh());
map.put("sName", sch.getsName());
map.put("lpName", sch.getLpName());
map.put("xlBm", sch.getXlBm());
map.put("xlName", sch.getXlName());
map.put("xlDir", sch.getXlDir());
map.put("qdzName", sch.getQdzName());
map.put("zdzName", sch.getZdzName());
map.put("fcsj", sch.getFcsj());
map.put("fcsjActualTime", sch.getFcsjActualTime());
map.put("dfsj", sch.getDfsj());
map.put("zdsj", sch.getZdsj());
map.put("zdsjActualTime", sch.getZdsjActualTime());
map.put("bcType", sch.getBcType());
map.put("remarks", sch.getRemark());
map.put("status", sch.getStatus());
//放站班次,放到的站点
map.put("majorStationName", sch.getMajorStationName());
rs.add(map);
}
return rs;
}
@RequestMapping("/getCurrentDayPlan")
public List<ScheduleRealInfo> getCurrentDayPlan(
@RequestParam String companyId,
@RequestParam String workId,
@RequestParam String secretKey) {
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findByGsbm(companyId)), rs = new ArrayList<>();
for (ScheduleRealInfo sch : all) {
if (sch.getGsBm() != null
&& sch.getGsBm().equals(companyId)
&& sch.getjGh().equals(workId)) {
rs.add(sch);
}
}
return rs;
}
@RequestMapping("/returnCCInfo")
public List<ScheduleRealInfo> returnCCInfo(@RequestParam String companyId, @RequestParam String secretKey) {
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll()), rs = new ArrayList<>();
for (ScheduleRealInfo sch : all) {
if (sch.getBcType().equals("out")
&& sch.getGsBm() != null
&& sch.getGsBm().equals(companyId)) {
rs.add(sch);
}
}
return rs;
}
@RequestMapping("/returnJCInfo")
public List<ScheduleRealInfo> returnJCInfo(@RequestParam String companyId, @RequestParam String secretKey) {
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll()), rs = new ArrayList<>();
for (ScheduleRealInfo sch : all) {
if (sch.getBcType().equals("in")
&& sch.getGsBm() != null
&& sch.getGsBm().equals(companyId)) {
rs.add(sch);
}
}
return rs;
}
/**
* 获取全量的进出场数据, 仅供接口项目调用。 由接口项目负责对外所有场站输出
*
* @return
*/
@RequestMapping("/findCurrInAndOut")
public List<ScheduleRealInfo> findCurrInAndOut(@RequestParam String secretKey) {
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
List<ScheduleRealInfo> all = new ArrayList<>(dayOfSchedule.findAll()), rs = new ArrayList<>();
for (ScheduleRealInfo sch : all) {
if (sch.getBcType().equals("in")
|| sch.getBcType().equals("out")) {
rs.add(sch);
}
}
return rs;
}
/**
* 向指定的车辆下发消息短语
*
* @return
*/
@RequestMapping(value = "/send60Phrase", method = RequestMethod.POST)
public int send60Phrase(@RequestBody Map<String, String> map, @RequestParam String secretKey) {
try {
String nbbm = map.get("nbbm");
String txt = map.get("txt");
String sender = map.get("sender");
if (txt.length() > 50)
txt = txt.substring(0, 50);
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return -500;
//车辆和设备号对照
String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
if (StringUtils.isEmpty(deviceId))
return -404;
//检查设备是否在线
long t = System.currentTimeMillis();
GpsEntity gps = gpsRealData.get(deviceId);
if (null == gps || (t - gps.getServerTimestamp()) > 1000 * 60 * 5)
return -405;
Short dispatchInstruct = 0;//消息短语
D60 d60 = new DirectiveCreator().createD60(nbbm, txt, dispatchInstruct, gps.getUpDown(), gps.getState(), gps.getLineId());
d60.setSender(sender);
// 发送指令
int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
d60.setHttpCode(code);
if (code != 0)
d60.setErrorText("网关通讯失败, code: " + code);
dayOfDirectives.put60(d60);
return d60.getMsgId();
} catch (Exception e) {
logger.error("", e);
return -500;
}
}
/**
* 向指定的车辆下发指令
* 中运量用
* @return
*/
@RequestMapping(value = "/send60Phrase/zyl", method = RequestMethod.POST)
public int send60PhraseZyl(@RequestBody Map<String, String> map, @RequestParam String secretKey) {
try {
String plate = map.get("plate");
String txt = map.get("txt");
String sender = map.get("sender");
short dispatchInstruct = Short.parseShort(map.get("instructionType"));
int direction = Integer.parseInt(map.get("direction"));
long alarmTime = Long.parseLong(map.get("alarmTime"));
if (txt.length() > 50)
txt = txt.substring(0, 50);
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return -500;
//车辆和设备号对照
String nbbm = BasicData.nbbmCompanyPlateMap.inverse().get(plate);
String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
if (StringUtils.isEmpty(deviceId))
return -404;
//检查设备是否在线
long t = System.currentTimeMillis();
GpsEntity gps = gpsRealData.get(deviceId);
if (null == gps || (t - gps.getServerTimestamp()) > 1000 * 60 * 5)
return -405;
D60 d60 = null;
if (dispatchInstruct == 2) {
d60 = new DirectiveCreator().createD60_02(nbbm, txt, direction, 0, new Date(alarmTime));
} else {
d60 = new DirectiveCreator().createD60(nbbm, txt, dispatchInstruct, direction, 0, gps.getLineId());
}
d60.setSender(sender);
// 发送指令
int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
d60.setHttpCode(code);
if (code != 0)
d60.setErrorText("网关通讯失败, code: " + code);
dayOfDirectives.put60(d60);
return d60.getMsgId();
} catch (Exception e) {
logger.error("", e);
return -500;
}
}
/**
* 根据msg id 查询指令响应情况
*
* @param msgIds
* @return
*/
@RequestMapping("/findD60Reply")
public List<Map<String, Object>> findD60Reply(@RequestParam String msgIds) {
List<Map<String, Object>> rs = new ArrayList<>();
try {
Map<String, Object> map = new HashMap();
List<String> ids = Splitter.on(",").splitToList(msgIds);
D60 d60;
for (String id : ids) {
if (StringUtils.isEmpty(id))
continue;
d60 = dayOfDirectives.get(Integer.parseInt(id));
if (null == d60)
continue;
map.put("msgId", d60.getMsgId());
map.put("deviceReplyTime", d60.getReply46Time());
map.put("jsyReplyTime", d60.getReply47Time());
rs.add(map);
}
} catch (Exception e) {
logger.error("", e);
}
return rs;
}
/**
* 获取路牌下所有班次
*
* @param lineCode
* @param lpName
* @return
*/
@RequestMapping("/findByLpName")
public List<ScheduleRealInfo> findByLpName(@RequestParam String lineCode, @RequestParam String lpName, @RequestParam String secretKey) {
try {
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
List<ScheduleRealInfo> list = dayOfSchedule.getLpScheduleMap().get(lineCode + "_" + lpName);
return list;
} catch (Exception e) {
logger.error("", e);
}
return null;
}
/**
* 调整出场班次待发,并烂掉后续班次(调派用)
*
* @param dad
* @return
*/
@RequestMapping(value = "/dftzAndDestroy", method = RequestMethod.POST)
public Map<String, Object> dftzAndDestroy(@RequestBody DftzAndDestroy dad, @RequestParam String secretKey) {
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
return tccExternalService.dftz(dad);
}
/**
* 换人换车,(调派用)
* @return
*/
@RequestMapping(value = "/tccHrhc", method = RequestMethod.POST)
public Map<String, Object> hrhc(@RequestBody String jsonStr, @RequestParam String secretKey){
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY))
return null;
jsonStr = StringEscapeUtils.unescapeHtml4(jsonStr);
JSONObject rootObj = JSON.parseObject(jsonStr);
List<ChangePersonCar> cpcs = JSONArray.parseArray(rootObj.getString("cpcs"), ChangePersonCar.class);
return tccExternalService.hrhc(cpcs, rootObj.getString("tccCode"), rootObj.getString("userId"));
}
/**
* 接收应急预案信息
* @return
*/
@RequestMapping(value = "/contingencyPlan", method = RequestMethod.POST)
public Map<String, Object> contingencyPlan(@RequestBody String jsonStr, @RequestParam String secretKey){
Map<String, Object> result = new HashMap<>();
result.put("errCode", 0);
if (StringUtils.isEmpty(secretKey) || !secretKey.equals(SECRE_KEY)) {
result.put("errCode", 501);
result.put("msg", "无效的key");
return result;
}
ObjectMapper mapper = new ObjectMapper();
try {
List<Map<String,String>> emergencyList = new ArrayList();
List<Map<String,String>> emergencyListAll = new ArrayList();
List<Map> maps = mapper.readValue(jsonStr,mapper.getTypeFactory().constructParametricType(List.class, Map.class));
for (Map map : maps) {
sendUtils.sendContingencyPlan(map);
emergencyList.add((Map<String, String>) map.get("emergencyLine"));
}
for (Map.Entry<String, String> m : BasicData.lineCode2NameMap.entrySet()){
for (Map<String,String> emer : emergencyList){
for (Map.Entry<String,String> t : emer.entrySet()){
if (m.getKey().equals(t.getKey())) {
emergencyListAll.add(emer);
Line line = lineRepository.findById(Integer.parseInt(t.getKey())).get();
line.setName(t.getValue());
Map<String, Object> status = service.update(line);
logger.info("应急接口修改线路名称-----------------------"+status.get("status"));
}
}
}
}
} catch (IOException e) {
result.put("errCode", 502);
result.put("msg", "json数据格式异常");
logger.info("", e);
}
return result;
}
}