2019-12-08 02:33:53 +01:00
|
|
|
<?php
|
|
|
|
namespace daemons;
|
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
use \Monolog\Logger;
|
|
|
|
use \Monolog\Handler\StreamHandler;
|
|
|
|
|
2019-12-08 02:33:53 +01:00
|
|
|
/**
|
|
|
|
* Main daemon class
|
|
|
|
*/
|
|
|
|
class Server extends AbstractDaemon
|
|
|
|
{
|
2019-12-12 00:56:30 +01:00
|
|
|
private $internal_user;
|
|
|
|
private $internal_phone;
|
|
|
|
private $internal_scheduled;
|
|
|
|
private $bdd;
|
|
|
|
|
2019-12-08 02:33:53 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2019-12-12 00:56:30 +01:00
|
|
|
$logger = new Logger('server');
|
|
|
|
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
|
|
|
|
|
|
|
$name = "RaspiSMS Server";
|
|
|
|
$pid_dir = PWD_PID;
|
2019-12-17 07:47:11 +01:00
|
|
|
$additional_signals = [];
|
2019-12-12 00:56:30 +01:00
|
|
|
$uniq = true; //Main server should be uniq
|
|
|
|
|
2019-12-08 02:33:53 +01:00
|
|
|
//Construct the server and add SIGUSR1 and SIGUSR2
|
2019-12-12 00:56:30 +01:00
|
|
|
parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq);
|
|
|
|
|
|
|
|
parent::start();
|
2019-12-08 02:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2019-12-12 00:56:30 +01:00
|
|
|
//Create the internal controllers
|
|
|
|
$this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
|
|
|
$this->internal_user = new \controllers\internals\User($this->bdd);
|
|
|
|
$this->internal_phone = new \controllers\internals\Phone($this->bdd);
|
|
|
|
$this->internal_scheduled = new \controllers\internals\Scheduled($this->bdd);
|
|
|
|
|
|
|
|
|
|
|
|
//Start all phones daemons
|
|
|
|
$phones = $this->internal_phone->get_all();
|
|
|
|
foreach ($phones as $phone)
|
|
|
|
{
|
2019-12-17 14:38:16 +01:00
|
|
|
$phone_name = 'RaspiSMS Phone ' . $phone['number'];
|
2019-12-12 00:56:30 +01:00
|
|
|
$pid_file = PWD_PID . '/' . $phone_name . '.pid';
|
|
|
|
|
|
|
|
if (file_exists($pid_file))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:38:16 +01:00
|
|
|
exec('php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &');
|
|
|
|
$this->logger->info('Command : ' . 'php ' . PWD . '/console.php controllers/internals/Console.php phone number=\'' . $phone['number'] . '\' > /dev/null &');
|
2019-12-17 07:47:11 +01:00
|
|
|
//Create a new daemon for the phone
|
2019-12-17 14:38:16 +01:00
|
|
|
//$phone = new \daemons\Phone($phone);
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$queues = [];
|
|
|
|
|
|
|
|
//Get all sms to send
|
|
|
|
$smss = $this->internal_scheduled->get_smss_to_send();
|
|
|
|
foreach ($smss as $sms)
|
|
|
|
{
|
2019-12-17 07:47:11 +01:00
|
|
|
//If queue has been deleted or does not exist, create a new one
|
|
|
|
$queue_id = (int) mb_substr($sms['origin'], 1);
|
|
|
|
if (!msg_queue_exists($queue_id))
|
2019-12-12 00:56:30 +01:00
|
|
|
{
|
2019-12-17 07:47:11 +01:00
|
|
|
$queues[$queue_id] = msg_get_queue($queue_id);
|
|
|
|
}
|
|
|
|
elseif (!isset($queues[$queue_id]))
|
|
|
|
{
|
|
|
|
$queues[$queue_id] = msg_get_queue($queue_id);
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:47:11 +01:00
|
|
|
$queue = $queues[$queue_id];
|
2019-12-12 00:56:30 +01:00
|
|
|
|
|
|
|
$msg = [
|
|
|
|
'text' => (string) $sms['text'],
|
|
|
|
'origin' => (string) $sms['origin'],
|
|
|
|
'destination' => (string) $sms['destination'],
|
|
|
|
'flash' => (bool) $sms['flash'],
|
|
|
|
];
|
|
|
|
|
|
|
|
msg_send($queue, SEND_MSG, $msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sleep(0.5);
|
2019-12-08 02:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function on_start()
|
|
|
|
{
|
2019-12-17 14:38:16 +01:00
|
|
|
$this->logger->info("Starting Server with pid " . getmypid());
|
2019-12-08 02:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function on_stop()
|
|
|
|
{
|
2019-12-17 14:38:16 +01:00
|
|
|
$this->logger->info("Stopping Server with pid " . getmypid ());
|
2019-12-08 02:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function handle_other_signals($signal)
|
|
|
|
{
|
2019-12-12 00:56:30 +01:00
|
|
|
$this->logger->info("Signal not handled by " . $this->name . " Daemon : " . $signal);
|
2019-12-08 02:33:53 +01:00
|
|
|
}
|
|
|
|
}
|