owlps/owlps-positioning/src/inputmedium.cc

172 lines
4.4 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comté
* (Université de Franche-Comté), France.
*/
#include "inputmedium.hh"
#include "calibrationrequest.hh"
#include "stock.hh"
#include "configuration.hh"
#include "posexcept.hh"
using namespace std ;
/* *** Constructors *** */
InputMedium::InputMedium():
current_line_nb(0)
{
current_request = new Request() ;
}
InputMedium::~InputMedium()
{
delete current_request ;
}
/* *** Operations *** */
/**
* Reads a Request, increments current_line_nb, updates #current_request
* and returns it.
*
* The input medium should be ready to be read before precessing
* requests; otherwise, #current_request is \link Request::clear()
* cleared\endlink and a blank Request is returned.
*
* @return The Request read, or an empty Request in case of error or
* EOF (note that when casted to bool, an empty Request is \em false,
* see Request::operator bool()).
*/
const Request& InputMedium::get_next_request()
{
clear_current_request() ;
if (! fill_current_request())
{
clear_current_request() ;
return *current_request ;
}
return *current_request ;
}
void InputMedium::clear_current_request()
{
if (dynamic_cast<CalibrationRequest*>(current_request) == NULL)
current_request->clear() ;
else
{
delete current_request ;
current_request = NULL ;
current_request = new Request() ;
}
}
/**
* The \em position argument can be changed by this function.
*/
void InputMedium::
fill_calibration_request_data(const string &mac_mobile, Point3D &position,
const Direction &direction, uint8_t type)
{
// We set the real coordinates (if found) for non-calibration requests
if (type != OWL_REQUEST_CALIBRATION &&
type != OWL_REQUEST_AUTOCALIBRATION)
{
if (position)
current_request->set_real_position(position) ;
return ;
}
if (position)
{
// Update the AP's coordinates if allowed (and if the mobile is
// an AP, of course)
if (Configuration::
bool_value("positioning.update-ap-coordinates-online"))
{
if (type == OWL_REQUEST_AUTOCALIBRATION &&
Configuration::bool_value("positioning.accept-new-aps"))
{
AccessPoint &transmitter =
const_cast<AccessPoint&>(
Stock::find_create_ap(mac_mobile)) ;
transmitter.set_coordinates(position) ;
}
else
{
try
{
AccessPoint &transmitter =
const_cast<AccessPoint&>(Stock::get_ap(mac_mobile)) ;
transmitter.set_coordinates(position) ;
}
catch (element_not_found &e)
{
// The mobile is not an AP or the AP does not exist
}
}
}
}
else if (type == OWL_REQUEST_AUTOCALIBRATION)
{
// If an autocalibration request does not contain the coordinates
// of the AP, we use the current coordinates of the AP as
// ReferencePoint.
try
{
AccessPoint &transmitter =
const_cast<AccessPoint&>(Stock::get_ap(mac_mobile)) ;
position = transmitter.get_coordinates() ;
}
catch (element_not_found &e)
{
// The mobile is not an AP or the AP does not exist
}
}
const ReferencePoint &reference_point =
Stock::find_create_reference_point(position) ;
current_request_to_calibration_request(
&reference_point, direction, type) ;
}
void InputMedium::
current_request_to_calibration_request(
const ReferencePoint *const reference_point,
const Direction &direction,
const uint_fast8_t request_type)
{
CalibrationRequest *calibration_request =
dynamic_cast<CalibrationRequest*>(current_request) ;
if (calibration_request != NULL)
{
calibration_request->set_reference_point(reference_point) ;
calibration_request->set_direction(direction) ;
calibration_request->set_type(request_type) ;
return ;
}
Request *tmp = current_request ;
current_request = NULL ;
current_request =
new CalibrationRequest(*tmp,
const_cast<ReferencePoint*>(reference_point),
direction, request_type) ;
delete tmp ;
}