You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

231 regels
11 KiB

  1. #!/usr/bin/perl
  2. #
  3. require '/usr/local/majormin/libUTILSgeneral.pl';
  4. require '/usr/local/majormin/web-lib.pl';
  5. #require '/usr/local/bin/ast/voice.pl';
  6. ### MAIN ###
  7. scheduler();
  8. ### END MAIN ###
  9. sub scheduler {
  10. # scheduler attivo
  11. # importo i records contenenti le chiamate in run
  12. @calls = &getRecordsXML("/opt/api_project_python/runningCalls.xml", "call");
  13. # if (!@calls){
  14. # # non appena finiscono le chiamate in coda, stoppa la ricorsione
  15. # return;
  16. # };
  17. # controllo lo stato del device, se libero effettuo la telefonata, se occupato richiama scheduler.
  18. do {
  19. if ($calls[0]{'status'} eq "setup"){
  20. $hw_id = $calls[0]{'hw_id'};
  21. $device_state = &device_state($hw_id);
  22. if ($device_state eq "Free"){
  23. `(cd /data/service/voip.majornet/ivr;/usr/local/bin/sox_wav2gsm)`;
  24. `rm $calls[0]{'tmpdir'}.wav`;
  25. # imposto un timeout nel caso in cui asterisk abbia problemi, o ci sia un problema imprevisto che
  26. # possa causare un loop (Timeout non più necessario, la chiamata ad asterisk è implementata in modo asincrono)
  27. # $timeout=600;
  28. # eval {
  29. # local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
  30. # alarm $timeout;
  31. # prendo il primo record e provo ad effettuare la chiamata
  32. 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'}|";
  33. my $callToVoicePl = `/usr/bin/perl /usr/local/bin/ast/voice.pl "$stringToVoicePl" &`;
  34. # alarm 0;
  35. # };
  36. # if ($@) {
  37. # die unless $@ eq "alarm\n"; # propagate unexpected errors
  38. # else {
  39. # # didn't
  40. # }
  41. }
  42. # if ($callToVoicePl eq "Success"){
  43. # $calls[0]{'status'} = "Success"
  44. # }
  45. # elsif ($callToVoicePl eq "Timeout"){
  46. # $calls[0]{'status'} = "Timeout"
  47. # }
  48. # else{
  49. # $calls[0]{'status'} = "Aborted"
  50. # }
  51. # cancelliamo il record contenente la chiamata attuale dal file delle chiamate in run
  52. #&deleteRecordXML("./runningCalls.xml", "call_id", $calls[0]{'call_id'});
  53. # &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call");
  54. }
  55. if ($calls[0]{'status'} eq "active"){
  56. ($sec,$min) = localtime();
  57. $time_now = ($min*60)+$sec;
  58. $time_delta = $time_now - $calls[0]{'last_action'};
  59. # my $filename = 'report.txt';
  60. # open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
  61. # print $fh "time now: ".$time_now." time delta: ".$time_delta."\nlast_action: ".$calls[0]{'last_action'};
  62. # close $fh;
  63. # if ($time_delta < 120){
  64. # &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'});
  65. # &addRecordXML(\%hash_call, "/opt/api_project_python/runningCalls.xml", "/opt/api_project_python/runningCallsTmpl.xml", "call");
  66. # }
  67. if ($time_delta > 120) {
  68. my %hash_call;
  69. $hash_call{'mode'}= $calls[0]{'mode'};
  70. $hash_call{'from'}= $calls[0]{'from'};
  71. $hash_call{'call_id'}= $calls[0]{'call_id'};
  72. $hash_call{'ntel'}=$calls[0]{'ntel'};
  73. $hash_call{'ackid'}=$calls[0]{'ackid'};
  74. $hash_call{'text_message'}=$calls[0]{'text_message'};
  75. $hash_call{'retry_message'}=$calls[0]{'retry_message'};
  76. $hash_call{'timeout'}=$calls[0]{'timeout'};
  77. $hash_call{'retry'}=$calls[0]{'retry'};
  78. $hash_call{'notification_send'}=$calls[0]{'notification_send'};
  79. $hash_call{'notification_to'}=$calls[0]{'notification_to'};
  80. $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'};
  81. $hash_call{'tmpdir'}=$calls[0]{'tmpdir'};
  82. $hash_call{'status'}=$calls[0]{'status'};
  83. $hash_call{'fromemail'}=$calls[0]{'fromemail'};
  84. $hash_call{'first_action'}=$calls[0]{'first_action'};
  85. $hash_call{'last_action'}=$calls[0]{'last_action'};
  86. $hash_call{'status'} = "interrupted call";
  87. `rm $calls[0]{'tmpdir'}.gsm`;
  88. &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'});
  89. &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call");
  90. }
  91. elsif ($time_delta < 0){
  92. my %hash_call;
  93. $hash_call{'mode'}= $calls[0]{'mode'};
  94. $hash_call{'from'}= $calls[0]{'from'};
  95. $hash_call{'call_id'}= $calls[0]{'call_id'};
  96. $hash_call{'ntel'}=$calls[0]{'ntel'};
  97. $hash_call{'ackid'}=$calls[0]{'ackid'};
  98. $hash_call{'text_message'}=$calls[0]{'text_message'};
  99. $hash_call{'retry_message'}=$calls[0]{'retry_message'};
  100. $hash_call{'timeout'}=$calls[0]{'timeout'};
  101. $hash_call{'retry'}=$calls[0]{'retry'};
  102. $hash_call{'notification_send'}=$calls[0]{'notification_send'};
  103. $hash_call{'notification_to'}=$calls[0]{'notification_to'};
  104. $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'};
  105. $hash_call{'tmpdir'}=$calls[0]{'tmpdir'};
  106. $hash_call{'status'}=$calls[0]{'status'};
  107. $hash_call{'fromemail'}=$calls[0]{'fromemail'};
  108. $hash_call{'first_action'}=$calls[0]{'first_action'};
  109. $hash_call{'last_action'}=$calls[0]{'last_action'};
  110. $hash_call{'status'} = "interrupted call";
  111. `rm $calls[0]{'tmpdir'}.gsm`;
  112. &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'});
  113. &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call");
  114. }
  115. }
  116. if ($calls[0]{'status'} eq "not answered"){
  117. my %hash_call;
  118. $hash_call{'mode'}= $calls[0]{'mode'};
  119. $hash_call{'from'}= $calls[0]{'from'};
  120. $hash_call{'call_id'}= $calls[0]{'call_id'};
  121. $hash_call{'ntel'}=$calls[0]{'ntel'};
  122. $hash_call{'ackid'}=$calls[0]{'ackid'};
  123. $hash_call{'text_message'}=$calls[0]{'text_message'};
  124. $hash_call{'retry_message'}=$calls[0]{'retry_message'};
  125. $hash_call{'timeout'}=$calls[0]{'timeout'};
  126. $hash_call{'retry'}=$calls[0]{'retry'};
  127. $hash_call{'notification_send'}=$calls[0]{'notification_send'};
  128. $hash_call{'notification_to'}=$calls[0]{'notification_to'};
  129. $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'};
  130. $hash_call{'tmpdir'}=$calls[0]{'tmpdir'};
  131. $hash_call{'status'}=$calls[0]{'status'};
  132. $hash_call{'fromemail'}=$calls[0]{'fromemail'};
  133. $hash_call{'first_action'}=$calls[0]{'first_action'};
  134. $hash_call{'last_action'}=$calls[0]{'last_action'};
  135. `rm $calls[0]{'tmpdir'}.gsm`;
  136. &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'});
  137. &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call");
  138. }
  139. if ($calls[0]{'status'} eq "correct_code"){
  140. my %hash_call;
  141. $hash_call{'mode'}= $calls[0]{'mode'};
  142. $hash_call{'from'}= $calls[0]{'from'};
  143. $hash_call{'call_id'}= $calls[0]{'call_id'};
  144. $hash_call{'ntel'}=$calls[0]{'ntel'};
  145. $hash_call{'ackid'}=$calls[0]{'ackid'};
  146. $hash_call{'text_message'}=$calls[0]{'text_message'};
  147. $hash_call{'retry_message'}=$calls[0]{'retry_message'};
  148. $hash_call{'timeout'}=$calls[0]{'timeout'};
  149. $hash_call{'retry'}=$calls[0]{'retry'};
  150. $hash_call{'notification_send'}=$calls[0]{'notification_send'};
  151. $hash_call{'notification_to'}=$calls[0]{'notification_to'};
  152. $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'};
  153. $hash_call{'tmpdir'}=$calls[0]{'tmpdir'};
  154. $hash_call{'status'}=$calls[0]{'status'};
  155. $hash_call{'fromemail'}=$calls[0]{'fromemail'};
  156. $hash_call{'first_action'}=$calls[0]{'first_action'};
  157. $hash_call{'last_action'}=$calls[0]{'last_action'};
  158. `rm $calls[0]{'tmpdir'}.gsm`;
  159. &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'});
  160. &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call");
  161. }
  162. if ($calls[0]{'status'} eq "wrong_code"){
  163. my %hash_call;
  164. $hash_call{'mode'}= $calls[0]{'mode'};
  165. $hash_call{'from'}= $calls[0]{'from'};
  166. $hash_call{'call_id'}= $calls[0]{'call_id'};
  167. $hash_call{'ntel'}=$calls[0]{'ntel'};
  168. $hash_call{'ackid'}=$calls[0]{'ackid'};
  169. $hash_call{'text_message'}=$calls[0]{'text_message'};
  170. $hash_call{'retry_message'}=$calls[0]{'retry_message'};
  171. $hash_call{'timeout'}=$calls[0]{'timeout'};
  172. $hash_call{'retry'}=$calls[0]{'retry'};
  173. $hash_call{'notification_send'}=$calls[0]{'notification_send'};
  174. $hash_call{'notification_to'}=$calls[0]{'notification_to'};
  175. $hash_call{'notification_bcc'}=$calls[0]{'notification_bcc'};
  176. $hash_call{'tmpdir'}=$calls[0]{'tmpdir'};
  177. $hash_call{'status'}=$calls[0]{'status'};
  178. $hash_call{'fromemail'}=$calls[0]{'fromemail'};
  179. $hash_call{'first_action'}=$calls[0]{'first_action'};
  180. $hash_call{'last_action'}=$calls[0]{'last_action'};
  181. `rm $calls[0]{'tmpdir'}.gsm`;
  182. &deleteRecordXML("/opt/api_project_python/runningCalls.xml", "call_id", $hash_call{'call_id'});
  183. &addRecordXML(\%hash_call, "/opt/api_project_python/terminatedCalls.xml", "/opt/api_project_python/terminatedCallsTmpl.xml", "call");
  184. }
  185. sleep(10);
  186. @calls = &getRecordsXML("/opt/api_project_python/runningCalls.xml", "call");
  187. $lenght = @calls;
  188. } while ($lenght);
  189. }
  190. sub device_state{
  191. $hw_id = shift;
  192. $device = `asterisk -rx "dongle show device state $hw_id"`;
  193. my @device = split /\n/, $device;
  194. my @device = split/: /, $device[2];
  195. $device_state = $device[1];
  196. chomp($device_state);
  197. return $device_state;
  198. }
  199. # my $filename = 'report.txt';
  200. # open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
  201. # print $fh $hw_id."\n";
  202. # close $fh;