owlps/owlps-positioner/src/inputcsv.cc

192 lines
5.9 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comte
* (Université de Franche-Comté), France.
*
* Copyright © Université de Franche-Comté 2007-2012.
*
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
*
***********************************************************************
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL:
* http://www.cecill.info
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided
* only with a limited warranty and the software's authors, the holder
* of the economic rights, and the successive licensors have only
* limited liability.
*
* In this respect, the user's attention is drawn to the risks
* associated with loading, using, modifying and/or developing or
* reproducing the software by the user in light of its specific status
* of free software, that may mean that it is complicated to manipulate,
* and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users
* are therefore encouraged to load and test the software's suitability
* as regards their requirements in conditions enabling the security of
* their systems and/or data to be ensured and, more generally, to use
* and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*
***********************************************************************
*/
#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 fills the current Request from the CSV input file.
* Blank lines and lines containing only spaces are skipped until a line
* containing a request is found.
*
* @returns \em true if #current_request was correctly filled.
* @returns \em false in case of error (file not opened, end of file,
* invalid field or wrong number of fields in the line).
*/
bool InputCSV::fill_current_request()
{
if (! file.next_line()) // End of file or error
return false ;
++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 false ;
}
PosUtil::to_upper(mac_mobile) ;
if (! Configuration::bool_value("positioning.accept-new-mobiles") &&
! Stock::mobile_exists(mac_mobile))
return false ;
const Mobile &mobile = Stock::find_create_mobile(mac_mobile) ;
current_request->set_mobile(&mobile) ;
// Read request type
uint_fast8_t type ;
uint_fast16_t type_r ;
if (! file.read_field(type_r))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read type.\n" ;
return false ;
}
type = type_r ;
current_request->set_type(type) ;
// Read the number of packets
uint_fast16_t nb_packets ;
if (! file.read_field(nb_packets))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read the number of packets.\n" ;
return false ;
}
current_request->set_nb_packets(nb_packets) ;
// Read Timestamp field
Timestamp timestamp ;
if (! file.read_timestamp(timestamp))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read timestamp.\n" ;
return false ;
}
current_request->set_time_sent(timestamp) ;
// Read position fields
Point3D position ;
if (! file.read_point3d(position))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read coordinates.\n" ;
return false ;
}
// Read direction field
Direction direction ;
int direction_int ;
if (! file.read_field(direction_int))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read direction.\n" ;
return false ;
}
if (direction_int != 0)
direction = direction_int ;
// Reading all the {AP_MAC;Packet_ID;SS}
unordered_map<string, Measurement> measurements ;
string mac_ap ;
while (file.read_field(mac_ap))
{
pkt_id_t packet_id ;
if (! file.read_field(packet_id))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read the packet ID.\n" ;
return false ;
}
int_fast16_t ss ;
if (! file.read_field(ss))
{
// Wrong number of fields
if (Configuration::is_configured("verbose"))
cerr << "InputCSV: cannot read the signal strength.\n" ;
return false ;
}
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(packet_id, ss) ;
}
if (measurements.empty())
return false ;
current_request->set_measurements(measurements) ;
// Calibration request?
fill_calibration_request_data(mac_mobile, position, direction, type) ;
return true ;
}