269 lines
5.7 KiB
C++
269 lines
5.7 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 "measurement.hh"
|
|
#include "posexcept.hh"
|
|
#include "configuration.hh"
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
using namespace std ;
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
/**
|
|
* Note that values pointed by #ap are not deleted.
|
|
*/
|
|
Measurement::~Measurement()
|
|
{
|
|
ss_list.clear() ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Read accessors *** */
|
|
|
|
|
|
/**
|
|
* @returns The SS of the packet number \em pkt_id.
|
|
* @returns 0 if the packet ID does not exist in the SS list.
|
|
*/
|
|
ss_t Measurement::get_ss(pkt_id_t pkt_id) const
|
|
{
|
|
std::map<pkt_id_t, ss_t>::const_iterator ss = ss_list.find(pkt_id) ;
|
|
if (ss == ss_list.end())
|
|
return 0 ;
|
|
return ss->second ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Write accessors *** */
|
|
|
|
|
|
/**
|
|
* @param packet_id The sequence number of the packet in the request,
|
|
* as sent by the mobile.
|
|
* @param ss_dbm The signal strength to add to #ss_list (in dBm).
|
|
*/
|
|
void Measurement::
|
|
add_ss(const pkt_id_t packet_id, const ss_t ss_dbm)
|
|
{
|
|
// Add the new value (in dBm) along with the packet identifier:
|
|
pair<pkt_id_t, ss_t> packet(make_pair(packet_id, ss_dbm)) ;
|
|
if (! ss_list.insert(packet).second)
|
|
{
|
|
cerr
|
|
<< "Cannot insert the packet (#" << packet_id << ", "
|
|
<< static_cast<int_fast16_t>(ss_dbm) << " dBm)\n" ;
|
|
return ;
|
|
}
|
|
|
|
// Update the average & variance with the new value:
|
|
update_average(ss_dbm) ;
|
|
}
|
|
|
|
|
|
/**
|
|
* Add an SS list to the current Measurement's SS list.
|
|
* If the new SS list contains a packet with the same ID, the old one
|
|
* is overwritten.
|
|
*/
|
|
void Measurement::
|
|
add_ss_list(const map<pkt_id_t, ss_t> &_ss_list)
|
|
{
|
|
// We cannot use insert() here because we want to overwrite the
|
|
// previous values with the same ID, if any.
|
|
for (map<pkt_id_t, ss_t>::const_iterator
|
|
i = _ss_list.begin() ; i != _ss_list.end() ; ++i)
|
|
ss_list[i->first] = i->second ;
|
|
|
|
recalculate_average() ;
|
|
}
|
|
|
|
|
|
/**
|
|
* Merge consists of adding the SS values of \em source to #ss_list. It
|
|
* is possible only if the #ap of the two Measurement are identical.
|
|
* @throw cannot_merge if the AP of the two Measurement are different.
|
|
*/
|
|
void Measurement::merge(const Measurement &source)
|
|
{
|
|
if (ap != source.ap)
|
|
throw cannot_merge(
|
|
"error when trying to merge measurements, APs are different") ;
|
|
|
|
add_ss_list(source.ss_list) ;
|
|
}
|
|
|
|
|
|
/**
|
|
* - #ap is not deleted, only initialised to NULL.
|
|
* - #ss_list is cleared.
|
|
* - #average_dbm, #average_mw and #variance are reset to 0.
|
|
*/
|
|
void Measurement::clear()
|
|
{
|
|
ss_list.clear() ;
|
|
average_dbm = 0 ;
|
|
average_mw = 0 ;
|
|
variance = 0 ;
|
|
variance_m2 = 0 ;
|
|
ap = NULL ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
float Measurement::similarity(const Measurement &source) const
|
|
{
|
|
string algorithm(
|
|
Configuration::string_value("positioning.ss-similarity")) ;
|
|
|
|
if (algorithm == "mean")
|
|
return ss_square_distance(source) ;
|
|
|
|
if (algorithm == "interval")
|
|
{
|
|
float
|
|
interval_min = average_dbm - get_std_deviation(),
|
|
interval_max = average_dbm + get_std_deviation() ;
|
|
|
|
// Count the number of SS of source that are within the interval
|
|
unsigned int nb_values = 0 ;
|
|
for (map<pkt_id_t, ss_t>::const_iterator ss =
|
|
source.ss_list.begin() ; ss != source.ss_list.end() ; ++ss)
|
|
if (interval_min < ss->second && ss->second < interval_max)
|
|
++nb_values ;
|
|
|
|
return 1 / nb_values ;
|
|
}
|
|
|
|
throw bad_configuration(
|
|
"Bad SS similarity algorithm name \""+ algorithm +"\"!") ;
|
|
}
|
|
|
|
|
|
void Measurement::recalculate_average()
|
|
{
|
|
average_dbm = 0 ;
|
|
average_mw = 0 ;
|
|
variance = 0 ;
|
|
variance_m2 = 0 ;
|
|
|
|
for (map<pkt_id_t, ss_t>::const_iterator
|
|
i = ss_list.begin() ; i != ss_list.end() ; ++i)
|
|
update_average(i->second) ;
|
|
}
|
|
|
|
|
|
void Measurement::update_average(ss_t ss_dbm)
|
|
{
|
|
// Convert the new SS in mW:
|
|
float ss_mw = pow(10, static_cast<float>(ss_dbm) / 10.0) ;
|
|
|
|
// Update the average:
|
|
float delta = ss_mw - average_mw ;
|
|
average_mw += delta / ss_list.size() ;
|
|
average_dbm = 10.0 * log10(average_mw) ;
|
|
|
|
// Update the variance:
|
|
variance_m2 += delta * (ss_mw - average_mw) ;
|
|
if (ss_list.size() > 1)
|
|
variance = variance_m2 / (ss_list.size() - 1) ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operators *** */
|
|
|
|
|
|
Measurement& Measurement::operator=(const Measurement &m)
|
|
{
|
|
if (this == &m)
|
|
return *this ;
|
|
|
|
ap = m.ap ;
|
|
ss_list = m.ss_list ;
|
|
average_dbm = m.average_dbm ;
|
|
average_mw = m.average_mw ;
|
|
variance = m.variance ;
|
|
variance_m2 = m.variance_m2 ;
|
|
|
|
return *this ;
|
|
}
|
|
|
|
|
|
bool Measurement::operator==(const Measurement &m) const
|
|
{
|
|
if (this == &m)
|
|
return true ;
|
|
|
|
return
|
|
ap == m.ap &&
|
|
ss_list == m.ss_list ;
|
|
}
|
|
|
|
|
|
|
|
const string Measurement::to_csv() const
|
|
{
|
|
ostringstream csv_line ;
|
|
|
|
if (ss_list.empty())
|
|
return "" ;
|
|
|
|
string mac_ap("") ;
|
|
if (ap)
|
|
mac_ap = ap->get_mac_addr() ;
|
|
|
|
for (map<pkt_id_t, ss_t>::const_iterator
|
|
i = ss_list.begin() ; i != ss_list.end() ; ++i)
|
|
{
|
|
if (i != ss_list.begin())
|
|
csv_line << ';' ;
|
|
csv_line
|
|
<< mac_ap << ';'
|
|
<< i->first << ';'
|
|
<< static_cast<int_fast16_t>(i->second) ;
|
|
}
|
|
|
|
return csv_line.str() ;
|
|
}
|
|
|
|
|
|
|
|
ostream &operator<<(ostream &os, const Measurement &m)
|
|
{
|
|
// MAC address
|
|
os
|
|
<< "AP: " << (m.ap ? m.ap->get_mac_addr() : "Unknown_AP")
|
|
<< ": " ;
|
|
|
|
// List of SS
|
|
if (m.ss_list.empty())
|
|
os << "No values" ;
|
|
else
|
|
for (map<pkt_id_t, ss_t>::const_iterator
|
|
i = m.ss_list.begin() ; i != m.ss_list.end() ; ++i)
|
|
{
|
|
if (i != m.ss_list.begin())
|
|
os << ';' ;
|
|
os << static_cast<int_fast16_t>(i->second)
|
|
<< '(' << i->first << ')' ;
|
|
}
|
|
os << " [AVG=" << m.average_dbm << "]" ;
|
|
|
|
return os ;
|
|
}
|