DirectiveBuffer.java
6.3 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
package com.bsth.vehicle.directive.buffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
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.JSON;
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.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.LineChange;
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.vehicle.directive.util.HttpUtils;
import com.bsth.websocket.handler.RealControlSocketHandler;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
/**
*
* @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 lineChangeRepository;
@Autowired
DirectiveService directiveService;
/**
* 等待入库的调度指令
*/
public static LinkedList<Directive60> transientList;
/**
* 等待确认的线路切换指令
*/
public static Map<String, LineChange> changeMap;
/**
* 当日调度指令缓存
*/
private static Map<Integer, Directive60> directiveMap;
@Autowired
Directive60Repository d60Repository;
/**
* 驾驶员上报数据
* {K: 线路编码}
*/
private static Multimap<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;
}
if(directive.isDispatch()){
//更新班次状态
ScheduleRealInfo sch = directive.getSch();
sch.setDirectiveState(reply.getStatus() * 100);
ScheduleBuffer.persistentList.add(sch);
//通知页面
directiveService.sendDirectiveToPage(sch);
}
transientList.add(directive);
}
/**
*
* @Title: reply64
* @Description: TODO(64 协议响应)
* @throws
*/
public void reply64(JSONObject json){
String key = json.getString("deviceId") + "_" + json.getString("timestamp");
LineChange change = changeMap.get(key);
if(null == change)
logger.warn("64响应 -找不到请求源,json: " + json);
else{
JSONObject data = json.getJSONObject("data");
changeMap.remove(key);
if(null == data)
logger.warn("64响应 data is null ,json: " + json);
else{
change.setRespAck(data.getShort("requestAck"));
//响应入库
lineChangeRepository.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(){
Iterator<Directive60> iterator = directiveMap.values().iterator();
List<Directive60> pList = new ArrayList<>();
Directive60 d60;
while(iterator.hasNext()){
d60 = iterator.next();
if(!d60.isPersistent())
pList.add(d60);
}
d60Repository.save(pList);
}
/**
*
* @Title: clear
* @Description: TODO(清理缓存)
* @throws
*/
public static void clear(){
transientList = new LinkedList<>();
directiveMap = new HashMap<>();
reportMultiMap = ArrayListMultimap.create();
changeMap = new HashMap<>();
}
}