<?php
require_once 'model/map.php';
require_once 'model/access_point.php';
require_once 'model/location.php';
require_once 'model/rssi_record.php';
require_once 'model/struct.php';





class Controller_localisation
{

  $a_pointsIP = {"192.168.2.11","192.168.2.8","192.168.2.9"};
  $a_pointPORT = 8080;//"8080";//5000

  public function localisation()
  {

    if(!empty($_GET['mac_addr']))
    {
      // ----------------------------------------------------------------------------------------- //
      // ------------------------- Translation from DB to our Stuctures -------------------------- //
      // ----------------------------------------------------------------------------------------- //

      $loc = $entityManager->select('location')
        ->from('rssi_record')
        ->groupBy('location')
        ->getQuery()
        ->getResult();
      $db = Array();
      foreach($loc as $l) {
        $ap = $entityManager->select('access_point')
          ->from('rssi_record')
          ->groupBy('location', 'access_point')
          ->getQuery()
          ->getResult();
          $myHistoFinger = new HistogramFingerprint();
          foreach($ap as $a) {
            $myHistoVal = new HistogramValue();
            $beans = $entityManager->getRepository('rssi_record')->findBy(array('location' => $l, 'access_point' => $a));
            foreach($beans as $b) {
              $myBean = new Bean();
              $myBean->_value = $b.getValue();
              $myBean->_occurence = $b.getOccurences();
              array_push($myHistoVal->_value,$myBean);
            }
            $myHistoVal->_macAddr = ($entityManager->getRepository('access_point')->findOneBy(array('id' => $a.getAccess_point())).getMac_addr());
            array_push($myHistoFinger->_mesurement,$myHistoVal);
          }
          $tmpPos = $entityManager->getRepository('location')->findOneBy(array('id' => $record.getLocation()));
          $myLocation = new Location();
          $myLocation->_x=$tmpPos.getX();
          $myLocation->_y=$tmpPos.getY();
          $myHistoFinger->_location = $myLocation;
          array_push($db, $myHistoFinger);
        }



      // ----------------------------------------------------------------------------------------- //
      // --------------------- Translation from AP response to our Stuctures --------------------- //
      // ----------------------------------------------------------------------------------------- //
      $devMeasurement = Array;
      $response = http_get("http://".$ip.":".$a_pointPORT."/RSSIRequest?mac=".$_GET['mac_addr'], array("timeout"=>1), $info);

      foreach($a_pointsIP as &$ip){
        $histoVal = new HistogramValue();
        $histoVal->_macAddr = $ip;
        $response = http_get("http://".$ip.":".$a_pointPORT."/RSSIRequest?mac=".$_GET['mac_addr'], array("timeout"=>1), $info);

        foreach($response["bean"] as $b) {
          $bean = new Bean();
          $bean->_value = $b["value"];
          $bean->_occurence = $b["occurence"];
          array_push($histoVal->_value,$bean);
        }
        array_push($devMeasurement,$histoVal);
      }


      // ----------------------------------------------------------------------------------------- //
      // ---------------------------------- Find Position ---------------------------------------- //
      // ----------------------------------------------------------------------------------------- //

      $loc = locate($decFingerPrint, $db);
      echo $loc->_x . ";" . $lox->_y;
    }
  }
}
