feat(label): Switch to JSON

This commit is contained in:
Andreas Mieke 2024-04-18 00:39:06 -04:00
parent b333e1577c
commit 536d27d272
Signed by: zenermerps
SSH key fingerprint: SHA256:Ne+hwc5QIgYlqCuLZ0LV3301Wo/p8UoGOrGC+T6S0t8
2 changed files with 4 additions and 23 deletions

View file

@ -6,6 +6,7 @@ from django.core.validators import MaxValueValidator
# printer supportt # printer supportt
import socket import socket
import io import io
import json
# InvenTree plugin libs # InvenTree plugin libs
from plugin import InvenTreePlugin from plugin import InvenTreePlugin
@ -32,12 +33,6 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
'description': _('Port of phomemo print server'), 'description': _('Port of phomemo print server'),
'default': '9100', '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): def print_label(self, **kwargs):
@ -45,28 +40,14 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
# Read settings # Read settings
ip_address = self.get_setting('IP_ADDRESS') ip_address = self.get_setting('IP_ADDRESS')
port = int(self.get_setting('PORT')) port = int(self.get_setting('PORT'))
threshold = self.get_setting('THRESHOLD') label_data = kwargs['label_instance']
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()
# Send the label to the printer # Send the label to the printer
try: try:
print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print_socket.settimeout(5) print_socket.settimeout(5)
print_socket.connect((ip_address, port)) print_socket.connect((ip_address, port))
print_socket.send(img_byte_arr) json.dump(label_data, print_socket)
print_socket.close() print_socket.close()
except Exception as error: except Exception as error:
raise ConnectionError('Error connecting to printer server: ' + str(error)) raise ConnectionError('Error connecting to printer server: ' + str(error))

View file

@ -11,7 +11,7 @@ s.listen(1)
while True: while True:
conn, addr = s.accept() conn, addr = s.accept()
print(addr) print(addr)
printer = open('test.png', 'wb') printer = open('test.json', 'w')
while True: while True:
data = conn.recv(91000) data = conn.recv(91000)
print('data received') print('data received')