Re: Check if PbxAppLoginAutomaton was successful from outside of class
Hello Khalil,
it seems you are replacing the function ReceiveInitialAppLoginResult from websocket.class.php with your own implementation. This is the reason, you have no correct value returned by the function getIsLoggedIn(), cause this part of the original function ReceiveInitialAppLoginResult is never executed:
if ((isset($msg->ok) ? $msg->ok : false) != "true") {
$this->log("login failed", "error");
$response->setMt("AppLoginFailure");
$this->isLoggedIn = false;
} else {
$this->log("successfully logged in", "runtime");
$response->setMt("AppLoginSuccess");
$this->isLoggedIn = true;
}
I would suggest to take a look on the RCC API axample implementation, which uses the PbxAppLoginAutomaton class without overloading its functions:
<?php
/*
* sample code to access PBX RCC API
* (c) innovaphone AG 2020
*/
date_default_timezone_set("Europe/Berlin");
// pbx data, you can override it by placing similar next 4 lines into file my-pbx-data.php
$pbxdns = "sindelfingen.sample.dom";
$pbxpw = "ip411";
$pbxapp = "rccapi";
// end of local pbx data
@include 'my-pbx-data.php';
require_once 'classes/websocket.class.php';
print "<pre>";
// turn on log output
AppPlatform\Log::logon();
// turn off all log messages
AppPlatform\Log::setLogLevel("", "", false);
// turn on log messages of some major categories for all sources
AppPlatform\Log::setLogLevel("", array("error", "runtime"), true);
// if you want to see a message trace, uncomment next line
AppPlatform\Log::setLogLevel("", array("smsg", "debug"), true);
// turn on all catgeories for the calling script
AppPlatform\Log::setLogLevel("script", "", true);
/*
* authenticate towards the PBX as an App service
*/
class PbxAppLoginAutomaton extends AppPlatform\AppLoginAutomaton {
/**
* @var string PBX IP address
*/
protected $pbxUrl;
/**
* @var \WebSocket\WSClient websocket to PBX
*/
protected $pbxWS;
function __construct($pbx, AppPlatform\AppServiceCredentials $cred, $useWS = false) {
$this->pbxUrl = (strpos($pbx, "s://") !== false) ? $pbx :
$this->pbxUrl = ($useWS ? "ws" : "wss") . "://$pbx/PBX0/APPS/websocket";
// create websocket towards the well known PBX URI
$this->pbxWS = new AppPlatform\WSClient("PBXWS", $this->pbxUrl);
parent::__construct($this->pbxWS, $cred);
}
}
/*
use the PbxAdminApi at PBX sindelfingen.sample.dom
There must be an App object with name "pbxadminapi" and password "ip411"
it needs to have the "Admin" and "PbxApi" flags set
*/
$app = new PbxAppLoginAutomaton($pbxdns, new AppPlatform\AppServiceCredentials($pbxapp, $pbxpw));
$app->run();
echo("getIsLoggedIn(): ");
var_dump($app->getIsLoggedIn());
// the class utilizes the PbxApi
class PbxApiSample extends AppPlatform\FinitStateAutomaton {
public function ReceiveInitialStart(\AppPlatform\Message $msg) {
$this->sendMessage(new AppPlatform\Message("Initialize", "api", "RCC"));
}
public function ReceiveInitialUserInfo(\AppPlatform\Message $msg) {
$this->log("UserInfo");
}
public function ReceiveInitialInitializeResult(\AppPlatform\Message $msg) {
$this->log("InitializeResult: " . $msg->api);
$this->sendMessage(new AppPlatform\Message("Devices", "api", "RCC","cn","test"));
}
public function ReceiveInitialDevicesResult(\AppPlatform\Message $msg) {
foreach( $msg->devices as $device) $hws[] = $device->hw;
$this->log("DevicesResult: " . implode($hws));
$this->sendMessage(new AppPlatform\Message("UserInitialize", "api", "RCC","cn","test", "hw", $hws[0]));
}
public function ReceiveInitialUserInitializeResult(\AppPlatform\Message $msg) {
$this->log("UserInitializeResult userid: " . $msg->user);
$userid = $msg->user;
//var_dump($msg);
//return "Dead";
$this->sendMessage(new AppPlatform\Message("UserCall", "api", "RCC","user", $msg->user, "e164", "91234"));
}
public function ReceiveInitialUserCallResult(\AppPlatform\Message $msg) {
$this->log("UserCallResult callid: " . $msg->call);
//$userid = $msg->user;
//var_dump($msg);
//return "Dead";
//$this->sendMessage(new AppPlatform\Message("UserCall", "api", "RCC","cn","Andreas (Trainer)", "hw", $hws[0]));
//return "Dead";
}
public function ReceiveInitialCallInfo(\AppPlatform\Message $msg) {
$this->log("CallInfo");
var_dump($msg);
if($msg->msg == 'r-conn' ) return "Dead";
}
}
AppPlatform\Log::setLogLevel("", "debug", true);
$pbxapi = new PbxApiSample($app->getWs(), "PBX");
$pbxapi->run();
?>
In this example is also a function ReceiveInitialCallInfo is implemented to log the call states infos, like connected etc.
Best Regards
Andreas Fink