From 0abaaed9cfe08d63a0fbb5184937c4012355063a Mon Sep 17 00:00:00 2001 From: Alan Oliveira Date: Mon, 16 Dec 2024 16:01:16 -0300 Subject: [PATCH] Update: Diversos --- classes/AGIEntrada.php | 58 ++++++++++++------ hitagi-entrada-swoole.php | 116 +++++++++++++++++++++++------------- hitagi-entrada.php | 17 ++++-- includes/wrapper-swoole.php | 42 +++++++------ log/log.html | 61 +++++++++++++------ log/log.txt | 56 +---------------- teste-swoole.php | 14 +++++ 7 files changed, 212 insertions(+), 152 deletions(-) create mode 100644 teste-swoole.php diff --git a/classes/AGIEntrada.php b/classes/AGIEntrada.php index b8b8b82..80e147c 100644 --- a/classes/AGIEntrada.php +++ b/classes/AGIEntrada.php @@ -9,10 +9,12 @@ class AGIEntrada public function __construct($injection, $globais = array()) { - $globais['logEnabled'] = !isset($globais['logEnabled']) ? false : $globais['logEnabled']; - $globais['appdir'] = !isset($globais['appdir']) ? dirname(__FILE__) : $globais['appdir']; - $globais['asteriskip'] = !isset($globais['asteriskip']) ? 'localhost' : $globais['asteriskip']; - $globais['isXampp'] = !isset($globais['isXampp']) ? false : $globais['isXampp']; + $globais['fgId'] = $globais['fgId'] ?? null; + $globais['hostname'] = $globais['hostname'] ?? null; + $globais['hostip'] = $globais['hostip'] ?? null; + $globais['logEnabled'] = $globais['logEnabled'] ?? null; + $globais['appdir'] = $globais['appdir'] ?? null; + $globais['isXampp'] = $globais['isXampp'] ?? null; $this->globais = $globais; $this->injection = $injection; } @@ -65,16 +67,14 @@ class AGIEntrada { $this->globais['fgItens'] = array(); $this->globais['fgItemStart'] = array(); - if ($this->globais['fgId'] != '') { - if ($this->globais['asteriskip'] != '') { + if (!is_null($this->globais['fgId'])) { + if ($this->globais['isXampp']) { + $target = $this->globais['appdir'] . DIRECTORY_SEPARATOR . 'dial' . DIRECTORY_SEPARATOR . $this->globais['fgId'] . '.json'; + } else if (!is_null($this->globais['hostip'])) { // http://177.107.205.248/fluxograma/data-json/8999.json - $target = 'http://' . $this->globais['asteriskip'] . '/fluxograma/data-json/' . $this->globais['fgId'] . '.json'; - } else { - if (file_exists('/etc/asterisk')) { - $target = '/var/www/html/fluxograma/data-json/' . $this->globais['fgId'] . '.json'; - } else { - $target = 'dial/' . $this->globais['fgId'] . '.json'; - } + $target = 'http://' . $this->globais['hostip'] . '/fluxograma/data-json/' . $this->globais['fgId'] . '.json'; + } else if (file_exists('/etc/asterisk')) { + $target = '/var/www/html/fluxograma/data-json/' . $this->globais['fgId'] . '.json'; } $itens = file_get_contents($target); if ($itens !== false) { @@ -2859,8 +2859,24 @@ class AGIEntrada public function run() { - $this->logStart(); - $this->logAdd("
" . print_r($this->globais, true) . "

"); + // $this->logStart(); + // $this->logAdd("
" . print_r($this->globais, true) . "

"); + + $this->agiLog("===> " . __FUNCTION__ . " <==="); + $this->agiLog(print_r($this->globais, true)); + + if (is_null($this->globais['fgId'])) { + $this->agiRunCmd('EXEC NOOP fgIdIsNull'); + return; + } + if (is_null($this->globais['hostname'])) { + $this->agiRunCmd('EXEC NOOP hostnameIsNull'); + return; + } + if (is_null($this->globais['hostip'])) { + $this->agiRunCmd('EXEC NOOP hostipIsNull'); + return; + } $this->fgGet(); @@ -2901,11 +2917,11 @@ class AGIEntrada $host = '172.31.187.152'; $user = 'appuser'; $password = 'nmvP$x23Vzb@T%Su'; - $hostname = gethostname(); - $hostname = str_replace('hitpbx-', 'hitpbx_', $hostname); - $database = $hostname; - if ($hostname == 'hitpbx-001') { + // $this->globais['hostname'] = gethostname(); + if ($this->globais['hostname'] == 'hitpbx-001') { $database = 'hitpbx-001'; + } else { + $database = str_replace('hitpbx-', 'hitpbx_', $this->globais['hostname']); } $port = '6033'; $this->globais['db'] = mysqli_connect($host, $user, $password, $database, $port) or die('Erro de conexão com o banco de dados'); @@ -2944,6 +2960,10 @@ class AGIEntrada // ... $response = $this->agiRunCmd('EXEC NOOP HITAGI'); + + // termina + // return; + $response = $this->agiRunCmd('ANSWER'); $response = $this->agiRunCmd('EXEC WAIT 1'); $response = $this->agiRunCmd('EXEC PLAYBACK beep'); diff --git a/hitagi-entrada-swoole.php b/hitagi-entrada-swoole.php index 8704f13..94b1198 100644 --- a/hitagi-entrada-swoole.php +++ b/hitagi-entrada-swoole.php @@ -1,66 +1,102 @@ column('agi', Swoole\Table::TYPE_INT); $table->column('data', Swoole\Table::TYPE_STRING, 65536); +$table->column('agi_arg', Swoole\Table::TYPE_STRING, 65536); $table->create(); -$server = new Swoole\Server("0.0.0.0", 4573); +$server = new Swoole\Server("0.0.0.0", 8073); $server->table = $table; $server->on('start', function ($server) { - echo "LOG ======> Servidor iniciou na porta 4573...." . PHP_EOL; + echo "LOG ======> Servidor iniciou na porta 8073...." . PHP_EOL; }); $server->on("connect", function (Server $server, $fd) { - echo "LOG ======> Cliente $fd conectado...." . PHP_EOL; + echo "LOG ======> Cliente $fd conectado...." . PHP_EOL; }); $server->on("receive", function ($server, $fd, $reactor_id, $data) { - echo "LOG ======> Dado recebido $data" . PHP_EOL; - $agiAlreadyExecuting = $server->table->get($fd, 'agi') === 1; - if (!$agiAlreadyExecuting) { - $server->table->set($fd, array( - 'agi' => 1, - 'data' => '', - )); - } - //FIRST CONNECTION - if (!$agiAlreadyExecuting) { - $server->table->set($fd, array( - 'agi' => 1, - 'data' => '', - )); - $agi = new AGIEntrada( - array( - 'agiRunCmd' => agiRunCmdSwooleWrapper($fd, $server), - 'agiLog' => function ($info) { - print_r($info); - print_r(''); + $table_data = $server->table->get($fd); + // echo "LOG ======> table_data: " . print_r($table_data, true) . PHP_EOL; + + if (!is_array($table_data)) { + // FIRST CONNECTION + echo 'LOG ======> data (FIRST): ' . PHP_EOL . $data . PHP_EOL; + $table_data = [ + 'data' => $data, + 'agi_arg' => json_encode([]) + ]; + $server->table->set($fd, $table_data); + } else { + // ALREADY CONNECTED + echo 'LOG ======> data (MORE): ' . PHP_EOL . $data . PHP_EOL; + $table_data['data'] .= $data; + $server->table->set($fd, $table_data); + } + + // GET ARGV + $argv = json_decode($table_data['agi_arg'], true); + $explodido = explode(PHP_EOL, $data); + foreach ($explodido as $linha) { + $linha = trim($linha); + if (empty($linha)) { + continue; } - ) - ); - $agi->run(); - } else { - //ALREADY CONNECTED, ONLY APPEND DATA TO BUFFER - $agiConnectionData = $server->table->get($fd, 'data'); - $agiConnectionData .= $data; - $server->table->set($fd, array( - 'agi' => 1, - 'data' => $agiConnectionData - )); - } + $partes = explode(':', $linha); + $comando = trim($partes[0]); + if (substr($comando, 0, 8) == 'agi_arg_') { + $number = str_replace('agi_arg_', '', $comando); + $argv[$number] = trim($partes[$number]); + } + } + $table_data['agi_arg'] = json_encode($argv); + $server->table->set($fd, $table_data); + + if (isset($argv['1'])) { + + $fgId = $argv[1]; // Numero do DID + // $hostname = $argv[2]; + // $hostip = $argv[3]; + // $fgId = '8999'; + $hostname = 'hitpbx-050'; + $hostip = '177.107.205.248'; + + $logEnabled = true; + $isXampp = false; + $appdir = dirname(__FILE__); + chdir($appdir); + date_default_timezone_set('America/Bahia'); + + $agi = new AGIEntrada( + array( + 'agiRunCmd' => agiRunCmdWrapper($fd, $server), + 'agiLog' => agiLogWrapper() + ), + array( + 'fgId' => $fgId, + 'hostname' => $hostname, + 'hostip' => $hostip, + 'logEnabled' => $logEnabled, + 'appdir' => $appdir, + 'isXampp' => $isXampp + ) + ); + $agi->run(); + + } }); $server->on("close", function ($server, $fd) { - echo "LOG ======> Cliente $fd desconectado...." . PHP_EOL; + echo "LOG ======> Cliente $fd desconectado...." . PHP_EOL; }); -$server->start(); \ No newline at end of file +$server->start(); diff --git a/hitagi-entrada.php b/hitagi-entrada.php index 2811c3b..8c5e4d1 100644 --- a/hitagi-entrada.php +++ b/hitagi-entrada.php @@ -7,11 +7,19 @@ use Hitlabs\Hitagi\AGIEntrada; $stdin = fopen('php://stdin', 'r'); $stdout = fopen('php://stdout', 'w'); -$fgId = (isset($argv[1])) ? $argv[1] : ''; // DID + +$fgId = (isset($argv[1])) ? $argv[1] : null; // Numero do DID +$hostname = (isset($argv[2])) ? $argv[2] : gethostname(); +$hostip = (isset($argv[3])) ? $argv[3] : 'localhost'; + +// $fgId = '8999'; +// $hostname = 'hitpbx-050'; +// $hostip = '177.107.205.248'; + $logEnabled = true; $isXampp = file_exists('C:\xampp'); -$asteriskip = ''; // 177.107.205.248 $appdir = dirname(__FILE__); + chdir($appdir); date_default_timezone_set('America/Bahia'); @@ -22,7 +30,8 @@ $agientrada = new AGIEntrada( ), array( 'fgId' => $fgId, - 'asteriskip' => $asteriskip, + 'hostname' => $hostname, + 'hostip' => $hostip, 'logEnabled' => $logEnabled, 'appdir' => $appdir, 'isXampp' => $isXampp @@ -32,4 +41,4 @@ $agientrada->run(); fclose($stdin); fclose($stdout); -exit(); \ No newline at end of file +exit(); diff --git a/includes/wrapper-swoole.php b/includes/wrapper-swoole.php index b910a7d..40e103b 100644 --- a/includes/wrapper-swoole.php +++ b/includes/wrapper-swoole.php @@ -1,20 +1,26 @@ EXECUTAR COMANDO $command\n"); - $server->send($fd, $command . "\n"); - $response = ''; - while (true) { - $response = $server->table->get($fd); - var_dump($response); - if ($response['data']) break; - sleep(1); - } - $server->table->set($fd, array( - 'agi' => 1, - 'data' => '', - )); - return $response['data']; - }; +function agiRunCmdWrapper($fd, $server) +{ + return function ($command) use ($server, $fd) { + print_r("-----> EXECUTAR COMANDO $command\n"); + $server->send($fd, $command . "\n"); + $response = ''; + while (true) { + $response = $server->table->get($fd); + // var_dump($response); + if ($response['data']) break; + sleep(1); + } + $server->table->set($fd, array( + 'agi' => 1, + 'data' => '', + )); + return $response['data']; + }; +} +function agiLogWrapper() +{ + return function ($texto) { + // ... + }; } -?> \ No newline at end of file diff --git a/log/log.html b/log/log.html index e743e1a..d9bfc6b 100644 --- a/log/log.html +++ b/log/log.html @@ -1,7 +1,8 @@ -2024-12-13 22:07:35
Array
+2024-12-16 09:55:44
Array
 (
-    [fgId] => 8999
-    [asteriskip] => 
+    [fgId] => 
+    [hostname] => 
+    [hostip] => 
     [logEnabled] => 1
     [appdir] => C:\xampp\htdocs\hit\gitea\hitagi-entrada
     [isXampp] => 1
@@ -25,27 +26,53 @@
 )
 


fgItemRun
Array
 (
-    [id] => 3
-    [idmaster] => 2
-    [cmd] => timeout
-    [value] => 
+    [id] => 1
+    [idmaster] => 0
+    [cmd] => start
+    [value] => 8999
 )
 


fgItemRun
Array
 (
-    [id] => 4
-    [idmaster] => 3
+    [id] => 2
+    [idmaster] => 1
     [cmd] => playaudio
-    [value] => Agradecimento.wav
-    [value2] => 2
+    [value] => 32021341DIGITEOCPF.mp3
+    [value2] => 5
     [value3] => 1
     [value4] => false
 )
 


fgItemRun
Array
 (
-    [id] => 5
-    [idmaster] => 4
-    [cmd] => queue
-    [value] => 13
-    [value2] => 5
+    [id] => 1
+    [idmaster] => 0
+    [cmd] => start
+    [value] => 8999
 )
-


fgItemRunQueue

\ No newline at end of file +


fgItemRun
Array
+(
+    [id] => 2
+    [idmaster] => 1
+    [cmd] => playaudio
+    [value] => 32021341DIGITEOCPF.mp3
+    [value2] => 5
+    [value3] => 1
+    [value4] => false
+)
+


fgItemRun
Array
+(
+    [id] => 1
+    [idmaster] => 0
+    [cmd] => start
+    [value] => 8999
+)
+


fgItemRun
Array
+(
+    [id] => 2
+    [idmaster] => 1
+    [cmd] => playaudio
+    [value] => 32021341DIGITEOCPF.mp3
+    [value2] => 5
+    [value3] => 1
+    [value4] => false
+)
+

\ No newline at end of file diff --git a/log/log.txt b/log/log.txt index 75717d8..699ba80 100644 --- a/log/log.txt +++ b/log/log.txt @@ -20,61 +20,9 @@ Array ===> agiRunCmd <=== SET VARIABLE AGIDIGITADO "" 200 result=1 (isXampp) -===> fgItemRunTimeout <=== -Array -( - [id] => 3 - [idmaster] => 2 - [cmd] => timeout - [value] => -) -===> fgItemRunPlayaudio <=== -Array -( - [id] => 4 - [idmaster] => 3 - [cmd] => playaudio - [value] => Agradecimento.wav - [value2] => 2 - [value3] => 1 - [value4] => false -) ===> agiRunCmd <=== -SET VARIABLE AGIDIGITADO "" -200 result=1 (isXampp) -===> fgItemRunQueue <=== -Array -( - [id] => 5 - [idmaster] => 4 - [cmd] => queue - [value] => 13 - [value2] => 5 -) -===> agiRunCmd <=== -SET VARIABLE __AGIQUEUEID "Q13" +SET VARIABLE AGIDIGITADO A 200 result=1 (isXampp) ===> agiRunCmd <=== -SET VARIABLE __AGIQUEUENOME "SAC" -200 result=1 (isXampp) -===> agiRunCmd <=== -SET VARIABLE MONITOR_FILENAME QUEUE_2024-12-13_22-07-35_20241213.220735_1138118400_8999 -200 result=1 (isXampp) -===> agiRunCmd <=== -SET VARIABLE __AGIRECORDNAME QUEUE_2024-12-13_22-07-35_20241213.220735_1138118400_8999 -200 result=1 (isXampp) -===> agiRunCmd <=== -GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(1001)} -200 result=1 (isXampp) -===> agiRunCmd <=== -GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(SIP1001)} -200 result=1 (isXampp) -===> agiRunCmd <=== -EXEC Dial isXampp&isXampp,30,Tt "" -200 result=1 (isXampp) -===> agiRunCmd <=== -GET VARIABLE DIALSTATUS -200 result=1 (isXampp) -===> agiRunCmd <=== -EXEC PLAYBACK /var/lib/asterisk/sounds/pt_BR/exten-unavail "" +EXEC QUEUELOG NONE,20241216.100723,NONE,INFO|IVRAPPEND|->A 200 result=1 (isXampp) diff --git a/teste-swoole.php b/teste-swoole.php new file mode 100644 index 0000000..4d76023 --- /dev/null +++ b/teste-swoole.php @@ -0,0 +1,14 @@ +on("request", function ($request, $response) { + $response->header("Content-Type", "text/plain"); + $response->end("Hello, world!"); +}); + +$server->start(); \ No newline at end of file