Skip to content

Commit

Permalink
Merge pull request #18 from greenfieldtech-nirs/Proper-Event-Handling
Browse files Browse the repository at this point in the history
Pull #5 - Proper event handling
  • Loading branch information
greenfieldtech-nirs committed Oct 9, 2014
2 parents 0f99e52 + 0551c90 commit 3dd26e8
Show file tree
Hide file tree
Showing 20 changed files with 702 additions and 472 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"src/interfaces/events.php",
"src/interfaces/mailboxes.php",
"src/interfaces/sounds.php",
"src/interfaces/playbacks.php"
"src/interfaces/playbacks.php",
"src/helper/logger.php"
]
}
}
5 changes: 2 additions & 3 deletions examples/ApplicationList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* phpari - A PHP Class Library for interfacing with Asterisk(R) ARI
* Copyright (C) 2014 Nir Simionovich
Expand All @@ -24,12 +25,10 @@
*/

require_once "../vendor/autoload.php";
require_once "examples-config.php";

$conn = new phpari(ARI_USERNAME, ARI_PASSWORD, "hello-world", ARI_SERVER, ARI_PORT, ARI_ENDPOINT); //create new object
$conn = new phpari("hello-world"); //create new object
$app = new applications($conn);

header('Content-Type: application/json');
echo json_encode($app->applications_list());


Expand Down
158 changes: 0 additions & 158 deletions examples/BasicAriConnector.php

This file was deleted.

172 changes: 172 additions & 0 deletions examples/BasicStasisApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

/**
* phpari - A PHP Class Library for interfacing with Asterisk(R) ARI
* Copyright (C) 2014 Nir Simionovich
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* Also add information on how to contact you by electronic and paper mail.
*
* Greenfield Technologies Ltd., hereby disclaims all copyright interest in
* the library `phpari' (a library for creating smart telephony applications)
* written by Nir Simionovich and its respective list of contributors.
*/

require_once "../vendor/autoload.php";

class BasicStasisApplication
{

private $ariEndpoint;
private $stasisClient;
private $stasisLoop;
private $phpariObject;
private $stasisChannelID;
private $dtmfSequence = "";

public $stasisLogger;

public function __construct($appname = NULL)
{
try {
if (is_null($appname))
throw new Exception("[" . __FILE__ . ":" . __LINE__ . "] Stasis application name must be defined!", 500);

$this->phpariObject = new phpari($appname);

$this->ariEndpoint = $this->phpariObject->ariEndpoint;
$this->stasisClient = $this->phpariObject->stasisClient;
$this->stasisLoop = $this->phpariObject->stasisLoop;
$this->stasisLogger = $this->phpariObject->stasisLogger;
$this->stasisEvents = $this->phpariObject->stasisEvents;
} catch (Exception $e) {
echo $e->getMessage();
exit(99);
}
}

public function setDtmf($digit = NULL)
{
try {

$this->dtmfSequence .= $digit;

return TRUE;

} catch (Exception $e) {
return FALSE;
}
}

// process stasis events
public function StasisAppEventHandler()
{
$this->stasisEvents->on('StasisStart', function ($event) {
$this->stasisLogger->notice("Event received: StasisStart");
$this->stasisChannelID = $event->channel->id;
$this->phpariObject->channels()->channel_answer($this->stasisChannelID);
$this->phpariObject->channels()->channel_playback($this->stasisChannelID, 'sound:demo-thanks', NULL, NULL, NULL, 'play1');
});

$this->stasisEvents->on('StasisEnd', function ($event) {
$this->stasisLogger->notice("Event received: StasisEnd");
$this->phpariObject->channels()->channel_delete($this->stasisChannelID);
});


$this->stasisEvents->on('PlaybackStarted', function ($event) {
$this->stasisLogger->notice("+++ PlaybackStarted +++ " . json_encode($event->playback) . "\n");
});

$this->stasisEvents->on('PlaybackFinished', function ($event) {
switch ($event->playback->id) {
case "play1":
$this->phpariObject->channels()->channel_playback($this->stasisChannelID, 'sound:demo-congrats', NULL, NULL, NULL, 'play2');
break;
case "play2":
$this->phpariObject->channels()->channel_playback($this->stasisChannelID, 'sound:demo-echotest', NULL, NULL, NULL, 'end');
break;
case "end":
$this->phpariObject->channels()->channel_continue($this->stasisChannelID);
break;
}
});

$this->stasisEvents->on('ChannelDtmfReceived', function ($event) {
$this->setDtmf($event->digit);
$this->stasisLogger->notice("+++ DTMF Received +++ [" . $event->digit . "] [" . $this->dtmfSequence . "]\n");
switch ($event->digit) {
case "*":
$this->dtmfSequence = "";
$this->stasisLogger->notice("+++ Resetting DTMF buffer\n");
break;
case "#":
$this->stasisLogger->notice("+++ Playback ID: " . $this->phpariObject->playbacks()->get_playback());
$this->phpariObject->channels()->channel_continue($this->stasisChannelID, "demo", "s", 1);
break;
default:
break;
}
});
}

public function StasisAppConnectionHandlers()
{
try {
$this->stasisClient->on("request", function ($headers) {
$this->stasisLogger->notice("Request received!");
});

$this->stasisClient->on("handshake", function () {
$this->stasisLogger->notice("Handshake received!");
});

$this->stasisClient->on("message", function ($message) {
$event = json_decode($message->getData());
$this->stasisLogger->notice('Received event: ' . $event->type);
$this->stasisEvents->emit($event->type, array($event));
});

} catch (Exception $e) {
echo $e->getMessage();
exit(99);
}
}

public function execute()
{
try {
$this->stasisClient->open();
$this->stasisLoop->run();
} catch (Exception $e) {
echo $e->getMessage();
exit(99);
}
}

}

$basicAriClient = new BasicStasisApplication("hello-world");

$basicAriClient->stasisLogger->info("Starting Stasis Program... Waiting for handshake...");
$basicAriClient->StasisAppEventHandler();

$basicAriClient->stasisLogger->info("Initializing Handlers... Waiting for handshake...");
$basicAriClient->StasisAppConnectionHandlers();

$basicAriClient->stasisLogger->info("Connecting... Waiting for handshake...");
$basicAriClient->execute();

exit(0);
5 changes: 2 additions & 3 deletions examples/BridgesList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* phpari - A PHP Class Library for interfacing with Asterisk(R) ARI
* Copyright (C) 2014 Nir Simionovich
Expand All @@ -24,10 +25,8 @@
*/

require_once "../vendor/autoload.php";
require_once "examples-config.php";

$conn = new phpari(ARI_USERNAME, ARI_PASSWORD, "hello-world", ARI_SERVER, ARI_PORT, ARI_ENDPOINT);
$conn = new phpari("hello-world"); //create new object
$bridges = new bridges($conn);

header('Content-Type: application/json');
echo json_encode($bridges->channel_list());

0 comments on commit 3dd26e8

Please sign in to comment.