feat(printer): Add basic printer plugin implementation
This commit is contained in:
parent
8b0dd6aedd
commit
aadadd8ebf
5 changed files with 119 additions and 0 deletions
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -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.
|
0
inventree_phomemo/__init__.py
Normal file
0
inventree_phomemo/__init__.py
Normal file
69
inventree_phomemo/phomemo_plugin.py
Normal file
69
inventree_phomemo/phomemo_plugin.py
Normal file
|
@ -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))
|
1
inventree_phomemo/version.py
Normal file
1
inventree_phomemo/version.py
Normal file
|
@ -0,0 +1 @@
|
|||
PHOMEMO_PLUGIN_VERSION = "0.0.0"
|
28
setup.py
Normal file
28
setup.py
Normal file
|
@ -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' ]
|
||||
}
|
||||
)
|
Loading…
Add table
Reference in a new issue