Newer
Older
#include <boost/program_options.hpp>
namespace po = boost::program_options;
static string fileName = "serials.dat";
static int boxNo = 0;
static bool debug = false;
static string username = "";
static string password = "";
static vector<string> apdSerials;
static std::vector<int> boxes;
static std::vector<int> positions;
void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]);
void loadSerialsFromFileName(string m_fileName, bool m_debug = false);
ProductionDatabaseClient *proddb = new ProductionDatabaseClient();
if (debug) cout << "Intitialized apdBoxSetter with version " << proddb->getVersion() << " of the database access libraries." << endl << endl
<< "Now trying to set Batch number " << boxNo << " for ADPs from serial file " << fileName << endl;
loadSerialsFromFileName(fileName);
if (username == "" || password == "") proddb->queryCredentials();
else proddb->setCredentials(username, password);
DatabaseClientResponse response = proddb->checkConnectivityAndCredentials();
cerr << "Connection to database failed because of error: " << response << endl;
if (debug) cout << "Connection successful!" << endl;
proddb->storeApdBoxNumber(apdSerials,boxes,positions);
cout << "\nBox " << boxNo << " was successfully assigned to APDs from serial file " << fileName << ". At least I hope so. In any case, something happened for " << apdSerials.size() << " APDs."
<< "\nThank you for using apdBoxSetter! :)" << endl << endl;
void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]) {
po::options_description desc("Available options");
desc.add_options()
("help", "produce help message")
("type", po::value<string>(), "(required) type of APDs [new, irr]")
("box", po::value<int>(), "(required) box number to be assigned")
("fileName", po::value<string>(), "file name or serial numbers [serials.dat]")
("debug", "emit additional messages for debugging purposes")
("user", po::value<string>(), "User name to connect to DB. Only used when combined with pass!")
("pass", po::value<string>(), "Password to connect to DB. Only used when combined with user!")
;
po::variables_map vm;
po::store(po::parse_command_line(m_argc, m_argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(0);
}
boxNo = int(vm["box"].as<int>());
} else {
cout << "Which box should these APDs be assigned to?" << endl;
try {
} catch (...) {
}
cerr << "Invalid batch number!" << endl;
}
if (vm.count("fileName")) {
fileName = vm["fileName"].as<string>();
cout << "Reading serials from " << fileName << endl;
}
if (vm.count("user")) {
username = vm["user"].as<string>();
}
if (vm.count("pass")) {
password = vm["pass"].as<string>();
}
if (vm.count("debug")) {
debug = true;
}
}
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
void loadSerialsFromFileName(string m_fileName, bool m_debug) {
ifstream in(m_fileName.c_str());
if (!in.good()) {
cerr << "Could not load serials. Does the input file exist?" << endl;
exit(0);
}
apdSerials.clear();
positions.clear();
boxes.clear();
int pos = 1;
string line = "";
while (in.good()) {
getline(in,line);
if (line.length() == 0 || !isdigit(line[0])) {
pos++;
continue;
}
apdSerials.push_back(line);
boxes.push_back(boxNo);
positions.push_back(pos++);
if (m_debug) cout << "Found serial: " << line << endl;
}
if (apdSerials.size() != positions.size() || apdSerials.size() != boxes.size()) {
cerr << "ERROR: Array sizes don't match!" << endl;
apdSerials.clear();
boxes.clear();
positions.clear();
return;
}
cerr << "Found " << apdSerials.size() << " APDs" << endl;
return;
}