pfatt/src/Commands/Monitor.php
2022-03-04 20:01:41 -06:00

61 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Pfatt\Commands;
use Pfatt\Config;
use Pfatt\Logger;
use Pfatt\NgController;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
final class Monitor extends Command
{
protected static $defaultName = 'monitor';
protected static $defaultDescription = 'Monitor connection to trigger 5268AC updates';
protected Config $config;
protected Logger $logger;
protected NgController $ngControl;
protected function configure(): void
{
// @todo Inject these. Maybe with a factory or container.
$this->config = new Config('', '', '');
$this->logger = new Logger('pfatt-5268AC');
$this->ngControl = new NgController($this->logger);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->logger->setOutput($output);
$this->logger->info('Starting 5268AC ping monitor ...');
register_shutdown_function(function () { $this->logger->info('Stopping 5268AC ping monitor ...'); });
while ($input->isInteractive()) {
if ($this->ping()) {
$this->ngControl->ngRmHook();
}
elseif (!$this->ngControl->ngIsConnected()) {
$this->ngControl->ngConnect();
}
sleep(5);
}
return Command::SUCCESS;
}
private function ping(): int
{
$process = proc_open('/sbin/ping -t2 -q -c1 ' . $this->config->getPingHost(), [], $pipes);
if ($process) {
return proc_close($process);
}
return 1;
}
}