owlps/owlps-positioning/inputlogcsv.cc

82 lines
1.8 KiB
C++
Raw Normal View History

#include "inputlogcsv.hh"
#include "posexcept.hh"
#include "mobile.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().get_timestamp_ms() ;
csv_line << ";0;0;0;0" ; // TODO: handle calibration request
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() ;
}