#package Gateway::Dinstar;
package Dinstar;

use 5.032001;
use strict;
use warnings;
use POSIX;
use JSON;
use WWW::Curl::Easy;
use Switch;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(receiveSms sendSms querySmsResult queryPortInfo getSlot convertSlot getSlotName changeToSlot enableSlot disableSlot calculateImei changeImei getPortInfo loginGateway);
our $VERSION = '0.01';
our $ABSTRACT = 'lib/Dinstar.pm';

# -------------
# sendSms
# -------------

sub synway_SendSms {

	my $ip = $_[0];
	my $number = $_[1];
	my $text = $_[2];
	my $port = $_[3];
	my $auth = $_[4];
	my $id = $_[5];

#{“event”:“txsms”,“userid”:“0”,“num”:“10086,10087,10088”,“port”:“1,2,3”,“encoding”:“0”,“smsinfo”:“hello world!”}

	my $url = sprintf( "%s/API/SendSMS", $ip ); 
	my $postfields = { port => int($port),
							 smsinfo => $text,
							 event => 'txtsms',
							 num => $number,
							 userid => $id,
							 encoding => 0							# 0 7bit   8 UCS-2
						  };

   my $jsonEncoded = encode_json( $postfields );
# 	printf( "%s\n", $jsonEncoded );
   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $jsonEncoded );
   $curl->setopt( CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLOPT_USERPWD, $auth);
   
   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";
	
   if ($retcode == 0) {
	   my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
        	my $resultSend = decode_json($response_body);
			return $resultSend;
     	}
   }
	return "";
}


# ----------------------------------
# querySmsResult
# ----------------------------------

sub synway_QuerySmsResult {
	my $ip = $_[0];
	my $port = $_[1];
	my $auth = $_[2];
	my $id = $_[3];
	my $fecha= $_[4];
	my $recipient = $_[5];

	my $url = sprintf( "%s/API/QueryTxSMS", $ip ); 

	my $postfields = { port => int($port),
							 smsinfo => $text,
							 event => 'querytxtsms',
							 userid => $id,
							 num => $recipient
						  };

   my $jsonEncoded = encode_json( $postfields );
#	printf( "%s\n", $jsonEncoded );
   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $jsonEncoded );
   $curl->setopt( CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLOPT_USERPWD, $auth);
   
   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";
	
   if ($retcode == 0) {
	   my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
#        	my $resultSend = decode_json($response_body);
#			return $resultSend;
			return $response_body;
     	}
   }
	return "";
}

# --------------------
# receiveSms
# --------------------
sub synway_ReceiveSms {

   my $ip = $_[0];
   my $port = $_[1];
   my $flag = $_[2];

   my $url = sprintf( "%s/api/query_incoming_sms", $ip ); 
   my $postfields = { port => $port,
                      flag => $flag };
   my $jsonEncoded = encode_json( $postfields );
   my $auth = "admin:pepito20";

#  print( "$url\n" );
   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $jsonEncoded );
   $curl->setopt( CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLOPT_USERPWD, $auth);
   
   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";

   if ($retcode == 0) {
		my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
        	my $resultSend = decode_json($response_body);
			return $resultSend;
     	}
   }
   return "";
}


# ------------------
# queryPortStatus
# ------------------

sub queryPortInfo {
   my $ip = $_[0];
   my $port = $_[1];
   my $auth_basic = $_[2];
	my $content = "";

   my $url = sprintf( "%s/api/get_port_info", $ip ); 
   if( $port == -1 ) {
	   $content = "info_type=type,imei,imsi,iccid,number,reg,slot,callstate,signal";
	}
   else {
		$content = "port=$port&info_type=type,imei,imsi,iccid,number,reg,slot,callstate,signal"; 
	}		

   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $content );
#   $curl->setopt( CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth_basic );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLINFO_HEADER_OUT, 1 );

   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";

   if ($retcode == 0) {
		my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
#       	my $resultSend = decode_json($response_body);
#			return $resultSend;
			return $response_body;
     	}
   }
   return "";
}


sub getSlot {
   my $port = $_[0];
   my $val = $_[1];

   my $data = "";
   my $sl = "A";
   switch( int($val) ) {
      case 0  { $sl = "A" }
      case 1  { $sl = "B" }
      case 2  { $sl = "C" }
      case 3  { $sl = "D" }
   }
   $data = sprintf( "%2d%s", intval( $port ), $sl );
   return $data;
}


sub convertSlot {
   my $val = $_[0];
   my $sl = "";

   switch( int($val) ) {
      case 0  { $sl = "A" }
      case 1  { $sl = "B" }
      case 2  { $sl = "C" }
      case 3  { $sl = "D" }
   }
   my $data = sprintf( "%s", $sl );
   return $data;
}

sub getSlotName {
   my $val = $_[0];
	my $sl;

   switch( int($val) ) {
      case 0  { $sl = "A" }
      case 1  { $sl = "B" }
      case 2  { $sl = "C" }
      case 3  { $sl = "D" }
		else { $sl = "X"; }
   }
   return $sl;
}

# -----------------------------------
# changeToSlot
# -----------------------------------

