diff --git a/adapters/AdapterInterface.php b/adapters/AdapterInterface.php index d3429f9..dd8f19c 100644 --- a/adapters/AdapterInterface.php +++ b/adapters/AdapterInterface.php @@ -53,7 +53,7 @@ * @param bool $flash : Is the SMS a Flash SMS * @return mixed Uid of the sended message if send, False else */ - public function send (string $destination, string $text, boolean $flash) : mixed; + public function send (string $destination, string $text, bool $flash); /** diff --git a/adapters/OvhSmsAdapter.php b/adapters/OvhSmsAdapter.php index fb04367..6983258 100644 --- a/adapters/OvhSmsAdapter.php +++ b/adapters/OvhSmsAdapter.php @@ -68,7 +68,7 @@ * @param bool $flash : Is the SMS a Flash SMS * @return mixed Uid of the sended message if send, False else */ - public function send (string $destination, string $text, boolean $flash) : mixed + public function send (string $destination, string $text, bool $flash) { return uniqid(); } diff --git a/adapters/TestAdapter.php b/adapters/TestAdapter.php index 760d313..40da02f 100644 --- a/adapters/TestAdapter.php +++ b/adapters/TestAdapter.php @@ -48,6 +48,17 @@ */ private $datas; + + /** + * Path for the file to read sms as a json from + */ + private $test_file_read = PWD_DATAS . '/test_read_sms.json'; + + /** + * Path for the file to write sms as a json in + */ + private $test_file_write = PWD_DATAS . '/test_write_sms.json'; + /** * Adapter constructor, called when instanciated by RaspiSMS @@ -68,8 +79,13 @@ * @param bool $flash : Is the SMS a Flash SMS * @return mixed Uid of the sended message if send, False else */ - public function send (string $destination, string $text, boolean $flash) : mixed + public function send (string $destination, string $text, bool $flash) { + $uid = uniqid(); + + $at = (new \DateTime())->format('Y-m-d H:i:s'); + file_put_contents($this->test_file_write, json_encode(['uid' => $uid, 'at' => $at, 'destination' => $destination, 'text' => $text, 'flash' => $flash]) . "\n", FILE_APPEND); + return uniqid(); } @@ -80,6 +96,26 @@ */ public function read () : array { - return []; + $file_contents = file_get_contents($this->test_file_read); + + //Empty file to avoid dual read + file_put_contents($this->test_file_read, ''); + + $smss = explode("\n", $file_contents); + + $return = []; + + foreach ($smss as $key => $sms) + { + $decode_sms = json_decode($sms, true); + if (NULL === $decode_sms) + { + continue; + } + + $return[] = $decode_sms; + } + + return $return; } } diff --git a/controllers/internals/Command.php b/controllers/internals/Command.php index 314b2fc..dc94c7b 100755 --- a/controllers/internals/Command.php +++ b/controllers/internals/Command.php @@ -87,7 +87,7 @@ namespace controllers\internals; { $extracted_command = []; - $decode_message = json_decode(trim($message)); + $decode_message = json_decode(trim($message), true); if ($decode_message === null) { return false; diff --git a/daemons/Phone.php b/daemons/Phone.php index 430324b..6e25423 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -69,15 +69,22 @@ class Phone extends AbstractDaemon */ private function send_smss () { - //Call message - $msgtype = null; - $maxsize = 409600; - $message = null; - $find_message = true; while ($find_message) { - msg_receive($this->msg_queue, QUEUE_TYPE_SEND_MSG, $msgtype, $maxsize, $message); + //Call message + $msgtype = null; + $maxsize = 409600; + $message = null; + + $error_code = null; + $success = msg_receive($this->msg_queue, QUEUE_TYPE_SEND_MSG, $msgtype, $maxsize, $message, TRUE, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found + + if (!$success && $error_code !== MSG_ENOMSG) + { + $this->logger->critical('Error reading MSG SEND Queue, error code : ' . $error); + return false; + } if (!$message) { @@ -98,7 +105,7 @@ class Phone extends AbstractDaemon $sended_sms_uid = $this->adapter->send($message['destination'], $message['text'], $message['flash']); if (!$sended_sms_uid) { - $this->logger->info('Failed send message : ' . json_encode($message)); + $this->logger->error('Failed send message : ' . json_encode($message)); $internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash'], 'failed'); continue; } @@ -106,7 +113,7 @@ class Phone extends AbstractDaemon //Run webhook $internal_setting = new \controllers\internals\Setting($this->bdd); $user_settings = $internal_setting->gets_for_user($this->phone['id_user']); - process_for_webhook($message, 'send_sms', $user_settings); + $this->process_for_webhook($message, 'send_sms', $user_settings); $this->logger->info('Successfully send message : ' . json_encode($message)); @@ -122,7 +129,6 @@ class Phone extends AbstractDaemon private function read_smss () { $internal_received = new \controllers\internals\Received($this->bdd); - $internal_command = new \controllers\internals\Command($this->bdd); $internal_setting = new \controllers\internals\Setting($this->bdd); $smss = $this->adapter->read(); @@ -146,7 +152,7 @@ class Phone extends AbstractDaemon $this->process_for_webhook($sms, 'receive_sms', $user_settings); - $this->internal_received->create($sms['at'], $sms['text'], $sms['origin'], $sms['destination'], 'unread', $is_command); + $internal_received->create($sms['at'], $sms['text'], $sms['origin'], $sms['destination'], 'unread', $is_command); } } @@ -158,15 +164,18 @@ class Phone extends AbstractDaemon */ private function process_for_command (array $sms) { + $internal_command = new \controllers\internals\Command($this->bdd); + $is_command = false; $command = $internal_command->check_for_command($this->phone['id_user'], $sms['text']); if ($command) { $is_command = true; + $sms['text'] = $command['updated_text']; exec($command['command']); } - return ['text' => $command['updated_text'], 'is_command' => $is_command]; + return ['text' => $sms['text'], 'is_command' => $is_command]; } @@ -198,7 +207,13 @@ class Phone extends AbstractDaemon 'destination' => $sms['destination'], ], ]; - msg_send($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $webhook); + + $error_code = null; + $success = msg_send($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $message, TRUE, TRUE, $error_code); + if (!$success) + { + $this->logger->critical("Failed send webhook message in queue, error code : " . $error_code); + } } } diff --git a/daemons/Webhook.php b/daemons/Webhook.php index 188bd19..5e79e69 100644 --- a/daemons/Webhook.php +++ b/daemons/Webhook.php @@ -39,15 +39,20 @@ class Webhook extends AbstractDaemon public function run() { - //Call message - $msgtype = null; - $maxsize = 409600; - $message = null; - $find_message = true; while ($find_message) { - msg_receive($this->webhook_queue, QUEUE_ID_WEBHOOK, $msgtype, $maxsize, $message); + //Call message + $msgtype = null; + $maxsize = 409600; + $message = null; + + $error_code = null; + $success = msg_receive($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $msgtype, $maxsize, $message, true, 0, $error_code); + if (!$success) + { + $this->logger->critical('Error for webhook queue reading, error code : ' . $error_code); + } if (!$message) { diff --git a/datas/test_read_sms.json b/datas/test_read_sms.json new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/datas/test_read_sms.json @@ -0,0 +1 @@ +