<?php
	set_time_limit(0);
	echo 'inicio';
	echo '<br>';
	$mysqli = new mysqli('10.118.144.75', 'internal', 'mYREbJG0E1lW', 'gpsimple');   
	$q  = "select gs_objects.imei from gs_objects where gs_objects.imei = '860896051582377'";
	//$q  = "select gs_objects.imei from gs_objects , gs_user_objects where gs_objects.imei = gs_user_objects.imei  and gs_user_objects.user_id = 1235";
	
	
	$r = mysqli_query($mysqli, $q);
$ct = 1;
	while($row = mysqli_fetch_row($r)){

		
		$imei = $row[0];
        echo('comenzando...');
		echo $imei;

		$q2  = "select dt_tracker,lat,lng from gs_object_data_".$imei." where date_add(dt_tracker, interval -4 hour) between '2024-04-01 00:00:00' and '2024-06-12 23:59:59'";
		//$q2  = "select dt_tracker,lat,lng from gs_object_data_".$imei." where date(dt_tracker) between '2023-06-19' and '2023-07-02' and speed > 11 ";
		//$q2  = "select dt_tracker,lat,lng from gs_object_data_".$imei." where dt_tracker between '2023-06-27 12:55:00' and '2023-06-27 13:00:00' and speed > 11 ";
		$r2 = mysqli_query($mysqli, $q2);
		
		echo $ct; 
		echo '</br>';
		while($row2 = mysqli_fetch_row($r2)){

			echo('comenzando api maxspeed...');
			echo $imei;

				$dt_tracker = $row2[0];
				$lat 		= $row2[1];
				$lng 		= $row2[2];

				
				
				$url = 'http://35.208.70.195/api/interpreter?data=[out:json];way(around:5.0,'.$lat.','.$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);
				
				/* echo $url;
				echo '</br>';
				$data = file_get_contents($url);
				$jsondata = json_decode($data,true);
				*/

				echo $jsondata;
				$result4 = 0;
				for($i = 0; $i < 16; $i++){
    					if (isset($jsondata['elements'][$i]['tags']['maxspeed']))
    					{
        					$result4 = $jsondata['elements'][$i]['tags']['maxspeed'];
							echo 'result 4 = '.$result4;
        					break;
    					}
				}
				$result2 = json_encode($result4);
				$result2 = str_replace('"','',$result2);

				$upd =	"update gs_object_data_".$imei.
					" set overpass = ".$result2.
					" where dt_tracker='".$dt_tracker.
					"' and lat=".$lat.
					" and lng=".$lng;

				$rowUpd = mysqli_query($mysqli, $upd);
				//echo date($dt_tracker).' '.$imei.'';
				echo('terminando api para: '.$imei. ' - '.$dt_tracker);
				echo '</br>';
		}
		echo("pasando al siguiente...");
		echo '</br>';
	$ct = $ct + 1;
	}
echo 'fin';
?>
