From bfaa0b7f89be8e1f0c06a7fce1d7ecae225ca292 Mon Sep 17 00:00:00 2001 From: Andreas Mieke Date: Sat, 20 Apr 2024 22:42:17 -0400 Subject: [PATCH] feat(barcode): Add barcode support --- inventree_phomemo/phomemo_label.py | 55 ++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/inventree_phomemo/phomemo_label.py b/inventree_phomemo/phomemo_label.py index 07ee7a0..9eea833 100644 --- a/inventree_phomemo/phomemo_label.py +++ b/inventree_phomemo/phomemo_label.py @@ -3,18 +3,21 @@ from django.utils.translation import gettext_lazy as _ from django.core.validators import MinValueValidator from django.core.validators import MaxValueValidator -# printer supportt +# printer support import socket -import io import json +# barcode helpers +from InvenTree.helpers_model import getModelsWithMixin +from InvenTree.models import InvenTreeBarcodeMixin + # InvenTree plugin libs from plugin import InvenTreePlugin -from plugin.mixins import LabelPrintingMixin, SettingsMixin +from plugin.mixins import LabelPrintingMixin, SettingsMixin, BarcodeMixin from inventree_phomemo.version import PHOMEMO_PLUGIN_VERSION -class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin): +class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenTreePlugin): AUTHOR = "Sergal.engineering" DESCRIPTION = "Label printing plugin for Phomemo printers" VERSION = PHOMEMO_PLUGIN_VERSION @@ -68,8 +71,6 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin): 'params': tpart.parameters_map() if hasattr(tpart, 'parameters_map') else None, 'category': tpart.category.name if hasattr(tpart, 'category') else None, 'category_path': tpart.category.pathstring if hasattr(tpart, 'category') else None, - 'location': tpart.location.name if hasattr(tpart, 'location') else None, - 'location_path': tpart.location.pathstring if hasattr(tpart, 'location') else None, 'barcode': barcode, 'type': kwargs['label_instance'].SUBDIR } @@ -85,3 +86,45 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin): print_socket.close() except Exception as error: raise ConnectionError('Error connecting to printer server: ' + str(error)) + + + @staticmethod + def get_supported_barcode_models(): + """Returns a list of database models which support barcode functionality.""" + return getModelsWithMixin(InvenTreeBarcodeMixin) + + def format_matched_response(self, label, model, instance): + """Format a response for the scanned data.""" + return {label: instance.format_matched_response()} + + def scan(self, barcode_data): + # Our barcode should be a string + if type(barcode_data) is not str: + barcode_data = str(barcode_data) + + # Our barcodes are at least two symbols long + if len(barcode_data) < 2: + return + + match barcode_data[0]: + case '1': + model_type = 'part' + case '2': + model_type = 'stockitem' + case '3': + model_type = 'stocklocation' + case '4': + model_type = 'build' + + supported_models = self.get_supported_barcode_models() + + for model in supported_models: + label = model.barcode_model_type() + + if label == model_type: + try: + pk = int(barcode_data[1:]) + instance = model.objects.get(pk=pk) + return self.format_matched_response(label, model, instance) + except (ValueError, model.DoesNotExist): + pass \ No newline at end of file