DayOfDirectives.java
4.97 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
package com.bsth.data.directive;
import com.alibaba.fastjson.JSONObject;
import com.bsth.data.LineConfigData;
import com.bsth.entity.directive.D60;
import com.bsth.entity.directive.D64;
import com.bsth.entity.directive.Directive;
import com.bsth.entity.directive.DirectiveReponse;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.service.directive.DirectiveService;
import com.bsth.websocket.handler.SendUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
/**
*
* @ClassName: CurrDayDirectives
* @Description: TODO(当天指令数据)
* @author PanZhao
* @date 2016年8月14日 下午5:23:59
*
*/
@Component
public class DayOfDirectives {
// 当日60指令缓存
private static Map<Integer, D60> d60Map;
// 线路切换指令 64
public static Map<String, D64> d64Map;
//等待C0_A4回复的用户
//public static Map<K, V>
//等待入库的指令
public static LinkedList<Directive> pstDirectives;
@Autowired
DirectiveService directiveService;
@Autowired
SendUtils sendUtils;
@Autowired
LineConfigData lineConfigData;
static Logger logger = LoggerFactory.getLogger(DayOfDirectives.class);
static{
d60Map = new HashMap<>();
d64Map = new HashMap<>();
pstDirectives = new LinkedList<>();
}
public void put60(D60 d60, boolean pst) {
d60Map.put(d60.getMsgId(), d60);
//等待持久化
if(pst)
pstDirectives.add(d60);
}
public void put64(D64 d64) {
d64Map.put(d64.getKey(), d64);
//等待持久化
pstDirectives.add(d64);
}
/**
*
* @Title: reply @Description: TODO(指令 46,47 响应) @throws
*/
public void reply(DirectiveReponse res) {
Integer msgId = res.getMsgId();
if (msgId == null) {
logger.error("reply error , msgId is null.");
return;
}
D60 d60 = d60Map.get(msgId);
if (null == d60) {
logger.error("找不到msgId: " + msgId + " 对应的指令数据");
return;
}
switch (res.getStatus()) {
case 0:
d60.setReply46((short) -1);// 失败
break;
case 1:
d60.setReply46((short) 0);// 发送成功
d60.setReply46Time(System.currentTimeMillis());
break;
case 2:
d60.setReply47((short) 0);// 驾驶员阅读
d60.setReply47Time(System.currentTimeMillis());
break;
}
// 等待持久化
if(!pstDirectives.contains(d60))
pstDirectives.add(d60);
ScheduleRealInfo sch = d60.getSch();
if (null == sch)
return;
if (d60.isDispatch()) {
// 更新班次状态
sch.setDirectiveState(res.getStatus() * 100);
// 通知页面
sendUtils.sendDirectiveToPage(sch);
}
}
/**
*
* @Title: reply64 @Description: TODO(64 协议响应) @throws
*/
public void reply64(JSONObject json) {
String key = json.getString("deviceId") + "_" + json.getString("timestamp");
D64 d64 = d64Map.get(key);
if (null == d64)
logger.warn("64响应 -找不到请求源,json: " + json);
else {
JSONObject data = json.getJSONObject("data");
if (null == data)
logger.warn("64响应 data is null ,json: " + json);
else {
d64.setRespAck(data.getShort("requestAck"));
// 持久化
if(!pstDirectives.contains(d64))
pstDirectives.add(d64);
}
}
}
/* private void saveD60(D60 d60) {
// 等47再入库
if (d60.getReply47() == null)
return;
directiveService.save(d60);
}*/
public void clear(String device){
int c60 = 0, c64 = 0;
//找到该设备的60数据
Collection<D60> d60s = d60Map.values();
List<D60> rem60List = new ArrayList<>();
for(D60 d60 : d60s){
if(d60.getDeviceId().equals(device))
rem60List.add(d60);
}
//清除60数据
for(D60 d60 : rem60List){
if(d60.getReply47() == null)
directiveService.save(d60);
if(null != d60Map.remove(d60.getMsgId()))
c60 ++;
}
if(c60 > 0)
logger.info("清除60数据 ," + c60);
//找到该设备的64数据
Collection<D64> d64s = d64Map.values();
List<D64> rem64List = new ArrayList<>();
for(D64 d64 : d64s){
if(d64.getDeviceId().equals(device))
rem64List.add(d64);
}
//清除64数据
for(D64 d64 : rem64List){
if(d64.getRespAck() == null)
directiveService.save64(d64);
if(null != d64Map.remove(d64.getKey()))
c64 ++;
}
if(c64 > 0)
logger.info("清除64数据 ," + c64);
}
public Collection<D60> all60(){
return d60Map.values();
}
public Collection<D64> all64(){
return d64Map.values();
}
public Collection<Directive> all(){
List<Directive> all = new ArrayList<>();
all.addAll(d60Map.values());
all.addAll(d64Map.values());
return all;
}
public static class DComparator implements Comparator<Directive>{
@Override
public int compare(Directive d1, Directive d2) {
return (int) (d2.getTimestamp() - d1.getTimestamp());
}
}
}