feat(label): Switch to JSON
This commit is contained in:
parent
b333e1577c
commit
536d27d272
2 changed files with 4 additions and 23 deletions
|
@ -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))
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Add table
Reference in a new issue