Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.05 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use DBI;
  4. use strict;
  5. use Time::Local;
  6.  
  7. # Config options
  8.  
  9. # Local DB connection settings
  10. my $dsn = 'DBI:mysql:asterisk:localhost';
  11. my $db_user_name = 'asteriskuser';
  12. my $db_password = 'asteriskpass';
  13. my $local_dbh = DBI->connect($dsn, $db_user_name, $db_password);
  14.  
  15. # Remote DB connection settings
  16. $dsn = 'DBI:mysql:asterisk:localhost';
  17. $db_user_name = 'asteriskuser';
  18. $db_password = 'asteriskpass';
  19. my $remote_dbh = DBI->connect($dsn, $db_user_name, $db_password);
  20.  
  21. # Define variables
  22. my $l_field='LINES_NUMBER'; # field name with lines number
  23. my $c_field='MAX_CALLS'; # maximum calls to one number
  24. my $sql='1'; #sql string
  25. my $sth='';
  26. my $wday=0; #day of the week
  27. my $numRows=0; #umber of found rows
  28. my @array; # tmp array
  29. my @queue_interval;
  30. my @queue_voice;
  31. my $queue_ids='';
  32. my @task_arr;
  33. my $l_count=0; # amount of free lines
  34. my $max_tries=0;
  35. my $tmp='';
  36. my $i=0;
  37. my $settings_check=0;
  38. my $res2_ids='';
  39. my $res3_ids='';
  40. my $update_ids='';
  41. my $ref='';
  42. my $filee;
  43.  
  44. # Get current settings
  45. &get_settings;
  46.  
  47. # Daemonize proccess
  48. DAEMON:
  49. $settings_check++;
  50. if ($settings_check==4) {
  51.     # Refresh settings
  52.     &get_settings;
  53.     $settings_check=0;
  54. }
  55. # Do the work
  56. &work;
  57. sleep 2;
  58. goto DAEMON;
  59.  
  60. sub get_settings
  61. {
  62.     $sql="SELECT `IDS`,`VALUE` FROM `t_autoinformator_settings`";
  63.     $sth = $remote_dbh->prepare($sql);
  64.     $sth->execute;
  65.     while (@array = $sth->fetchrow_array()) {
  66.         $l_count = int($array[1]) if $array[0] eq $l_field;
  67.         $max_tries = int($array[1]) if $array[0] eq $c_field ;
  68.     }
  69.     $l_count=1 if $l_count==0;
  70.     $max_tries=1 if $max_tries==0;
  71.     $sth->finish;
  72. }
  73.  
  74. sub work
  75. {
  76.     $sql="SELECT `id`, `phone`, `result`, UNIX_TIMESTAMP(last_time), `count`, `voice`, `interval` FROM t_autoinformator_temp";
  77.     $sth = $local_dbh->prepare($sql);
  78.     $sth->execute;
  79.     $numRows = $sth->rows;
  80.     if ($numRows<=0) {
  81.         $sth->finish;
  82.         print "Temp table is empty. Trying to add some tasks...\n";
  83.         print &temp_fill;
  84.         return 0;
  85.     } else {
  86.         # check calls state
  87.         $i=0;
  88.         while (@array = $sth->fetchrow_array()) {
  89.             # We should got different status for tasks and update some tables
  90.             if (int($array[2])==2) {
  91.                 # call successfully complited
  92.                 $res2_ids="$array[0],$res2_ids";
  93.             }elsif(int($array[2])==3 && int($array[4])==0){
  94.                 # call aborted
  95.                 $res3_ids="$array[0],$res3_ids";
  96.             }elsif(int($array[2])!=2 && int($array[2])!=3 && int($array[2])!=4) {
  97.                 # call isn't complited, so we should check for turn
  98.                 if(int($array[4])>0 && (int($array[3])+int($array[6])) <= time) {
  99.                     # it's our turn
  100.                     # store id in array, we will update our table later
  101.                     $update_ids="$array[0],$update_ids";
  102.                     # store all ids, phones and voices, we will create call files later
  103.                     # let's use queue_voice array for this, for memmory economy ;)
  104.                     $i++;
  105.                     $queue_voice[$i]="$array[0];$array[1];$array[5]";
  106.                 }
  107.             }
  108.         }
  109.         # Update t_autoinformator_debtor_phone and remove complited calls from temp
  110.         # This 2 queries whould be joined into one
  111.         $res2_ids=substr($res2_ids,0,-1);
  112.         $res3_ids=substr($res3_ids,0,-1);
  113.         @array=split(',',$res2_ids);
  114.         @task_arr=split(',',$res3_ids);
  115.         if ( @array > 0)    {
  116.             $sql="UPDATE `t_autoinformator_debtor_phone` SET `STATUS`=2 WHERE `ID` IN ($res2_ids)";
  117.             $remote_dbh->do("$sql");
  118.         }
  119.         if ( @array > 0)    {
  120.             $sql="UPDATE `t_autoinformator_debtor_phone` SET `STATUS`=3 WHERE `ID` IN ($res3_ids)";
  121.             $remote_dbh->do("$sql");
  122.         }
  123.         if ( @array>0 || @task_arr>0)   {
  124.             if(@array>0 && @task_arr>0) {
  125.                 $tmp=substr("$res2_ids,$res3_ids",0,-1);
  126.             }elsif(@array>0 && @task_arr==0){
  127.                 $tmp=$res2_ids;
  128.             }elsif(@array==0 && @task_arr>0){
  129.                 $tmp=$res3_ids;
  130.             }
  131.             $sql="DELETE FROM `t_autoinformator_temp` WHERE `ID` IN ($tmp)";
  132.             $local_dbh->do("$sql");
  133.         }
  134.         # Check for awaited tasks
  135.         if ($i>0) {
  136.             # Some tasks awaiting. Make call files for them.
  137.             foreach $tmp (@queue_voice) {
  138.                 @array=split(';',$tmp);
  139.                 $filee="/var/spool/asterisk/outgoing/z_".$array[0].".call";
  140.                 #$tmp="/var/tmp/z_".$arr{'telef'}.".call";
  141.                 if(!(-e $filee)){
  142.                     open (FILE, ">/var/tmp/z_".$array[0].".call");
  143.                     print FILE "Channel: Local/",$array[1],"\@outgoing\n";
  144.                     #print FILE "MaxRetries: 0\n";
  145.                     print FILE "WaitTime: 30\n";
  146.                     print FILE "Context: dialout\n";
  147.                     print FILE "Extension: s\n";
  148.                     print FILE "Priority: 1\n";
  149.                     print FILE "Set: PassedInfo=",$array[0],"-",$array[1],"-",$array[2],"\n";
  150.                     close (FILE);
  151.                 }
  152.             }
  153.             # Move files to asterisk spool directory
  154.             system("mv /var/tmp/z_*.call /var/spool/asterisk/outgoing/");
  155.             # Update task status in database
  156.             $update_ids=substr($update_ids,0,-1);
  157.             @array=split(',',$update_ids);
  158.             $sql="UPDATE `t_autoinformator_temp` SET `result`=1, `count`=(`count`-1) WHERE `ID` IN ($update_ids)";
  159.             $local_dbh->do("$sql");
  160.         }
  161.         # Fill temporary table
  162.         &temp_fill;
  163.     }
  164. } # end sub work
  165.  
  166. sub temp_fill
  167. {
  168.     # Current day of the week for DB format
  169.     ($wday) = (localtime(time))[6]-1;
  170.     $wday=6 if $wday<0;
  171.  
  172.     $sql="
  173.         SELECT `ID`, `INTERVAL` , `VOICE`
  174.         FROM `t_autoinformator`
  175.         WHERE `ACTIVE`=1
  176.         AND
  177.             (`DAYS` LIKE '$wday'
  178.             OR `DAYS` LIKE '\%$wday\%'
  179.             OR `DAYS` LIKE '\%$wday'
  180.             OR `DAYS` LIKE '$wday\%')
  181.         AND
  182.             (`DATE_START` < CURDATE()
  183.             OR
  184.                 (`DATE_START` = CURDATE()
  185.                 AND `TIME_START` <= CURTIME() ) )
  186.         AND `DATE_END` >= CURDATE()
  187.         AND
  188.             ( (`INTERVAL1_BEGIN` <= CURTIME() AND `INTERVAL1_END` >= CURTIME())
  189.             OR (`INTERVAL2_BEGIN` <= CURTIME() AND `INTERVAL2_END` >= CURTIME())
  190.             OR (`INTERVAL3_BEGIN` <= CURTIME() AND `INTERVAL3_END` >= CURTIME())
  191.             OR (`INTERVAL4_BEGIN` <= CURTIME() AND `INTERVAL4_END` >= CURTIME()) )
  192.     ";
  193.     #print $sql;
  194.     $sth = $remote_dbh->prepare($sql);
  195.     #find active queues
  196.     $sth->execute;
  197.     $numRows = $sth->rows;
  198.     if ($numRows<=0) {
  199.         $sth->finish;
  200.         return "No active queues found.\n";
  201.     } else {
  202.         $queue_ids='';
  203.         while (@array = $sth->fetchrow_array()) {
  204.  
  205.             $queue_interval[$array[0]]=int(3600/$array[1]); # interval set by 'calls per hour'
  206.             $queue_voice[$array[0]]=$array[2]; # voice file
  207.             $queue_ids="$array[0],$queue_ids";
  208.  
  209.         }
  210.         $queue_ids=substr($queue_ids,0,-1);
  211.         $sth->finish;
  212.  
  213.         $sql="SELECT $l_count-count(id) FROM `t_autoinformator_temp` WHERE `result`=1";
  214.         $sth = $local_dbh->prepare($sql);
  215.         $sth->execute;
  216.         $l_count = int($sth->fetchrow_array());
  217.         $sth->finish;
  218.         if ($l_count<=0) {
  219.             return "Active queues found but all lines usage exceed.\n";
  220.         }else{
  221.             $sql="
  222.                 SELECT `t_autoinformator_debtor_phone`.`ID`, `t_phone`.`NUMBER`, `t_autoinformator_debtor`.`AUTOINFORMATOR`
  223.                 FROM `t_autoinformator_debtor_phone`, `t_autoinformator_debtor`, `t_phone`
  224.                 WHERE `t_autoinformator_debtor_phone`.`AUTOINFORMATOR_DEBTOR`=`t_autoinformator_debtor`.`ID`
  225.                 AND `t_autoinformator_debtor`.`AUTOINFORMATOR` IN ($queue_ids)
  226.                 AND `t_autoinformator_debtor_phone`.`PHONE`=`t_phone`.`ID`
  227.                 AND `t_autoinformator_debtor_phone`.`STATUS`=0 LIMIT $l_count;
  228.             ";
  229.             print $sql;
  230.             $sth = $remote_dbh->prepare($sql);
  231.             $sth->execute;
  232.             $numRows = $sth->rows;
  233.             if ($numRows<=0) {
  234.                 $sth->finish;
  235.                 return "No tasks with status=0 for dial found.\n";
  236.             } else {
  237.                 $tmp="INSERT INTO `t_autoinformator_temp` (`id`, `phone`, `result`, `count`, `voice`, `interval`) VALUES";
  238.                 $queue_ids='';
  239.                 $i=0;
  240.                 while (@array = $sth->fetchrow_array()) {
  241.                     $task_arr[0]=$array[0]; # task id
  242.                     $task_arr[1]=$array[1]; # phone number
  243.                     $task_arr[2]=$queue_interval[$array[2]]; # interval
  244.                     $task_arr[3]=$queue_voice[$array[2]]; # voice file
  245.                     $tmp="$tmp ('$task_arr[0]','$task_arr[1]',0,'$max_tries','$task_arr[3]','$task_arr[2]'),";
  246.                     $queue_ids="$task_arr[0],$queue_ids";
  247.                     $i++;
  248.                 }
  249.                 $sth->finish;
  250.                 $tmp=substr($tmp,0,-1);
  251.                 $queue_ids=substr($queue_ids,0,-1);
  252.                 $local_dbh->do("$tmp");
  253.                 $sql="UPDATE `t_autoinformator_debtor_phone` SET `STATUS`=1 WHERE `ID` IN ($queue_ids)";
  254.                 $remote_dbh->do("$sql");
  255.                 return "Added $i task(s) to temp table\n";
  256.             }
  257.         }
  258.     }
  259. } #end sub temp_fill
  260.  
  261. $local_dbh->disconnect();
  262. $remote_dbh->disconnect();
  263. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement