DirectiveBuffer.java
3.62 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
package com.bsth.vehicle.directive.buffer;
import java.util.HashMap;
import java.util.LinkedList;
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.vehicle.directive.entity.Directive;
import com.bsth.vehicle.directive.entity.DirectiveReply;
import com.bsth.vehicle.directive.entity.DriverReport;
import com.bsth.vehicle.directive.entity.LineChange;
import com.bsth.vehicle.directive.repository.DriverReportRepository;
import com.bsth.vehicle.directive.repository.LineChangeRepository;
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
DriverReportRepository driverReportRepository;
@Autowired
LineChangeRepository lineChangeRepository;
/**
* 等待入库的调度指令
*/
public static LinkedList<Directive> transientList;
/**
* 等待确认的线路切换指令
*/
public static Map<String, LineChange> changeMap;
/**
* 当日调度指令缓存
*/
private static Map<Integer, Directive> directiveMap;
/**
* 驾驶员上报数据
* {K: 线路编码}
*/
private static Multimap<Integer, DriverReport> reportMultiMap;
static{
transientList = new LinkedList<>();
directiveMap = new HashMap<>();
reportMultiMap = ArrayListMultimap.create();
changeMap = new HashMap<>();
}
public static void put(Directive 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;
}
Directive 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()){
directive.getSch().setDirectiveState(reply.getStatus() * 100);
}
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(DriverReport report){
logger.info("驾驶员上报");
//实时入库
driverReportRepository.save(report);
reportMultiMap.put(report.getData().getLineId(), report);
}
}