Browse Source

added my own version to get labels from parts.shimatta.net

stefan 4 days ago
parent
commit
cf0ba9cdea

+ 2 - 2
.vscode/launch.json

@@ -8,9 +8,9 @@
             "name": "Python: Kenkyusho Storage Query",
             "type": "python",
             "request": "launch",
-            "program": "${workspaceFolder}/shimatta_kenkyusho_print_storage.py",
+            "program": "${workspaceFolder}/shimatta_kenkyusho_print_label.py",
             "console": "integratedTerminal",
-            "args": ["https://lab.shimatta.de/"]
+            "args": ["https://parts.shimatta.net/"]
         },
         {
             "name": "Python: Example Print",

+ 10 - 0
generate_comp_label.py

@@ -0,0 +1,10 @@
+#!/bin/python
+
+import shimatta_label.label_image as li
+import shimatta_label.brother_ql_wrapper as ql_wrapper
+from shimatta_label.component_label import ComponentLabelSmdMiceToiletPocket
+
+label = ComponentLabelSmdMiceToiletPocket()
+label.put_content('210R', 'Samsung', 'R0603')
+
+ql_wrapper.print_labels([label] * 8)

+ 4 - 5
generate_storage_label.py

@@ -2,10 +2,9 @@
 
 import shimatta_label.label_image as li
 import shimatta_label.brother_ql_wrapper as ql_wrapper
-from shimatta_label.storage_label import StorageLabel
+from shimatta_label.storage_label import StorageLabelSmdMiceToilet, StorageLabel, StorageLabelSmdMiceToiletBottomQR
 
-label = StorageLabel()
-label.put_content('[stor_uuid]1c5cf30a-829c-4716-abfc-9ae2d4d493b2', '/Test1')
+label = StorageLabelSmdMiceToiletBottomQR()
+label.put_content('[stor_uuid]a4909bb4-4d69-4e58-8fbd-fba4fa62d9fb', '/Test1', 'asd aslkha kljdgask')
 
-printer = ql_wrapper.BrotherQlPrinter(label_format='29x90')
-printer.print_image(label.get_pillow_image(), rotation=90, cut=True)
+ql_wrapper.print_label(label)

+ 1 - 1
requirements.txt

@@ -2,7 +2,7 @@ attrs==21.3.0
 brother-ql==0.9.4
 click==8.0.3
 future==0.18.2
-numpy==1.21.5
+numpy
 packbits==0.6
 pandas==1.3.5
 Pillow==8.4.0

+ 64 - 18
shimatta_kenkyusho_print_label.py

@@ -10,6 +10,12 @@ import getpass
 import json
 import re
 
+MICE_TOILET_TEMPLATE = 'http://parts.shimatta.net/api/v1/parts/storages/075acbaf-ed60-47b9-9bd5-98bfbc3a79ea/'
+
+def char_range(c1, c2):
+    """Generates the characters from `c1` to `c2`, exclusive."""
+    for c in range(ord(c1), ord(c2)):
+        yield chr(c)
 
 def handle_storage_qr_code(base_url, token, uuid):
 	storage_query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/storages/')
@@ -19,7 +25,7 @@ def handle_storage_qr_code(base_url, token, uuid):
 		sys.exit(-3)
 
 	print('Storage found: ')
-	storage_json = json.loads(query_res.content)
+	storage_json = query_res.json()
 	if storage_json['count'] != 1:
 		print('Storage json does not contain correct amount of storage entries')
 		sys.exit(-1)
@@ -28,10 +34,54 @@ def handle_storage_qr_code(base_url, token, uuid):
 	print(f'ID: {storage["id"]}')
 	print(f'Path: {storage["full_path"]}')
 
-	label = shimatta_label.storage_label.StorageLabel()
-	label.put_content(f'[stor_uuid]{uuid}', storage['full_path'])
+	labels = []
+
+	if storage['template'] == MICE_TOILET_TEMPLATE:
+
+		row = input('Select which row shall be printed [1..3]:')
+		if row not in char_range('1', '4'):
+			print('Error - invalid row')
+			return None
+		row = int(row)
+
+		side = input('Select which side shall be printed [t,b]:')
+		if side not in ['t', 'b']:
+			print('Error - invalid side')
+			return None
+
+		# get children
+		query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
+		if query_res.status_code != 200:
+			print('Request for children unsuccessful')
+			sys.exit(-3)
+
+		storage_json = query_res.json()
+		if storage_json['count'] != 24:
+			print('Storage json does not contain correct amount of storage entries')
+			sys.exit(-1)
+
+		# pagination
+		storage_list = storage_json['results']
+		while storage_json['next']:
+			query_res = requests.get(storage_json['next'], headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
+			storage_json = query_res.json()
+			storage_list.extend(storage_json['results'])
+
+		storage_dict = {storage['name']: storage for storage in storage_list}
 
-	return label
+		if side == 'b':
+
+			for column in char_range('a','i'):
+				label = shimatta_label.storage_label.StorageLabelSmdMiceToiletBottomQR()
+				field_uuid = storage_dict[f'{column}{row}']['id']
+				label.put_content(f'[stor_uuid]{field_uuid}')
+				labels.append(label)
+	else:
+		label = shimatta_label.storage_label.StorageLabel()
+		label.put_content(f'[stor_uuid]{uuid}', storage['full_path'], storage['verbose_name'])
+		labels.append(label)
+
+	return labels
 
 def handle_component_qr_code(base_url, token, uuid):
 	query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/components/')
@@ -54,12 +104,14 @@ def handle_component_qr_code(base_url, token, uuid):
 		manufacturer = ''
 
 	print(f'Component found: Name: {name}, Package: {pkg}, Manufacturer: {manufacturer}')
-	label = shimatta_label.component_label.ComponentLabel()
+
+	labels = []
+	label = shimatta_label.component_label.ComponentLabelSmall()
 	label.put_content(f'[comp_uuid]{uuid}', component['name'], manufacturer, pkg)
-	return label
+	labels.append(label)
+
+	return labels
 
-printer_model = 'QL-800'
-printer_connection = 'usb://0x04f9:0x209b'
 if len(sys.argv) < 2:
 	print(f'Usage: {sys.argv[0]} <shimatta kenkyusho url>')
 	sys.exit(-1)
@@ -88,18 +140,12 @@ while True:
 
 	matches = qr_re.match(qr)
 	if matches.group('prefix') == 'stor_uuid' :
-		label = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
+		labels = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
 	elif matches.group('prefix') == 'comp_uuid':
-		label = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
+		labels = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
 	else:
 		print('Invalid QR code!')
 		continue
 
-	
-
-	printer = ql_wrapper.BrotherQlPrinter(model=printer_model, printer_connection=printer_connection, label_format='29x90')
-	printer.print_image(label.get_pillow_image(), rotation=90, cut=True)
-
-	cont = input('Printed! Continue? [Y/n] ')
-	if cont.lower() == 'n':
-		sys.exit(0)
+	if labels:
+		ql_wrapper.print_labels(labels)

+ 16 - 4
shimatta_label/brother_ql_wrapper.py

@@ -7,12 +7,13 @@ class BrotherQlPrinter():
 
     backend = 'pyusb'
 
-    def __init__(self, model='QL-800', printer_connection='usb://0x04f9:0x209b', label_format='12', red_label=False):
+    def __init__(self, model='QL-810W', printer_connection='usb://0x04f9:0x209c', label_format='12', red_label=False, dpi_600=False):
         self.model = model
         self.printer = printer_connection
         self.label = label_format
         self.red_label = red_label
-    
+        self.dpi_600 = dpi_600
+
     def print_image(self, image, rotation='Auto', cut=True):
         qlr = BrotherQLRaster(self.model)
         qlr.exception_on_warning = True
@@ -24,8 +25,19 @@ class BrotherQlPrinter():
             dither = False,
             compress = False,
             red = self.red_label,
-            dpi_600=False,
+            dpi_600=self.dpi_600,
             hq = True,
             cut = cut
             )
-        send(instructions=instructions, printer_identifier=self.printer, backend_identifier=self.backend, blocking=True)
+        send(instructions=instructions, printer_identifier=self.printer, backend_identifier=self.backend, blocking=True)
+
+def print_label(label):
+    printer = BrotherQlPrinter(label_format=label.ql_format, red_label=label.red_label, dpi_600=label.dpi_600)
+    printer.print_image(label.get_pillow_image(), rotation=label.ql_rotation, cut=True)
+
+def print_labels(labels):
+    
+    printer = BrotherQlPrinter(label_format=labels[0].ql_format, red_label=labels[0].red_label, dpi_600=labels[0].dpi_600)
+    for label in labels[:-1]:
+        printer.print_image(label.get_pillow_image(), rotation=label.ql_rotation, cut=False)
+    printer.print_image(labels[-1].get_pillow_image(), rotation=labels[-1].ql_rotation, cut=True)

+ 89 - 4
shimatta_label/component_label.py

@@ -5,13 +5,20 @@ class ComponentLabel(Label):
     pixels_x = 991
     pixels_y = 306
 
+    text_pos_x = 320
+
+    ql_format = '62'
+    ql_rotation = 90
+    dpi_600 = True
+    red_label = False
+
     def __init__(self):
         super().__init__()
 
     def put_content(self, qr_data, component_name, manufacturer, package):
-        self.draw_text(component_name, 320, self.pixels_y/2-45, size=38, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
-        self.draw_text(manufacturer, 320, self.pixels_y/2, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
-        self.draw_text(package, 320, self.pixels_y/2+40, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
+        self.draw_text(component_name, self.text_pos_x, self.pixels_y/2-45, size=38, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
+        self.draw_text(manufacturer, self.text_pos_x, self.pixels_y/2, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
+        self.draw_text(package, self.text_pos_x, self.pixels_y/2+40, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
         qr = qrcode.QRCode(
             version=1,
             error_correction=qrcode.constants.ERROR_CORRECT_L,
@@ -22,4 +29,82 @@ class ComponentLabel(Label):
         qr.make(fit=True)
         qr_image = qr.make_image(fill_color="black", back_color="white")
         qr_y_size = qr_image.size[1]
-        self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
+        self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
+
+
+class ComponentLabelSmallest(ComponentLabel):
+    pixels_x = 212
+    pixels_y = 212
+
+    ql_format = '12'
+    ql_rotation = 90
+    dpi_600 = True
+    red_label = False
+
+
+    def __init__(self):
+        super().__init__()
+
+    def put_content(self, qr_data, component_name=None, manufacturer=None, package=None):
+        qr = qrcode.QRCode(
+            version=1,
+            error_correction=qrcode.constants.ERROR_CORRECT_L,
+            box_size=7.2,
+            border=0,
+        )
+        qr.add_data(qr_data)
+        qr.make(fit=True)
+        qr_image = qr.make_image(fill_color="black", back_color="white")
+        qr_y_size = qr_image.size[1]
+        self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
+
+
+class ComponentLabelSmall(ComponentLabel):
+    pixels_x = 600
+    pixels_y = 212
+
+    ql_format = '12'
+    ql_rotation = 90
+    dpi_600 = True
+    red_label = False
+    text_pos_x = 235
+
+
+    def __init__(self):
+        super().__init__()
+
+    def put_content(self, qr_data, component_name='', manufacturer='', package=''):
+        self.draw_text(component_name, self.text_pos_x, self.pixels_y/2-65, size=42, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
+        self.draw_text(manufacturer, self.text_pos_x, self.pixels_y/2, size=36, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
+        self.draw_text(package, self.text_pos_x, self.pixels_y/2+55, size=36, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
+        qr = qrcode.QRCode(
+            version=1,
+            error_correction=qrcode.constants.ERROR_CORRECT_L,
+            box_size=7.2,
+            border=0,
+        )
+        qr.add_data(qr_data)
+        qr.make(fit=True)
+        qr_image = qr.make_image(fill_color="black", back_color="white")
+        qr_y_size = qr_image.size[1]
+        self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
+
+class ComponentLabelSmdMiceToiletPocket(Label):
+    pixels_x = 307
+    pixels_y = 212
+
+    ql_format = '12'
+    ql_rotation = 270
+    dpi_600 = True
+    red_label = False
+
+    def __init__(self):
+        super().__init__()
+
+    def put_content(self, heading, line1=None, line2=None):
+
+        self.draw_text(heading, self.pixels_x/2, 10, size=50, font_file='bold', centered_x=True, centered_y=True, scale_to_fit=True)
+        if line1:
+            self.draw_text(line1, self.pixels_x/2, 80, size=40, centered_x=True, centered_y=True, scale_to_fit=True)
+        if line2:
+            self.draw_text(line2, self.pixels_x/2, 165, size=40, centered_x=True, centered_y=True, scale_to_fit=True)

+ 76 - 6
shimatta_label/storage_label.py

@@ -2,22 +2,92 @@ from .label_image import Label
 import qrcode
 
 class StorageLabel(Label):
-    pixels_x = 991
-    pixels_y = 306
+    pixels_x = 696
+    pixels_y = 250
+
+    text_pos_x = 225
+
+    qr_size = 9
+    text_size_verbose_name = 48
+    text_size_name = 32
+
+    ql_format = '62'
+    ql_rotation = 0
+    dpi_600 = False
+    red_label = True
 
     def __init__(self):
         super().__init__()
 
-    def put_content(self, qr_data, storage_name):
-        self.draw_text(storage_name, 320, self.pixels_y/2, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
+    def put_content(self, qr_data, storage_name, verbose_name=None):
+
+        name_pos = self.pixels_y/2
+        name_font_file = 'bold'
+        if verbose_name:
+            self.draw_text(verbose_name,
+                            self.text_pos_x,
+                            self.pixels_y/4,
+                            size=self.text_size_verbose_name,
+                            font_file='bold',
+                            scale_to_fit=True,
+                            centered_x=False,
+                            centered_y=True)
+            name_pos = self.pixels_y/4 * 3
+            name_font_file = None
+
+        self.draw_text(storage_name,
+                       self.text_pos_x,
+                       name_pos,
+                       size=self.text_size_name,
+                       font_file=name_font_file,
+                       scale_to_fit=True,
+                       centered_x=False,
+                       centered_y=True)
+        qr = qrcode.QRCode(
+            version=1,
+            error_correction=qrcode.constants.ERROR_CORRECT_L,
+            box_size=7,
+            border=0,
+        )
+        qr.add_data(qr_data)
+        qr.make(fit=True)
+        qr_image = qr.make_image(fill_color="black", back_color="white")
+        qr_y_size = qr_image.size[1]
+        self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
+
+class StorageLabelSmdMiceToilet(StorageLabel):
+    pixels_x = 1108
+    pixels_y = 200
+
+    qr_size = 7
+    text_size_verbose_name = 64
+    text_size_name = 48
+    text_pos_x = 320
+
+    ql_format = '50'
+    ql_rotation = 0
+    dpi_600 = True
+    red_label = False
+
+class StorageLabelSmdMiceToiletBottomQR(StorageLabel):
+    pixels_x = 307
+    pixels_y = 212
+
+    ql_format = '12'
+    ql_rotation = 270
+    dpi_600 = True
+    red_label = False
+
+    def put_content(self, qr_data, storage_name=None, verbose_name=None):
         qr = qrcode.QRCode(
             version=1,
             error_correction=qrcode.constants.ERROR_CORRECT_L,
-            box_size=8,
+            box_size=7,
             border=0,
         )
         qr.add_data(qr_data)
         qr.make(fit=True)
         qr_image = qr.make_image(fill_color="black", back_color="white")
+        qr_x_size = qr_image.size[0]
         qr_y_size = qr_image.size[1]
-        self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
+        self.img.paste(qr_image, box=(int(self.pixels_x/2 - qr_x_size/2), int(self.pixels_y/2 - qr_y_size/2)))