LineConfigData.java
4.28 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
package com.bsth.data;
import com.bsth.Application;
import com.bsth.entity.Line;
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().equals("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().equals("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.setReadReverse(true);
conf.setLine(line);
//开始运营时间
conf.setStartOpt("02:00");
//出场时间类型
conf.setOutConfig(0);
//调度指令模板
conf.setSchDirectiveTemp("");
//应急停靠默认值
conf.setYjtkStart("00:00");
conf.setYjtkEnd("23:59");
set(conf);
}
/**
* 重新从数据库加载
*/
public void reload() {
Iterator<LineConfig> itr = lineConfigService.findAll().iterator();
while (itr.hasNext())
setBuffer(itr.next());
}
}