#package Gateway::Dinstar;
package Dinstar;

use 5.032001;
use strict;
use warnings;
use POSIX;
use JSON;
use WWW::Curl::Easy;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(receiveSms);
our $VERSION = '0.01';
our $ABSTRACT = 'lib/Dinstar.pm';

# -------------
# receiveSms
# -------------

sub 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 "";
}

BEGIN { 
print "This is BEGIN Block\n" 
}

END { 
print "In the end block\n"; 
}
1;
	

