T2Life.cc
Go to the documentation of this file.
1 #include "T2Life.h"
2 #include <string>
3 #include <fstream>
4 #include <sstream>
5 #include <vector>
6 #include <iostream>
7 #include <utl/Reader.h>
8 #include <fwk/CentralConfig.h>
9 
10 using namespace utl;
11 using namespace sdet;
12 using namespace std;
13 
14 
15 T2Life* T2Life::gfT2Life = nullptr;
16 
17 
18 T2Life::T2Life()
19 {
20  Branch tb =
21  fwk::CentralConfig::GetInstance()->GetTopBranch("T2LifeFileManager").GetChild("T2Files").GetFirstChild();
22 
23  while (tb) {
24  tb.GetChild("start").GetData(fCurrentFileStart);
25  tb.GetChild("end").GetData(fCurrentFileEnd);
26  string filename;
27  tb.GetChild("filename").GetData(filename);
28 
29  fFile = new ifstream(filename.c_str());
30 
31  fFiles.insert(make_pair(fCurrentFileStart, make_pair(fCurrentFileEnd, fFile)));
32  tb = tb.GetNextSibling();
33  }
34 }
35 
36 
37 T2Life&
38 T2Life::GetT2Life()
39 {
40  if (!gfT2Life)
41  gfT2Life = new T2Life();
42 
43  return *gfT2Life;
44 }
45 
46 
47 const vector<unsigned int>&
48 T2Life::GetValidStations(unsigned int theGPSSecond)
49 {
50  SetFile(theGPSSecond);
51 
52  if (theGPSSecond < fCurrentStop && theGPSSecond >= fCurrentStart)
53  return fValidStations;
54 
55  if (theGPSSecond < fCurrentStart) { // requested earlier time
56  fFile->seekg(0);
57  cerr << "Rewinding T2Life file. Why?" << endl;
58  }
59 
60  fValidStations.clear();
61  string line;
62  istringstream buffer;
63  bool found = false;
64  int line_number = 0;
65  while (getline(*fFile, line)) { // find next valid line
66  ++line_number;
67  buffer.str(line);
68  unsigned int start;
69  unsigned int end;
70  buffer >> start >> end;
71 
72  if (theGPSSecond < start)
73  break;
74 
75  if (theGPSSecond < end) {
76  found = true;
77  fCurrentStart = start;
78  fCurrentStop = end;
79  break;
80  }
81  }
82 
83  if (!found) {
84  cerr << theGPSSecond << " out of range in T2Life. Why?\n"
85  << "line " << line_number << '\n'
86  << line << endl;
87  fCurrentStart = 0;
88  fCurrentStop = 0;
89  fFile->seekg(0);
90  return fValidStations;
91  }
92 
93  for (unsigned i = 0; i < 101; ++i)
94  fValidStations.push_back(0);
95 
96  int flag;
97  while (buffer >> flag)
98  fValidStations.push_back(flag);
99 
100  return fValidStations;
101 }
102 
103 
104 void
105 T2Life::SetFile(unsigned int theGPSSecond)
106 {
107  if (theGPSSecond < fCurrentFileEnd && theGPSSecond > fCurrentFileStart)
108  return;
109 
110  std::map<unsigned int, std::pair<unsigned int, std::ifstream*> >::iterator file =
111  --fFiles.upper_bound(theGPSSecond);
112 
113  fFile = file->second.second;
114  fCurrentFileStart = file->first;
115  fCurrentFileEnd = file->second.first;
116  fCurrentStart = file->first;
117  fCurrentStop = file->first;
118 }
119 
120 
121 namespace sdet {
122 
123  const std::vector<unsigned int>&
124  GetStationsStatusFromT2Files(unsigned int theGPSSecond)
125  {
126  return T2Life::GetT2Life().GetValidStations(theGPSSecond);
127  }
128 
129 }
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
Branch GetNextSibling() const
Get next sibling of this branch.
Definition: Branch.cc:284
Class representing a document branch.
Definition: Branch.h:107
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
const string file
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
const std::vector< unsigned int > & GetStationsStatusFromT2Files(unsigned int theGPSSecond)
Definition: T2Life.cc:124
Branch GetFirstChild() const
Get first child of this Branch.
Definition: Branch.cc:98
char * filename
Definition: dump1090.h:266
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)

, generated on Tue Sep 26 2023.