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




class Controller_calibration
{


  public function setPosition()
  {

    $a_pointsIP = array("192.168.1.8");//,"192.168.2.8","192.168.2.9");
    $a_pointPORT = 8080;//"8080";//5000


    include 'bootstrap.php';
  //  localhost/Where-I-Am/index.php/calibration/calibration?mac_addr=32:A3:55:F2:B1:8C
  /*
    $newMap = new map();
    $newMap->setDescription('map from UTBM building H');
    $newMap->setPx_width(30);
    $newMap->setPx_height(30);
    $newMap->setN_height(10);
    $newMap->setN_width(40);
    $newMap->setContent('07251728883883762FA');
    $entityManager->persist($newMap);

    $newPos = new location();
    $newPos->setMap($newMap);
    $newPos->setX(3);
    $newPos->setY(13);
    $entityManager->persist($newPos);

    $newPos2 = new location();
    $newPos2->setMap($newMap);
    $newPos2->setX(31);
    $newPos2->setY(19);
    $entityManager->persist($newPos2);

    $newAp = new access_point();
    $newAp->setLocation($newPos);
    $newAp->setMac_addr("32:A3:55:F2:B1:8C");
    $entityManager->persist($newAp);

    $newAp2 = new access_point();
    $newAp2->setLocation($newPos2);
    $newAp2->setMac_addr("32:A3:55:F2:B1:84");
    $entityManager->persist($newAp2);


    $record = new rssi_record();
    $record->setLocation($newPos);
    $record->setAccess_point($newAp);
    $record->setValue(88);
    $record->setOccurences(1);
    $entityManager->persist($record);

    $record2 = new rssi_record();
    $record2->setLocation($newPos);
    $record2->setAccess_point($newAp2);
    $record2->setValue(17);
    $record2->setOccurences(1);
    $entityManager->persist($record2);
    $entityManager->flush();*/

    if(!empty($_GET['posx']) && !empty($_GET['posy']) && !empty($_GET['mac_addr']))
    {

      $newMap = new map();
      $newMap->setDescription('map from UTBM building H');
      $newMap->setPx_width(30);
      $newMap->setPx_height(30);
      $newMap->setN_height(10);
      $newMap->setN_width(40);
      $newMap->setContent('07251728883883762FA');
      $entityManager->persist($newMap);

      $newPos = new location();
      $newPos->setMap($newMap);
      $newPos->setX($_GET['posx']);
      $newPos->setY($_GET['posy']);
      $entityManager->persist($newPos);
      $entityManager->flush();

        foreach($a_pointsIP as &$ip){
          $response = http_get("http://".$ip.":".$a_pointPORT."/RSSIRequest", $info);
          print_r($info);
          //$response = http_get("http://".$ip.":".$a_pointPORT."/RSSIRequest?mac=".$_GET['mac_addr'], array("timeout"=>1), $info);
          $arraySS = $reponse["ss"];
          $loc = $entityManager->getRepository('location')->findOneBy(array('x' => $_GET['posx'], 'y' => $_GET['posy']));
          if($loc ===null)
          {
            echo "pos doesn't exist";
          }
          else {
            $ap = $entityManager->getRepository('access_point')->findOneBy(array('mac_addr' => $ip));
            if ($ap === null)
            {
              echo "ap doesn't exist";
            }
            else {
              foreach($arraySS as $ss){
                $rssi_record = $entityManager->getRepository('rssi_record')->findOneBy(array('location' => $loc->getId(), 'access_point' => $ap->getId(),'value' => $ss));
                if ($rssi_record === null) {
                  $rssi_record = new rssi_record();
                  $rssi_record->setLocation($loc->getId());
                  $rssi_record->setValue($ss);
                  $rssi_record->setOccurences(1);
              }
            }
          }
        }
      }
    }
    else {
      echo "get are not set";
    }

  }
}


?>
