#!/usr/bin/perl # require '/usr/local/majormin/libUTILSgeneral.pl'; require '/usr/local/majormin/web-lib.pl'; #require '/usr/local/bin/ast/voice.pl'; ### MAIN ### scheduler(); ### END MAIN ### sub scheduler { # scheduler attivo # importo i records contenenti le chiamate in run @calls = &getRecordsXML("/opt/api_project_python/runningCalls.xml", "call"); # if (!@calls){ # # non appena finiscono le chiamate in coda, stoppa la ricorsione # return; # }; # controllo lo stato del device, se libero effettuo la telefonata, se occupato richiama scheduler. do { if ($calls[0]{'status'} eq "setup"){ $hw_id = $calls[0]{'hw_id'}; $device_state = &device_state($hw_id); if ($device_state eq "Free"){ `(cd /data/service/voip.majornet/ivr;/usr/local/bin/sox_wav2gsm)`; `rm $calls[0]{'tmpdir'}.wav`; # imposto un timeout nel caso in cui asterisk abbia problemi, o ci sia un problema imprevisto che # possa causare un loop (Timeout non più necessario, la chiamata ad asterisk è implementata in modo asincrono) # $timeout=600; # eval { # local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required # alarm $timeout; # prendo il primo record e provo ad effettuare la chiamata my $stringToVoicePl = "voicegateway|".$calls[0]{'from'}."|".$calls[0]{'call_id'}."|".$calls[0]{'ntel'}."|".$calls[0]{'ackid'}."|".$calls[0]{'text_message'}."|".$calls[0]{'retry_message'}."|".$calls[0]{'timeout'}."|".$calls[0]{'retry'}."|".$calls[0]{'notification_send'}."|".$calls[0]{'notification_to'}."|digiovine@afasystems.it|$calls[0]{'tmpdir'}|"; my $callToVoicePl = `/usr/bin/perl /usr/local/bin/ast/voice.pl "$stringToVoicePl" &`; # alarm 0; # }; # if ($@) { # die unless $@ eq "alarm\n"; # propagate unexpected errors # else { # # didn't # } } # if ($callToVoicePl eq "Success"){ # $calls[0]{'status'} = "Success" # } # elsif ($callToVoicePl eq "Timeout"){ # $calls[0]{'status'} = "Timeout" # } # else{ # $calls[0]{'status'} = "Aborted" # } # cancelliamo il record contenente la chiamata attuale dal file delle chiamate in run #&deleteRecordXML("./runningCalls.xml", "call_id", $calls[0]{'call_id'}); # &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call"); } if ($calls[0]{'status'} eq "active"){ ($sec,$min) = localtime(); $time_now = ($min*60)+$sec; $time_delta = $time_now - $calls[0]{'last_action'}; # my $filename = 'report.txt'; # open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; # print $fh "time now: ".$time_now." time delta: ".$time_delta."\nlast_action: ".$calls[0]{'last_action'}; # close $fh; # if ($time_delta < 120){ # &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'}); # &addRecordXML(\%hash_call, "/opt/api_project_python/runningCalls.xml", "/opt/api_project_python/runningCallsTmpl.xml", "call"); # } if ($time_delta > 120) { my %hash_call; $hash_call{'mode'}= $calls[0]{'mode'}; $hash_call{'from'}= $calls[0]{'from'}; $hash_call{'call_id'}= $calls[0]{'call_id'}; $hash_call{'ntel'}=$calls[0]{'ntel'}; $hash_call{'ackid'}=$calls[0]{'ackid'}; $hash_call{'text_message'}=$calls[0]{'text_message'}; $hash_call{'retry_message'}=$calls[0]{'retry_message'}; $hash_call{'timeout'}=$calls[0]{'timeout'}; $hash_call{'retry'}=$calls[0]{'retry'}; $hash_call{'notification_send'}=$calls[0]{'notification_send'}; $hash_call{'notification_to'}=$calls[0]{'notification_to'}; $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'}; $hash_call{'tmpdir'}=$calls[0]{'tmpdir'}; $hash_call{'status'}=$calls[0]{'status'}; $hash_call{'fromemail'}=$calls[0]{'fromemail'}; $hash_call{'first_action'}=$calls[0]{'first_action'}; $hash_call{'last_action'}=$calls[0]{'last_action'}; $hash_call{'status'} = "interrupted call"; `rm $calls[0]{'tmpdir'}.gsm`; &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'}); &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call"); } elsif ($time_delta < 0){ my %hash_call; $hash_call{'mode'}= $calls[0]{'mode'}; $hash_call{'from'}= $calls[0]{'from'}; $hash_call{'call_id'}= $calls[0]{'call_id'}; $hash_call{'ntel'}=$calls[0]{'ntel'}; $hash_call{'ackid'}=$calls[0]{'ackid'}; $hash_call{'text_message'}=$calls[0]{'text_message'}; $hash_call{'retry_message'}=$calls[0]{'retry_message'}; $hash_call{'timeout'}=$calls[0]{'timeout'}; $hash_call{'retry'}=$calls[0]{'retry'}; $hash_call{'notification_send'}=$calls[0]{'notification_send'}; $hash_call{'notification_to'}=$calls[0]{'notification_to'}; $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'}; $hash_call{'tmpdir'}=$calls[0]{'tmpdir'}; $hash_call{'status'}=$calls[0]{'status'}; $hash_call{'fromemail'}=$calls[0]{'fromemail'}; $hash_call{'first_action'}=$calls[0]{'first_action'}; $hash_call{'last_action'}=$calls[0]{'last_action'}; $hash_call{'status'} = "interrupted call"; `rm $calls[0]{'tmpdir'}.gsm`; &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'}); &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call"); } } if ($calls[0]{'status'} eq "not answered"){ my %hash_call; $hash_call{'mode'}= $calls[0]{'mode'}; $hash_call{'from'}= $calls[0]{'from'}; $hash_call{'call_id'}= $calls[0]{'call_id'}; $hash_call{'ntel'}=$calls[0]{'ntel'}; $hash_call{'ackid'}=$calls[0]{'ackid'}; $hash_call{'text_message'}=$calls[0]{'text_message'}; $hash_call{'retry_message'}=$calls[0]{'retry_message'}; $hash_call{'timeout'}=$calls[0]{'timeout'}; $hash_call{'retry'}=$calls[0]{'retry'}; $hash_call{'notification_send'}=$calls[0]{'notification_send'}; $hash_call{'notification_to'}=$calls[0]{'notification_to'}; $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'}; $hash_call{'tmpdir'}=$calls[0]{'tmpdir'}; $hash_call{'status'}=$calls[0]{'status'}; $hash_call{'fromemail'}=$calls[0]{'fromemail'}; $hash_call{'first_action'}=$calls[0]{'first_action'}; $hash_call{'last_action'}=$calls[0]{'last_action'}; `rm $calls[0]{'tmpdir'}.gsm`; &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'}); &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call"); } if ($calls[0]{'status'} eq "correct_code"){ my %hash_call; $hash_call{'mode'}= $calls[0]{'mode'}; $hash_call{'from'}= $calls[0]{'from'}; $hash_call{'call_id'}= $calls[0]{'call_id'}; $hash_call{'ntel'}=$calls[0]{'ntel'}; $hash_call{'ackid'}=$calls[0]{'ackid'}; $hash_call{'text_message'}=$calls[0]{'text_message'}; $hash_call{'retry_message'}=$calls[0]{'retry_message'}; $hash_call{'timeout'}=$calls[0]{'timeout'}; $hash_call{'retry'}=$calls[0]{'retry'}; $hash_call{'notification_send'}=$calls[0]{'notification_send'}; $hash_call{'notification_to'}=$calls[0]{'notification_to'}; $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'}; $hash_call{'tmpdir'}=$calls[0]{'tmpdir'}; $hash_call{'status'}=$calls[0]{'status'}; $hash_call{'fromemail'}=$calls[0]{'fromemail'}; $hash_call{'first_action'}=$calls[0]{'first_action'}; $hash_call{'last_action'}=$calls[0]{'last_action'}; `rm $calls[0]{'tmpdir'}.gsm`; &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'}); &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call"); } if ($calls[0]{'status'} eq "wrong_code"){ my %hash_call; $hash_call{'mode'}= $calls[0]{'mode'}; $hash_call{'from'}= $calls[0]{'from'}; $hash_call{'call_id'}= $calls[0]{'call_id'}; $hash_call{'ntel'}=$calls[0]{'ntel'}; $hash_call{'ackid'}=$calls[0]{'ackid'}; $hash_call{'text_message'}=$calls[0]{'text_message'}; $hash_call{'retry_message'}=$calls[0]{'retry_message'}; $hash_call{'timeout'}=$calls[0]{'timeout'}; $hash_call{'retry'}=$calls[0]{'retry'}; $hash_call{'notification_send'}=$calls[0]{'notification_send'}; $hash_call{'notification_to'}=$calls[0]{'notification_to'}; $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'}; $hash_call{'tmpdir'}=$calls[0]{'tmpdir'}; $hash_call{'status'}=$calls[0]{'status'}; $hash_call{'fromemail'}=$calls[0]{'fromemail'}; $hash_call{'first_action'}=$calls[0]{'first_action'}; $hash_call{'last_action'}=$calls[0]{'last_action'}; `rm $calls[0]{'tmpdir'}.gsm`; &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'}); &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call"); } sleep(10); @calls = &getRecordsXML("/opt/api_project_python/runningCalls.xml", "call"); $lenght = @calls; } while ($lenght); } sub device_state{ $hw_id = shift; $device = `asterisk -rx "dongle show device state $hw_id"`; my @device = split /\n/, $device; my @device = split/: /, $device[2]; $device_state = $device[1]; chomp($device_state); return $device_state; } # my $filename = 'report.txt'; # open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; # print $fh $hw_id."\n"; # close $fh;