157 lines
4.4 KiB
C++
157 lines
4.4 KiB
C++
#include "inputcsv.hh"
|
|
#include "posutil.hh"
|
|
#include "posexcept.hh"
|
|
#include "stock.hh"
|
|
#include "calibrationrequest.hh"
|
|
#include "configuration.hh"
|
|
|
|
#include <iostream>
|
|
#include <boost/tr1/unordered_map.hpp>
|
|
|
|
using namespace std ;
|
|
using std::tr1::unordered_map ;
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
/**
|
|
* This function reads the next Request in the CSV input file. Blank
|
|
* lines and lines containing only spaces are skipped.
|
|
*
|
|
* #file should be opened before proceeding requests; otherwise,
|
|
* #current_request is \link Request::clear() cleared\endlink and a
|
|
* blank Request is returned. The file must be valid,
|
|
* semicolon-separated (\em not comma-separated).
|
|
*
|
|
* @return The read Request, or a blank Request in case of error (file
|
|
* not opened, end of file, invalid field or wrong number of fields in
|
|
* the line).
|
|
*/
|
|
const Request& InputCSV::get_next_request()
|
|
{
|
|
clear_current_request() ;
|
|
|
|
if (! file.next_line()) // End of file or error
|
|
return *current_request ;
|
|
++current_line_nb ;
|
|
|
|
// Read Mobile MAC field
|
|
string mac_mobile ;
|
|
if (! file.read_field(mac_mobile)) // Wrong number of fields
|
|
{
|
|
if (Configuration::is_configured("verbose"))
|
|
cerr << "InputCSV: cannot read mac_mobile.\n" ;
|
|
return *current_request ;
|
|
}
|
|
PosUtil::to_upper(mac_mobile) ;
|
|
|
|
if (! Configuration::bool_value("positioning.accept-new-mobiles") &&
|
|
! Stock::mobile_exists(mac_mobile))
|
|
return *current_request ;
|
|
|
|
const Mobile &mobile = Stock::find_create_mobile(mac_mobile) ;
|
|
current_request->set_mobile(&mobile) ;
|
|
|
|
// Read request type
|
|
uint8_t type ;
|
|
uint16_t type_r ;
|
|
if (! file.read_field(type_r))
|
|
{
|
|
// Wrong number of fields: blank current request
|
|
current_request->clear() ;
|
|
if (Configuration::is_configured("verbose"))
|
|
cerr << "InputCSV: cannot read type.\n" ;
|
|
return *current_request ;
|
|
}
|
|
type = type_r ;
|
|
current_request->set_type(type) ;
|
|
|
|
// Read Timestamp field
|
|
Timestamp timestamp ;
|
|
if (! file.read_timestamp(timestamp))
|
|
{
|
|
// Wrong number of fields: blank current request
|
|
current_request->clear() ;
|
|
if (Configuration::is_configured("verbose"))
|
|
cerr << "InputCSV: cannot read timestamp.\n" ;
|
|
return *current_request ;
|
|
}
|
|
current_request->set_time_sent(timestamp) ;
|
|
|
|
// Read position fields
|
|
Point3D position ;
|
|
if (! file.read_point3d(position))
|
|
{
|
|
// Wrong number of fields: blank current request
|
|
current_request->clear() ;
|
|
if (Configuration::is_configured("verbose"))
|
|
cerr << "InputCSV: cannot read coordinates.\n" ;
|
|
return *current_request ;
|
|
}
|
|
|
|
// Read direction field
|
|
Direction direction ;
|
|
int direction_int ;
|
|
if (! file.read_field(direction_int))
|
|
{
|
|
// Wrong number of fields: blank current request
|
|
current_request->clear() ;
|
|
if (Configuration::is_configured("verbose"))
|
|
cerr << "InputCSV: cannot read direction.\n" ;
|
|
return *current_request ;
|
|
}
|
|
if (direction_int != 0)
|
|
direction = direction_int ;
|
|
|
|
// Reading {MAC_AP;SS} couples
|
|
unordered_map<string, Measurement> measurements ;
|
|
|
|
string mac_ap ;
|
|
while (file.read_field(mac_ap))
|
|
{
|
|
int ss ;
|
|
if (! file.read_field(ss))
|
|
{
|
|
// Wrong number of fields: blank current request
|
|
current_request->clear() ;
|
|
if (Configuration::is_configured("verbose"))
|
|
cerr << "InputCSV: cannot read mac_ap.\n" ;
|
|
return *current_request ;
|
|
}
|
|
PosUtil::to_upper(mac_ap) ;
|
|
|
|
if (! Configuration::bool_value("positioning.accept-new-aps") &&
|
|
! Stock::ap_exists(mac_ap))
|
|
continue ;
|
|
|
|
const AccessPoint &ap = Stock::find_create_ap(mac_ap) ;
|
|
measurements[mac_ap].set_ap(&ap) ;
|
|
measurements[mac_ap].add_ss(ss) ;
|
|
}
|
|
if (measurements.empty())
|
|
{
|
|
current_request->clear() ;
|
|
return *current_request ;
|
|
}
|
|
current_request->set_measurements(measurements) ;
|
|
|
|
// Calibration request?
|
|
if (type == OWL_REQUEST_CALIBRATION ||
|
|
type == OWL_REQUEST_AUTOCALIBRATION)
|
|
{
|
|
const ReferencePoint &reference_point =
|
|
Stock::find_create_reference_point(position) ;
|
|
current_request_to_calibration_request(&reference_point,
|
|
direction, type) ;
|
|
}
|
|
|
|
// We set the real coordinates (if found) only for non-calibration
|
|
// requests
|
|
else if (position)
|
|
current_request->set_real_position(position) ;
|
|
|
|
return *current_request ;
|
|
}
|