owlps/owlps-positioning/src/inputmedium.cc

136 lines
3.6 KiB
C++
Raw Normal View History

#include "inputmedium.hh"
#include "calibrationrequest.hh"
#include "stock.hh"
#include "configuration.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 in 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)
{
if ((type == OWL_REQUEST_CALIBRATION ||
type == OWL_REQUEST_AUTOCALIBRATION)
&&
(Configuration::bool_value("positioning.accept-new-aps") ||
Stock::ap_exists(mac_mobile)))
{
AccessPoint &transmitter =
const_cast<AccessPoint&>(Stock::find_create_ap(mac_mobile)) ;
// If an autocalibration request does not contain the coordinates
// of the AP, we use the current coordinates of the AP as
// ReferencePoint.
if (type == OWL_REQUEST_AUTOCALIBRATION && ! position)
position = transmitter.get_coordinates() ;
// Update the AP's coordinates if allowed. A 'false' position
// (i.e. 0;0;0) is only set for calibration requests, to avoid
// setting the coordinates to 0;0;0 if an autocalibration
// request that does not contain the coordinates is received.
else if (Configuration::
bool_value("positioning.update-ap-coordinates-online"))
transmitter.set_coordinates(position) ;
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) ;
}
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 ;
}