Extract function from core of daemons
This commit is contained in:
parent
67c7753e7d
commit
ac64fe9d2d
|
@ -377,6 +377,26 @@ namespace controllers\internals;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a scheduled message
|
||||||
|
* @param int $id_scheduled : Id of the message to send
|
||||||
|
* @param string $text : Text of the message
|
||||||
|
* @param string $origin : Origin of the message
|
||||||
|
* @param string $destination : Destination of the message
|
||||||
|
* @param bool $flash : Is the message a flash sms
|
||||||
|
* return bool: false on error, true else
|
||||||
|
*/
|
||||||
|
public function send (int $id_scheduled, string $text, string $origin, string $destination, bool $flash) : bool
|
||||||
|
{
|
||||||
|
//TODO : Do the sending (càd, instanciate a phone adapter and send the message)
|
||||||
|
|
||||||
|
$now = new \DateTime();
|
||||||
|
$at = $now->format('Y-m-d H:i:s');
|
||||||
|
$internal_sended = new \controllers\internals\Sended($this->bdd);
|
||||||
|
$this->delete($id_scheduled);
|
||||||
|
return $internal_sended->create($at, $text, $origin, $destination, $flash);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return numbers for a scheduled message
|
* Return numbers for a scheduled message
|
||||||
|
|
|
@ -42,6 +42,17 @@ class Phone extends AbstractDaemon
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Send a sms
|
||||||
|
$this->send_sms();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send sms
|
||||||
|
*/
|
||||||
|
public function send_sms () : bool
|
||||||
|
{
|
||||||
|
//Call message
|
||||||
$msgtype = null;
|
$msgtype = null;
|
||||||
$maxsize = 409600;
|
$maxsize = 409600;
|
||||||
$message = null;
|
$message = null;
|
||||||
|
@ -50,25 +61,22 @@ class Phone extends AbstractDaemon
|
||||||
|
|
||||||
if (!$message)
|
if (!$message)
|
||||||
{
|
{
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
|
||||||
$internal_sended = new \controllers\internals\Sended($bdd);
|
|
||||||
|
|
||||||
//If message received, update last message time
|
//If message received, update last message time
|
||||||
$this->last_message_at = microtime(true);
|
$this->last_message_at = microtime(true);
|
||||||
|
|
||||||
//Register message as sended
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||||
$now = new \DateTime();
|
$internal_scheduled = new \controllers\internals\Scheduled($bdd);
|
||||||
$now = $now->format('Y-m-d H:i:s');
|
$internal_scheduled->send($message['id_scheduled'], $message['text'], $message['origin'], $message['destination'], $message['flash']);
|
||||||
$internal_sended->create($now, $message['text'], $message['origin'], $message['destination'], $message['flash']);
|
|
||||||
|
|
||||||
//Close bdd
|
//Close bdd
|
||||||
$bdd = null;
|
$bdd = null;
|
||||||
$internal_sended = null;
|
$internal_scheduled = null;
|
||||||
|
|
||||||
$this->logger->info('Send message : ' . json_encode($message));
|
$this->logger->info('Send message : ' . json_encode($message));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,10 +9,11 @@ use \Monolog\Handler\StreamHandler;
|
||||||
*/
|
*/
|
||||||
class Server extends AbstractDaemon
|
class Server extends AbstractDaemon
|
||||||
{
|
{
|
||||||
private $internal_user;
|
|
||||||
private $internal_phone;
|
private $internal_phone;
|
||||||
private $internal_scheduled;
|
private $internal_scheduled;
|
||||||
|
private $internal_received;
|
||||||
private $bdd;
|
private $bdd;
|
||||||
|
private $daemons_queues = [];
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
@ -35,13 +36,35 @@ class Server extends AbstractDaemon
|
||||||
{
|
{
|
||||||
//Create the internal controllers
|
//Create the internal controllers
|
||||||
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||||
$this->internal_user = new \controllers\internals\User($this->bdd);
|
|
||||||
$this->internal_phone = new \controllers\internals\Phone($this->bdd);
|
$this->internal_phone = new \controllers\internals\Phone($this->bdd);
|
||||||
$this->internal_scheduled = new \controllers\internals\Scheduled($this->bdd);
|
$this->internal_scheduled = new \controllers\internals\Scheduled($this->bdd);
|
||||||
|
$this->internal_received = new \controllers\internals\Received($this->bdd);
|
||||||
|
|
||||||
|
|
||||||
//Start all phones daemons
|
//Start all phones daemons
|
||||||
$phones = $this->internal_phone->get_all();
|
$phones = $this->internal_phone->get_all();
|
||||||
|
$this->start_daemons($phones);
|
||||||
|
|
||||||
|
|
||||||
|
//Send smss
|
||||||
|
$smss = $this->internal_scheduled->get_smss_to_send();
|
||||||
|
$this->daemons_queues = $this->send_smss($this->daemons_queues, $smss, $this->internal_scheduled); //Add new queues to array of queues
|
||||||
|
|
||||||
|
|
||||||
|
//Read smss
|
||||||
|
//$this->read_smss($this->internal_received);
|
||||||
|
|
||||||
|
sleep(0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to start phones daemons
|
||||||
|
* @param array $phones : Phones to start daemon for if the daemon is not already started
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function start_daemons (array $phones) : void
|
||||||
|
{
|
||||||
foreach ($phones as $phone)
|
foreach ($phones as $phone)
|
||||||
{
|
{
|
||||||
$phone_name = 'RaspiSMS Phone ' . $phone['number'];
|
$phone_name = 'RaspiSMS Phone ' . $phone['number'];
|
||||||
|
@ -55,14 +78,21 @@ class Server extends AbstractDaemon
|
||||||
//Create a new daemon for the phone
|
//Create a new daemon for the phone
|
||||||
exec('php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &');
|
exec('php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$queues = [];
|
|
||||||
|
|
||||||
//Get all sms to send
|
/**
|
||||||
$smss = $this->internal_scheduled->get_smss_to_send();
|
* Function to get messages to send and transfer theme to daemons
|
||||||
|
* @param array $queues : Queues for phones
|
||||||
|
* @param array $smss : Smss to send
|
||||||
|
* @param \controllers\internals\Scheduled $internal_scheduled : Internal Scheduled
|
||||||
|
* @return array : array of queues with new queues appened
|
||||||
|
*/
|
||||||
|
public function send_smss (array $queues, array $smss, \controllers\internals\Scheduled $internal_scheduled) : array
|
||||||
|
{
|
||||||
foreach ($smss as $sms)
|
foreach ($smss as $sms)
|
||||||
{
|
{
|
||||||
//If queue has been deleted or does not exist, create a new one
|
//If the queue has been deleted or does not exist, create a new one
|
||||||
$queue_id = (int) mb_substr($sms['origin'], 1);
|
$queue_id = (int) mb_substr($sms['origin'], 1);
|
||||||
if (!msg_queue_exists($queue_id))
|
if (!msg_queue_exists($queue_id))
|
||||||
{
|
{
|
||||||
|
@ -76,19 +106,17 @@ class Server extends AbstractDaemon
|
||||||
$queue = $queues[$queue_id];
|
$queue = $queues[$queue_id];
|
||||||
|
|
||||||
$msg = [
|
$msg = [
|
||||||
'text' => (string) $sms['text'],
|
'id_scheduled' => $sms['id_scheduled'],
|
||||||
'origin' => (string) $sms['origin'],
|
'text' => $sms['text'],
|
||||||
'destination' => (string) $sms['destination'],
|
'origin' => $sms['origin'],
|
||||||
'flash' => (bool) $sms['flash'],
|
'destination' => $sms['destination'],
|
||||||
|
'flash' => $sms['flash'],
|
||||||
];
|
];
|
||||||
|
|
||||||
msg_send($queue, SEND_MSG, $msg);
|
msg_send($queue, SEND_MSG, $msg);
|
||||||
|
|
||||||
//Delete the scheduled sms after sending
|
|
||||||
$this->internal_scheduled->delete($sms['id_scheduled']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep(0.5);
|
return $queues;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue