diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6c76296 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Sergal.engineering + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/inventree_phomemo/__init__.py b/inventree_phomemo/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/inventree_phomemo/phomemo_plugin.py b/inventree_phomemo/phomemo_plugin.py new file mode 100644 index 0000000..8366c60 --- /dev/null +++ b/inventree_phomemo/phomemo_plugin.py @@ -0,0 +1,69 @@ +# 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)) + data = label_image + print_socket.send(data.encode()) + print_socket.close() + except Exception as error: + raise ConnectionError('Error connecting to printer server: ' + str(error)) diff --git a/inventree_phomemo/version.py b/inventree_phomemo/version.py new file mode 100644 index 0000000..aa88b61 --- /dev/null +++ b/inventree_phomemo/version.py @@ -0,0 +1 @@ +PHOMEMO_PLUGIN_VERSION = "0.0.0" \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..ab33060 --- /dev/null +++ b/setup.py @@ -0,0 +1,28 @@ +from setuptools import setup, find_packages + +from inventree_phomemo.version import PHOMEMO_PLUGIN_VERSION + +with open('README.md', encoding='utf-8') as f: + long_description = f.read() + +setup( + name = "inventree-phomemo-plugin", + version = PHOMEMO_PLUGIN_VERSION, + author = "Sergal.engineering", + author_email = "zener@theserg.al", + license = "MIT", + description = "Phomemo label plugin for InvenTree", + long_description = long_description, + long_description_content_type = 'text/markdown', + keywords = "inventree phomemo label plugin", + url = "https://git.merp.digital/sergal-engineering/inventree-phomemo-plugin/", + + packages = find_packages(), + scripts = [], + + install_requires = [], + + entry_points = { + 'inventree_plugins': [ 'PhomemoLabelPlugin = inventree_phomemo.phomemo_label:PhomemoLabelPlugin' ] + } +)