feat(label): Switch to POST request

This commit is contained in:
Andreas Mieke 2024-04-21 21:55:38 -04:00
parent 1cf5a58807
commit 85981d81b5
Signed by: zenermerps
SSH key fingerprint: SHA256:Ne+hwc5QIgYlqCuLZ0LV3301Wo/p8UoGOrGC+T6S0t8

View file

@ -5,8 +5,7 @@ from django.core.validators import MaxValueValidator
from django.http import JsonResponse from django.http import JsonResponse
# printer support # printer support
import socket import requests
import json
# barcode helpers # barcode helpers
from InvenTree.helpers_model import getModelsWithMixin from InvenTree.helpers_model import getModelsWithMixin
@ -28,15 +27,10 @@ class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenT
TITLE = "Phomemo Label Printer" TITLE = "Phomemo Label Printer"
SETTINGS = { SETTINGS = {
'IP_ADDRESS': { 'URL': {
'name': _('IP Address'), 'name': _('URL'),
'description': _('IP address of phomemo print server'), 'description': _('URL of the print endpoint'),
'default': '', 'default': 'http://localhost:8080/print',
},
'PORT': {
'name': _('Port'),
'description': _('Port of phomemo print server'),
'default': '9100',
}, },
} }
@ -85,21 +79,11 @@ class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenT
outputs.append(self.get_fields(label)) outputs.append(self.get_fields(label))
# Read settings # Read settings
ip_address = self.get_setting('IP_ADDRESS') url = self.get_setting('URL')
port = int(self.get_setting('PORT'))
# Dump to json
data = json.dumps(outputs)
# Send the label to the printer # Send the label to the printer
try: r = requests.post(url, json=outputs)
print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) r.raise_for_status()
print_socket.settimeout(5)
print_socket.connect((ip_address, port))
print_socket.sendall(bytes(data,encoding="utf-8"))
print_socket.close()
except Exception as error:
raise ConnectionError('Error connecting to printer server: ' + str(error))
return JsonResponse({ return JsonResponse({
'success': True, 'success': True,