mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-16 12:18:19 +01:00
* added handling of LCSC barcode decoding and part loading on Label Scanner * when a part is scanned and not found, the scanner did not redraw so scanning subsequent parts was not possible without reloading the browser page. fixed the barcode scanner initialization and shutdown so it redraws properly after part not found * added redirection to part page on successful scan of lcsc, digikey, and mouser barcodes. added create part button if part does not exist in database * added augmented mode to label scanner to use vendor labels for part lookup to see part storage location quickly * shrink camera height on mobile so augmented information can been viewed onscreen * handle momentarily bad reads from qrcode library * removed augmented checkbox and combined functionality into info mode checkbox. changed barcode scanner to use XHR callback for barcode decoding to avoid problems with form submission and camera caused by page reloaded when part not found. * fix scanning of part-db barcodes to redirect to storage location or part lots. made scan result messages conditional for parts or other non-part barcodes * fix static analysis errors * added unit tests for meeting code coverage report * fix @MayNiklas reported bug: when manually submitting the form (from a barcode scan or manual input) redirect to Create New part screen for the decoded information instead of showing 'Format Unknown' popup error message * fix @d-buchmann bug: clear 'scan-augmented-result' field upon rescan of new barcode * fix @d-buchmann bug: after scanning in Info mode, if Info mode is turned off when scanning a part that did not exist, it now redirects user to create part page * fix @d-buchmann bug: make barcode decode table 100% width of page * fix bug with manual form submission where a part does not exist but decodes properly which causes the camera to not redraw on page reload due to unclean shutdown. this is an existing bug in the scanner interface. steps to produce the issue: - have camera active - put in code in Input - info mode ticked - click submit button on page reload the camera does not reactivate * fixed translation messages * Use symfony native functions to generate the routes for part creation * Use native request functions for request param parsing * Refactored LCSCBarcocdeScanResult to be an value object like the other Barcode results * Added test for LCSCBarcodeScanResult * Fixed exception when submitting form for info mode * Made BarcodeSourceType a backed enum, so that it can be used in Request::getEnum() * Moved database queries from BarcodeRedirector to PartRepository * Fixed modeEnum parsing * Fixed test errors * Refactored BarcodeRedirector logic to be more universal * Fixed BarcodeScanResultHandler test * Refactored BarcodeScanResultHandler to be able to resolve arbitary entities from scans * Moved barcode to info provider logic from Controller to BarcodeScanResultHandler service * Improved augmentented info styling and allow to use it with normal form submit too * Correctly handle nullable infoURL in ScanController * Replaced the custom controller for fragment replacements with symfony streams This does not require a complete new endpoint * Removed data-lookup-url attribute from scan read box * Removed unused translations * Added basic info block when an storage location was found for an barcode * Fixed phpstan issues * Fixed tests * Fixed part image for mobile view * Added more tests for BarcodeScanResultHandler service * Fixed tests --------- Co-authored-by: Jan Böhmer <mail@jan-boehmer.de>
158 lines
4.4 KiB
PHP
158 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\LabelSystem\BarcodeScanner;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* This class represents the content of a lcsc.com barcode
|
|
* Its data structure is represented by {pbn:...,on:...,pc:...,pm:...,qty:...}
|
|
*/
|
|
readonly class LCSCBarcodeScanResult implements BarcodeScanResultInterface
|
|
{
|
|
|
|
/** @var string|null (pbn) */
|
|
public ?string $pickBatchNumber;
|
|
|
|
/** @var string|null (on) */
|
|
public ?string $orderNumber;
|
|
|
|
/** @var string|null LCSC Supplier part number (pc) */
|
|
public ?string $lcscCode;
|
|
|
|
/** @var string|null (pm) */
|
|
public ?string $mpn;
|
|
|
|
/** @var int|null (qty) */
|
|
public ?int $quantity;
|
|
|
|
/** @var string|null Country Channel as raw value (CC) */
|
|
public ?string $countryChannel;
|
|
|
|
/**
|
|
* @var string|null Warehouse code as raw value (WC)
|
|
*/
|
|
public ?string $warehouseCode;
|
|
|
|
/**
|
|
* @var string|null Unknown numeric code (pdi)
|
|
*/
|
|
public ?string $pdi;
|
|
|
|
/**
|
|
* @var string|null Unknown value (hp)
|
|
*/
|
|
public ?string $hp;
|
|
|
|
/**
|
|
* @param array<string, string> $fields
|
|
*/
|
|
public function __construct(
|
|
public array $fields,
|
|
public string $rawInput,
|
|
) {
|
|
|
|
$this->pickBatchNumber = $this->fields['pbn'] ?? null;
|
|
$this->orderNumber = $this->fields['on'] ?? null;
|
|
$this->lcscCode = $this->fields['pc'] ?? null;
|
|
$this->mpn = $this->fields['pm'] ?? null;
|
|
$this->quantity = isset($this->fields['qty']) ? (int)$this->fields['qty'] : null;
|
|
$this->countryChannel = $this->fields['cc'] ?? null;
|
|
$this->warehouseCode = $this->fields['wc'] ?? null;
|
|
$this->pdi = $this->fields['pdi'] ?? null;
|
|
$this->hp = $this->fields['hp'] ?? null;
|
|
|
|
}
|
|
|
|
public function getSourceType(): BarcodeSourceType
|
|
{
|
|
return BarcodeSourceType::LCSC;
|
|
}
|
|
|
|
/**
|
|
* @return array|float[]|int[]|null[]|string[] An array of fields decoded from the barcode
|
|
*/
|
|
public function getDecodedForInfoMode(): array
|
|
{
|
|
// Keep it human-friendly
|
|
return [
|
|
'Barcode type' => 'LCSC',
|
|
'MPN (pm)' => $this->mpn ?? '',
|
|
'LCSC code (pc)' => $this->lcscCode ?? '',
|
|
'Qty' => $this->quantity !== null ? (string) $this->quantity : '',
|
|
'Order No (on)' => $this->orderNumber ?? '',
|
|
'Pick Batch (pbn)' => $this->pickBatchNumber ?? '',
|
|
'Warehouse (wc)' => $this->warehouseCode ?? '',
|
|
'Country/Channel (cc)' => $this->countryChannel ?? '',
|
|
'PDI (unknown meaning)' => $this->pdi ?? '',
|
|
'HP (unknown meaning)' => $this->hp ?? '',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Parses the barcode data to see if the input matches the expected format used by lcsc.com
|
|
* @param string $input
|
|
* @return bool
|
|
*/
|
|
public static function isLCSCBarcode(string $input): bool
|
|
{
|
|
$s = trim($input);
|
|
|
|
// Your example: {pbn:...,on:...,pc:...,pm:...,qty:...}
|
|
if (!str_starts_with($s, '{') || !str_ends_with($s, '}')) {
|
|
return false;
|
|
}
|
|
|
|
// Must contain at least pm: and pc: (common for LCSC labels)
|
|
return (stripos($s, 'pm:') !== false) && (stripos($s, 'pc:') !== false);
|
|
}
|
|
|
|
/**
|
|
* Parse the barcode input string into the fields used by lcsc.com
|
|
* @param string $input
|
|
* @return self
|
|
*/
|
|
public static function parse(string $input): self
|
|
{
|
|
$raw = trim($input);
|
|
|
|
if (!self::isLCSCBarcode($raw)) {
|
|
throw new InvalidArgumentException('Not an LCSC barcode');
|
|
}
|
|
|
|
$inner = substr($raw, 1, -1); // remove { }
|
|
|
|
$fields = [];
|
|
|
|
// This format is comma-separated pairs, values do not contain commas in your sample.
|
|
$pairs = array_filter(
|
|
array_map(trim(...), explode(',', $inner)),
|
|
static fn(string $s): bool => $s !== ''
|
|
);
|
|
|
|
foreach ($pairs as $pair) {
|
|
$pos = strpos($pair, ':');
|
|
if ($pos === false) {
|
|
continue;
|
|
}
|
|
|
|
$k = trim(substr($pair, 0, $pos));
|
|
$v = trim(substr($pair, $pos + 1));
|
|
|
|
if ($k === '') {
|
|
continue;
|
|
}
|
|
|
|
$fields[$k] = $v;
|
|
}
|
|
|
|
if (!isset($fields['pm']) || trim($fields['pm']) === '') {
|
|
throw new InvalidArgumentException('LCSC barcode missing pm field');
|
|
}
|
|
|
|
return new self($fields, $raw);
|
|
}
|
|
}
|