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

#include <productiondatabaseclient.h>
#include <boost/program_options.hpp>
#include "toolbox.cpp"
#include "BulkDbAccess.h"

#include "QtGui/QApplication"

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

static string fileName = "serials.dat";
static string username = "";
static string password = "";
static short irradiationDay = 0;
static short irradiationMonth = 0;
static short irradiationYear = 0;
static string date = "";
static double temp = 10000;
static double duration = -1;
static string change = "";
static bool debug = false;

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 for serial numbers [serials.dat]")
      ("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!")
      ("day", po::value<short>(), "Day of arrival")
      ("month", po::value<short>(), "Month of arrival")
      ("year", po::value<short>(), "Year of arrival")
      ("temp", po::value<double>(), "Temperature during annealing")
      ("time", po::value<double>(), "Duration of annealing")
      ("debug", "emit additional messages for debugging purposes")
      ;

  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("day")) {
    irradiationDay = vm["day"].as<short>();
  }
  if (vm.count("month")) {
    irradiationMonth = vm["month"].as<short>();
  }
  if (vm.count("year")) {
    irradiationYear = vm["year"].as<short>();
  }
  if (vm.count("temp")) {
    temp = vm["temp"].as<double>();
  }
  if (vm.count("time")) {
    duration = vm["time"].as<double>();
  }
  if (vm.count("debug")) {
    debug = true;
  }
}

int main(int argc, char* argv[]) {
  QApplication *_app = new QApplication(argc, argv);

  processArgumentsAndQueryMissing(argc, argv);

  if (irradiationDay <= 0 | irradiationDay > 31 | irradiationMonth <= 0 | irradiationMonth > 12 | irradiationYear < 1000) {
    cerr << "Please use the '--day', '--month' and '--year' options to enter a valid date!" << std::endl;
    exit(1);
  }
  if (duration < 0 | temp > 500) {
    cerr << "Please use the '--dose' and '--temp' options to enter irradiation dose and temperature!" << std::endl;
    exit(1);
  }

  vector<string> apdSerials;

  apdSerials = loadSerialsFromFileName(fileName);

  if ( apdSerials.size() == 0 ) {
    cout << "No data to write! Exiting." << endl;
    return 0;
  }
  cout << "Data will be written for " << apdSerials.size() << " APDs." << endl;

  ProductionDatabaseClient *_proddbclient = new ProductionDatabaseClient();

  if (username == "" || password == "") _proddbclient->queryCredentials();
  else _proddbclient->setCredentials(username, password);
  DatabaseClientResponse response = _proddbclient->checkConnectivityAndCredentials();
  if (response != Successful ) {
    cerr << "Connection to database failed because of error: " << response << endl;
    return response;
  }
  if (debug) cout << "Connection successful!" << endl;

  _proddbclient->storeApdAnnealingInfo(apdSerials,temp, duration, irradiationDay,irradiationMonth,irradiationYear);

  cout << "\nSuccessfully entered annealing for " << duration << " hours at " << temp << " degrees celsius on " << irradiationDay << "." << irradiationMonth << "." << irradiationYear << " to APDs from serial file " << fileName << "!\n\nThank you for using the ApdToolbox! :)" << endl << endl;

  _app->exit(0);
  return (-apdSerials.empty());
}