From 536d27d2727d46dee74c89c630384107f339d7cf Mon Sep 17 00:00:00 2001 From: Andreas Mieke Date: Thu, 18 Apr 2024 00:39:06 -0400 Subject: [PATCH] feat(label): Switch to JSON --- inventree_phomemo/phomemo_label.py | 25 +++---------------------- inventree_phomemo/server.py | 2 +- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/inventree_phomemo/phomemo_label.py b/inventree_phomemo/phomemo_label.py index c69f9d5..180b841 100644 --- a/inventree_phomemo/phomemo_label.py +++ b/inventree_phomemo/phomemo_label.py @@ -6,6 +6,7 @@ from django.core.validators import MaxValueValidator # printer supportt import socket import io +import json # InvenTree plugin libs from plugin import InvenTreePlugin @@ -32,12 +33,6 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin): 'description': _('Port of phomemo print server'), 'default': '9100', }, - 'THRESHOLD': { - 'name': _('Threshold'), - 'description': _('Threshold for B/W conversion (0-255)'), - 'validator': [int, MinValueValidator(0), MaxValueValidator(255)], - 'default': 200, - }, } def print_label(self, **kwargs): @@ -45,28 +40,14 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin): # Read settings ip_address = self.get_setting('IP_ADDRESS') port = int(self.get_setting('PORT')) - threshold = self.get_setting('THRESHOLD') - label_image = kwargs['png_file'] - - # Extract width (x) and height (y) information. - width = kwargs['width'] - height = kwargs['height'] - - # convert image to B/W - fn = lambda x: 255 if x > threshold else 0 - label_image = label_image.convert('L').point(fn, mode='1') - - ##### Convert to phomemo - img_byte_arr = io.BytesIO() - label_image.save(img_byte_arr, format='PNG') - img_byte_arr = img_byte_arr.getvalue() + label_data = kwargs['label_instance'] # Send the label to the printer try: print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print_socket.settimeout(5) print_socket.connect((ip_address, port)) - print_socket.send(img_byte_arr) + json.dump(label_data, print_socket) print_socket.close() except Exception as error: raise ConnectionError('Error connecting to printer server: ' + str(error)) diff --git a/inventree_phomemo/server.py b/inventree_phomemo/server.py index 1f51e09..c0efce9 100644 --- a/inventree_phomemo/server.py +++ b/inventree_phomemo/server.py @@ -11,7 +11,7 @@ s.listen(1) while True: conn, addr = s.accept() print(addr) - printer = open('test.png', 'wb') + printer = open('test.json', 'w') while True: data = conn.recv(91000) print('data received')