[#203] Réceptions — Parcours de pesée multi-étapes (première partie) (!3)

| 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:
AUTIN Tristan
2026-01-14 07:17:34 +00:00
committed by Autin
parent 9fb0fc12b8
commit 4a77449a41
44 changed files with 1976 additions and 73 deletions
+68
View 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
View 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"}';
}
}