<?php

header('Content-Type: application/json');
// header('Authorization: Basic anVsaWV0YWdhcnJpZG9AZ21haWwuY29tOmpnMjAyMQ==');

include('gps.php');

$email='julietagarrido@gmail.com';
$pass='jg2021';

//$b= gps::loginAdmin();
$b= gps::login($email, $pass);
$response2 = $b->response;
$responseCode2 = $b->responseCode;

if($responseCode2 == 200){
    
    $a = gps::positions(gps::$cookie);
    $response = $a->response;
    $responseCode = $a->responseCode;
    if($responseCode2 == 200){
        $array = json_decode( $response, true);
        
        foreach ( $array as $value) {
            
            $c = gps::devices(gps::$cookie,$value['deviceId']);
            $response3 = $c->response;
            $responseCode3 = $c->responseCode;
            
            if($responseCode3 == 200){
                $array2 = json_decode( $response3, true);
                $tem = $value['attributes']['temp1'];
                $tem2 = $value['attributes']['temp2'];
                $di2 = $value['attributes']['di2'];
                $sos = $value['attributes']['alarm'];
                $ignition = $value['attributes']['ignition'];
                $rssi = $value['attributes']['rssi'];
                $sat = $value['attributes']['sat'];
                
                if(empty($tem)){
                    $tem = 999;
                }
                
                if(empty($tem2)){
                    $tem2 = 999;
                }
                
                if(empty($di2)){
                    $di2 = 0;
                }
                
                if(!empty($sos)){
                    if($sos == 'sos'){
                        $sos = 1;
                    }else{
                        $sos = 0;
                    }
                }else{
                   $sos = 0 ; 
                }
                
                if(empty($ignition)){
                    $ignition = 0;
                }
                
                if(empty($rssi)){
                    $rssi = 0;
                }
                
                if(empty($sat)){
                    $sat = 0;
                }
                
                $serverTime = $value['serverTime'];
                
                $dt = new DateTime($serverTime);
                $tz = new DateTimeZone('America/Santiago'); // or whatever zone you're after
                
                $dt->setTimezone($tz);
                $time = $dt->format('c');
           
                
                $user = 'WS_controlenruta';
                $contra = '$$WS20';

                /*$buffer = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tem='http://tempuri.org/'>
                <soapenv:Header>
                <tem:Authentication>
                <!--Optional:-->
                <tem:Usuario>".$user."</tem:Usuario>
                <!--Optional:-->
                <tem:Clave>".$contra."</tem:Clave>
                </tem:Authentication>
                </soapenv:Header>
                <soapenv:Body>
                <tem:WM_INS_REPORTE_PUNTO_A_PUNTO>
                <!--Optional:-->
                <tem:ID_REG>".$array2[0]['uniqueid']."</tem:ID_REG>
                <tem:LATITUD>" .$value['latitude']. "</tem:LATITUD>
                <tem:LONGITUD>" .$value['longitude']. "</tem:LONGITUD>
                <tem:SENTIDO>".round($value['course']). "</tem:SENTIDO>
                <tem:VELOCIDAD>" .round($value['speed']). "</tem:VELOCIDAD>
                <!--Optional:-->
                <tem:FH_DATO>" .$time. "</tem:FH_DATO>
                <!--Optional:-->
                <tem:PLACA>" .$array2[0]['name']."</tem:PLACA>
                <tem:CANT_SATELITES>".$sat."</tem:CANT_SATELITES>
                <tem:HDOP>".$rssi."</tem:HDOP>
                <tem:TEMP1>".$tem."</tem:TEMP1>
                <tem:TEMP2>".$tem2."</tem:TEMP2>
                <tem:TEMP3>999</tem:TEMP3>
                <tem:SENSORA_1>999</tem:SENSORA_1>
                <tem:AP>".$di2."</tem:AP>
                <tem:IGNICION>".$ignition."</tem:IGNICION>
                <tem:PANICO>".$sos."</tem:PANICO>
                <tem:SENSORD_1>-1</tem:SENSORD_1>
                <!--Optional:-->
                <tem:TRANS>'Transporte'</tem:TRANS>
                </tem:WM_INS_REPORTE_PUNTO_A_PUNTO>
                </soapenv:Body>
                </soapenv:Envelope>";*/
                
                $buffer = "<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:tem='http://tempuri.org/'>
                           <soap:Header>
                              <tem:Authentication>
                                 <!--Optional:-->
                                 <tem:Usuario>".$user."</tem:Usuario>
                                 <!--Optional:-->
                                 <tem:Clave>".$contra."</tem:Clave>
                              </tem:Authentication>
                           </soap:Header>
                           <soap:Body>
                              <tem:WM_INS_REPORTE_CLASS>
                                 <!--Optional:-->
                                 <tem:Tabla>
                                    <!--Zero or more repetitions:-->
                                    <tem:Datos>
                                       <!--Optional:-->
                                       <tem:ID_REG>".$array2[0]['uniqueid']."</tem:ID_REG>
                                       <tem:LATITUD>" .$value['latitude']. "</tem:LATITUD>
                                       <tem:LONGITUD>" .$value['longitude']. "</tem:LONGITUD>
                                       <tem:SENTIDO>".round($value['course']). "</tem:SENTIDO>
                                       <tem:VELOCIDAD>" .round($value['speed']). "</tem:VELOCIDAD>
                                       <!--Optional:-->
                                       <tem:FH_DATO>" .$time. "</tem:FH_DATO>
                                       <!--Optional:-->
                                       <tem:PLACA>" .$array2[0]['name']."</tem:PLACA>
                                       <tem:CANT_SATELITES>".$sat."</tem:CANT_SATELITES>
                                       <tem:HDOP>".$rssi."</tem:HDOP>
                                       <tem:TEMP1>".$tem."</tem:TEMP1>
                                       <tem:TEMP2>".$tem2."</tem:TEMP2>
                                       <tem:TEMP3>999</tem:TEMP3>
                                       <tem:SENSORA_1>999</tem:SENSORA_1>
                                       <tem:AP>".$di2."</tem:AP>
                                       <tem:IGNICION>".$ignition."</tem:IGNICION>
                                       <tem:PANICO>".$sos."</tem:PANICO>
                                       <tem:SENSORD_1>-1</tem:SENSORD_1>
                                       <!--Optional:-->
                                       <tem:TRANS>'Transporte'</tem:TRANS>
                                    </tem:Datos>
                                 </tem:Tabla>
                              </tem:WM_INS_REPORTE_CLASS>
                           </soap:Body>
                        </soap:Envelope>";
                            
                //$url = "http://ww2.qanalytics.cl/gps_test/service.asmx";
               // $url= "http://ww2.qanalytics.cl/gps_controlenruta/service.asmx";
               $url= "https://ww3.qanalytics.cl/gps_controlenruta/service.asmx";
                $input_xml = $buffer;
                //setting the curl parameters.
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                // Following line is compulsary to add as it is:
                curl_setopt($ch, CURLOPT_POSTFIELDS,$input_xml);
                curl_setopt($ch, CURLOPT_USERPWD, "WS_controlenruta:$$WS20");
                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Content-Length: " . strlen($input_xml)));
                $data = curl_exec($ch);
                $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                curl_close($ch);
                
                //convert the XML result into array
               // $array_data = json_decode(json_encode(simplexml_load_string($data)), true);
     

                    $valid = 'ERROR';
                    if ($httpcode>=200 && $httpcode<300) {
                        $valid='CORRECTO' ; 
                        
                    }            
                
                print_r(array(
            	"Request" => $buffer,
            	"WebService" => $url,
            	"Respuesta_WebService" => $valid,
            	"Fecha_de_envio" => date('c', time()),
            ));
         
               

            }else{
                echo 'No se encontraron los dispositivos.';
            }
           
        }
    }else{
        echo 'No se encontraron posiciones.';    
    }
}else{
    echo 'No se logro iniciar sesion en plataforma.';
}

$c= gps::logout(gps::$cookie);
$response3 = $c->response;
$responseCode3 = $c->responseCode;

exit();

?>