<?php
include '../config.php';
date_default_timezone_set('Asia/Kolkata');
header('Content-Type: application/json');
$fromDate="1574881886";//$_POST['fromDate'];
$toDate="1574923286";//$_POST['toDate'];
//$requestTime=time();
$accountID="gvk-up-108";
$devices=getDevices($accountID);
for($dindx=0;$dindx<count($devices);$dindx++){
   $signalGaps=getEventData($accountID,$devices[$dindx][0],$fromDate,$toDate);
   var_dump($signalGaps);
}

function TimeFormate($timeDiff){
    $hours=0;
    $min=0;
    $sec=0;
    if ($timeDiff==0) {
        return 0;
    }
    if($timeDiff>=3600){
       $hours=intval($timeDiff/(60*60));
        $tempmin=$timeDiff%(60*60);
        $result=TimeFormate($tempmin);
        return $hours."Hr :".$result;
    }else if($timeDiff>=60){
        $min=intval($timeDiff/60);
        $tempsec=$timeDiff%60;
        return $min." Min :".TimeFormate($tempsec);
    } else {
         return $timeDiff." sec";    
    }
}


function getDevices($accountID){
      global $gtsusername, $gtspassword, $gtsdb, $apiname,$gtsserver,$LiveEventDBserver;
      $devices=array(array());
      $Query="select a.deviceID,b.groupID from Device a,DeviceList b where a.accountID=b.accountID and a.deviceID=b.deviceID limit 1";
      $gtsconnect = mysqli_connect($gtsserver, $gtsusername, $gtspassword) or die ($apiname." Unable to connect to the gtsdb: " . mysqli_error($gtsconnect));
      mysqli_select_db($gtsconnect,$gtsdb);
      $rs =mysqli_query($gtsconnect,$Query) or die ($apiname." Query error to gtsdb: " . mysqli_error($gtsconnect));
      $i=0;
      while($row = mysqli_fetch_assoc($rs)) {
          $devices[$i][0]=$row['deviceID'];
           $devices[$i][1]=$row['groupID'];
          $i++;
      }
      mysqli_close($gtsconnect);
     return $devices; 
}


function getEventData($accountID,$deviceID,$fromDate,$toDate){
      global $gtsusername, $gtspassword, $gtsdb, $apiname,$gtsserver,$LiveEventDBserver;
      $eventData=array(array());
      $Query = " SELECT heading, geozoneID,timestamp,deviceID,timestamp,speedKPH,address,creationTime,latitude,longitude FROM `EventData` where accountID='$accountID' and   deviceID='$deviceID'  and timestamp between '$fromDate' and '$toDate' and latitude<>0 and longitude<>0 order by timestamp asc ";  //request from web for particular vehicle
      $gtsconnect = mysqli_connect($gtsserver, $gtsusername, $gtspassword) or die ($apiname." Unable to connect to the gtsdb: " . mysqli_error($gtsconnect));
      mysqli_select_db($gtsconnect,$gtsdb);
      $rs =mysqli_query($gtsconnect,$Query) or die ($apiname." Query error to gtsdb: " . mysqli_error($gtsconnect));
      $i=0; 
      $signalGap=array();
      $sigindx=0;
      while($row = mysqli_fetch_assoc($rs)) {
           if($i>){
              $difftime=abs($eventData[$i-1][8]-$row['creationTime']);
              echo $difftime;
              if($difftime>300){
                 $signalGap[$sigindx]=$eventData[$i-1][8];
                 $signalGap[$sigindx][9]=$difftime;
                 $sigindx++;
              }
           }
           $eventData[$i][1]=$row['deviceID'];
           $eventData[$i][2]=$row['statusCode'];
           $eventData[$i][3]=$row['time1'];
           $eventData[$i][4]=$row['address'];
           $eventData[$i][5]=$row['latitude'];
           $eventData[$i][6]=$row['longitude'];
           $eventData[$i][7]=$row['timestamp'];
           $eventData[$i][8]=$row['creationTime'];
           $i++;
       }
       mysqli_close($gtsconnect);
       return $signalGap;
}

function findDirection($degree){
  if($degree>=0 && $degree<10){return 'N';}
  else if($degree>10 && $degree<80){return 'NE';}
  else if($degree>=80 && $degree<100){return 'E';}
  else if($degree>=100 && $degree<170){return 'SE';}
  else if($degree>=170 && $degree<190){return 'S';}
  else if($degree>=190 && $degree<260){return 'SW';}
  else if($degree>=260 && $degree<280){return 'W';}
  else if($degree>=280 && $degree<350){return 'NW';}
  else if($degree>=350 && $degree<360){return 'N';}
}



?>
