Browse Source

add Temperature/Hygrometry API

develop
Simon Vieille 2 months ago
parent
commit
38b433a63c
Signed by: deblan <simon@deblan.fr> GPG Key ID: 03383D15A1D31745
12 changed files with 400 additions and 2 deletions
  1. +3
    -0
      .gitignore
  2. +1
    -0
      composer.json
  3. +42
    -0
      src/Controller/HygrometryApiController.php
  4. +2
    -2
      src/Controller/MonitoringApiController.php
  5. +42
    -0
      src/Controller/TemperatureApiController.php
  6. +57
    -0
      src/Entity/Hygrometry.php
  7. +57
    -0
      src/Entity/Temperature.php
  8. +37
    -0
      src/Migrations/Version20200316130907.php
  9. +50
    -0
      src/Repository/HygrometryRepository.php
  10. +50
    -0
      src/Repository/TemperatureRepository.php
  11. +47
    -0
      src/Validator/ApiValidator.php
  12. +12
    -0
      symfony.lock

+ 3
- 0
.gitignore View File

@@ -19,5 +19,8 @@
/.web-server-pid
###< symfony/web-server-bundle ###

/Makefile
/public/.htaccess
/.mage.yml
/node_modules
/public/motion/snapshots/*

+ 1
- 0
composer.json View File

@@ -6,6 +6,7 @@
"ext-ctype": "*",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.1",
"symfony/apache-pack": "^1.0",
"symfony/asset": "4.4.*",
"symfony/console": "4.4.*",
"symfony/dotenv": "4.4.*",


+ 42
- 0
src/Controller/HygrometryApiController.php View File

@@ -0,0 +1,42 @@
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Validator\ApiValidator;
use App\Entity\Hygrometry;

class HygrometryApiController extends AbstractController
{
/**
* @Route("/api/hygrometry/create", name="api_hygrometry_create", methods={"POST"})
*/
public function create(Request $request, ApiValidator $validator): Response
{
if ('application/json' === $request->getContentType()) {
return $this->json([], 400);
}

$content = $request->getContent();

if (!$validator->isValidContent($content)) {
return $this->json([], 400);
}

$data = json_decode($content, true);

$entity = new Hygrometry();
$entity
->setDate(new \DateTime($data['date']))
->setValue((float) $data['value']);

$manager = $this->getDoctrine()->getManager();
$manager->persist($entity);
$manager->flush();

return $this->json([], 201);
}
}

+ 2
- 2
src/Controller/MonitoringApiController.php View File

@@ -10,7 +10,7 @@ use Symfony\Component\HttpFoundation\Request;
class MonitoringApiController extends AbstractController
{
/**
* @Route("/api/monitoring/temperature", name="api_monitoring_temperature")
* @Route("/api/monitoring/temperature", name="api_monitoring_temperature", methods={"GET"})
*/
public function temperature(Request $request)
{
@@ -52,7 +52,7 @@ class MonitoringApiController extends AbstractController
}

/**
* @Route("/api/monitoring/hygrometry", name="api_monitoring_hygrometry")
* @Route("/api/monitoring/hygrometry", name="api_monitoring_hygrometry", methods={"GET"})
*/
public function hygrometry(Request $request)
{


+ 42
- 0
src/Controller/TemperatureApiController.php View File

@@ -0,0 +1,42 @@
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Validator\ApiValidator;
use App\Entity\Temperature;

class TemperatureApiController extends AbstractController
{
/**
* @Route("/api/temperature/create", name="api_temperature_create", methods={"POST"})
*/
public function create(Request $request, ApiValidator $validator): Response
{
if ('application/json' === $request->getContentType()) {
return $this->json([], 400);
}

$content = $request->getContent();

if (!$validator->isValidContent($content)) {
return $this->json([], 400);
}

$data = json_decode($content, true);

$entity = new Temperature();
$entity
->setDate(new \DateTime($data['date']))
->setValue((float) $data['value']);

$manager = $this->getDoctrine()->getManager();
$manager->persist($entity);
$manager->flush();

return $this->json([], 201);
}
}

+ 57
- 0
src/Entity/Hygrometry.php View File

@@ -0,0 +1,57 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\HygrometryRepository")
*/
class Hygrometry
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="datetime")
*/
private $date;

/**
* @ORM\Column(type="float")
*/
private $value;

public function getId(): ?int
{
return $this->id;
}

public function getDate(): ?\DateTimeInterface
{
return $this->date;
}

public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;

return $this;
}

