Asterisk as Wholesale. PAMI instead of AGI. Stable AMI connection.

<?php
ini_set('display_errors', 0);

//declare(ticks=1);
/**
 * PAMI basic use example.
 *
 * PHP Version 5
 *
 * @category Pami
 * @author   Marcelo Gornstein <marcelog@gmail.com>
 * @license  http://www.noneyet.ar/ Apache License 2.0
 * @version  SVN: $Id$
 * @link     http://www.noneyet.ar/
 *
 * Copyright 2011 Marcelo Gornstein <marcelog@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

if ($argc != 7) {
    echo "Use: $argv[0] <host> <port> <user> <pass> <connect timeout> <read timeout>";
    exit (254);
}

////////////////////////////////////////////////////////////////////////////////
// Mandatory stuff to bootstrap.
////////////////////////////////////////////////////////////////////////////////


require(implode(DIRECTORY_SEPARATOR, array(
    __DIR__,
    'vendor',
    'autoload.php'
)));

use PAMI\Client\Impl\ClientImpl;
use PAMI\Listener\IEventListener;
use PAMI\Action\AGIAction;
use PAMI\Message\Event\EventMessage;
use PAMI\Message\Event\NewchannelEvent;
use PAMI\Message\Action\GetVarAction;
use PAMI\Message\Action\SetVarAction;

class A implements IEventListener
{
    private $_client;
//    private $_id;
//    private $_pamiOptions;
    private $_link;

    public function handle(EventMessage $event)
    {
//      if ($event->GetName() == '')
//        return
//            $event instanceof DialEvent
//            && $event->getSubEvent() == 'Begin'
//        ;

//        var_dump($event);
    if ($event instanceof NewchannelEvent && $event->GetContext() == "internal") {
        $time = microtime();
//      echo $event->GetContext()," ", $event->GetChannelState()," ", $event->GetName()," ", $event->GetCallerIDNum()," ", $event->GetExtension();
        $log = $time." ".$event->GetContext()." ".$event->GetChannelState()." ".$event->GetName()." ".$event->GetCallerIDNum()." ".$event->GetExtension();

        //      $b = new ClientImpl($options);
        //      get
        //      get exten
        //      $prov1 = makefunction (cli,ext)
        //      set var PROVIDER1 = $prov1
        //      $a->send();


        //$this->_client->open();
        //      echo "send message\n";




        if($var != "")    {
            $resp = $this->_client->send(new SetVarAction('PROVIDER1', $var, $event->GetChannel()));
            $log = $log." PROVIDER1 is ".$var;
            if (!$resp->isComplete())
                $log = $log." Can not send message for PROVIDER ".$var;
        } else   {
            $resp = $this->_client->send(new SetVarAction('PROVIDER1', "-",$event->GetChannel()));
            $log = $log." PROVIDER1 is -";
            if (!$resp->isComplete())
                $log = $log." Can not send message ";
        }

        $dur =  microtime() - $time;
        $log = $log." Qeuries time: $dur";
        syslog(LOG_INFO, "$log");
        $log = "";

    }


}

    public function __construct($linksql,$ac)
    {
        $this->_link = $linksql;
        $this->_client = $ac;
    }

}


////////////////////////////////////////////////////////////////////////////////
// Code STARTS.
////////////////////////////////////////////////////////////////////////////////
//error_reporting(E_ALL);
ini_set('display_errors', 1);

try
{

    $options = array(
        'host' => $argv[1],
        'port' => $argv[2],
        'username' => $argv[3],
        'secret' => $argv[4],
        'connect_timeout' => $argv[5],
        'read_timeout' => $argv[6],
        'scheme' => 'tcp://', // try tls://
    );

    $ac = new ClientImpl($options);
    $a = new ClientImpl($options);

        $ac->open();

        $a->registerEventListener(new A($link,$ac));
        $a->open();

        $time = time();
        while(true)//(time() - $time) < 60) // Wait for events.
        {

            usleep(1000); // 1ms delay
            try {
                $a->process();
            } catch (Exception $t) {
                echo $t->GetMessage();
                var_dump($a);
                var_dump($t);
            }
        }
        $ac->close();
        $a->close(); // send logoff and close the connection.
} catch (Exception $e) {
        echo $e->getMessage() . "\n";
        var_dump($a);
        var_dump($t);

}
////////////////////////////////////////////////////////////////////////////////
// Code ENDS.
////////////////////////////////////////////////////////////////////////////////

Tags: , , , ,
| 03/07/2019 | Posted in Asterisk, Готовые решения |

Leave a Reply