mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-06-06 14:46:27 +02:00
Move to raspisms dir
This commit is contained in:
parent
34a6f7de65
commit
40fccf133c
278 changed files with 109 additions and 2020 deletions
|
@ -1,217 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace daemons;
|
||||
|
||||
/**
|
||||
* Class defining the global structur of a Linux Daemon.
|
||||
*/
|
||||
abstract class AbstractDaemon
|
||||
{
|
||||
protected $name;
|
||||
protected $uniq;
|
||||
protected $logger;
|
||||
protected $no_parent;
|
||||
protected $pid_dir;
|
||||
private $is_running = true;
|
||||
private $signals = [
|
||||
SIGTERM,
|
||||
SIGINT,
|
||||
SIGCHLD,
|
||||
SIGHUP,
|
||||
];
|
||||
|
||||
/**
|
||||
* Class used to handle POSIX signals and fork from the current process.
|
||||
*
|
||||
* @param string $name : The name of the class
|
||||
* @param object $logger : A PSR3 logger instance
|
||||
* @param string $pid_dir : Directory for the pid files
|
||||
* @param bool $no_parent : Should the daemon be disconnected from his parent process
|
||||
* @param array $signals :An array containing additional POSIX signals to handle [optionel]
|
||||
* @param bool $uniq : Must the process be uniq ?
|
||||
*/
|
||||
protected function __construct(string $name, object $logger, string $pid_dir = '/var/run', $no_parent = false, array $signals = [], bool $uniq = false)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->logger = $logger;
|
||||
$this->no_parent = $no_parent;
|
||||
$this->pid_dir = $pid_dir;
|
||||
$this->signals = array_merge($this->signals, $signals);
|
||||
$this->uniq = $uniq;
|
||||
|
||||
//Allow script to run indefinitly
|
||||
set_time_limit(0);
|
||||
|
||||
//Register signals
|
||||
$this->register_signals();
|
||||
}
|
||||
|
||||
/**
|
||||
* True if the daemon is running.
|
||||
*/
|
||||
public function is_running()
|
||||
{
|
||||
return $this->is_running;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to handle properly SIGINT, SIGTERM, SIGCHLD and SIGHUP.
|
||||
*
|
||||
* @param int $signal
|
||||
* @param mixed $signinfo
|
||||
*/
|
||||
protected function handle_signal(int $signal, $signinfo)
|
||||
{
|
||||
if (SIGTERM === $signal || SIGINT === $signal)
|
||||
{ //Stop the daemon
|
||||
$this->is_running = false;
|
||||
}
|
||||
elseif (SIGHUP === $signal)
|
||||
{ //Restart the daemon
|
||||
$this->on_stop();
|
||||
$this->on_start();
|
||||
}
|
||||
elseif (SIGCHLD === $signal)
|
||||
{ //On daemon child stopping
|
||||
pcntl_waitpid(-1, $status, WNOHANG);
|
||||
}
|
||||
else
|
||||
{ //All the other signals
|
||||
$this->handle_other_signals($signal);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch the infinite loop executing the "run" abstract method.
|
||||
*/
|
||||
protected function start()
|
||||
{
|
||||
//If process must be uniq and a process with the same pid file is already running
|
||||
if (file_exists($this->pid_dir . '/' . $this->name . '.pid') && $this->uniq)
|
||||
{
|
||||
$this->logger->info('Another process named ' . $this->name . ' is already running.');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//If we must make the daemon independant from any parent, we do a fork and die operation
|
||||
if ($this->no_parent)
|
||||
{
|
||||
$pid = pcntl_fork(); //Fork current process into a child, so we can kill current process and keep only the child with parent PID = 1
|
||||
|
||||
if (-1 === $pid)
|
||||
{ //Impossible to run script
|
||||
$this->logger->critical('Impossible to create a subprocess.');
|
||||
|
||||
return false;
|
||||
}
|
||||
if ($pid)
|
||||
{ //Current script
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->logger->info("Process {$this->name} started as a child with pid " . getmypid() . '.');
|
||||
|
||||
//Child script
|
||||
$sid = posix_setsid(); //Try to make the child process a main process
|
||||
if (-1 === $sid)
|
||||
{ //Error
|
||||
$this->logger->critical("Cannot make the child process with pid {$pid} independent.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$this->logger->info('The child process with pid ' . getmypid() . ' is now independent.');
|
||||
}
|
||||
|
||||
//Create pid dir if not exists
|
||||
if (!file_exists($this->pid_dir))
|
||||
{
|
||||
$success = mkdir($this->pid_dir, 0777, true);
|
||||
if (!$success)
|
||||
{
|
||||
$this->logger->critical('Cannot create PID directory : ' . $this->pid_dir);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
//Set process name
|
||||
cli_set_process_title($this->name);
|
||||
|
||||
//Write the pid of the process into a file
|
||||
file_put_contents($this->pid_dir . '/' . $this->name . '.pid', getmypid());
|
||||
|
||||
//Really start the daemon
|
||||
$this->on_start();
|
||||
|
||||
try
|
||||
{
|
||||
while ($this->is_running)
|
||||
{
|
||||
pcntl_signal_dispatch(); //Call dispatcher for signals
|
||||
$this->run();
|
||||
}
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$this->logger->critical('Exception : ' . $e->getMessage() . ' in ' . $e->getFile() . ' line ' . $e->getLine());
|
||||
}
|
||||
|
||||
//Stop the daemon
|
||||
$this->on_stop();
|
||||
|
||||
//Delete pid file
|
||||
if (file_exists($this->pid_dir . '/' . $this->name . '.pid'))
|
||||
{
|
||||
unlink($this->pid_dir . '/' . $this->name . '.pid');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to implement the code that run infinetly (actually, it run one time but repeat the operation infinetly.
|
||||
*/
|
||||
abstract protected function run();
|
||||
|
||||
/**
|
||||
* Override to execute code before the ''run'' method on daemon start.
|
||||
*/
|
||||
abstract protected function on_start();
|
||||
|
||||
/**
|
||||
* Override to execute code after the ''run'' method on daemon shutdown.
|
||||
*/
|
||||
abstract protected function on_stop();
|
||||
|
||||
/**
|
||||
* Override to handle additional POSIX signals.
|
||||
*
|
||||
* @param int $signal : Signal sent by interrupt
|
||||
*/
|
||||
abstract protected function handle_other_signals(int $signal);
|
||||
|
||||
/**
|
||||
* Used to register POSIX signals.
|
||||
*/
|
||||
private function register_signals()
|
||||
{
|
||||
//Enable a tick at every 1 instruction, allowing us to run a function frequently, for exemple looking at signal status
|
||||
declare(ticks=1);
|
||||
|
||||
foreach ($this->signals as $signal)
|
||||
{
|
||||
//For each signal define the method handle_signal of the current class as the way to handle it
|
||||
@pcntl_signal($signal, [
|
||||
'self',
|
||||
'handle_signal',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,131 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace daemons;
|
||||
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Main daemon class.
|
||||
*/
|
||||
class Launcher extends AbstractDaemon
|
||||
{
|
||||
private $internal_phone;
|
||||
private $internal_scheduled;
|
||||
private $internal_received;
|
||||
private $bdd;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$name = 'RaspiSMS Daemon Launcher';
|
||||
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = true; //Launcher should be rattach to PID 1
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Main server should be uniq
|
||||
|
||||
//Construct the server and add SIGUSR1 and SIGUSR2
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
//Create the internal controllers
|
||||
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
$this->internal_phone = new \controllers\internals\Phone($this->bdd);
|
||||
|
||||
$this->start_sender_daemon();
|
||||
|
||||
$this->start_webhook_daemon();
|
||||
|
||||
$phones = $this->internal_phone->get_all();
|
||||
$this->start_phones_daemons($phones);
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to start sender daemon.
|
||||
*/
|
||||
public function start_sender_daemon()
|
||||
{
|
||||
$name = 'RaspiSMS Daemon Sender';
|
||||
$pid_file = PWD_PID . '/' . $name . '.pid';
|
||||
|
||||
if (file_exists($pid_file))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Create a new daemon for sender
|
||||
$pid = null;
|
||||
exec('php ' . PWD . '/console.php controllers/internals/Console.php sender > /dev/null 2>&1 &');
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to start webhook daemon.
|
||||
*/
|
||||
public function start_webhook_daemon()
|
||||
{
|
||||
$name = 'RaspiSMS Daemon Webhook';
|
||||
$pid_file = PWD_PID . '/' . $name . '.pid';
|
||||
|
||||
if (file_exists($pid_file))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Create a new daemon for webhook
|
||||
exec('php ' . PWD . '/console.php controllers/internals/Console.php webhook > /dev/null 2>&1 &');
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to start phones daemons.
|
||||
*
|
||||
* @param array $phones : Phones to start daemon for if the daemon is not already started
|
||||
*/
|
||||
public function start_phones_daemons(array $phones)
|
||||
{
|
||||
foreach ($phones as $phone)
|
||||
{
|
||||
$phone_name = 'RaspiSMS Daemon Phone ' . $phone['number'];
|
||||
$pid_file = PWD_PID . '/' . $phone_name . '.pid';
|
||||
|
||||
if (file_exists($pid_file))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//Create a new daemon for the phone
|
||||
exec('php ' . PWD . '/console.php controllers/internals/Console.php phone --id_phone=\'' . $phone['id'] . '\' > /dev/null 2>&1 &');
|
||||
}
|
||||
}
|
||||
|
||||
public function on_start()
|
||||
{
|
||||
$this->logger->info('Starting Launcher with pid ' . getmypid());
|
||||
}
|
||||
|
||||
public function on_stop()
|
||||
{
|
||||
$this->logger->info('Stopping Launcher with pid ' . getmypid());
|
||||
}
|
||||
|
||||
public function handle_other_signals($signal)
|
||||
{
|
||||
$this->logger->info('Signal not handled by ' . $this->name . ' Daemon : ' . $signal);
|
||||
}
|
||||
}
|
|
@ -1,287 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace daemons;
|
||||
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Phone daemon class.
|
||||
*/
|
||||
class Phone extends AbstractDaemon
|
||||
{
|
||||
private $msg_queue;
|
||||
private $msg_queue_id;
|
||||
private $webhook_queue;
|
||||
private $last_message_at;
|
||||
private $phone;
|
||||
private $adapter;
|
||||
private $bdd;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $phone : A phone table entry
|
||||
*/
|
||||
public function __construct(array $phone)
|
||||
{
|
||||
$this->phone = $phone;
|
||||
$this->msg_queue_id = (int) mb_substr($this->phone['number'], 1);
|
||||
|
||||
$name = 'RaspiSMS Daemon Phone ' . $this->phone['number'];
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = false; //Phone should be rattach to manager, so manager can stop him easily
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Each phone should be uniq
|
||||
|
||||
//Construct the daemon
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
//Stop after 5 minutes of inactivity to avoid useless daemon
|
||||
if ((microtime(true) - $this->last_message_at) > 5 * 60)
|
||||
{
|
||||
posix_kill(getmypid(), SIGTERM); //Send exit signal to the current process
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
|
||||
//Send smss in queue
|
||||
$this->send_smss();
|
||||
|
||||
//Read received smss
|
||||
$this->read_smss();
|
||||
|
||||
usleep(0.5 * 1000000);
|
||||
}
|
||||
|
||||
public function on_start()
|
||||
{
|
||||
//Set last message at to construct time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
$this->msg_queue = msg_get_queue($this->msg_queue_id);
|
||||
$this->webhook_queue = msg_get_queue(QUEUE_ID_WEBHOOK);
|
||||
|
||||
//Instanciate adapter
|
||||
$adapter_class = $this->phone['adapter'];
|
||||
$this->adapter = new $adapter_class($this->phone['number'], $this->phone['adapter_datas']);
|
||||
|
||||
$this->logger->info('Starting Phone daemon with pid ' . getmypid());
|
||||
}
|
||||
|
||||
public function on_stop()
|
||||
{
|
||||
//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 daemon with pid ' . getmypid());
|
||||
}
|
||||
|
||||
public function handle_other_signals($signal)
|
||||
{
|
||||
$this->logger->info('Signal not handled by ' . $this->name . ' Daemon : ' . $signal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send sms.
|
||||
*/
|
||||
private function send_smss()
|
||||
{
|
||||
$find_message = true;
|
||||
while ($find_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 && MSG_ENOMSG !== $error_code)
|
||||
{
|
||||
$this->logger->critical('Error reading MSG SEND Queue, error code : ' . $error_code);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$message)
|
||||
{
|
||||
$find_message = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$internal_sended = new \controllers\internals\Sended($this->bdd);
|
||||
|
||||
//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->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;
|
||||
}
|
||||
|
||||
//Run webhook
|
||||
$internal_setting = new \controllers\internals\Setting($this->bdd);
|
||||
$user_settings = $internal_setting->gets_for_user($this->phone['id_user']);
|
||||
$this->process_for_webhook($message, 'send_sms', $user_settings);
|
||||
|
||||
$this->logger->info('Successfully send message : ' . json_encode($message));
|
||||
|
||||
$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_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);
|
||||
$this->logger->info('after command');
|
||||
$sms['text'] = $command_result['text'];
|
||||
$is_command = $command_result['is_command'];
|
||||
|
||||
$this->process_for_webhook($sms, 'receive_sms', $user_settings);
|
||||
|
||||
$this->process_for_transfer($sms, $user_settings);
|
||||
|
||||
$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)
|
||||
{
|
||||
$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' => $sms['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;
|
||||
}
|
||||
|
||||
$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'],
|
||||
],
|
||||
];
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a sms to transfer it by mail.
|
||||
*
|
||||
* @param array $sms : The sms
|
||||
* @param array $user_settings : Use settings
|
||||
*/
|
||||
private function process_for_transfer(array $sms, array $user_settings)
|
||||
{
|
||||
if (!$user_settings['transfer'])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$internal_user = new \controllers\internals\User($this->bdd);
|
||||
$user = $internal_user->get($this->phone['id_user']);
|
||||
|
||||
if (!$user)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->logger->info('Transfer sms to ' . $user['email'] . ' : ' . json_encode($sms));
|
||||
|
||||
\controllers\internals\Tool::send_email($user['email'], EMAIL_TRANSFER_SMS, ['sms' => $sms]);
|
||||
}
|
||||
}
|
|
@ -1,109 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace daemons;
|
||||
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Main daemon class.
|
||||
*/
|
||||
class Sender extends AbstractDaemon
|
||||
{
|
||||
private $internal_phone;
|
||||
private $internal_scheduled;
|
||||
private $internal_received;
|
||||
private $bdd;
|
||||
private $queues = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$name = 'RaspiSMS Daemon Sender';
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = false; //Webhook should be rattach to manager, so manager can stop him easily
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Webhook should be uniq
|
||||
|
||||
//Construct the daemon
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
//Create the internal controllers
|
||||
$this->internal_scheduled = new \controllers\internals\Scheduled($this->bdd);
|
||||
|
||||
//Get smss and transmit order to send to appropriate phone daemon
|
||||
$smss = $this->internal_scheduled->get_smss_to_send();
|
||||
$this->transmit_smss($smss); //Add new queues to array of queues
|
||||
|
||||
usleep(0.5 * 1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get messages to send and transfer theme to phones daemons.
|
||||
*
|
||||
* @param array $smss : Smss to send
|
||||
*/
|
||||
public function transmit_smss(array $smss): void
|
||||
{
|
||||
foreach ($smss as $sms)
|
||||
{
|
||||
//If queue not already exists
|
||||
$queue_id = (int) mb_substr($sms['origin'], 1);
|
||||
if (!msg_queue_exists($queue_id) || !isset($queues[$queue_id]))
|
||||
{
|
||||
$this->queues[$queue_id] = msg_get_queue($queue_id);
|
||||
}
|
||||
|
||||
$msg = [
|
||||
'id_user' => $sms['id_user'],
|
||||
'id_scheduled' => $sms['id_scheduled'],
|
||||
'text' => $sms['text'],
|
||||
'origin' => $sms['origin'],
|
||||
'destination' => $sms['destination'],
|
||||
'flash' => $sms['flash'],
|
||||
];
|
||||
|
||||
msg_send($this->queues[$queue_id], QUEUE_TYPE_SEND_MSG, $msg);
|
||||
|
||||
$this->internal_scheduled->delete($sms['id_scheduled']);
|
||||
}
|
||||
}
|
||||
|
||||
public function on_start()
|
||||
{
|
||||
$this->logger->info('Starting Sender with pid ' . getmypid());
|
||||
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
}
|
||||
|
||||
public function on_stop()
|
||||
{
|
||||
$this->logger->info('Stopping Sender with pid ' . getmypid());
|
||||
|
||||
//Delete queues on daemon close
|
||||
foreach ($this->queues as $queue_id => $queue)
|
||||
{
|
||||
$this->logger->info('Closing queue : ' . $queue_id);
|
||||
msg_remove_queue($queue);
|
||||
}
|
||||
}
|
||||
|
||||
public function handle_other_signals($signal)
|
||||
{
|
||||
$this->logger->info('Signal not handled by ' . $this->name . ' Daemon : ' . $signal);
|
||||
}
|
||||
}
|
|
@ -1,111 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace daemons;
|
||||
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Phone daemon class.
|
||||
*/
|
||||
class Webhook extends AbstractDaemon
|
||||
{
|
||||
private $webhook_queue;
|
||||
private $last_message_at;
|
||||
private $phone;
|
||||
private $adapter;
|
||||
private $bdd;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $phone : A phone table entry
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$name = 'RaspiSMS Daemon Webhook';
|
||||
$logger = new Logger($name);
|
||||
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
||||
$pid_dir = PWD_PID;
|
||||
$no_parent = false; //Sended should be rattach to manager, so manager can stop him easily
|
||||
$additional_signals = [];
|
||||
$uniq = true; //Sender should be uniq
|
||||
|
||||
//Construct the daemon
|
||||
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
||||
|
||||
parent::start();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$find_message = true;
|
||||
while ($find_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)
|
||||
{
|
||||
$find_message = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->logger->info('Trigger webhook : ' . json_encode($message));
|
||||
|
||||
//Do the webhook http query
|
||||
$curl = curl_init();
|
||||
curl_setopt($curl, CURLOPT_URL, $message['url']);
|
||||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($curl, CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $message['datas']);
|
||||
curl_exec($curl);
|
||||
curl_close($curl);
|
||||
}
|
||||
|
||||
usleep(0.5 * 1000000);
|
||||
}
|
||||
|
||||
public function on_start()
|
||||
{
|
||||
//Set last message at to construct time
|
||||
$this->last_message_at = microtime(true);
|
||||
|
||||
$this->webhook_queue = msg_get_queue(QUEUE_ID_WEBHOOK);
|
||||
|
||||
$this->logger->info('Starting Webhook daemon with pid ' . getmypid());
|
||||
}
|
||||
|
||||
public function on_stop()
|
||||
{
|
||||
//Delete queue on daemon close
|
||||
$this->logger->info('Closing queue : ' . QUEUE_ID_WEBHOOK);
|
||||
msg_remove_queue($this->webhook_queue);
|
||||
|
||||
$this->logger->info('Stopping Webhook daemon with pid ' . getmypid());
|
||||
}
|
||||
|
||||
public function handle_other_signals($signal)
|
||||
{
|
||||
$this->logger->info('Signal not handled by ' . $this->name . ' Daemon : ' . $signal);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue