From ac64fe9d2d2198bc73d96ea322089d1b6a83a6c1 Mon Sep 17 00:00:00 2001 From: osaajani Date: Sat, 4 Jan 2020 19:30:06 +0100 Subject: [PATCH] Extract function from core of daemons --- controllers/internals/Scheduled.php | 20 +++++++++++ daemons/Phone.php | 28 +++++++++------ daemons/Server.php | 56 +++++++++++++++++++++-------- 3 files changed, 80 insertions(+), 24 deletions(-) diff --git a/controllers/internals/Scheduled.php b/controllers/internals/Scheduled.php index d673b03..121e133 100755 --- a/controllers/internals/Scheduled.php +++ b/controllers/internals/Scheduled.php @@ -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 diff --git a/daemons/Phone.php b/daemons/Phone.php index 2242ae3..2dfc854 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -42,6 +42,17 @@ class Phone extends AbstractDaemon return true; } + //Send a sms + $this->send_sms(); + } + + + /** + * Send sms + */ + public function send_sms () : bool + { + //Call message $msgtype = null; $maxsize = 409600; $message = null; @@ -50,25 +61,22 @@ class Phone extends AbstractDaemon 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 $this->last_message_at = microtime(true); - - //Register message as sended - $now = new \DateTime(); - $now = $now->format('Y-m-d H:i:s'); - $internal_sended->create($now, $message['text'], $message['origin'], $message['destination'], $message['flash']); + + $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8'); + $internal_scheduled = new \controllers\internals\Scheduled($bdd); + $internal_scheduled->send($message['id_scheduled'], $message['text'], $message['origin'], $message['destination'], $message['flash']); //Close bdd $bdd = null; - $internal_sended = null; + $internal_scheduled = null; $this->logger->info('Send message : ' . json_encode($message)); + return true; } diff --git a/daemons/Server.php b/daemons/Server.php index 04cca34..76d933c 100644 --- a/daemons/Server.php +++ b/daemons/Server.php @@ -9,10 +9,11 @@ use \Monolog\Handler\StreamHandler; */ class Server extends AbstractDaemon { - private $internal_user; private $internal_phone; private $internal_scheduled; + private $internal_received; private $bdd; + private $daemons_queues = []; public function __construct() { @@ -35,13 +36,35 @@ class Server extends AbstractDaemon { //Create the internal controllers $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_scheduled = new \controllers\internals\Scheduled($this->bdd); + $this->internal_received = new \controllers\internals\Received($this->bdd); //Start all phones daemons $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) { $phone_name = 'RaspiSMS Phone ' . $phone['number']; @@ -55,14 +78,21 @@ class Server extends AbstractDaemon //Create a new daemon for the phone 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) { - //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); if (!msg_queue_exists($queue_id)) { @@ -76,19 +106,17 @@ class Server extends AbstractDaemon $queue = $queues[$queue_id]; $msg = [ - 'text' => (string) $sms['text'], - 'origin' => (string) $sms['origin'], - 'destination' => (string) $sms['destination'], - 'flash' => (bool) $sms['flash'], + 'id_scheduled' => $sms['id_scheduled'], + 'text' => $sms['text'], + 'origin' => $sms['origin'], + 'destination' => $sms['destination'], + 'flash' => $sms['flash'], ]; msg_send($queue, SEND_MSG, $msg); - - //Delete the scheduled sms after sending - $this->internal_scheduled->delete($sms['id_scheduled']); } - sleep(0.5); + return $queues; }