LineConfigData.java
5.08 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
package com.bsth.data;
import com.bsth.Application;
import com.bsth.entity.Line;
import com.bsth.entity.realcontrol.D80ReplyTemp;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.service.LineService;
import com.bsth.service.realcontrol.LineConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author PanZhao
* @ClassName: LineConfigData
* @Description: TODO(线路配置数据管理)
* @date 2016年8月15日 下午2:50:19
*/
@Component
@Order(value = 2)
public class LineConfigData implements CommandLineRunner {
static Logger logger = LoggerFactory.getLogger(LineConfigData.class);
// 线路编码和配置
private Map<String, LineConfig> lineConfMap;
@Autowired
LineConfigService lineConfigService;
@Autowired
LineService lineService;
//入库缓冲
static LinkedList<LineConfig> saveBuffers = new LinkedList<>();
@Autowired
LineConfigPersistThread configPersistThread;
@Override
public void run(String... arg0) throws Exception {
lineConfMap = new HashMap<>();
Iterator<LineConfig> itr = lineConfigService.findAll().iterator();
while (itr.hasNext())
setBuffer(itr.next());
//异步入库
Application.mainServices.scheduleWithFixedDelay(configPersistThread, 60, 60, TimeUnit.SECONDS);
}
/**
* 起点发出,应用缓冲区设置参数
* @param sch
* @param timestamp
* @return
*/
public long applyOut(ScheduleRealInfo sch, Long timestamp) {
LineConfig config = lineConfMap.get(sch.getXlBm());
int diff = sch.getXlDir()=="0"?config.getUpOutDiff():config.getDownOutDiff();
return timestamp - (diff * 1000);
}
/**
* 终点到达,应用缓冲区设置参数
* @param sch
* @param timestamp
* @return
*/
public long applyIn(ScheduleRealInfo sch, Long timestamp){
LineConfig config = lineConfMap.get(sch.getXlBm());
int diff = sch.getXlDir()=="0"?config.getUpInDiff():config.getDownInDiff();
return timestamp - (diff * 1000);
}
@Component
private static class LineConfigPersistThread extends Thread {
@Autowired
LineConfigService lineConfigService;
@Override
public void run() {
LineConfig config;
for (int i = 0; i < 800; i++) {
config = saveBuffers.poll();
if (config == null)
break;
try {
lineConfigService.save(config);
}catch (Exception e){
logger.error("", e);
}
}
}
}
public LineConfig get(String lineCode) {
return lineConfMap.get(lineCode);
}
public Collection<LineConfig> getAll() {
return lineConfMap.values();
}
public void set(LineConfig conf) {
//lineConfigService.save(conf);
saveBuffers.add(conf);
setBuffer(conf);
}
public void setBuffer(LineConfig conf) {
lineConfMap.put(conf.getLine().getLineCode(), conf);
}
/**
* @Title: init
* @Description: TODO(初始化配置信息)
*/
public void init(String lineCode) throws Exception {
LineConfig conf = new LineConfig();
//线路
Line line = lineService.findByLineCode(lineCode);
if (null == line)
throw new NullPointerException("异常的lineCode");
conf.setLine(line);
//开始运营时间
conf.setStartOpt("02:00");
//托管状态
conf.setTrust(true);
//出场时间类型
conf.setOutConfig(0);
//进场时间类型
//conf.setInConfig(1);
//短语模板
conf.setPhraseTemps("");
//调度指令模板
conf.setSchDirectiveTemp("");
//80指令回复 闵行用
D80ReplyTemp t50 = new D80ReplyTemp(conf, (short) 0x50, "同意,回电详谈", "不同意,请回电"), t60 = new D80ReplyTemp(conf, (short) 0x60, "同意,回电详谈", "不同意,请回电"), tA2 = new D80ReplyTemp(conf, (short) 0xA2, "同意,回电详谈", "不同意,请回电"), t70 = new D80ReplyTemp(conf, (short) 0x70, "同意,回电详谈", "不同意,请回电"), t11 = new D80ReplyTemp(conf, (short) 0x11, "同意,回电详谈", "不同意,请回电");
//应急停靠默认值
conf.setYjtkStart("00:00");
conf.setYjtkEnd("23:59");
Set<D80ReplyTemp> temps = conf.getD80Temps();
temps.add(t50);
temps.add(t60);
temps.add(tA2);
temps.add(t70);
temps.add(t11);
set(conf);
}
}