feat : Ajout de pinia, création de la table weight et reception mise en place du système de step pour les receptions (WIP)
This commit is contained in:
31
src/Dto/PontBasculeReading.php
Normal file
31
src/Dto/PontBasculeReading.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Dto;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
final readonly class PontBasculeReading
|
||||
{
|
||||
public function __construct(
|
||||
private ?int $dsd,
|
||||
private ?float $weight,
|
||||
private ?DateTimeImmutable $datetime = null,
|
||||
) {}
|
||||
|
||||
public function getDsd(): ?int
|
||||
{
|
||||
return $this->dsd;
|
||||
}
|
||||
|
||||
public function getWeight(): ?float
|
||||
{
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
public function getDatetime(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->datetime;
|
||||
}
|
||||
}
|
||||
199
src/Entity/Reception.php
Normal file
199
src/Entity/Reception.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
|
||||
use App\State\ReceptionWeighingProvider;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
#[ORM\Table(name: 'reception')]
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
),
|
||||
new GetCollection(
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
),
|
||||
new Post(
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
denormalizationContext: ['groups' => ['reception:write']],
|
||||
),
|
||||
new Patch(
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
denormalizationContext: ['groups' => ['reception:write']],
|
||||
),
|
||||
new Get(
|
||||
uriTemplate: '/receptions/weigh',
|
||||
openapi: new OpenApiOperation(
|
||||
summary: 'Fetch the current weight reading',
|
||||
description: 'Queries the pont-bascule and returns the weight data.',
|
||||
),
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
provider: ReceptionWeighingProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
class Reception
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
#[Groups(['reception:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
#[Groups(['reception:read', 'reception:write'])]
|
||||
private ?int $dsd = null;
|
||||
|
||||
#[ORM\Column(type: 'float', nullable: true)]
|
||||
#[Groups(['reception:read', 'reception:write'])]
|
||||
private ?float $weight = null;
|
||||
|
||||
#[ORM\Column(length: 20, nullable: true)]
|
||||
#[Groups(['reception:read', 'reception:write'])]
|
||||
private ?string $licensePlate = null;
|
||||
|
||||
#[ORM\Column(options: ['default' => 0])]
|
||||
#[Groups(['reception:read', 'reception:write'])]
|
||||
private int $currentStep = 0;
|
||||
|
||||
#[ORM\Column(options: ['default' => false])]
|
||||
#[Groups(['reception:read', 'reception:write'])]
|
||||
private bool $isValid = false;
|
||||
|
||||
#[ORM\Column(name: 'date_reception', type: 'datetime_immutable')]
|
||||
#[Groups(['reception:read'])]
|
||||
private ?DateTimeImmutable $receptionDate = null;
|
||||
|
||||
#[ORM\OneToOne(targetEntity: Weight::class, mappedBy: 'reception', cascade: ['persist', 'remove'])]
|
||||
private ?Weight $weightEntry = null;
|
||||
|
||||
public function __construct(
|
||||
?int $dsd = null,
|
||||
?float $weight = null,
|
||||
?DateTimeImmutable $receptionDate = null,
|
||||
) {
|
||||
$this->dsd = $dsd;
|
||||
$this->weight = $weight;
|
||||
$this->receptionDate = $receptionDate;
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
#[Groups(['reception:read'])]
|
||||
public function getDsd(): ?int
|
||||
{
|
||||
return $this->dsd;
|
||||
}
|
||||
|
||||
public function setDsd(?int $dsd): self
|
||||
{
|
||||
$this->dsd = $dsd;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[Groups(['reception:read'])]
|
||||
public function getWeight(): ?float
|
||||
{
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
public function setWeight(?float $weight): self
|
||||
{
|
||||
$this->weight = $weight;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[Groups(['reception:read'])]
|
||||
public function getLicensePlate(): ?string
|
||||
{
|
||||
return $this->licensePlate;
|
||||
}
|
||||
|
||||
public function setLicensePlate(?string $licensePlate): self
|
||||
{
|
||||
$this->licensePlate = $licensePlate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[Groups(['reception:read'])]
|
||||
public function getCurrentStep(): int
|
||||
{
|
||||
return $this->currentStep;
|
||||
}
|
||||
|
||||
public function setCurrentStep(int $currentStep): self
|
||||
{
|
||||
$this->currentStep = $currentStep;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[Groups(['reception:read'])]
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->isValid;
|
||||
}
|
||||
|
||||
public function setIsValid(bool $isValid): self
|
||||
{
|
||||
$this->isValid = $isValid;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[Groups(['reception:read'])]
|
||||
public function getReceptionDate(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->receptionDate;
|
||||
}
|
||||
|
||||
public function setReceptionDate(?DateTimeImmutable $receptionDate): self
|
||||
{
|
||||
$this->receptionDate = $receptionDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWeightEntry(): ?Weight
|
||||
{
|
||||
return $this->weightEntry;
|
||||
}
|
||||
|
||||
public function setWeightEntry(?Weight $weightEntry): self
|
||||
{
|
||||
$this->weightEntry = $weightEntry;
|
||||
|
||||
if (null !== $weightEntry && $weightEntry->getReception() !== $this) {
|
||||
$weightEntry->setReception($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[ORM\PrePersist]
|
||||
public function initializeReceptionDate(): void
|
||||
{
|
||||
if (null === $this->receptionDate) {
|
||||
$this->receptionDate = new DateTimeImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
103
src/Entity/Weight.php
Normal file
103
src/Entity/Weight.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'weight')]
|
||||
class Weight
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\OneToOne(inversedBy: 'weightEntry')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private ?Reception $reception = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $grossWeight = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $tareWeight = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
private ?DateTimeImmutable $grossWeighedAt = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
private ?DateTimeImmutable $tareWeighedAt = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getReception(): ?Reception
|
||||
{
|
||||
return $this->reception;
|
||||
}
|
||||
|
||||
public function setReception(?Reception $reception): self
|
||||
{
|
||||
$this->reception = $reception;
|
||||
|
||||
if (null !== $reception && $reception->getWeightEntry() !== $this) {
|
||||
$reception->setWeightEntry($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGrossWeight(): ?int
|
||||
{
|
||||
return $this->grossWeight;
|
||||
}
|
||||
|
||||
public function setGrossWeight(?int $grossWeight): self
|
||||
{
|
||||
$this->grossWeight = $grossWeight;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTareWeight(): ?int
|
||||
{
|
||||
return $this->tareWeight;
|
||||
}
|
||||
|
||||
public function setTareWeight(?int $tareWeight): self
|
||||
{
|
||||
$this->tareWeight = $tareWeight;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGrossWeighedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->grossWeighedAt;
|
||||
}
|
||||
|
||||
public function setGrossWeighedAt(?DateTimeImmutable $grossWeighedAt): self
|
||||
{
|
||||
$this->grossWeighedAt = $grossWeighedAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTareWeighedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->tareWeighedAt;
|
||||
}
|
||||
|
||||
public function setTareWeighedAt(?DateTimeImmutable $tareWeighedAt): self
|
||||
{
|
||||
$this->tareWeighedAt = $tareWeighedAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
30
src/Exception/PontBasculeException.php
Normal file
30
src/Exception/PontBasculeException.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
final class PontBasculeException extends RuntimeException
|
||||
{
|
||||
public static function transportFailure(string $details): self
|
||||
{
|
||||
return new self('Erreur lors de la communication avec le pont bascule: '.$details, 500);
|
||||
}
|
||||
|
||||
public static function invalidPayload(): self
|
||||
{
|
||||
return new self('Réponse invalide du pont bascule.', 500);
|
||||
}
|
||||
|
||||
public static function missingPayloadField(string $field): self
|
||||
{
|
||||
return new self('Réponse incomplète du pont bascule: champ "'.$field.'" manquant.', 500);
|
||||
}
|
||||
|
||||
public static function unreadableValues(): self
|
||||
{
|
||||
return new self('Impossible de lire les valeurs de pesée du pont bascule.', 500);
|
||||
}
|
||||
}
|
||||
68
src/Service/PontBasculePayloadDecoder.php
Normal file
68
src/Service/PontBasculePayloadDecoder.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Dto\PontBasculeReading;
|
||||
use App\Exception\PontBasculeException;
|
||||
|
||||
final class PontBasculePayloadDecoder
|
||||
{
|
||||
public function decode(string $body): PontBasculeReading
|
||||
{
|
||||
// Payload is JSON with a "response_ascii" string containing STX (0x02) segments.
|
||||
$payload = json_decode($body, true);
|
||||
if (!is_array($payload)) {
|
||||
throw PontBasculeException::invalidPayload();
|
||||
}
|
||||
|
||||
$ascii = $payload['response_ascii'] ?? null;
|
||||
if (!is_string($ascii)) {
|
||||
throw PontBasculeException::missingPayloadField('response_ascii');
|
||||
}
|
||||
|
||||
$dsd = null;
|
||||
$net = null;
|
||||
|
||||
// Each segment starts with a 2-digit code followed by the numeric value.
|
||||
$segments = preg_split('/\\x02/', $ascii) ?: [];
|
||||
foreach ($segments as $segment) {
|
||||
$segment = trim($segment);
|
||||
if ('' === $segment) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!preg_match('/^(\d{2})(\d+)(?:\.kg)?/', $segment, $matches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$code = $matches[1];
|
||||
$value = $matches[2];
|
||||
|
||||
// Code 99 holds the DSD value.
|
||||
if ('99' === $code) {
|
||||
$dsd = (int) ltrim($value, '0');
|
||||
if (0 === $dsd && '' !== $value) {
|
||||
$dsd = 0;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Code 03 is the net weight; other codes are ignored for now.
|
||||
if ('03' === $code) {
|
||||
$net = (float) ltrim($value, '0');
|
||||
if (0.0 === $net && '' !== $value) {
|
||||
$net = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $dsd && null === $net) {
|
||||
throw PontBasculeException::unreadableValues();
|
||||
}
|
||||
|
||||
return new PontBasculeReading($dsd, $net);
|
||||
}
|
||||
}
|
||||
51
src/Service/PontBasculeService.php
Normal file
51
src/Service/PontBasculeService.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Dto\PontBasculeReading;
|
||||
use App\Exception\PontBasculeException;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
final class PontBasculeService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $httpClient,
|
||||
private readonly PontBasculePayloadDecoder $payloadDecoder,
|
||||
private readonly string $endpoint,
|
||||
private readonly bool $bypass,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @TODO Voir pour que le pont-bascule retourne la date
|
||||
*/
|
||||
public function fetch(): PontBasculeReading
|
||||
{
|
||||
if ($this->bypass) {
|
||||
$body = $this->getBypassPayload();
|
||||
} else {
|
||||
try {
|
||||
$response = $this->httpClient->request('POST', $this->endpoint);
|
||||
$body = $response->getContent(false);
|
||||
} catch (TransportExceptionInterface $exception) {
|
||||
throw PontBasculeException::transportFailure($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$reading = $this->payloadDecoder->decode($body);
|
||||
|
||||
return new PontBasculeReading(
|
||||
$reading->getDsd(),
|
||||
$reading->getWeight(),
|
||||
new DateTimeImmutable(),
|
||||
);
|
||||
}
|
||||
|
||||
private function getBypassPayload(): string
|
||||
{
|
||||
return '{"ok":true,"busy":false,"mode":"serial","port":"/dev/ttyUSB0","baudrate":9600,"request_hex":"01 10 39 39 4D 0D 0A","response_hex":"01 02 30 34 30 32 30 30 02 30 31 30 30 31 34 32 30 2E 6B 67 20 02 30 32 30 30 30 30 30 30 2E 6B 67 20 02 30 33 30 30 31 34 32 30 2E 6B 67 20 02 39 39 30 30 31 32 31 0D 0A","response_ascii":"\u0001\u0002040200\u000201001420.kg \u000202000000.kg \u000203001420.kg \u00029900121"}';
|
||||
}
|
||||
}
|
||||
34
src/State/ReceptionWeighingProvider.php
Normal file
34
src/State/ReceptionWeighingProvider.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Entity\Reception;
|
||||
use App\Exception\PontBasculeException;
|
||||
use App\Service\PontBasculeService;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
final readonly class ReceptionWeighingProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private PontBasculeService $pontBasculeService,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?Reception
|
||||
{
|
||||
try {
|
||||
$result = $this->pontBasculeService->fetch();
|
||||
} catch (PontBasculeException $exception) {
|
||||
throw new HttpException(500, $exception->getMessage(), $exception);
|
||||
}
|
||||
|
||||
return new Reception(
|
||||
dsd: $result->getDsd(),
|
||||
weight: $result->getWeight(),
|
||||
receptionDate: $result->getDatetime(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user