#!/usr/bin/perl

use DBI;
use POSIX;
use Time::HiRes;
use JSON;
use Config::Tiny;
use Data::Dumper;
use Cwd;
use URI;
use WWW::Curl::Easy;
use Data::Dumper;
use Dinstar;

my $logfile = "/var/www/html/PortalSMS/QueryAni.log";
my $ip = "";
my $port = 0;
my $id = 0;
my $auth = "";

# --------------------------------
# MariaDB
# --------------------------------
my $driver = "mysql"; 
my $database = "TestSMS";
my $dsn = "DBI:$driver:database=smpp:127.0.0.1";
my $userid = "root";
my $password = "dialer19";
my $dbhost = "10.0.66.119";

my $dbh;
my $dbConnect = 1;
my $dsn = "DBI:$driver:database=$database:$dbhost";

writeToLog( $logfile, "", 1 );
writeToLog( $logfile, "QueryANI Version 0.2", 0 );
writeToLog( $logfile, "", 1 );

   # --------------------------------
   # Connect to MariaDB
   # --------------------------------
   while( $dbConnect ) {
      if( ( $dbh = DBI->connect($dsn, $userid, $password ) ) == 0 ) {
         writeToLog( $logfile, sprintf( "Cannot connect to MariaDB: $DBI::errstr", 0 ) );
         writeToLog( $logfile, "", 1 );
         Time::HiRes::sleep(10);
      }
      else {
         $dbConnect = 0;
         writeToLog( $logfile, sprintf( "Connected to MariaDB @%s", $dbhost ) );
         writeToLog( $logfile, "", 1 );
         last;
      }
   }

   # -----------------------------------------
   # Me fijo que puertos estan habilitados
   # -----------------------------------------
   $sql = sprintf( "select sql_no_cache gw, gateway_ports.port as gw_port, slot, ip, api_user, api_password, query_ani, query_txt, query_ani_response from gateway_ports left join planes on planes.id=gateway_ports.plan left join gateways on gateways.id=gw where gateway_ports.status=1 and registered=1;" );
   $sth = $dbh->prepare( $sql );
   if ( $sth->err ) {
      if( --$errorCount > 0 ) {
         writeToLog( $logfile, sprintf( "DBI prepare Error: Count: $errorCount => $sth->err : $sth->errstr" ), 0 );
      }
      Time::HiRes::sleep(10);
      next;
   }

   if( $sth->execute() ) {
      while (my @row = $sth->fetchrow_array()) {
         my ($gw, $port, $slot, $ip, $api_user, $api_password, $query_ani, $query_txt, $query_ani_response ) = @row;
         $auth = $api_user . ":" . $api_password;

         if( $query_ani ne "" && $query_txt ne "" ) {
#           printf( "%s: %d, %d, '%s': %s -> %s\n", $ip, $gw, $port, $slot, $query_ani , $query_txt);   
            my $sendCode = sendSms( $ip, $query_ani, $query_txt, $port, $auth, $port );
#           print( Dumper( $sendCode ) );
            my $errCode = $sendCode->{error_code};
            if( $errCode == 202 ) {
               my $smsInQueue = $sendCode["sms_in_queue"];
               printf( "ANI Message sended\n" );
            }
            else {
               printf( "ANI Message error %d\n", $errCode );
            }
         }
      }
   }
   $sth->finish();

   # --------------------------------------
   # writeToLog
   # --------------------------------------

   sub writeToLog 
   {  
      my $filename = $_[0];
      my $text = $_[1];
      my $mode = $_[2];
   
      if( open(FH, '>>', $filename) ) {
         if( $mode == 1 ) {
            $text = "+--------------------------------------------------------------------------------------------------+";
         }
         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
         my $hoy = sprintf( "%02d:%02d:%04d %02d:%02d:%02d", $mday, $mon + 1, $year + 1900, $hour, $min, $sec );
         printf( FH sprintf( "%s: %s\n", $hoy, $text ) );
         printf( sprintf( "%s: %s\n", $hoy, $text ) );
         close(FH);
      }      
   }




