#!/usr/bin/perl 
use strict;
use POSIX;
use JSON;
use WWW::Curl::Easy;

package Dinstar;


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

sub receiveSms( $ip, $port, $flag ) {

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

   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_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);
     }
   }
}

BEGIN { 
}

END { 
}
1;
	

