Event socket bug fix and more docs (#6823)

* Add documentation to methods. Use is_resource for added type detection

* Allow connect to specify timeout in microseconds with default 30,000

* Update calling mechanism for event sockets

* Update project for new singleton event sockets

* remove unused variable

* catch errors on closing the socket
This commit is contained in:
frytimo
2023-12-02 17:16:18 -07:00
committed by GitHub
parent 44567f7a05
commit 3a4c2f72e2
74 changed files with 1620 additions and 1533 deletions
+2 -6
View File
@@ -399,9 +399,7 @@
$cmd .= "answer-state: confirmed\n";
//send the event
$fp = event_socket_create();
$switch_result = event_socket_request($fp, $cmd);
unset($fp);
$switch_result = event_socket::command($cmd);
}
else {
$presence = new presence;
@@ -421,9 +419,7 @@
$cmd .= "answer-state: terminated\n";
//send the event
$fp = event_socket_create();
$switch_result = event_socket_request($fp, $cmd);
unset($fp);
$switch_result = event_socket::command($cmd);
}
}
}
@@ -39,10 +39,10 @@
if ($this->enabled == "true") {
//update the call center status
$user_status = "Logged Out";
$fp = event_socket_create();
if ($fp) {
$esl = event_socket::create();
if ($esl->is_connected()) {
$switch_cmd .= "callcenter_config agent set status ".$_SESSION['username']."@".$this->domain_name." '".$user_status."'";
$switch_result = event_socket_request($fp, 'api '.$switch_cmd);
$switch_result = event_socket::api($switch_cmd);
}
//update the database user_status
@@ -42,11 +42,11 @@
//feature_event method
public function send_notify() {
$fp = event_socket_create();
if ($fp) {
$esl = event_socket::create();
if ($esl->is_connected()) {
// Get the SIP profiles for the extension
$command = "sofia_contact */{$this->extension}@{$this->domain_name}";
$contact_string = event_socket_request($fp, "api ".$command);
$contact_string = event_socket::api($command);
// The first value in the array will be full matching text, the second one will be the array of profile matches
preg_match_all('/sofia\/([^,]+)\/(?:[^,]+)/', $contact_string, $matches);
if (sizeof($matches) != 2 || sizeof($matches[1]) < 1) {
@@ -72,12 +72,11 @@
$event .= "forward_no_answer: " . $this->forward_no_answer_destination . "\n";
$event .= "doNotDisturbOn: " . $this->do_not_disturb . "\n";
$event .= "ringCount: " . $this->ring_count . "\n";
event_socket_request($fp, $event);
event_socket::command($event);
}
fclose($fp);
}
} //function
} //class
?>
+54 -45
View File
@@ -3,7 +3,7 @@
//check the permission
if (defined('STDIN')) {
//includes files
require_once dirname(__DIR__, 4) . "/resources/require.php";
require_once dirname(__DIR__, 4) . "/resources/require.php";
}
else {
exit;
@@ -31,54 +31,63 @@
exit;
}
//connect to event socket
$fp = event_socket_create();
//use global conf created from require.php
global $conf;
//get the list
$sql = "select domain_name, extension, user_context, do_not_disturb, description ";
$sql .= "from v_extensions as e, v_domains as d ";
$sql .= "where do_not_disturb = 'true' ";
$sql .= "and e.domain_uuid = d.domain_uuid ";
$sql .= "and enabled = 'true' ";
$database = new database;
$results = $database->select($sql, $parameters, 'all');
unset($parameters);
//set the event socket connection settings
$host = $conf['switch.event_socket.host'] ?? $conf['event_socket.ip_address'] ?? '127.0.0.1';
$port = $conf['switch.event_socket.port'] ?? $conf['event_socket.port'] ?? '8021';
$pass = $conf['switch.event_socket.password'] ?? $conf['event_socket.password'] ?? 'ClueCon';
//view_array($results);
foreach($results as $row) {
//connect to event socket using a lower timeout because we are on cli
$esl = event_socket::create($host, $port, $pass, 10000);
//build the event
$cmd = "sendevent PRESENCE_IN\n";
$cmd .= "proto: sip\n";
$cmd .= "login: ".$row['extension']."@".$row['domain_name']."\n";
$cmd .= "from: ".$row['extension']."@".$row['domain_name']."\n";
$cmd .= "status: Active (1 waiting)\n";
$cmd .= "rpid: unknown\n";
$cmd .= "event_type: presence\n";
$cmd .= "alt_event_type: dialog\n";
$cmd .= "event_count: 1\n";
$cmd .= "unique-id: ".uuid()."\n";
$cmd .= "Presence-Call-Direction: outbound\n";
$cmd .= "answer-state: confirmed\n";
//$cmd .= "answer-state: early\n";
//$cmd .= "answer-state: terminated\n";
//ensure we are connected
if ($esl->is_connected()) {
//send message to the console
if (isset($debug)) {
echo "\n";
echo "[presence] dnd ".$row['extension']."@".$row['domain_name']."\n";
//get the list
$sql = "select domain_name, extension, user_context, do_not_disturb, description ";
$sql .= "from v_extensions as e, v_domains as d ";
$sql .= "where do_not_disturb = 'true' ";
$sql .= "and e.domain_uuid = d.domain_uuid ";
$sql .= "and enabled = 'true' ";
$database = new database;
$results = $database->select($sql, $parameters, 'all');
unset($parameters);
foreach ($results as $row) {
//build the event
$cmd = "sendevent PRESENCE_IN\n";
$cmd .= "proto: sip\n";
$cmd .= "login: " . $row['extension'] . "@" . $row['domain_name'] . "\n";
$cmd .= "from: " . $row['extension'] . "@" . $row['domain_name'] . "\n";
$cmd .= "status: Active (1 waiting)\n";
$cmd .= "rpid: unknown\n";
$cmd .= "event_type: presence\n";
$cmd .= "alt_event_type: dialog\n";
$cmd .= "event_count: 1\n";
$cmd .= "unique-id: " . uuid() . "\n";
$cmd .= "Presence-Call-Direction: outbound\n";
$cmd .= "answer-state: confirmed\n";
//$cmd .= "answer-state: early\n";
//$cmd .= "answer-state: terminated\n";
//send message to the console
if (isset($debug)) {
echo "\n";
echo "[presence] dnd " . $row['extension'] . "@" . $row['domain_name'] . "\n";
}
//send the event
$result = event_socket::command($cmd);
if (isset($debug)) {
print_r($result, false);
}
}
//send the event
$result = event_socket_request($fp, $cmd);
if (isset($debug)) {
print_r($result, false);
}
}
else {
trigger_error("Unable to connect to FreeSWITCH using $host, $port, $password", E_USER_ERROR);
}
/*
* * * * * cd /var/www/fusionpbx && php /var/www/fusionpbx/app/call_forward/resources/jobs/dnd.php
*/
?>
/*
* * * * * cd /var/www/fusionpbx && php /var/www/fusionpbx/app/call_forward/resources/jobs/dnd.php
*/