-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTurnout.cpp
More file actions
154 lines (135 loc) · 3.28 KB
/
Turnout.cpp
File metadata and controls
154 lines (135 loc) · 3.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
#include "Turnout.h"
Turnout::Turnout(log4cpp::Category *logger)
{
//ctor
closed_code = 2;
throw_code = 4;
this->logger = logger;
turnoutFile = "";
}
Turnout::~Turnout()
{
//dtor
}
int Turnout::load(string fileName){
turnoutFile = fileName;
string key;
int value;
logger->debug("[Turnout] Turnout opening file %s",fileName.c_str());
try{
ifstream infile(fileName);
while (infile >> key >> value){
logger->debug("[Turnout] Adding turnout %s %d",key.c_str(),value);
addTurnout(key,value);
}
infile.close();
}
catch(...){
return 0;
}
//return number of turnouts entries
return turnouts_code.size();
}
int Turnout::reload(){
if (turnoutFile.size() > 0){
turnouts_code.clear();
turnouts.clear();
return load(turnoutFile);
}
return 0;
}
string Turnout::getStartInfo(){
stringstream ss;
ss << "PTT";
ss << DELIM_BRACET;
ss << "Turnouts";
ss << DELIM_KEY;
ss << "Turnout";
ss << DELIM_BRACET;
ss << "Closed";
ss << DELIM_KEY;
ss << closed_code;
ss << DELIM_BRACET;
ss << "Thrown";
ss << DELIM_KEY;
ss << throw_code;
//PTT]\[Turnouts}|{Turnout]\[Closed}|{2]\[Thrown}|{4
if (turnouts_code.size() == 0){
ss << "\n";
logger->debug("[Turnout] Turnout start info %s",ss.str().c_str());
return ss.str();
}
ss << "\nPTL";
std::map<string,int>::iterator it = turnouts_code.begin();
while(it != turnouts_code.end())
{
ss << DELIM_BRACET;
ss << "MT+";
ss << it->second;
ss << ";-";
ss << it->second;
ss << DELIM_KEY;
ss << it -> first;
ss << DELIM_KEY;
ss << closed_code;
it++;
}
logger->debug("[Turnout] Turnout start info %s",ss.str().c_str());
return ss.str();
}
string Turnout::getTurnoutMsg(int tcode){
int v = closed_code;
if (getTurnoutState(tcode) == TURNOUT_STATE::THROWN){
v = throw_code;
}
stringstream ss;
ss << "PTA";
ss << v;
ss << "MT+";
ss << tcode;
ss << ";-";
ss << tcode;
logger->debug("[Turnout] Turnout message %s",ss.str().c_str());
return ss.str();
}
int Turnout::getCloseCode(){
return closed_code;
}
int Turnout::getThrownCode(){
return throw_code;
}
void Turnout::CloseTurnout(int tcode){
if (turnouts.count(tcode) > 0){
turnouts[tcode] = TURNOUT_STATE::CLOSED;
}
}
void Turnout::ThrownTurnout(int tcode){
if (turnouts.count(tcode) > 0){
turnouts[tcode] = TURNOUT_STATE::THROWN;
}
}
TURNOUT_STATE Turnout::getTurnoutState(int tcode){
if (turnouts.count(tcode) > 0){
return turnouts[tcode];
}
return TURNOUT_STATE::UNKNOWN;
}
int Turnout::getTurnoutCode(string tname){
if (turnouts_code.count(tname) > 0){
return turnouts_code[tname];
}
return 0;
}
int Turnout::size(){
return turnouts.size();
}
void Turnout::addTurnout(string tname,int tcode){
if (turnouts.count(tcode)==0){
turnouts_code.insert(pair<string,int>(tname,tcode));
turnouts.insert(pair<int,TURNOUT_STATE>(tcode,TURNOUT_STATE::UNKNOWN));
}
}
bool Turnout::exists(int tcode){
if (turnouts.count(tcode)>0){return true;}
return false;
}