Update: Diversos

main
Alan Oliveira 2024-12-16 16:01:16 -03:00
parent 1ec599bd92
commit 0abaaed9cf
7 changed files with 212 additions and 152 deletions

View File

@ -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("<pre>" . print_r($this->globais, true) . "</pre><br>");
// $this->logStart();
// $this->logAdd("<pre>" . print_r($this->globais, true) . "</pre><br>");
$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');

View File

@ -1,66 +1,102 @@
<?php
require __DIR__ . 'vendor/autoload.php';
require __DIR__ . 'includes/wrapper-swoole.php';
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/includes/wrapper-swoole.php';
use Hitlabs\Hitagi\AGIEntrada;
use Swoole\Runtime;
use Swoole\Table;
use Swoole\Server;
Runtime::enableCoroutine();
$table = new Table(1024);
$table->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();
$server->start();

View File

@ -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();
exit();

View File

@ -1,20 +1,26 @@
<?php
function agiRunCmdSwooleWrapper($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 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) {
// ...
};
}
?>

View File

@ -1,7 +1,8 @@
2024-12-13 22:07:35<hr><pre>Array
2024-12-16 09:55:44<hr><pre>Array
(
[fgId] => 8999
[asteriskip] =>
[fgId] =>
[hostname] =>
[hostip] =>
[logEnabled] => 1
[appdir] => C:\xampp\htdocs\hit\gitea\hitagi-entrada
[isXampp] => 1
@ -25,27 +26,53 @@
)
</pre><br><hr>fgItemRun<br><pre>Array
(
[id] => 3
[idmaster] => 2
[cmd] => timeout
[value] =>
[id] => 1
[idmaster] => 0
[cmd] => start
[value] => 8999
)
</pre><br><hr>fgItemRun<br><pre>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
)
</pre><br><hr>fgItemRun<br><pre>Array
(
[id] => 5
[idmaster] => 4
[cmd] => queue
[value] => 13
[value2] => 5
[id] => 1
[idmaster] => 0
[cmd] => start
[value] => 8999
)
</pre><br><hr>fgItemRunQueue<br><br>
</pre><br><hr>fgItemRun<br><pre>Array
(
[id] => 2
[idmaster] => 1
[cmd] => playaudio
[value] => 32021341DIGITEOCPF.mp3
[value2] => 5
[value3] => 1
[value4] => false
)
</pre><br><hr>fgItemRun<br><pre>Array
(
[id] => 1
[idmaster] => 0
[cmd] => start
[value] => 8999
)
</pre><br><hr>fgItemRun<br><pre>Array
(
[id] => 2
[idmaster] => 1
[cmd] => playaudio
[value] => 32021341DIGITEOCPF.mp3
[value2] => 5
[value3] => 1
[value4] => false
)
</pre><br>

View File

@ -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)

14
teste-swoole.php 100644
View File

@ -0,0 +1,14 @@
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/includes/wrapper-swoole.php';
use Swoole\Http\Server;
$server = new Server("0.0.0.0", 8073);
$server->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello, world!");
});
$server->start();