hitagi-entrada/hitagi-entrada-swoole.php

130 lines
3.7 KiB
PHP
Raw Normal View History

2024-12-12 18:39:48 +00:00
<?php
2024-12-16 19:01:16 +00:00
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/includes/wrapper-swoole.php';
2024-12-12 18:39:48 +00:00
2024-12-14 01:14:11 +00:00
use Hitlabs\Hitagi\AGIEntrada;
2024-12-12 18:39:48 +00:00
use Swoole\Runtime;
use Swoole\Table;
use Swoole\Server;
2024-12-16 19:01:16 +00:00
2024-12-12 18:39:48 +00:00
Runtime::enableCoroutine();
$table = new Table(1024);
$table->column('agi', Swoole\Table::TYPE_INT);
2024-12-12 18:39:48 +00:00
$table->column('data', Swoole\Table::TYPE_STRING, 65536);
2024-12-16 19:01:16 +00:00
$table->column('agi_arg', Swoole\Table::TYPE_STRING, 65536);
2024-12-12 18:39:48 +00:00
$table->create();
2024-12-16 19:01:16 +00:00
$server = new Swoole\Server("0.0.0.0", 8073);
2024-12-12 18:39:48 +00:00
$server->table = $table;
$server->on('start', function ($server) {
2024-12-16 19:01:16 +00:00
echo "LOG ======> Servidor iniciou na porta 8073...." . PHP_EOL;
2024-12-12 18:39:48 +00:00
});
$server->on("connect", function (Server $server, $fd) {
2024-12-16 19:01:16 +00:00
echo "LOG ======> Cliente $fd conectado...." . PHP_EOL;
2024-12-12 18:39:48 +00:00
});
$server->on("receive", function ($server, $fd, $reactor_id, $data) {
2024-12-16 19:01:16 +00:00
$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 ======> Receive data (FIRST): ' . PHP_EOL . $data . PHP_EOL;
2024-12-16 19:01:16 +00:00
$table_data = [
'data' => $data,
'agi_arg' => json_encode([])
];
$server->table->set($fd, $table_data);
} else {
// ALREADY CONNECTED
echo 'LOG ======> Receive data (MORE): ' . PHP_EOL . $data . PHP_EOL;
2024-12-16 19:01:16 +00:00
$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;
2024-12-12 18:39:48 +00:00
}
2024-12-16 19:01:16 +00:00
$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);
$server_close = false;
if (strpos($table_data['data'], 'HANGUP') !== false) {
$server_close = true;
}
if ((isset($argv['1'])) && ($table_data['agi'] != 1)) {
$table_data = [
'agi' => 1
];
$server->table->set($fd, $table_data);
2024-12-16 19:01:16 +00:00
$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();
// agiRunCmdWrapper($fd,$server)('EXEC NOOP HITAGI2');
// agiRunCmdWrapper($fd,$server)('ANSWER');
// agiRunCmdWrapper($fd,$server)('EXEC WAIT 1');
// agiRunCmdWrapper($fd,$server)('EXEC PLAYBACK beep');
// agiRunCmdWrapper($fd,$server)('EXEC WAIT 1');
// agiRunCmdWrapper($fd,$server)('GET VARIABLE CALLERID(all)');
// // agiRunCmdWrapper($fd,$server)('HANGUP');
$server_close = true;
}
if ($server_close) {
$server->close($fd);
2024-12-16 19:01:16 +00:00
}
2024-12-12 18:39:48 +00:00
});
$server->on("close", function ($server, $fd) {
2024-12-16 19:01:16 +00:00
echo "LOG ======> Cliente $fd desconectado...." . PHP_EOL;
2024-12-12 18:39:48 +00:00
});
2024-12-16 19:01:16 +00:00
$server->start();