Skip to content
Snippets Groups Projects
apdUnitCreator.cpp 4.73 KiB
Newer Older
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

#include <productiondatabaseclient.h>
#include <boost/program_options.hpp>

using namespace std;
using namespace ProductionDatabase;
namespace po = boost::program_options;

struct unitInfo {
    std::string redSerial = "";
    std::string blueSerial = "";
    std::string crystalSerial = "";
    std::string barCode = "";
};

static string fileName = "serials.dat";
static int boxNo = 0;
static bool debug = false;
static string username = "";
static string password = "";

static vector<unitInfo> newUnits;

void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]);
void loadSerialsFromFileName(string m_fileName, bool m_debug = false);

int main(int argc, char* argv[]) {
    processArgumentsAndQueryMissing(argc, argv);
Jan Reher's avatar
Jan Reher committed
    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);

Jan Reher's avatar
Jan Reher committed
    if (username == "" || password == "") proddb->queryCredentials();
    else proddb->setCredentials(username, password);
    DatabaseClientResponse response = proddb->checkConnectivityAndCredentials();
    if (response != Successful ) {
        cerr << "Connection to database failed because of error: " << response << endl;
        return response;
    }
    if (debug) cout << "Connection successful!" << endl;
    int nFailed = 0;
Jan Reher's avatar
Jan Reher committed
    for (auto newUnit = newUnits.begin(); newUnit < newUnits.end(); newUnit++) {
        try {
            proddb->createApdCapsule(newUnit->redSerial, newUnit->blueSerial);
            std::string capsuleSerial = newUnit->blueSerial + "/" + newUnit->redSerial;
            proddb->createApdUnit(capsuleSerial, newUnit->crystalSerial, newUnit->barCode);
        }
        catch (std::exception &e) {
            nFailed++;
            std::cerr << "An error occurred on APD paid with red APD " << newUnit->redSerial << " and blue APD " << newUnit->blueSerial << "!" << std::endl;
            std::cerr << e.what() << std::endl << std::endl;
    cout << "Created " << newUnits.size() << " APD Capsules and assigned them to Units." << std::endl;
    if (nFailed > 0)
        cerr << "Data entry failed for " << nFailed << " entries!" << std::endl;
}

void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]) {
    po::options_description desc("Available options");
    desc.add_options()
            ("help", "produce help message")
            ("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);
    }
    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;
    }
}

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);
    }


    int pos = 1;

    string line = "";
    while (in.good()) {
        getline(in,line);
        if (line.length() == 0 || !isdigit(line[0])) {
          pos++;
          continue;
        }
        std::stringstream linestream(line);
        linestream >> newUnit.redSerial >> newUnit.blueSerial >> newUnit.crystalSerial >> barCode;

        if (barCode <1000000 )
        barCode += 1909000000;
        newUnit.barCode = std::to_string(barCode);

        std::cerr << "Found new unit with RS = " << newUnit.redSerial << ", BS = " << newUnit.blueSerial << ", CS = " << newUnit.crystalSerial << ", BC = " << newUnit.barCode << std::endl;
        newUnits.push_back(newUnit);
        if (m_debug) cout << "Found serial: " << line << endl;
    }

    cerr << "Found " << newUnits.size() << " new Units to be entered." << endl;