st-ten-1/src/ui/barcode_recipe_selection/barcode_recipe_selection.py
2025-09-16 17:18:00 +02:00

139 lines
5.1 KiB
Python

import sys
import peewee
from PyQt5 import Qt
from lib.helpers import timing
from PyQt5.QtCore import Qt, QTimer, QThread
from PyQt5.QtGui import QKeySequence, QPixmap, QPalette, QColor
from PyQt5.QtWidgets import QShortcut, QApplication, QDialog, QVBoxLayout, QLabel, QPushButton, QPlainTextEdit, \
QHBoxLayout, QMessageBox
from ui.test_test import Test_Test
from ui.widget import Widget
from lib import db
from lib.db.models import Recipes , Users
test_scan="xxx\nyyy\nzzz"
class Barcode_Recipe_Selection(Test_Test):
def __init__(self, parent):
super().__init__()
self.parent=parent
self.components=parent.components
self.recipe_db_model=Recipes
self.debounce_time = 200
self.barcode_input_l.textChanged.connect(self.debounce)
self.status_palettes = {
True: QPalette(),
"": QPalette(),
"warning": QPalette(),
False: QPalette(),
None: QPalette(),
}
self.status_palettes[True].setColor(QPalette.Base, Qt.green)
self.status_palettes[False].setColor(QPalette.Base, Qt.red)
self.status_palettes["warning"].setColor(QPalette.Base, QColor(255, 165, 0))
self.status_palettes[""].setColor(QPalette.Base, QColor(255, 255, 0))
self.recipe_selection_b.clicked.connect(self.parent.set_recipe_mode_table)
self.barcode_input_l.setFocus()
self.focus_timer = QTimer()
self.focus_timer.setSingleShot(True)
self.focus_timer.timeout.connect(self.set_focus)
self.wait_timer = QTimer()
self.wait_timer.setSingleShot(True)
self.wait_timer.timeout.connect(self.get)
self.ok_timer = QTimer()
self.ok_timer.setSingleShot(True)
self.ok_timer.timeout.connect(self.set_recipe)
def start(self, recipe=None, step=None, pieces=None):
self.barcode_input_l.setPalette(self.status_palettes[None])
self.barcode_input_l.setPlainText("")
self.barcode_input_l.setFocus()
if self.parent.tag_loaded_recipe is not None:
self.get(self.parent.tag_loaded_recipe)
self.focus_timer.start(500)
def debounce(self):
if self.wait_timer.remainingTime()>0:
self.wait_timer.setInterval(self.debounce_time)
else:
self.wait_timer.start(self.debounce_time)
def get(self, data=None, override=False):
if data is None:
data = self.barcode_input_l.toPlainText()
if not len(data):
data = None
if data is None:
return
else:
lines = data.splitlines()
#lines = data.split("-")
candidates = [i for i in lines if len(i) in(9,10,11,12,13,17,18)]
if len(candidates)>0:
# RECIPE CODE FOUND
self.recipe=candidates[-1]
self.barcode_input_l.setPalette(self.status_palettes[True])
if "--write-tags" not in sys.argv:
self.ok_timer.start(2000)
else:
self.parent.components["rfid"].write_tag(data)
else:
# RECIPE CODE NOT FOUND
self.barcode_input_l.setPalette(self.status_palettes[False])
self.focus_timer.start(3000)
def set_recipe(self):
try:
# LOOKUP RECIPE
recipe = self.recipe_db_model.get_by_id(self.recipe)
self.parent.set_recipe(recipe)
except peewee.DoesNotExist:
self.barcode_input_l.setPalette(self.status_palettes[False])
self.focus_timer.start(3000)
self.parent_assembly_widget().set_text("RICETTA NON TROVATA",bg_color="red")
def set_focus(self):
self.barcode_input_l.setFocus()
self.barcode_input_l.setPlainText("")
self.barcode_input_l.setPalette(self.status_palettes[""])
if getattr(self.parent.centralWidget,"set_text"):
if "fixture_id" in self.components.keys():
self.parent_assembly_widget().set_text(
"INSERIRE LA DIMA PER SELEZIONARE AUTOMATICAMENTE LA RICETTA DI COLLAUDO")
else:
self.parent_assembly_widget().set_text("SCANSIONARE BARCODE SELEZIONE RICETTA")
def tag_write(self, data_to_write=None):
self.parent_assembly_widget().set_text("SCRITTURA TAG NFC ", bg_color=" yellow")
tag_data = data_to_write if data_to_write else self.barcode_input_l.toPlainText().strip()
if not tag_data:
QMessageBox.warning(self, "Warning", "Il campo di input è vuoto. Scansiona un QR code o un tag NFC per continuare")
return
if self.parent.components["fixture_id"].current_data:
confirmation_result = QMessageBox.question(
self,
"Tag già scritto",
"Il tag contiene già dei dati. Vuoi sovrascriverli?",
QMessageBox.Yes | QMessageBox.No,
)
if confirmation_result == QMessageBox.No:
return
self.parent.components["fixture_id"].write_tag(tag_data)
QMessageBox.information(self, "Successo", "Il tag è stato scritto correttamente.")