156 lines
7.9 KiB
Python
156 lines
7.9 KiB
Python
"""Paired picking coordinator. Free movements still use BarcodeService unchanged."""
|
|
|
|
from dataclasses import replace
|
|
|
|
from barcode_service import BarcodeService, BarcodeViewState, BarcodeActionResult
|
|
from pickinglist_pair_repository import PickingPairRepository, PairSnapshot
|
|
from version_info import module_version
|
|
|
|
__version__ = module_version(__name__)
|
|
|
|
|
|
class BarcodePairService(BarcodeService):
|
|
def __init__(self, repository, operator_id, pair_repository=None):
|
|
super().__init__(repository, operator_id)
|
|
self.pairs = pair_repository or PickingPairRepository(repository.db_client)
|
|
self._pair: PairSnapshot | None = None
|
|
self._pair_paused = False
|
|
|
|
@property
|
|
def pair_active(self) -> bool:
|
|
return self._pair is not None
|
|
|
|
async def prepare_pair(self):
|
|
"""Identify an existing pair without advancing or simulating a scan."""
|
|
self._pair = await self.pairs.snapshot()
|
|
return self._state
|
|
|
|
def _decorate(self, state, document, *, paused=False):
|
|
slot = self._pair.documents.index(document) + 1
|
|
if paused:
|
|
confirmed = state.mode == "confirm"
|
|
state = replace(state, mode="manual_unload", destination_readonly=False,
|
|
auto_advance_delay_ms=0,
|
|
scanned_pallet="" if confirmed else state.scanned_pallet,
|
|
destination_barcode=self.NON_SCAFFALATA_BARCODE if confirmed else state.destination_barcode)
|
|
return replace(state, pair_run_id=self._pair.run_id, pair_slot=slot,
|
|
pair_document=document,
|
|
queue_label="Pausa PL" if paused else f"Lista {slot} - PL {document}")
|
|
|
|
def _proposal(self, snapshot, candidate):
|
|
row = snapshot.display_row(candidate.document, candidate.pallet)
|
|
self._current_priority_state = 1
|
|
self._priority_documents[1] = candidate.document
|
|
self._pair = snapshot
|
|
location = self._display_location(cella=candidate.cell_id, ubicazione=row["Ubicazione"])
|
|
self._state = self._decorate(BarcodeViewState(
|
|
mode="priority_high", status_text=f"Ok Cella - {location}",
|
|
status_color=self.LIGHT_GREEN, source_location=location,
|
|
document=candidate.document, expected_pallet=candidate.pallet,
|
|
customer=" - ".join(str(row.get(k) or "") for k in ("CodNazione", "NAZIONE")).strip(" -"),
|
|
destination_barcode=self.SHIPPED_BARCODE, destination_readonly=True,
|
|
), candidate.document)
|
|
self._pair_paused = False
|
|
return BarcodeActionResult(True, self._state)
|
|
|
|
async def start_priority_queue(self, id_stato):
|
|
if self._pair_paused:
|
|
return BarcodeActionResult(False, self._state, "Premi ESC per uscire dalla pausa.")
|
|
snapshot = await self.pairs.snapshot()
|
|
if self._pair and snapshot and snapshot.run_id != self._pair.run_id:
|
|
raise RuntimeError("Prenotazione cambiata durante il giro. Riavviare il terminalino.")
|
|
if snapshot:
|
|
self._pair = snapshot
|
|
if id_stato != 1:
|
|
return BarcodeActionResult(False, self._state, "Giro bilista: usa F1.")
|
|
snapshot.route() # Validate before releasing anything.
|
|
await self.pairs.command("F", self.operator_id, run_id=snapshot.run_id)
|
|
refreshed = await self.pairs.snapshot()
|
|
if refreshed and refreshed.run_id != snapshot.run_id:
|
|
raise RuntimeError("Il giro e cambiato. Ricaricare il terminalino.")
|
|
if refreshed:
|
|
route = refreshed.route()
|
|
if not route.pending:
|
|
raise RuntimeError("Residui cambiati durante il caricamento. Riprovare.")
|
|
return self._proposal(refreshed, route.pending[0])
|
|
if self._pair:
|
|
self._pair = None
|
|
self._priority_documents.clear()
|
|
self._current_priority_state = -1
|
|
self._state = BarcodeViewState(mode="manual_unload", status_text="Giro terminato. Riprenota le eventuali UDC saltate.",
|
|
destination_barcode=self.NON_SCAFFALATA_BARCODE)
|
|
return BarcodeActionResult(True, self._state)
|
|
return await super().start_priority_queue(id_stato)
|
|
|
|
async def skip_current_picking_pallet(self):
|
|
if not self._pair:
|
|
return await super().skip_current_picking_pallet()
|
|
state = self._state
|
|
if state.mode != "priority_high" or self._pair_paused:
|
|
return BarcodeActionResult(False, state, "Nessuna UDC proposta da saltare.")
|
|
await self.pairs.command("K", self.operator_id, run_id=self._pair.run_id,
|
|
document=state.pair_document, pallet=state.expected_pallet)
|
|
self._state = replace(state, mode="confirm", status_text="UDC saltata, resta da prelevare",
|
|
status_color=self.RED, auto_advance_delay_ms=1500, scanned_pallet="")
|
|
return BarcodeActionResult(True, self._state)
|
|
|
|
def begin_priority_pause(self, id_stato):
|
|
if not self._pair:
|
|
return super().begin_priority_pause(id_stato)
|
|
document = self._state.pair_document
|
|
self._pair_paused = True
|
|
state = super().begin_priority_pause(1)
|
|
self._state = self._decorate(state, document, paused=True)
|
|
return self._state
|
|
|
|
async def resume_pair(self, saved):
|
|
snapshot = await self.pairs.snapshot()
|
|
if not snapshot or not self._pair or snapshot.run_id != self._pair.run_id:
|
|
raise RuntimeError("La coppia in pausa non e piu disponibile. Verificare la prenotazione.")
|
|
route = snapshot.route()
|
|
if saved is not None:
|
|
candidate = next((c for c in route.pending if c.document == saved.pair_document
|
|
and c.pallet == saved.expected_pallet), None)
|
|
if candidate:
|
|
return self._proposal(snapshot, candidate)
|
|
# Only advance if the paused UDC is no longer pending.
|
|
self._pair_paused = False
|
|
try:
|
|
return await self.start_priority_queue(1)
|
|
except Exception:
|
|
self._pair_paused = True
|
|
raise
|
|
|
|
async def submit(self, **kwargs):
|
|
before = self._state
|
|
if self._pair and not self._pair_paused and before.mode != "priority_high":
|
|
return BarcodeActionResult(False, before, "Attendi la prossima UDC.")
|
|
result = await super().submit(**kwargs)
|
|
if self._pair and before.pair_document:
|
|
state = self._decorate(result.state, before.pair_document, paused=self._pair_paused)
|
|
if result.ok and not self._pair_paused and before.mode == "priority_high":
|
|
state = replace(state, auto_advance_delay_ms=3000, destination_readonly=True)
|
|
self._state = state
|
|
return BarcodeActionResult(result.ok, state, result.message)
|
|
return result
|
|
|
|
async def submit_unload_with_source_check(self, **kwargs):
|
|
if self._pair and not self._pair_paused:
|
|
return BarcodeActionResult(False, self._state, "Metti in pausa la lista per un movimento libero.")
|
|
document = self._state.pair_document
|
|
result = await super().submit_unload_with_source_check(**kwargs)
|
|
if self._pair and document:
|
|
self._state = self._decorate(result.state, document, paused=self._pair_paused)
|
|
return BarcodeActionResult(result.ok, self._state, result.message)
|
|
return result
|
|
|
|
async def reconcile_after_submit_exception(self, **kwargs):
|
|
before = self._state
|
|
result = await super().reconcile_after_submit_exception(**kwargs)
|
|
if self._pair and before.pair_document:
|
|
self._state = self._decorate(result.state, before.pair_document, paused=self._pair_paused)
|
|
if self._pair_paused:
|
|
self._state = replace(self._state, auto_advance_delay_ms=0, destination_readonly=False)
|
|
return BarcodeActionResult(result.ok, self._state, result.message)
|
|
return result
|