PilotReport.java
7.84 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
package com.bsth.data.pilot80;
import com.bsth.data.BasicData;
import com.bsth.data.LineConfigData;
import com.bsth.data.gpsdata_v2.GpsRealData;
import com.bsth.data.gpsdata_v2.status_manager.GpsStatusManager;
import com.bsth.data.msg_queue.DirectivePushQueue;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.directive.D80;
import com.bsth.entity.directive.DC0_A4;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.repository.directive.D80Repository;
import com.bsth.repository.directive.DC0A4Repository;
import com.bsth.service.directive.DirectiveService;
import com.bsth.websocket.handler.SendUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author PanZhao
* @ClassName: PilotReport
* @Description: TODO(设备80协议上报处理)
* @date 2016年8月14日 下午11:37:51
*/
@Component
public class PilotReport {
@Autowired
D80Repository d80Repository;
@Autowired
DayOfSchedule dayOfSchedule;
@Autowired
LineConfigData lineConfigData;
@Autowired
DirectiveService directiveService;
@Autowired
GpsRealData gpsRealData;
@Autowired
SendUtils sendUtils;
@Autowired
DC0A4Repository dc0A4Repository;
@Autowired
GpsStatusManager gpsStatusManager;
private static ConcurrentHashMap<Integer, D80> d80Maps;
/**
* 设备 ——> 最后一条请求出场记录
*/
public static ConcurrentHashMap<String, D80> qqccMap;
Logger logger = LoggerFactory.getLogger(PilotReport.class);
static {
d80Maps = new ConcurrentHashMap<>();
qqccMap = new ConcurrentHashMap<>();
}
public void report(D80 d80) {
if (d80 == null)
return;
try {
//入库
d80Repository.save(d80);
//入缓存
d80Maps.put(d80.getId(), d80);
String nbbm = BasicData.deviceId2NbbmMap.get(d80.getDeviceId());
//上报时,在执行的班次
if(StringUtils.isNotEmpty(nbbm)){
ScheduleRealInfo sch = dayOfSchedule.executeCurr(nbbm);
if(null != sch)
d80.setSchId(sch.getId());
}
//处理
switch (d80.getData().getRequestCode()) {
//出场请求
case 0xA3:
qqccMap.put(d80.getDeviceId(), d80);
ScheduleRealInfo outSch = dayOfSchedule.searchNearByBcType(nbbm, "out");
//如果有对应出场班次
if (outSch != null && StringUtils.isEmpty(outSch.getFcsjActual())) {
//没有计划里程的出场班次,出场既是首发站,发送下一班次的调度指令
if (outSch.getJhlc() == null)
outSch = dayOfSchedule.next(outSch);
//下发调度指令
DirectivePushQueue.put6002(outSch, dayOfSchedule.doneSum(nbbm), "请出@系统", "同意出场;");
}
break;
}
//反射搜索用 瞬时字段
d80.setLineId(d80.getData().getLineId());
d80.setNbbm(d80.getData().getNbbm());
d80.setRequestCode(d80.getData().getRequestCode());
//推送到页面
sendUtils.send80ToPage(d80);
} catch (Exception e) {
logger.error("", e);
}
}
public void report(DC0_A4 c0a4) {
String deviceId = c0a4.getData().getDeviceId();
if (StringUtils.isNotEmpty(deviceId))
c0a4.setId(deviceId);
//入库
dc0A4Repository.save(c0a4);
}
/**
* @Title: reply
* @Description: TODO(调度员回复)
*/
public void reply(D80 d80) {
Short reqCode = d80.getData().getRequestCode();
switch (reqCode) {
case 0xA3:
//出场请求回复
applyOutReply(d80);
break;
case 0xA5:
//进场请求回复
applyInReply(d80);
break;
}
}
/**
* @Title: applyOutReply
* @Description: TODO(出场请求回复)
*/
public void applyOutReply(D80 d80) {
//同意
if (d80.getConfirmRs() == 0) {
String nbbm = BasicData.deviceId2NbbmMap.get(d80.getDeviceId());
ScheduleRealInfo sch = dayOfSchedule.searchNearByBcType(nbbm, "out");
if (null == sch)
return;
LineConfig conf = lineConfigData.get(sch.getXlBm());
if (conf.getOutConfig() == 1) {
try{
//最大允许时间阈值 2 小时
if(Math.abs(d80.getTimestamp() - sch.getDfsjT()) > 1000 * 60 * 60 * 2)
return;
}catch (Exception e){
logger.error("", e);
}
//为相关班次写入请求出场时间
sch.setFcsjActualAll(d80.getTimestamp());
dayOfSchedule.save(sch);
//通知页面
sendUtils.refreshSch(sch);
}
}
}
/**
* @Title: applyInReply
* @Description: TODO(进场请求回复)
*/
public void applyInReply(D80 d80) {
//同意
if (d80.getConfirmRs() == 0) {
String nbbm = BasicData.deviceId2NbbmMap.get(d80.getDeviceId());
ScheduleRealInfo sch = dayOfSchedule.nextByBcType(nbbm, "in");
if (null == sch)
return;
LineConfig conf = lineConfigData.get(sch.getXlBm());
if (conf.getOutConfig() == 1) {
//为相关班次写入进场时间
sch.setZdsjActualAll(d80.getTimestamp());
//没有里程的进场班次
if (isEmpty(sch.getBcsj()) && isEmpty(sch.getJhlc()))
sch.setFcsjActualAll(d80.getTimestamp());
dayOfSchedule.save(sch);
//通知页面
sendUtils.refreshSch(sch);
}
}
}
public boolean isEmpty(Integer v) {
return v == null || v.equals(0);
}
public boolean isEmpty(Double v) {
return v == null || v.equals(0.0);
}
/**
* @Title: unconfirmed80
* @Description: TODO(根据lineCode 获取未处理的80数据)
*/
public List<D80> unconfirmed80(String lineCode) {
List<D80> lineAll = findByLine(lineCode), rs = new ArrayList<>();
for (D80 d80 : lineAll)
if (!d80.isConfirm())
rs.add(d80);
return rs;
}
public List<D80> findByLine(String lineCode) {
List<D80> rs = new ArrayList<>();
for (D80 d80 : d80Maps.values()) {
if (d80 != null && d80.getData().getLineId().equals(lineCode))
rs.add(d80);
}
return rs;
}
public D80 findById(int id) {
return d80Maps.get(id);
}
public Collection<D80> findAll() {
return d80Maps.values();
}
public void clear(String lineCode) {
logger.info("清除 80数据 before: " + d80Maps.size());
List<D80> rems = findByLine(lineCode);
for (D80 d80 : rems) {
d80Maps.remove(d80.getId());
qqccMap.remove(d80.getDeviceId());
}
rems.clear();
logger.info("清除 80数据 after: " + d80Maps.size());
}
}