-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainclass.cpp
239 lines (210 loc) · 7.17 KB
/
mainclass.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "mainclass.h"
#include <momamission.hpp>
#include <gedimission.hpp>
#include <backports/qcommandlineparser.h>
MomGse::MomDataInterpretationFactory momaDataInterpFactory;
GediGse::GediDataInterpretationFactory gediDataInterpFactory;
using GseLib::DataInterpretation;
using GseLib::TmMeta;
using GseLib::TmDatabaseRow;
using GseLib::TMPacketPtr;
using GseLib::TelemetryFile;
using MomGse::MetaMarker;
// Declare type_pair struct used in type_map.
struct type_pair{
QString name;
int count;
};
QVector<int> extractHkIds(const QString& hkidArg)
{
QVector<int> hkids;
const QStringList tokens = hkidArg.split(',', QString::SkipEmptyParts);
foreach (const QString & token, tokens)
{
bool success;
int hkid = token.toInt(&success);
if (success)
{
if (!hkids.contains(hkid))
hkids.push_back(hkid);
}
else
{
// Assume a range of HKIDs, such as "5-10"
QStringList highAndLow = token.split('-');
if (highAndLow.size() != 2)
{
qDebug() << token << " <-- invalid range";
throw std::invalid_argument("invalid range of HKIDs");
}
bool low_is_ok;
bool high_is_ok;
int low_hkid = highAndLow[0].toInt(&low_is_ok);
int high_hkid = highAndLow[1].toInt(&high_is_ok);
if ((!low_is_ok || !high_is_ok) || (low_hkid >= high_hkid))
{
qDebug() << token << " <-- invalid range";
throw std::invalid_argument("invalid range of HKIDs");
}
for (int curr_hkid = low_hkid; curr_hkid <= high_hkid; curr_hkid++)
{
if (!hkids.contains(curr_hkid))
hkids.push_back(curr_hkid);
}
}
}
return hkids;
}
CmdLineArgs parseArguments(const QStringList & arguments)
{
QCommandLineParser parser;
parser.setApplicationDescription("Summarizes TM packet data");
parser.addHelpOption();
parser.addVersionOption();
const QCommandLineOption fileOption(QStringList() << "f" << "file",
"Path to TM file to extract data from", "FILE");
parser.addOption(fileOption);
const QCommandLineOption apidOption(QStringList() << "a" << "apid", "List of APIDs to check if exists in TM file. Ex. 1,2,3 or 1-3", "IDs");
parser.addOption(apidOption);
parser.process(arguments);
CmdLineArgs results;
// Sets tmFileName to flagged name
results.tmFileName = parser.value(fileOption);
// No option provided, search the current directory
if (results.tmFileName.isEmpty())
{
TmMeta tmMeta(QDir::current().absolutePath());
if (tmMeta.isValid())
results.tmFileName = tmMeta.tmFileName();
}
//Checks for existance of file
const QFileInfo tmFileInfo(results.tmFileName);
if (!tmFileInfo.exists() || !tmFileInfo.isFile())
{
std::cerr << "tmfile does not exist: " << results.tmFileName << "\n\n";
parser.showHelp(1);
}
results.aPids = extractHkIds(parser.value(apidOption));
return results;
}
bool isMomaMission(const TmMeta & tmMeta)
{
return tmMeta.mission().compare("moma", Qt::CaseInsensitive) == 0;
}
bool isGediMission(const TmMeta & tmMeta)
{
return tmMeta.mission().compare("gedi", Qt::CaseInsensitive) == 0;
}
GseLib::TelemetryFilePtr createTmFile(const QString& tmFileName)
{
QFileInfo tmFileInfo(tmFileName);
TmMeta tmMeta(tmFileInfo.absolutePath());
if (!tmMeta.isValid())
{
std::cerr << "Valid tm.meta is required" << std::endl;
exit(1);
}
if (isMomaMission(tmMeta))
{
GseLib::Config699::setDefaultGroup(GseLib::Config699::MOMA_GROUP);
DataInterpretation::setFactory(&momaDataInterpFactory);
DataInterpretation::current().loadDatabase(tmMeta.patchFiles());
TelemetryFile::setSharedCache(new MomGse::MomTelemetryCache);
TelemetryFile::sharedCache()->setPacketFormat(GseLib::TelemetryCacheBase::TomPacketFormat);
TelemetryFile::setSharedCacheFile(tmFileInfo.absoluteFilePath().toStdString());
}
else if (isGediMission(tmMeta))
{
GseLib::Config699::setDefaultGroup(GseLib::Config699::GEDI_GROUP);
DataInterpretation::setFactory(&gediDataInterpFactory);
DataInterpretation::current().loadDatabase(tmMeta.patchFiles());
TelemetryFile::setSharedCache(new GediGse::GediTelemetryCache);
TelemetryFile::sharedCache()->setPacketFormat(GseLib::TelemetryCacheBase::CcsdsPacketFormat);
TelemetryFile::setSharedCacheFile(tmFileInfo.absoluteFilePath().toStdString());
}
else
{
std::cerr << "Unsupported mission: " << tmMeta.mission().toStdString() << std::endl;
exit(1);
}
return GseLib::TelemetryFilePtr(new GseLib::TelemetryFile);
}
bool summarizeData(GseLib::TelemetryFilePtr tmFile, QVector<int> aPids)
{
//
// Creates a map type_map that stores the type and # of ocurrences
// Reads from tmFile and adds packet to map
//
TMPacketPtr pkt;
int sum = 0;
std::map<int, type_pair> type_map;
while ((pkt = tmFile->getNext()) != tmFile->end())
{
int type = pkt->type();
QString name = pkt->typeName();
if (type_map.count(type)){
type_map[type].count ++;
}
else {
type_map[type].count = 1;
type_map[type].name = name;
}
}
// If aPids is empty, output all found types
// Else, iterate through aPids, check if it exists in type_map, output
// If not found, exit()
std::cout << "Type_Name\tType/APID\tPackets_Found" << "\n";
if(aPids.isEmpty()){
for(std::map<int,type_pair>::iterator it = type_map.begin(); it != type_map.end(); ++it) {
std::cout << type_map[it->first].name << "\t" <<it->first << "\t" << type_map[it->first].count << "\n";
sum += type_map[it->first].count;
}
}
else {
bool flag = false;
for(int i=0; i < aPids.size(); i++){
if(type_map.count(aPids[i])){
std::cout << type_map[aPids[i]].name << "\t" << aPids[i] << "\t" << type_map[aPids[i]].count << "\n";
sum += type_map[aPids[i]].count;
}
else {
flag = true;
continue;
}
flag = false;
}
if(flag){
return false;
}
}
std::cout << "Total packets: " << sum << "\n";
return true;
}
MainClass::MainClass(QObject *parent) :
QObject(parent)
{
}
void MainClass::run()
{
CmdLineArgs args = parseArguments(qApp->arguments());
GseLib::TelemetryFilePtr tmFile = createTmFile(args.tmFileName);
if (!summarizeData(tmFile, args.aPids)){
::exit(1);
}
quit();
}
/**
* Triggers the application to quit by signaling to the QApplication instance.
*/
void MainClass::quit()
{
emit finished();
}
// /**
// * Shortly after quit() is called the QCoreApplication will signal this routine.
// * This is a good place to delete any objects that were created in the
// * constructor and/or to stop any threads.
// */
void MainClass::aboutToQuitApp()
{
}