2019-12-12 00:56:30 +01:00
|
|
|
<?php
|
|
|
|
namespace daemons;
|
|
|
|
|
|
|
|
use \Monolog\Logger;
|
|
|
|
use \Monolog\Handler\StreamHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phone daemon class
|
|
|
|
*/
|
|
|
|
class Phone extends AbstractDaemon
|
|
|
|
{
|
|
|
|
private $msg_queue;
|
2019-12-17 07:47:11 +01:00
|
|
|
private $queue_id;
|
|
|
|
private $last_message_at;
|
2019-12-12 00:56:30 +01:00
|
|
|
|
2019-12-17 14:38:16 +01:00
|
|
|
public function __construct($phone_number)
|
2019-12-12 00:56:30 +01:00
|
|
|
{
|
2019-12-17 14:38:16 +01:00
|
|
|
$this->queue_id = (int) mb_substr($phone_number, 1);
|
2019-12-12 00:56:30 +01:00
|
|
|
|
2019-12-17 14:38:16 +01:00
|
|
|
$name = 'RaspiSMS Phone ' . $phone_number;
|
2019-12-12 00:56:30 +01:00
|
|
|
|
|
|
|
$logger = new Logger($name);
|
|
|
|
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/raspisms.log', Logger::DEBUG));
|
2019-12-17 07:47:11 +01:00
|
|
|
|
|
|
|
$pid_dir = PWD_PID;
|
|
|
|
$additional_signals = [];
|
|
|
|
$uniq = true; //Main server should be uniq
|
2019-12-12 00:56:30 +01:00
|
|
|
|
|
|
|
//Construct the server and add SIGUSR1 and SIGUSR2
|
|
|
|
parent::__construct($name, $logger, $pid_dir, $additional_signals, $uniq);
|
2019-12-17 14:38:16 +01:00
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
parent::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2019-12-17 07:47:11 +01:00
|
|
|
if ( (microtime(true) - $this->last_message_at) > 5 * 60 )
|
|
|
|
{
|
|
|
|
$this->is_running = false;
|
|
|
|
$this->logger->info("End running");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-04 19:30:06 +01:00
|
|
|
//Send a sms
|
|
|
|
$this->send_sms();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send sms
|
|
|
|
*/
|
|
|
|
public function send_sms () : bool
|
|
|
|
{
|
|
|
|
//Call message
|
2019-12-12 00:56:30 +01:00
|
|
|
$msgtype = null;
|
|
|
|
$maxsize = 409600;
|
|
|
|
$message = null;
|
|
|
|
|
|
|
|
msg_receive($this->msg_queue, SEND_MSG, $msgtype, $maxsize, $message);
|
|
|
|
|
|
|
|
if (!$message)
|
|
|
|
{
|
2020-01-04 19:30:06 +01:00
|
|
|
return false;
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
|
2019-12-17 07:47:11 +01:00
|
|
|
//If message received, update last message time
|
|
|
|
$this->last_message_at = microtime(true);
|
2020-01-04 19:30:06 +01:00
|
|
|
|
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
|
|
|
$internal_scheduled = new \controllers\internals\Scheduled($bdd);
|
|
|
|
$internal_scheduled->send($message['id_scheduled'], $message['text'], $message['origin'], $message['destination'], $message['flash']);
|
2019-12-20 18:31:19 +01:00
|
|
|
|
|
|
|
//Close bdd
|
|
|
|
$bdd = null;
|
2020-01-04 19:30:06 +01:00
|
|
|
$internal_scheduled = null;
|
2019-12-20 18:31:19 +01:00
|
|
|
|
|
|
|
$this->logger->info('Send message : ' . json_encode($message));
|
2020-01-04 19:30:06 +01:00
|
|
|
return true;
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function on_start()
|
|
|
|
{
|
2019-12-17 07:47:11 +01:00
|
|
|
//Set last message at to construct time
|
|
|
|
$this->last_message_at = microtime(true);
|
|
|
|
|
|
|
|
$this->msg_queue = msg_get_queue($this->queue_id);
|
|
|
|
|
2019-12-17 14:38:16 +01:00
|
|
|
$this->logger->info("Starting Phone with pid " . getmypid());
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function on_stop()
|
|
|
|
{
|
2019-12-17 07:47:11 +01:00
|
|
|
$this->logger->info("Closing queue : " . $this->queue_id);
|
|
|
|
msg_remove_queue($this->msg_queue); //Delete queue on daemon close
|
|
|
|
|
2019-12-17 14:38:16 +01:00
|
|
|
$this->logger->info("Stopping Phone with pid " . getmypid ());
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function handle_other_signals($signal)
|
|
|
|
{
|
|
|
|
$this->logger->info("Signal not handled by " . $this->name . " Daemon : " . $signal);
|
|
|
|
}
|
|
|
|
}
|