From 78fe0c97997975bc4283c30a99575580ce70f4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 10 Jan 2022 17:31:39 +0100 Subject: [PATCH] Add additional scripts --- .vscode/launch.json | 10 ++- print_storage_matrix_code.py | 33 ++++++++++ shimatta_kenkyusho_print_label.py | 105 ++++++++++++++++++++++++++++++ shimatta_label/component_label.py | 25 +++++++ 4 files changed, 171 insertions(+), 2 deletions(-) create mode 100755 print_storage_matrix_code.py create mode 100755 shimatta_kenkyusho_print_label.py create mode 100644 shimatta_label/component_label.py diff --git a/.vscode/launch.json b/.vscode/launch.json index 4e3b52e..536a1bb 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,7 +4,14 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ - + { + "name": "Python: Kenkyusho Storage Query", + "type": "python", + "request": "launch", + "program": "${workspaceFolder}/shimatta_kenkyusho_print_storage.py", + "console": "integratedTerminal", + "args": ["https://lab.shimatta.de/"] + }, { "name": "Python: Example Print", "type": "python", @@ -22,5 +29,4 @@ "args": [] } ] - } \ No newline at end of file diff --git a/print_storage_matrix_code.py b/print_storage_matrix_code.py new file mode 100755 index 0000000..9a1adc9 --- /dev/null +++ b/print_storage_matrix_code.py @@ -0,0 +1,33 @@ +#!/bin/python + +import shimatta_label.label_image as li +import shimatta_label.brother_ql_wrapper as ql_wrapper +import sys +import treepoem + +class DotmatrixLabel(li.Label): + pixels_x = 120 + pixels_y = 106 + + def put_matrix_code(self, qr): + code = treepoem.generate_barcode('datamatrix', qr).convert('1') + box = (int(self.pixels_x/2 -code.size[0]/2), int(self.pixels_y/2 - code.size[1]/2)) + self.img.paste(code, box=box) + +printer_model = 'QL-800' +printer_connection = 'usb://0x04f9:0x209b' + +if len(sys.argv) >= 2: + qr_data = sys.argv[1] +else: + qr_data = input('Scan QR Code: ') + +while qr_data: + + label = DotmatrixLabel() + label.put_matrix_code(qr_data) + #label.save('/tmp/foo.png') + printer = ql_wrapper.BrotherQlPrinter(model=printer_model, printer_connection=printer_connection, label_format='12') + printer.print_image(label.get_pillow_image(), cut=True, rotation=90) + + qr_data = input('Scan QR Code: ') \ No newline at end of file diff --git a/shimatta_kenkyusho_print_label.py b/shimatta_kenkyusho_print_label.py new file mode 100755 index 0000000..8291bee --- /dev/null +++ b/shimatta_kenkyusho_print_label.py @@ -0,0 +1,105 @@ +#!/bin/python + +import shimatta_label.storage_label +import shimatta_label.component_label +import shimatta_label.brother_ql_wrapper as ql_wrapper +import sys +import requests +import urllib.parse +import getpass +import json +import re + + +def handle_storage_qr_code(base_url, token, uuid): + storage_query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/storages/') + query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'id':uuid}) + if query_res.status_code != 200: + print('Request unsuccessful') + sys.exit(-3) + + print('Storage found: ') + storage_json = json.loads(query_res.content) + if storage_json['count'] != 1: + print('Storage json does not contain correct amount of storage entries') + sys.exit(-1) + + storage = storage_json['results'][0] + 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']) + + return label + +def handle_component_qr_code(base_url, token, uuid): + query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/components/') + query_res = requests.get(query_url, headers={'Authorization': f'Token {token}'}, params={'id': uuid}) + if query_res.status_code != 200: + print('Request unsuccessful') + sys.exit(-3) + + component = json.loads(query_res.content) + + if component['count'] != 1: + sys.exit(-1) + component = component['results'][0] + pkg = '' + name = component['name'] + if component.get('package_data') is not None: + pkg = component['package_data']['name'] + manufacturer = component.get('ro_manufacturer_name') + if manufacturer is None: + manufacturer = '' + + print(f'Component found: Name: {name}, Package: {pkg}, Manufacturer: {manufacturer}') + label = shimatta_label.component_label.ComponentLabel() + label.put_content(f'[comp_uuid]{uuid}', component['name'], manufacturer, pkg) + return label + +printer_model = 'QL-800' +printer_connection = 'usb://0x04f9:0x209b' +if len(sys.argv) < 2: + print(f'Usage: {sys.argv[0]} ') + sys.exit(-1) + +qr_re = re.compile(r'^\[(?P[a-zA-Z_]+)\](?P[a-fA-F0-9\-]+)') + +kenkyusho_url = sys.argv[1] +print(f"Shimatta Kenkyusho instance: {kenkyusho_url}") + +login_url = urllib.parse.urljoin(kenkyusho_url, 'api/v1/token-auth/') +print(f'Token auth url: {login_url}') + +username = input('Username: ') +password = getpass.getpass() + +ans = requests.post(login_url, data= {'username': username, 'password': password}) +if ans.status_code != 200: + print(f'Got status code {ans.status_code}') + sys.exit(-1) + +login_token = json.loads(ans.content)['token'] + + +while True: + qr = input('Please scan Storage or Component QR: ') + + matches = qr_re.match(qr) + if matches.group('prefix') == 'stor_uuid' : + label = 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')) + 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) diff --git a/shimatta_label/component_label.py b/shimatta_label/component_label.py new file mode 100644 index 0000000..69d5647 --- /dev/null +++ b/shimatta_label/component_label.py @@ -0,0 +1,25 @@ +from .label_image import Label +import qrcode + +class ComponentLabel(Label): + pixels_x = 991 + pixels_y = 306 + + 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) + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=8, + 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))) \ No newline at end of file