2020-01-07 01:31:34 +01:00
|
|
|
<?php
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
namespace daemons;
|
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
use controllers\internals\Queue;
|
|
|
|
use Exception;
|
2020-01-17 18:19:25 +01:00
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Logger;
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Main daemon class.
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
|
|
|
class Sender extends AbstractDaemon
|
|
|
|
{
|
|
|
|
private $internal_scheduled;
|
|
|
|
private $bdd;
|
2024-10-31 21:22:56 +01:00
|
|
|
private ?Queue $queue;
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$name = 'RaspiSMS Daemon Sender';
|
2020-01-08 18:20:17 +01:00
|
|
|
$logger = new Logger($name);
|
2020-03-04 04:18:26 +01:00
|
|
|
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/daemons.log', Logger::DEBUG));
|
2020-01-07 01:31:34 +01:00
|
|
|
$pid_dir = PWD_PID;
|
2020-01-08 18:20:17 +01:00
|
|
|
$no_parent = false; //Webhook should be rattach to manager, so manager can stop him easily
|
2020-01-07 01:31:34 +01:00
|
|
|
$additional_signals = [];
|
2020-01-08 18:20:17 +01:00
|
|
|
$uniq = true; //Webhook should be uniq
|
2020-01-07 01:31:34 +01:00
|
|
|
|
2020-01-08 18:20:17 +01:00
|
|
|
//Construct the daemon
|
|
|
|
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
parent::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->internal_scheduled = new \controllers\internals\Scheduled($this->bdd);
|
|
|
|
|
|
|
|
//Get smss and transmit order to send to appropriate phone daemon
|
2021-09-17 22:31:23 +02:00
|
|
|
$smss_per_scheduled = $this->internal_scheduled->get_smss_to_send();
|
|
|
|
$this->transmit_smss($smss_per_scheduled); //Add new queue to array of queues
|
2020-01-07 01:31:34 +01:00
|
|
|
|
|
|
|
usleep(0.5 * 1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-04 03:28:34 +01:00
|
|
|
* Function to transfer smss to send to phones daemons.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2021-09-17 22:31:23 +02:00
|
|
|
* @param array $smss_per_scheduled : Smss to send per scheduled id
|
2020-01-07 01:31:34 +01:00
|
|
|
*/
|
2021-09-17 22:31:23 +02:00
|
|
|
public function transmit_smss(array $smss_per_scheduled): void
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
2021-09-17 22:31:23 +02:00
|
|
|
foreach ($smss_per_scheduled as $id_scheduled => $smss)
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
2022-09-28 20:01:55 +02:00
|
|
|
foreach ($smss as $sms)
|
|
|
|
{
|
2021-09-17 22:31:23 +02:00
|
|
|
$msg = [
|
|
|
|
'id_user' => $sms['id_user'],
|
|
|
|
'id_scheduled' => $sms['id_scheduled'],
|
|
|
|
'text' => $sms['text'],
|
|
|
|
'id_phone' => $sms['id_phone'],
|
|
|
|
'destination' => $sms['destination'],
|
|
|
|
'flash' => $sms['flash'],
|
|
|
|
'mms' => $sms['mms'],
|
2023-02-24 16:29:10 +01:00
|
|
|
'tag' => $sms['tag'],
|
2021-09-17 22:31:23 +02:00
|
|
|
'medias' => $sms['medias'] ?? [],
|
|
|
|
];
|
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
|
2022-09-28 20:01:55 +02:00
|
|
|
// Message type is forged from a prefix concat with the phone ID
|
|
|
|
$message_type = (int) QUEUE_TYPE_SEND_MSG_PREFIX . $sms['id_phone'];
|
2024-10-31 21:22:56 +01:00
|
|
|
$this->queue->push(json_encode($msg), $message_type);
|
2022-09-28 20:01:55 +02:00
|
|
|
$this->logger->info('Transmit sms send signal to phone ' . $sms['id_phone'] . ' on queue ' . QUEUE_ID_PHONE . ' with message type ' . $message_type . '.');
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 22:31:23 +02:00
|
|
|
$this->logger->info('Scheduled ' . $id_scheduled . ' treated and deleted.');
|
|
|
|
$this->internal_scheduled->delete($id_scheduled);
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function on_start()
|
|
|
|
{
|
2024-10-31 21:22:56 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$this->logger->info('Starting Sender with pid ' . getmypid());
|
|
|
|
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
|
|
|
$this->queue = new Queue(QUEUE_ID_PHONE);
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
$this->logger->error('Failed to start sender daemon : ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
public function on_stop()
|
2020-01-07 01:31:34 +01:00
|
|
|
{
|
2022-09-28 20:01:55 +02:00
|
|
|
//Delete queue on daemon close
|
2024-10-31 21:22:56 +01:00
|
|
|
$this->logger->info('Closing queue : ' . QUEUE_ID_PHONE);
|
|
|
|
$this->queue->close();
|
|
|
|
|
2020-01-17 18:47:08 +01:00
|
|
|
$this->logger->info('Stopping Sender with pid ' . getmypid());
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle_other_signals($signal)
|
|
|
|
{
|
2020-01-17 18:47:08 +01:00
|
|
|
$this->logger->info('Signal not handled by ' . $this->name . ' Daemon : ' . $signal);
|
2020-01-07 01:31:34 +01:00
|
|
|
}
|
|
|
|
}
|