Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix driver to new interface working in workers #3

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The easiest way to set this up, is using cups and configure a RAW printer device
## Installation

> [!IMPORTANT]
> This plugin is only compatible with InvenTree>=0.14 because this uses the new label printer driver interface introduced with [inventree/InvenTree#4824](https://github.com/inventree/InvenTree/pull/4824).
> This plugin is only compatible with InvenTree>=0.16 because this uses the new label printer driver interface introduced with [inventree/InvenTree#4824](https://github.com/inventree/InvenTree/pull/4824) and was fixed with 0.16 to work inside of workers.

Goto "Admin Center > Plugins > Install Plugin" and enter `inventree-dymo-plugin` as package name.

Expand Down
19 changes: 12 additions & 7 deletions inventree_dymo/InvenTreeDymoPlugin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import socket
from django.db import models
from django.db.models.query import QuerySet
from rest_framework.request import Request
from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator, MaxValueValidator

from plugin import InvenTreePlugin
from plugin.base.label.mixins import LabelItemType
from plugin.machine.machine_types import LabelPrinterBaseDriver, LabelPrinterMachine
from label.models import LabelTemplate
from report.models import LabelTemplate

from .version import DYMO_PLUGIN_VERSION
from .dymo import DymoLabel, RoleSelect, PrintDensity
Expand All @@ -19,7 +18,8 @@ class InvenTreeDymoPlugin(InvenTreePlugin):
VERSION = DYMO_PLUGIN_VERSION

# Machine driver registry is only available in InvenTree 0.14.0 and later
MIN_VERSION = "0.14.0"
# Machine driver interface was fixed with 0.16.0 to work inside of inventree workers
MIN_VERSION = "0.16.0"

TITLE = "InvenTree Dymo Plugin"
SLUG = "inventree-dymo-plugin"
Expand Down Expand Up @@ -94,7 +94,7 @@ def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

def print_labels(self, machine: LabelPrinterMachine, label: LabelTemplate, items: QuerySet[LabelItemType], request: Request, **kwargs):
def print_labels(self, machine: LabelPrinterMachine, label: LabelTemplate, items: QuerySet[models.Model], **kwargs):
"""Print labels using a Dymo label printer."""
printing_options = kwargs.get('printing_options', {})

Expand All @@ -109,12 +109,16 @@ def print_labels(self, machine: LabelPrinterMachine, label: LabelTemplate, items

for item in items:
dpi = {"TEXT": 300, "GRAPHIC": 600}[dymo_label.mode]
png = self.render_to_png(label, item, request, dpi=dpi)
png = self.render_to_png(label, item, dpi=dpi)

for _copies in range(printing_options.get('copies', 1)):
dymo_label.add_label(png)

data = dymo_label.get_data()
self.send_data(machine, data)

def send_data(self, machine: LabelPrinterMachine, data: bytearray):
machine.set_status(LabelPrinterMachine.MACHINE_STATUS.UNKNOWN)

ip_addr = machine.get_setting('SERVER', 'D')
port = machine.get_setting('PORT', 'D')
Expand All @@ -125,4 +129,5 @@ def print_labels(self, machine: LabelPrinterMachine, label: LabelTemplate, items
print_socket.send(data)
print_socket.close()
except Exception as e:
raise ConnectionError(f"Error connection to network printer: {e}")
machine.set_status(LabelPrinterMachine.MACHINE_STATUS.DISCONNECTED)
machine.handle_error(f"Error connecting to network printer: {e}")
2 changes: 1 addition & 1 deletion inventree_dymo/dymo.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def dump_label(self, png: Image.Image):
self.pb.short_form_feed()


class DymoTape(DymoLabel):
class DymoTape(DymoLabelBase):
def __init__(
self,
*,
Expand Down
2 changes: 1 addition & 1 deletion inventree_dymo/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DYMO_PLUGIN_VERSION = "0.1.0"
DYMO_PLUGIN_VERSION = "1.0.0"
Loading