95 lines
2.3 KiB
C++
95 lines
2.3 KiB
C++
#include "inputlogcsv.hh"
|
|
#include "posexcept.hh"
|
|
#include "mobile.hh"
|
|
#include "calibrationrequest.hh"
|
|
#include "referencepoint.hh"
|
|
|
|
#include <sstream>
|
|
|
|
using namespace std ;
|
|
using std::tr1::unordered_map ;
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
/**
|
|
* Prepares the InputLogCSV to write to a CSV file.
|
|
* @param filename The name of the file to open.
|
|
* @throw error_opening_input_file if the file cannot be opened.
|
|
*/
|
|
InputLogCSV::InputLogCSV(const string &filename):
|
|
log_file_name(filename)
|
|
{
|
|
log_file.open(log_file_name.c_str()) ;
|
|
if (! log_file)
|
|
throw error_opening_input_file(log_file_name) ;
|
|
}
|
|
|
|
|
|
InputLogCSV::~InputLogCSV()
|
|
{
|
|
log_file.close() ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
/**
|
|
* @return true if the request has been logged successfuly, or false
|
|
* if not.
|
|
*/
|
|
bool InputLogCSV::log_request(const Request &request)
|
|
{
|
|
if (! log_file)
|
|
return false ;
|
|
|
|
log_file << request_to_csv(request).c_str() ;
|
|
return true ;
|
|
}
|
|
|
|
|
|
const string InputLogCSV::request_to_csv(const Request &request) const
|
|
{
|
|
ostringstream csv_line ;
|
|
if (request.get_mobile() != NULL)
|
|
csv_line << request.get_mobile()->get_mac_addr() ;
|
|
csv_line << ';' << request.get_timestamp() << ';' ;
|
|
|
|
const CalibrationRequest *calibration_request =
|
|
dynamic_cast<const CalibrationRequest*>(&request) ;
|
|
if (calibration_request == NULL)
|
|
csv_line << "0;0;0;0" ;
|
|
else
|
|
{
|
|
csv_line
|
|
<< calibration_request->get_reference_point()->get_x() << ';'
|
|
<< calibration_request->get_reference_point()->get_y() << ';'
|
|
<< calibration_request->get_reference_point()->get_z() << ';'
|
|
<< static_cast<int>(calibration_request->get_direction()) ;
|
|
}
|
|
|
|
const unordered_map<string, Measurement> &measurements =
|
|
request.get_measurements() ;
|
|
for (unordered_map<string, Measurement>::const_iterator i
|
|
= measurements.begin() ; i != measurements.end() ; ++i)
|
|
{
|
|
const vector<int> &ss_list = i->second.get_ss_list() ;
|
|
const string &ap_mac = i->second.get_ap() != NULL
|
|
? i->second.get_ap()->get_mac_addr()
|
|
: "" ;
|
|
for (vector<int>::const_iterator i = ss_list.begin() ;
|
|
i != ss_list.end() ; ++i)
|
|
{
|
|
csv_line << ';' << ap_mac ;
|
|
csv_line << ';' << *i ;
|
|
}
|
|
}
|
|
|
|
csv_line << '\n' ;
|
|
return csv_line.str() ;
|
|
}
|