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
|
|
|
|
|
|
|
|
# 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',
|
|
|
|
},
|
|
|
|
'THRESHOLD': {
|
|
|
|
'name': _('Threshold'),
|
|
|
|
'description': _('Threshold for B/W conversion (0-255)'),
|
|
|
|
'validator': [int, MinValueValidator(0), MaxValueValidator(255)],
|
|
|
|
'default': 200,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def print_label(self, **kwargs):
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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-17 21:54:13 -04:00
|
|
|
data = label_image.tobytes("PNG")
|
2024-04-17 21:32:39 -04:00
|
|
|
print_socket.send(data.encode())
|
|
|
|
print_socket.close()
|
|
|
|
except Exception as error:
|
|
|
|
raise ConnectionError('Error connecting to printer server: ' + str(error))
|