| Numéro du ticket | Titre du ticket | |------------------|-----------------| | #203 | Réceptions — Parcours de pesée multi-étapes | ## Description de la PR [#203] Réceptions — Parcours de pesée multi-étapes ## Modification du .env ## Check list - [x] Pas de régression - [x] TU/TI/TF rédigée - [x] TU/TI/TF OK - [x] CHANGELOG modifié Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/3 Reviewed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-authored-by: AUTIN Tristan <tristan@yuno.malio.fr> Co-committed-by: AUTIN Tristan <tristan@yuno.malio.fr>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Dto;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
|
||||
final readonly class PontBasculeReading
|
||||
{
|
||||
public function __construct(
|
||||
#[Groups(['reception:weigh:read'])]
|
||||
private ?int $dsd,
|
||||
#[Groups(['reception:weigh:read'])]
|
||||
private ?float $weight,
|
||||
#[Groups(['reception:weigh:read'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
private ?DateTimeImmutable $weighedAt = null,
|
||||
) {}
|
||||
|
||||
public function getDsd(): ?int
|
||||
{
|
||||
return $this->dsd;
|
||||
}
|
||||
|
||||
public function getWeight(): ?float
|
||||
{
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
public function getWeighedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->weighedAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
<?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\Dto\PontBasculeReading;
|
||||
use App\State\ReceptionWeighingProvider;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
#[ORM\Table(name: 'reception')]
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
requirements: ['id' => '\d+'],
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
),
|
||||
new GetCollection(
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
),
|
||||
new Post(
|
||||
normalizationContext: ['groups' => ['reception:read']],
|
||||
denormalizationContext: ['groups' => ['reception:write']],
|
||||
),
|
||||
new Patch(
|
||||
requirements: ['id' => '\d+'],
|
||||
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:weigh:read']],
|
||||
output: PontBasculeReading::class,
|
||||
provider: ReceptionWeighingProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
class Reception
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
#[Groups(['reception:read'])]
|
||||
private ?int $id = 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', 'reception:write'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
private ?DateTimeImmutable $receptionDate = null;
|
||||
|
||||
#[ORM\OneToMany(targetEntity: Weight::class, mappedBy: 'reception', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
||||
#[Groups(['reception:read'])]
|
||||
private Collection $weights;
|
||||
|
||||
public function __construct(
|
||||
?DateTimeImmutable $receptionDate = null,
|
||||
) {
|
||||
$this->receptionDate = $receptionDate;
|
||||
$this->weights = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
#[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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Weight>
|
||||
*/
|
||||
public function getWeights(): Collection
|
||||
{
|
||||
return $this->weights;
|
||||
}
|
||||
|
||||
public function addWeight(Weight $weight): self
|
||||
{
|
||||
if (!$this->weights->contains($weight)) {
|
||||
$this->weights->add($weight);
|
||||
$weight->setReception($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeWeight(Weight $weight): self
|
||||
{
|
||||
if ($this->weights->removeElement($weight)) {
|
||||
if ($weight->getReception() === $this) {
|
||||
$weight->setReception(null);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[ORM\PrePersist]
|
||||
public function initializeReceptionDate(): void
|
||||
{
|
||||
if (null === $this->receptionDate) {
|
||||
$this->receptionDate = new DateTimeImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?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 DateTimeImmutable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\Serializer\Attribute\Context;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'weight')]
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(normalizationContext: ['groups' => ['weight:read']]),
|
||||
new GetCollection(normalizationContext: ['groups' => ['weight:read']]),
|
||||
new Post(
|
||||
normalizationContext: ['groups' => ['weight:read']],
|
||||
denormalizationContext: ['groups' => ['weight:write']],
|
||||
),
|
||||
new Patch(
|
||||
normalizationContext: ['groups' => ['weight:read']],
|
||||
denormalizationContext: ['groups' => ['weight:write']],
|
||||
),
|
||||
],
|
||||
)]
|
||||
#[UniqueEntity(fields: ['reception', 'type'], message: 'A weighing already exists for this type.')]
|
||||
class Weight
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
#[Groups(['reception:read', 'weight:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'weights')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[Groups(['weight:read', 'weight:write'])]
|
||||
private ?Reception $reception = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
#[Groups(['reception:read', 'weight:read', 'weight:write'])]
|
||||
#[Assert\PositiveOrZero]
|
||||
private ?int $dsd = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
#[Groups(['reception:read', 'weight:read', 'weight:write'])]
|
||||
#[Assert\PositiveOrZero]
|
||||
private ?int $weight = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
#[Groups(['reception:read', 'weight:read', 'weight:write'])]
|
||||
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
|
||||
private ?DateTimeImmutable $weighedAt = null;
|
||||
|
||||
#[ORM\Column(length: 10)]
|
||||
#[Groups(['reception:read', 'weight:read', 'weight:write'])]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Choice(choices: ['gross', 'tare'])]
|
||||
private string $type = 'gross';
|
||||
|
||||
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->getWeights()->contains($this)) {
|
||||
$reception->addWeight($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDsd(): ?int
|
||||
{
|
||||
return $this->dsd;
|
||||
}
|
||||
|
||||
public function setDsd(?int $dsd): self
|
||||
{
|
||||
$this->dsd = $dsd;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWeight(): ?int
|
||||
{
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
public function setWeight(?int $weight): self
|
||||
{
|
||||
$this->weight = $weight;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWeighedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->weighedAt;
|
||||
}
|
||||
|
||||
public function setWeighedAt(?DateTimeImmutable $weighedAt): self
|
||||
{
|
||||
$this->weighedAt = $weighedAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function setType(string $type): self
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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"}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Dto\PontBasculeReading;
|
||||
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 = []): ?PontBasculeReading
|
||||
{
|
||||
try {
|
||||
$result = $this->pontBasculeService->fetch();
|
||||
} catch (PontBasculeException $exception) {
|
||||
throw new HttpException(500, $exception->getMessage(), $exception);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user