mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-21 08:56:27 +02:00
Separate daemons and responsabilities insides thoses daemons. Add webhook & commands, still to test
This commit is contained in:
parent
78b3ded31d
commit
b5a36b1169
9 changed files with 530 additions and 184 deletions
|
@ -10,15 +10,24 @@ use \Monolog\Handler\StreamHandler;
|
|||
class Phone extends AbstractDaemon
|
||||
{
|
||||
private $msg_queue;
|
||||
private $queue_id;
|
||||
private $msg_queue_id;
|
||||
private $webhook_queue;
|
||||
private $last_message_at;
|
||||
private $phone;
|
||||
private $adapter;
|
||||
private $bdd;
|
||||
|
||||
public function __construct($phone_number)
|
||||
/**
|
||||
* Constructor
|
||||
* @param array $phone : A phone table entry
|
||||
*/
|
||||
public function __construct(array $phone)
|
||||
{
|
||||
$this->queue_id = (int) mb_substr($phone_number, 1);
|
||||
$this->phone = $phone;
|
||||
$this->msg_queue_id = (int) mb_substr($this->phone['number'], 1);
|
||||
|
||||
$name = 'RaspiSMS Daemon Phone ' . $this->phone['number'];
|
||||
|
||||
$name = 'RaspiSMS Phone ' . $phone_number;
|
||||
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
|
||||
|
@ -35,51 +44,162 @@ class Phone extends AbstractDaemon
|
|||
|
||||
public function run()
|
||||
{
|
||||
//Stop after 5 minutes of inactivity to avoid useless daemon
|
||||
if ( (microtime(true) - $this->last_message_at) > 5 * 60 )
|
||||
{
|
||||
$this->is_running = false;
|
||||
$this->logger->info("End running");
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
|
||||
//Send smss in queue
|
||||
$this->send_smss();
|
||||
|
||||
//Send a sms
|
||||
$this->send_sms();
|
||||
//Read received smss
|
||||
$this->read_smss();
|
||||
|
||||
usleep(0.5 * 1000000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send sms
|
||||
*/
|
||||
public function send_sms () : bool
|
||||
private function send_smss ()
|
||||
{
|
||||
//Call message
|
||||
$msgtype = null;
|
||||
$maxsize = 409600;
|
||||
$message = null;
|
||||
|
||||
msg_receive($this->msg_queue, SEND_MSG, $msgtype, $maxsize, $message);
|
||||
$find_message = true;
|
||||
while ($find_message)
|
||||
{
|
||||
msg_receive($this->msg_queue, QUEUE_TYPE_SEND_MSG, $msgtype, $maxsize, $message);
|
||||
|
||||
if (!$message)
|
||||
if (!$message)
|
||||
{
|
||||
$find_message = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
//Update last message time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
$now = new \DateTime();
|
||||
$at = $now->format('Y-m-d H:i:s');
|
||||
|
||||
$message['at'] = $at;
|
||||
|
||||
$this->logger->info('Try send message : ' . json_encode($message));
|
||||
|
||||
$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));
|
||||
$internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash'], 'failed');
|
||||
continue;
|
||||
}
|
||||
|
||||
//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->logger->info('Successfully send message : ' . json_encode($message));
|
||||
|
||||
$internal_sended = new \controllers\internals\Sended($this->bdd);
|
||||
$internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $sended_sms_uid, $this->phone['adapter'], $message['flash']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read smss for a number
|
||||
*/
|
||||
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();
|
||||
if (!$smss)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//Get users settings
|
||||
$user_settings = $internal_setting->gets_for_user($this->phone['id_user']);
|
||||
|
||||
|
||||
//Process smss
|
||||
foreach ($smss as $sms)
|
||||
{
|
||||
$this->logger->info('Receive message : ' . json_encode($sms));
|
||||
|
||||
$command_result = $this->process_for_command($sms);
|
||||
$sms['text'] = $command_result['text'];
|
||||
$is_command = $command_result['is_command'];
|
||||
|
||||
$this->process_for_webhook($sms, 'receive_sms', $user_settings);
|
||||
|
||||
$this->internal_received->create($sms['at'], $sms['text'], $sms['origin'], $sms['destination'], 'unread', $is_command);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process a sms to find if its a command and so execute it
|
||||
* @param array $sms : The sms
|
||||
* @return array : ['text' => new sms text, 'is_command' => bool]
|
||||
*/
|
||||
private function process_for_command (array $sms)
|
||||
{
|
||||
$is_command = false;
|
||||
$command = $internal_command->check_for_command($this->phone['id_user'], $sms['text']);
|
||||
if ($command)
|
||||
{
|
||||
$is_command = true;
|
||||
exec($command['command']);
|
||||
}
|
||||
|
||||
return ['text' => $command['updated_text'], 'is_command' => $is_command];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process a sms to transmit a webhook query to webhook daemon if needed
|
||||
* @param array $sms : The sms
|
||||
* @param string $webhook_type : Type of webhook to trigger
|
||||
* @param array $user_settings : Use settings
|
||||
*/
|
||||
private function process_for_webhook (array $sms, string $webhook_type, array $user_settings)
|
||||
{
|
||||
if (!$user_settings['webhook'])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//If message received, update last message time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
$now = new \DateTime();
|
||||
$at = $now->format('Y-m-d H:i:s');
|
||||
|
||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
$internal_sended = new \controllers\internals\Sended($bdd);
|
||||
$internal_sended->create($at, $message['text'], $message['origin'], $message['destination'], $message['flash']);
|
||||
|
||||
//Close bdd
|
||||
$bdd = null;
|
||||
$internal_scheduled = null;
|
||||
|
||||
$this->logger->info('Send message : ' . json_encode($message));
|
||||
return true;
|
||||
$internal_webhook = new \controllers\internals\Webhook($this->bdd);
|
||||
|
||||
$webhooks = $internal_webhook->gets_for_type_and_user($this->phone['id_user'], $webhook_type);
|
||||
foreach ($webhooks as $webhook)
|
||||
{
|
||||
$message = [
|
||||
'url' => $webhook['url'],
|
||||
'datas' => [
|
||||
'webhook_type' => $webhook['type'],
|
||||
'at' => $sms['at'],
|
||||
'text' => $sms['text'],
|
||||
'origin' => $sms['origin'],
|
||||
'destination' => $sms['destination'],
|
||||
],
|
||||
];
|
||||
msg_send($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $webhook);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -87,19 +207,24 @@ class Phone extends AbstractDaemon
|
|||
{
|
||||
//Set last message at to construct time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
$this->msg_queue = msg_get_queue($this->queue_id);
|
||||
|
||||
$this->logger->info("Starting Phone with pid " . getmypid());
|
||||
$this->msg_queue = msg_get_queue($this->msg_queue_id);
|
||||
$this->webhook_queue = msg_get_queue(QUEUE_ID_WEBHOOK);
|
||||
|
||||
//Instanciate adapter
|
||||
$this->adapter = new \adapters\TestAdapter($this->phone['number'], $this->phone['adapter_datas']);
|
||||
|
||||
$this->logger->info("Starting Phone daemon with pid " . getmypid());
|
||||
}
|
||||
|
||||
|
||||
public function on_stop()
|
||||
{
|
||||
$this->logger->info("Closing queue : " . $this->queue_id);
|
||||
msg_remove_queue($this->msg_queue); //Delete queue on daemon close
|
||||
//Delete queue on daemon close
|
||||
$this->logger->info("Closing queue : " . $this->msg_queue_id);
|
||||
msg_remove_queue($this->msg_queue);
|
||||
|
||||
$this->logger->info("Stopping Phone with pid " . getmypid ());
|
||||
$this->logger->info("Stopping Phone daemon with pid " . getmypid ());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue