88 lines
3.1 KiB
PHP
88 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgAgent;
|
|
|
|
use App\Entity\Client;
|
|
use App\Entity\Partition;
|
|
use App\Model\OgLiveStatus;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpClient\HttpClient;
|
|
use Symfony\Component\HttpClient\Internal\ClientState;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Attribute\AsController;
|
|
use Symfony\Component\Validator\Exception\ValidatorException;
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
#[AsController]
|
|
class StatusAction extends AbstractController
|
|
{
|
|
public function __construct(
|
|
protected readonly EntityManagerInterface $entityManager,
|
|
protected readonly HttpClientInterface $httpClient
|
|
)
|
|
{
|
|
$httpClient = HttpClient::create([
|
|
'verify_peer' => false,
|
|
'verify_host' => false,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @throws TransportExceptionInterface
|
|
* @throws ServerExceptionInterface
|
|
* @throws RedirectionExceptionInterface
|
|
* @throws ClientExceptionInterface
|
|
*/
|
|
public function __invoke(Client $client): JsonResponse
|
|
{
|
|
if (!$client->getIp()) {
|
|
throw new ValidatorException('IP is required');
|
|
}
|
|
|
|
try {
|
|
$response = $this->httpClient->request('POST', 'https://' . $client->getIp() . ':8000/ogAdmClient/status');
|
|
|
|
$statusCode = $response->getStatusCode();
|
|
$client->setStatus($statusCode === Response::HTTP_OK ? 'active' : 'off');
|
|
|
|
} catch (TransportExceptionInterface $e) {
|
|
$client->setStatus('off');
|
|
|
|
return new JsonResponse(
|
|
data: ['error' => $e->getMessage()],
|
|
status: Response::HTTP_INTERNAL_SERVER_ERROR
|
|
);
|
|
}
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
if (isset($data['cfg'])) {
|
|
foreach ($data['cfg'] as $cfg) {
|
|
$partitionEntity = $this->entityManager->getRepository(Partition::class)
|
|
->findOneBy(['client' => $client, 'diskNumber' => $cfg['disk'], 'partitionNumber' => $cfg['par']]);
|
|
|
|
if (!$partitionEntity) {
|
|
$partitionEntity = new Partition();
|
|
}
|
|
|
|
$partitionEntity->setClient($client);
|
|
$partitionEntity->setDiskNumber($cfg['disk']);
|
|
$partitionEntity->setPartitionNumber($cfg['par']);
|
|
$partitionEntity->setSize($cfg['tam']);
|
|
$partitionEntity->setMemoryUsage($cfg['uso']);
|
|
$this->entityManager->persist($partitionEntity);
|
|
}
|
|
}
|
|
|
|
$this->entityManager->persist($client);
|
|
$this->entityManager->flush();
|
|
|
|
return new JsonResponse(status: Response::HTTP_OK);
|
|
}
|
|
} |