<?php
// error_reporting(0);
    include('/var/www/html/track/server/s_insert.php');
    include('/var/www/html/track/server/eventos.php');
    $data = json_decode(file_get_contents("php://input"), true);
    $json_string = file_get_contents('php://input');
    $log_file = 'messages.json2';
    file_put_contents($log_file, $json_string);


    $loc = $data;

    $loc['overpass'] = 0;
    
    if (!isset($loc['imei'])) {
        //file_put_contents($log_file, 'if not isset imei');
        mysqli_close($ms);
        die;
    }

    if ($loc['lat'] == 0 && $loc['lng'] == 0) {
        //file_put_contents($log_file, 'lat,lng 0');
        mysqli_close($ms);
        die;
    }


    $loc['dt_server'] = gmdate("Y-m-d H:i:s");
    if (!isset($loc['dt_tracker'])) {
        //file_put_contents($log_file, 'if not isset dt_server');
        mysqli_close($ms);
        die;
    }

    $q_run_speed = "SELECT run_speed, overpass FROM gs_objects WHERE imei = '".$loc['imei']."'";
    $r_run_speed = mysqli_query($ms, $q_run_speed) ;
    
    //error_log($q_run_speed,E_NOTICE);

    $row = mysqli_fetch_array($r_run_speed);

    $respeed =$row['run_speed'];
    $reoverpass =$row['overpass'];

    if ($reoverpass==1 && $loc['imei'] != '860896052011822') {
    // if ($loc['imei'] != '860896052011822') {

        $url = 'http://35.208.70.195/api/interpreter?data=[out:json];way(around:5.0,'.$loc['lat'].','.$loc['lng'].');out;';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $jsondata = json_decode(curl_exec($ch), true);
        curl_close($ch);
        
        $result4 = 0;
        for($i = 0; $i < 16; $i++){
                if (isset($jsondata['elements'][$i]['tags']['maxspeed']))
                {
                    $result4 = $jsondata['elements'][$i]['tags']['maxspeed'];
                    break;
                }
        }

        $result2 = json_encode($result4);
        $result2 = str_replace('"','',$result2);
        $loc['overpass'] = intval($result2);

        var_dump('********overpass '.$loc['overpass']);
    }
    
    if ($respeed == '1') {
        $direferncia_speed = $loc["speed"] - $loc["params"]["io37"];
        if($direferncia_speed > 10){
            $loc["speed"] = $loc["params"]["io37"];
        }
        if($direferncia_speed < -10){
            $loc["speed"] = $loc["params"]["io37"];
        }
    }
        
    $params = json_encode($loc['params']);
    
    $loc['params'] =  json_decode($params, true);
    $evento = "";
    $id_tarjeta = "";
    $on_off = 0;

    foreach($loc['params'] as $clave => $valor) {
        if($clave == 'io247'){
            if($valor == '1'){
                $evento  = "shock";
            } else if ($valor == '2'){
                $evento  = "limited crash trace (device not calibrated)";
            } else if ($valor == '3'){
                $evento  = "limited crash trace (device is calibrated)";
            } else if ($valor == '4'){
                $evento  = "full crash trace (device not calibrated)";
            }  else if ($valor == '5'){
                $evento  = "full crash trace (device is calibrated)";
            }  else if ($valor == '6'){
                $evento  = "crash detected (device not calibrated)";
            }
        }   

        if($clave == 'io253'){
            if($valor == '1'){
                $evento  = "haccel";
            } else if ($valor == '2'){
                $evento  = "hbrake";
            } else if ($valor == '3'){
                $evento  = "hcorn";
            }
        }  

        if($clave == 'io252'){
            if($valor == '0'){
                $evento  = "conexion";
            } else if ($valor == '1'){
                $evento  = "desconexion";
            }
        }              
        if($clave == 'io281'){
            $evento  = "dtc:".$valor;
        } 
        if($clave == 'io78'){
            $id_tarjeta  = $valor;
        } 
        if($clave == 'io1' || $clave == 'io239'){
            $on_off  = $valor;
        } 
       
    }
         
    $loc['event'] = $evento;
    $loc['id_tarjeta'] = $id_tarjeta;
    $loc['on_off'] = $on_off ;

    // $loc['loc_valid'] = 1;


    if ($loc["op"] == "loc") {
        insert_db_loc($loc);
    } elseif ($loc["op"] == "noloc") {
        insert_db_noloc($loc);
    } elseif ($loc["op"] == "imgloc") {
        insert_db_imgloc($loc);
    }
           

    mysqli_close($ms);
    die;
