terrarium-web/src/Controller/MonitoringApiController.php

187 lines
5.4 KiB
PHP

<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\TemperatureRepository;
use App\Repository\HygrometryRepository;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MonitoringApiController extends AbstractController
{
/**
* @Route("/api/monitoring/temperature", name="api_monitoring_temperature", methods={"GET"})
*/
public function temperature(
Request $request,
TemperatureRepository $repository,
ParameterBagInterface $params
): JsonResponse
{
$labels = [];
$datasets = [];
$entities = $repository->findByDateRange(new \DateTime('now - 1 day'), new \DateTime('now'));
$line = [
'label' => 'Relevés de la température',
'data' => [],
'borderColor' => 'rgb(255, 205, 86)',
'fill' => false,
];
$line2 = [
'label' => 'Idéal le jour',
'data' => [],
'borderColor' => 'rgb(255, 200, 200)',
'fill' => false,
];
$line3 = [
'label' => 'Minimum la nuit',
'data' => [],
'borderColor' => 'rgb(216, 234, 255)',
'fill' => false,
];
$lightOn = [
'label' => 'Lumière allumée',
'data' => [],
'borderColor' => 'rgb(230, 217, 149)',
'fill' => false,
];
$lightOff = [
'label' => 'Lumière éteinte',
'data' => [],
'borderColor' => 'rgb(214, 214, 214)',
'fill' => false,
];
$lightOnAt = (int) $params->get('light_on_at');
$lightOffAt = (int) $params->get('light_off_at');
foreach ($entities as $entity) {
$hour = (int) $entity->getDate()->format('H');
$labels[] = $entity->getDate()->format('H:i');
$line['data'][] = $entity->getValue();
$line2['data'][] = 27;
$line3['data'][] = 23;
$lightTime = (int) $entity->getDate()->format('gi');
if ($lightTime >= $lightOn && $lightTime <= $lightOff) {
$lightOn['data'][] = 5;
$lightOff['data'][] = 0;
} else {
$lightOn['data'][] = 0;
$lightOff['data'][] = 5;
}
}
$datasets[] = $line;
$datasets[] = $line2;
$datasets[] = $line3;
$datasets[] = $lightOn;
$datasets[] = $lightOff;
return $this->createGraphResponse($labels, $datasets);
}
/**
* @Route("/api/monitoring/hygrometry", name="api_monitoring_hygrometry", methods={"GET"})
*/
public function hygrometry(
Request $request,
HygrometryRepository $repository,
ParameterBagInterface $params
): JsonResponse
{
$labels = [];
$datasets = [];
$entities = $repository->findByDateRange(new \DateTime('now - 1 day'), new \DateTime('now'));
$line = [
'label' => 'Niveau d\'humidité de l\'atmosphère en %',
'data' => [],
'borderColor' => 'rgb(54, 162, 235)',
'backgroundColor' => 'rgb(216, 234, 255)',
'fill' => true,
];
$line2 = [
'label' => 'Valeur maximale',
'data' => [],
'borderColor' => 'rgb(200, 200, 200)',
'fill' => false,
];
$lightOn = [
'label' => 'Lumière allumée',
'data' => [],
'borderColor' => 'rgb(230, 217, 149)',
'fill' => false,
];
$lightOff = [
'label' => 'Lumière éteinte',
'data' => [],
'borderColor' => 'rgb(214, 214, 214)',
'fill' => false,
];
$lightOnAt = (int) $params->get('light_on_at');
$lightOffAt = (int) $params->get('light_off_at');
foreach ($entities as $entity) {
$labels[] = $entity->getDate()->format('H:i');
$line['data'][] = $entity->getValue();
$line2['data'][] = 50;
if ($lightTime >= $lightOn && $lightTime <= $lightOff) {
$lightOn['data'][] = 5;
$lightOff['data'][] = 0;
} else {
$lightOn['data'][] = 0;
$lightOff['data'][] = 5;
}
}
$datasets[] = $line;
$datasets[] = $line2;
$datasets[] = $lightOn;
$datasets[] = $lightOff;
return $this->createGraphResponse($labels, $datasets);
}
protected function createGraphResponse(array $labels, array $datasets): JsonResponse
{
$config = [
'data' => [
'labels' => $labels,
'datasets' => $datasets,
],
'options' => [
'responsive' => false,
'ratio' => 10,
'maintainAspectRatio' => false,
'legend' => [
'align' => 'end',
],
'scales' => [
'yAxes' => [[
'ticks' => [
'beginAtZero' => true,
],
]],
],
],
];
return $this->json($config);
}
}