DayOfDirectives.java
4.35 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
package com.bsth.data.directive;
import com.alibaba.fastjson.JSONObject;
import com.bsth.data.LineConfigData;
import com.bsth.data.schedule.DayOfSchedule;
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.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
/**
*
* @ClassName: CurrDayDirectives
* @Description: TODO(当天指令数据)
* @author PanZhao
* @date 2016年8月14日 下午5:23:59
*
*/
@Component
public class DayOfDirectives {
// 当日60指令缓存
private static ConcurrentMap<Integer, D60> d60Map;
// 线路切换指令 64
public static ConcurrentMap<String, D64> d64Map;
//等待插入的指令
public static ConcurrentLinkedQueue<Directive> pstDirectives;
//等待更新的指令
public static ConcurrentLinkedQueue<D60> pstD60s;
@Autowired
DirectiveService directiveService;
@Autowired
SendUtils sendUtils;
@Autowired
LineConfigData lineConfigData;
@Autowired
private DayOfSchedule dayOfSchedule;
static Logger logger = LoggerFactory.getLogger(DayOfDirectives.class);
static{
d60Map = new ConcurrentHashMap<>();
d64Map = new ConcurrentHashMap<>();
pstDirectives = new ConcurrentLinkedQueue<>();
pstD60s = new ConcurrentLinkedQueue<>();
}
public void put60(D60 d60) {
d60Map.put(d60.getMsgId(), d60);
//等待持久化
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;
}
//更新60数据
pstD60s.add(d60);
ScheduleRealInfo sch = d60.getSch();
if (null == sch)
return;
if (d60.isDispatch()) {
// 更新班次状态
sch.setDirectiveState(res.getStatus() * 100);
dayOfSchedule.save(sch);
// 通知页面
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 {
logger.info(d64.getDeviceId() + "_" + d64.getData().getLineId() + "响应:" + data.getShort("requestAck"));
/*d64.setRespAck(data.getShort("requestAck"));
// 持久化*/
//64 响应不入库了...
}
}
}
@Autowired
DirectivesPstThread directivesPstThread;
public void clearAll(){
d60Map = new ConcurrentHashMap<>();
d64Map = new ConcurrentHashMap<>();
logger.info("清除指令数据 ,,,");
}
public Collection<D60> all60(){
return d60Map.values();
}
public Collection<D64> all64(){
return d64Map.values();
}
public D60 get(Integer msgId){
return d60Map.get(msgId);
}
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());
}
}
}