sub changeToSlot {
	my $ip = $_[0];
	my $port = $_[1];
	my $slot = $_[2];
	my $auth = $_[3];
	my $slot_num = "";

   switch( $slot ) {
      case ( "A" || "a" )
         { $slot_num = 0 }

      case ( "B" || "b" )
         { $slot_num = 1 }

      case ( "C" || "c" )
         { $slot_num = 2 }

      case ( "D" || "d" )
         { $slot_num = 3 }
   }

   my $url = sprintf( "%s/api/set_port_info", $ip ); 
   my $content = "port=" . $port . "&action=slot" . "&param=" . $slot_num;

   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $content );
#  $curl->setopt( CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLINFO_HEADER_OUT, 1 );

   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";

   if ($retcode == 0) {
		my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
        	my $resultSend = decode_json($response_body);
			return "Ok";
     	}
   }
   return "Error";
}


# --------------
# enableSlot
# --------------

sub synway_EnableSlot {
	my $ip = $_[0];
	my $port = $_[1];
	my $slot = $_[2];
	my $auth = $_[3];
	my $jsessionid = $_[4];
	my $slot_num = "";
	my $sn = $_[5];

   my $url = "$ip/API/SwitchCard";
   my $content = "event=switchcard,port=$port.$slot";
	my $device = sprintf( "devckie=%s", $sn ); 

   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $content );
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLINFO_HEADER_OUT, 1 );

   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";

   if ($retcode == 0) {
		my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
			return $response_code;
     	}
   }
   return 0;
}

# --------------
# disableSlot
# --------------

sub synway_DisableSlot {
   return 0;
}

# --------------------------------
# calculateIMEI
# --------------------------------

sub calculateImei {

	my $imei = $_[0];
	my $sum;
	my $odd;
	my $even;
	my $chk;
   my @change =  (  0, 2, 4, 6, 8, 1, 3, 5, 7, 9 );

# ------------------------------------------------------
#     1. Compute the sum of all digits on odd places.
#     2. Replace the digits on even places by the formula:
#
#        0=>0, 1=>2, 2=>4, 3=>6, 4=>8, 5=>1, 6=>3, 7=>5, 
#        8=>7, 9=>9 and summarize them.
#
#     3. Summarize the two results.
#     4. In case the sum of digits ends in 0, 0 is the 
#        check digit. Otherwise, the checksum is equal to 
#        the number that needs to be added to the result 
#        to get the next highest "round" number.
# ------------------------------------------------------ 

	my @newImei = split (//, $imei); 

   $odd = int( $newImei[0] ) + int( $newImei[2] ) + int( $newImei[4] ) + int( $newImei[6] ) +
    	    int( $newImei[8] ) + int( $newImei[10] ) + int( $newImei[12] );

   $even = $change[int( $newImei[1] )] + $change[int( $newImei[3] )] + $change[int( $newImei[5] )] +
    	     $change[int( $newImei[7] )] + $change[int( $newImei[9] )] + $change[int( $newImei[11] )] + $change[int( $newImei[13] )];

   $sum = int($odd) + int($even);
   $sum = $sum % 10;
   $chk = 10 - $sum;
   my $newData = substr($imei,0,14) . $chk;
   return $newData;
}


# ------------
# changeIMEI
# ------------
sub changeIMEI {
	my $ip = $_[0];
	my $port = $_[1];
	my $auth = $_[2];
	my $imei = $_[3];

   my $url = "$ip/api/set_port_info";
   my $calculated = $imei;
   my $content = "port=" . $port . "&action=imei" . "&imei=" . $calculated . "&param=" . $calculated; 

   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $content );
   $curl->setopt( CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLINFO_HEADER_OUT, 1 );

   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";

   if ($retcode == 0) {
		my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
 	     	print "IMEI change succesful\n";
			return $calculated;
     	}
   }
   return "";
}

# ------------
# getPortInfo
# ------------

sub synway_GetPortInfo {
	my $ip = $_[0];
	my $port = $_[1];
	my $auth = $_[2];

   my $url = "$ip/API/QueryPortInfo";
   my $content = "event=getportinfo&port=$port&info_type=type,imei,number,reg,slot,callstate,signal";

   my $curl = WWW::Curl::Easy->new;
   $curl->setopt( CURLOPT_HEADER,0);
   $curl->setopt( CURLOPT_URL, $url);
   $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
   $curl->setopt( CURLOPT_POSTFIELDS, $content );
   $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   $curl->setopt( CURLOPT_USERPWD, $auth );
   $curl->setopt( CURLOPT_POST, 1);
   $curl->setopt( CURLOPT_TIMEOUT, 0);
   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
   $curl->setopt( CURLINFO_HEADER_OUT, 1 );

   my $response_body;
   $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
   my $retcode = $curl->perform;
   my $error_text = "";

   if ($retcode == 0) {
		my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
     	if( $response_code == 200 ) {
		   my $data = json_decode($response_body);
			return $data;
     	}
   }
   return "";
}

   # -----------------------------------
   # loginDinstar
   # -----------------------------------

   sub synway_LoginGateway {
      return "";
   }

BEGIN { 
#print "This is BEGIN Block\n" 
}

END { 
#print "In the end block\n"; 
}
1;
	

