Sends a message to ami.
public send ( PAMI\Message\OutgoingMessage $message ) : PAMI\Message\Response\ResponseMessage | ||
$message | PAMI\Message\OutgoingMessage | Message to send. |
return | PAMI\Message\Response\ResponseMessage |
/** * Sends a message to AMI. * * @param OutgoingMessage $action Action to send * * @throws \PAMI\Client\Exception\ClientException * * @return \PAMI\Message\Response\ResponseMessage */ public function sendAction(OutgoingMessage $action) { $params = new ArrayObject(['action' => $action]); $event = new Event(__FUNCTION__ . '.pre', $this, $params); $results = $this->getEventManager()->triggerEventUntil(function ($response) { return $response instanceof ResponseMessage; }, $event); if ($results->stopped()) { return $results->last(); } $response = $this->connection->send($action); $params['response'] = $response; $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, $params); return $response; }
$a = new ClientImpl($options); $a->registerEventListener(new A()); $a->open(); // SMS $sms = new VGSMSMSTxAction(); $sms->setContentType('text/plain; charset=ASCII'); $msg = $argv[5]; $phone = $argv[6]; $sms->setContent($msg); $sms->setTo($phone); // SMS multipart MSG - This is used to send 1 big message splitted in several parts, up to 255 messages if ($argv[7] == 1) { $sms->setConcatRefId('58'); $sms->setConcatTotalMsg('2'); $sms->setConcatSeqNum('1'); $a->send($sms); $sms->setContent('---Testing Multipart message '); $sms->setTo($phone); $sms->setConcatRefId('58'); $sms->setConcatTotalMsg('2'); $sms->setConcatSeqNum('2'); } $a->send($sms); $time = time(); while (true) { usleep(1000); // 1ms delay // Since we declare(ticks=1) at the top, the following line is not necessary $a->process(); } $a->close();
public function handle(EventMessage $event) { var_dump($event); } } //////////////////////////////////////////////////////////////////////////////// // Code STARTS. //////////////////////////////////////////////////////////////////////////////// error_reporting(E_ALL); ini_set('display_errors', 1); try { $options = array('log4php.properties' => realpath(__DIR__) . DIRECTORY_SEPARATOR . 'log4php.properties', 'host' => $argv[1], 'port' => $argv[2], 'username' => $argv[3], 'secret' => $argv[4], 'connect_timeout' => $argv[5], 'read_timeout' => $argv[6], 'scheme' => 'tcp://'); $a = new ClientImpl($options); $a->registerEventListener(new A()); $a->open(); var_dump($a->send(new DongleSendUSSDAction('dongle01', '*101#'))); var_dump($a->send(new DongleSendPDUAction('dongle01', 'AT+CSMS=0 '))); var_dump($a->send(new DongleRestartAction('now', 'dongle01'))); var_dump($a->send(new DongleResetAction('dongle01'))); var_dump($a->send(new DongleReloadAction('now'))); var_dump($a->send(new DongleStopAction('now', 'dongle01'))); var_dump($a->send(new DongleStartAction('dongle01'))); var_dump($a->send(new DongleSendSMSAction('dongle01', '+666666666', 'a message'))); var_dump($a->send(new ListCommandsAction())); var_dump($a->send(new QueueStatusAction())); var_dump($a->send(new QueueStatusAction())); var_dump($a->send(new QueueStatusAction())); var_dump($a->send(new CoreShowChannelsAction())); var_dump($a->send(new SIPPeersAction())); var_dump($a->send(new StatusAction())); var_dump($a->send(new CommandAction('sip show peers')));
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $pamiClientOptions = array('log4php.properties' => $input->getOption('log4php-configuration'), 'host' => $input->getOption('ami-host'), 'scheme' => 'tcp://', 'port' => $input->getOption('ami-port'), 'username' => $input->getOption('ami-username'), 'secret' => $input->getOption('ami-password'), 'connect_timeout' => $input->getOption('ami-connect-timeout'), 'read_timeout' => $input->getOption('ami-read-timeout')); $pamiClient = new ClientImpl($pamiClientOptions); $output->write("<comment>Opening asterisk connection... </comment>"); $pamiClient->open(); $output->writeln('<info>done</info>'); $output->write("<comment>Opening rabbitmq connection... </comment>"); $amqpConn = new AMQPConnection($input->getOption('rabbit-host'), $input->getOption('rabbit-port'), $input->getOption('rabbit-username'), $input->getOption('rabbit-password'), $input->getOption('rabbit-vhost')); $ch = $amqpConn->channel(); $exchange = $input->getOption('rabbit-exchange-name'); $ch->exchange_declare($exchange, 'fanout', false, true, false); $output->writeln('<info>done</info>'); $i = 0; $counter = 0; $pamiClient->registerEventListener(function (EventMessage $event) use($output, $ch, $exchange, &$counter, &$i) { // Send to RabbitMQ $msg = new AMQPMessage(json_encode($event->getKeys()), array('content_type' => 'application/json', 'timestamp' => time(), 'delivery_mode' => 2)); $ch->basic_publish($msg, $exchange); gc_collect_cycles(); $output->writeln(sprintf(" >> <comment>[%s] [%s bytes]</comment> <info>%s</info>", date('Y-m-d G:i:s') . substr((string) microtime(), 1, 8), memory_get_usage(), $event->getName())); $counter = 0; $i = 0; }); $closer = function ($autoExit = true) use($pamiClient, $amqpConn, $ch, $output) { $output->writeln(''); $output->write('<comment>Closing ami connection... </comment>'); $pamiClient->close(); // send logoff and close the connection. $output->writeln("<info>done</info>"); $output->write('<comment>Closing rabbitmq connection... </comment>'); $ch->close(); $amqpConn->close(); $output->writeln("<info>done</info>"); if ($autoExit) { exit(0); } }; declare (ticks=1) { pcntl_signal(\SIGINT, $closer); pcntl_signal(\SIGTERM, $closer); while (true) { try { usleep(1000); // 1ms delay $i++; if ($i == 10000) { // show some feedback every 10000 iterations or so, roughly every 10 seconds if nothing is processed $counter++; $output->writeln(sprintf(" >> <comment>Waiting for events... [%d seconds]</comment> <info>Ping...</info>", $counter * 10)); $i = 0; // for every 10 seconds that go by, send a ping/pong event to the asterisk server // if send times out, it'll throw an exception, which will end this script... supervisor should restart /** @var ResponseMessage $pong */ $pong = $pamiClient->send(new \PAMI\Message\Action\PingAction()); if ('Success' == $pong->getKey('response')) { // Send the pong event onto RabbitMQ. This has a faux keep-alive effect on the RabbitMQ connection $msg = new AMQPMessage(json_encode($pong->getKeys()), array('content_type' => 'application/json', 'timestamp' => time(), 'delivery_mode' => 2)); $ch->basic_publish($msg, $exchange); $output->writeln(sprintf(" >> <comment>[%s] [%s bytes]</comment> <info>%s</info>", date('Y-m-d G:i:s') . substr((string) microtime(), 1, 8), memory_get_usage(), 'Pong')); } } $pamiClient->process(); } catch (\Exception $e) { // try to close any connections $closer(false); // rethrow the exception throw $e; } } } $closer(false); return 0; }
/** * Sends a message to ami. * * @param ActionMessage $message AMI Command. * * @return Response */ public function send(ActionMessage $message) { return $this->_ami->send($message); }