public function getValue(): ?float
{
return $this->value;
}

public function setValue(float $value): self
{
$this->value = $value;

return $this;
}
}

+ 57
- 0
src/Entity/Temperature.php View File

@@ -0,0 +1,57 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\TemperatureRepository")
*/
class Temperature
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="datetime")
*/
private $date;

/**
* @ORM\Column(type="float")
*/
private $value;

public function getId(): ?int
{
return $this->id;
}

public function getDate(): ?\DateTimeInterface
{
return $this->date;
}

public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;

return $this;
}

public function getValue(): ?float
{
return $this->value;
}

public function setValue(float $value): self
{
$this->value = $value;

return $this;
}
}

+ 37
- 0
src/Migrations/Version20200316130907.php View File

@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20200316130907 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}

public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('CREATE TABLE hygrometry (id INT AUTO_INCREMENT NOT NULL, date DATETIME NOT NULL, value DOUBLE PRECISION NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE temperature (id INT AUTO_INCREMENT NOT NULL, date DATETIME NOT NULL, value DOUBLE PRECISION NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('DROP TABLE hygrometry');
$this->addSql('DROP TABLE temperature');
}
}

+ 50
- 0
src/Repository/HygrometryRepository.php View File

@@ -0,0 +1,50 @@
<?php

namespace App\Repository;

use App\Entity\Hygrometry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
* @method Hygrometry|null find($id, $lockMode = null, $lockVersion = null)
* @method Hygrometry|null findOneBy(array $criteria, array $orderBy = null)
* @method Hygrometry[] findAll()
* @method Hygrometry[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class HygrometryRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Hygrometry::class);
}

// /**
// * @return Hygrometry[] Returns an array of Hygrometry objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('h')
->andWhere('h.exampleField = :val')
->setParameter('val', $value)
->orderBy('h.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/

/*
public function findOneBySomeField($value): ?Hygrometry
{
return $this->createQueryBuilder('h')
->andWhere('h.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

+ 50
- 0
src/Repository/TemperatureRepository.php View File

@@ -0,0 +1,50 @@
<?php

namespace App\Repository;

use App\Entity\Temperature;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
* @method Temperature|null find($id, $lockMode = null, $lockVersion = null)
* @method Temperature|null findOneBy(array $criteria, array $orderBy = null)
* @method Temperature[] findAll()
* @method Temperature[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TemperatureRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Temperature::class);
}

// /**
// * @return Temperature[] Returns an array of Temperature objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->orderBy('t.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/

/*
public function findOneBySomeField($value): ?Temperature
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

+ 47
- 0
src/Validator/ApiValidator.php View File

@@ -0,0 +1,47 @@
<?php

namespace App\Validator;

/**
* class ApiValidator.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ApiValidator
{
/**
* isValidContent
*
* @param string|null $content
*
* @return bool
*/
public function isValidContent(? string $content): bool
{
$json = json_decode($content, true);

if (!is_array($json)) {
return false;
}

if (count($json) !== 2) {
return false;
}

if (!isset($json['date'], $json['value'])) {
return false;
}

try {
new \DateTime($json['date']);
} catch (\Exception $e) {
return false;
}

if (filter_var($json['value'], FILTER_VALIDATE_FLOAT) === null) {
return false;
}

return true;
}
}

+ 12
- 0
symfony.lock View File

@@ -141,6 +141,18 @@
"config/packages/sensio_framework_extra.yaml"
]
},
"symfony/apache-pack": {
"version": "1.0",
"recipe": {
"repo": "github.com/symfony/recipes-contrib",
"branch": "master",
"version": "1.0",
"ref": "410b9325a37ef86f1e47262c61738f6202202bca"
},
"files": [
"public/.htaccess"
]
},
"symfony/asset": {
"version": "v4.4.5"
},


Loading…
Cancel
Save