2024-04-17 21:32:39 -04:00
|
|
|
# translation
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.core.validators import MinValueValidator
|
|
|
|
from django.core.validators import MaxValueValidator
|
|
|
|
|
|
|
|
# printer supportt
|
|
|
|
import socket
|
2024-04-17 22:07:05 -04:00
|
|
|
import io
|
2024-04-18 00:39:06 -04:00
|
|
|
import json
|
2024-04-17 21:32:39 -04:00
|
|
|
|
|
|
|
# InvenTree plugin libs
|
|
|
|
from plugin import InvenTreePlugin
|
|
|
|
from plugin.mixins import LabelPrintingMixin, SettingsMixin
|
|
|
|
|
|
|
|
from inventree_phomemo.version import PHOMEMO_PLUGIN_VERSION
|
|
|
|
|
|
|
|
class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
|
|
|
|
AUTHOR = "Sergal.engineering"
|
|
|
|
DESCRIPTION = "Label printing plugin for Phomemo printers"
|
|
|
|
VERSION = PHOMEMO_PLUGIN_VERSION
|
|
|
|
NAME = "Phomemo"
|
|
|
|
SLUG = "phomemo"
|
|
|
|
TITLE = "Phomemo Label Printer"
|
|
|
|
|
|
|
|
SETTINGS = {
|
|
|
|
'IP_ADDRESS': {
|
|
|
|
'name': _('IP Address'),
|
|
|
|
'description': _('IP address of phomemo print server'),
|
|
|
|
'default': '',
|
|
|
|
},
|
|
|
|
'PORT': {
|
|
|
|
'name': _('Port'),
|
|
|
|
'description': _('Port of phomemo print server'),
|
|
|
|
'default': '9100',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def print_label(self, **kwargs):
|
|
|
|
|
|
|
|
# Read settings
|
|
|
|
ip_address = self.get_setting('IP_ADDRESS')
|
|
|
|
port = int(self.get_setting('PORT'))
|
2024-04-18 00:53:43 -04:00
|
|
|
|
|
|
|
object_to_print = kwargs['label_instance'].object_to_print
|
|
|
|
|
2024-04-20 21:51:01 -04:00
|
|
|
match kwargs['label_instance'].SUBDIR:
|
|
|
|
case 'part':
|
|
|
|
tpart = object_to_print
|
|
|
|
barcode = '1' + str(object_to_print.pk)
|
|
|
|
case 'stockitem':
|
|
|
|
tpart = object_to_print.part
|
|
|
|
barcode = '2' + str(object_to_print.pk)
|
2024-04-20 22:10:22 -04:00
|
|
|
case 'stocklocation':
|
2024-04-20 21:51:01 -04:00
|
|
|
tpart = object_to_print
|
|
|
|
barcode = '3' + str(object_to_print.pk)
|
|
|
|
case 'build':
|
|
|
|
tpart = object_to_print
|
|
|
|
barcode = '4' + str(object_to_print.pk)
|
|
|
|
case _:
|
|
|
|
tpart = object_to_print
|
|
|
|
barcode = '0' + str(object_to_print.pk)
|
2024-04-20 21:58:33 -04:00
|
|
|
print(f"!! Unsupported item type: {kwargs['label_instance'].SUBDIR}")
|
2024-04-18 00:53:43 -04:00
|
|
|
|
|
|
|
fields = {
|
|
|
|
'name': tpart.name,
|
|
|
|
'description': tpart.description,
|
|
|
|
'pk': tpart.pk,
|
2024-04-20 22:06:09 -04:00
|
|
|
'params': tpart.parameters_map() if hasattr(tpart, 'parameters_map') else None,
|
|
|
|
'category': tpart.category.name if hasattr(tpart, 'category') else None,
|
|
|
|
'category_path': tpart.category.pathstring if hasattr(tpart, 'category') else None,
|
2024-04-20 22:10:22 -04:00
|
|
|
'location': tpart.location.name if hasattr(tpart, 'location') else None,
|
|
|
|
'location_path': tpart.location.pathstring if hasattr(tpart, 'location') else None,
|
2024-04-20 21:58:33 -04:00
|
|
|
'barcode': barcode,
|
|
|
|
'type': kwargs['label_instance'].SUBDIR
|
2024-04-18 00:53:43 -04:00
|
|
|
}
|
2024-04-17 21:32:39 -04:00
|
|
|
|
2024-04-18 01:04:11 -04:00
|
|
|
data = json.dumps(fields)
|
|
|
|
|
2024-04-17 21:32:39 -04:00
|
|
|
# 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))
|
2024-04-18 01:04:11 -04:00
|
|
|
print_socket.sendall(bytes(data,encoding="utf-8"))
|
2024-04-17 21:32:39 -04:00
|
|
|
print_socket.close()
|
|
|
|
except Exception as error:
|
|
|
|
raise ConnectionError('Error connecting to printer server: ' + str(error))
|