All working reception and sending with webhook. Commands still to test. Update test adapter to use local files

This commit is contained in:
osaajani 2020-01-07 17:55:16 +01:00
parent b5a36b1169
commit 768714cc1a
7 changed files with 80 additions and 23 deletions

View File

@ -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);
/**

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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)
{

1
datas/test_read_sms.json Normal file
View File

@ -0,0 +1 @@