mirror of
https://github.com/inventree/InvenTree.git
synced 2025-09-13 14:11:37 +00:00
Machine registry improvements (#10150)
* Add wrapper function for machine registry * Decorate entrypoint functions * Docstrings * Fix for boolean setting * Add playwright tests * Use proper entrypoints * Ensure settings are fetched correctly * Prevent recursion of machine registry decorator * Fix machine status display * Enhanced warning msg * Add simple machine sample printer * Adds playwright tests for machine UI * re-throw exception * Define 'machine' plugin mixin class * Adjust machine discovery * Use plugin mixins for registering machine types and drivers * Adjust unit test * Remove plugin static files when deactivating * Force machine reload when plugin registry changes * Add plugins specific to testing framework * Add test for plugin loading sequence * Add session caching - Significantly reduce DB hits * Enhanced unit testing and test plugins * Refactor unit tests * Further unit test fixes * Adjust instance rendering * Display table of available drivers * Cleanup * ADjust unit test * Tweak unit test * Add docs on new mixin type * Tweak machine overview docs * Tweak playwright tests * Additional unit test * Add unit test for calling machine func * Enhanced playwright tests * Account for database not being ready
This commit is contained in:
@@ -135,6 +135,8 @@ Supported mixin classes are:
|
||||
| [EventMixin](./mixins/event.md) | Respond to events |
|
||||
| [LabelPrintingMixin](./mixins/label.md) | Custom label printing support |
|
||||
| [LocateMixin](./mixins/locate.md) | Locate and identify stock items |
|
||||
| [MachineDriverMixin](./mixins/machine.md) | Integrate custom machine drivers
|
||||
| [MailMixin](./mixins/mail.md) | Send custom emails |
|
||||
| [NavigationMixin](./mixins/navigation.md) | Add custom pages to the web interface |
|
||||
| [NotificationMixin](./mixins/notification.md) | Send custom notifications in response to system events |
|
||||
| [ReportMixin](./mixins/report.md) | Add custom context data to reports |
|
||||
|
@@ -1,12 +1,18 @@
|
||||
## Label printer
|
||||
---
|
||||
title: Label Printer Machines
|
||||
---
|
||||
|
||||
## Label Printer
|
||||
|
||||
Label printer machines can directly print labels for various items in InvenTree. They replace standard [`LabelPrintingMixin`](../mixins/label.md) plugins that are used to connect to physical printers. Using machines rather than a standard `LabelPrintingMixin` plugin has the advantage that machines can be created multiple times using different settings but the same driver. That way multiple label printers of the same brand can be connected.
|
||||
|
||||
### Writing your own printing driver
|
||||
### Writing A Custom Driver
|
||||
|
||||
To implement a custom label printer driver, you need to write a plugin which implements the [MachineDriverMixin](../mixins/machine.md) and returns a list of label printer drivers in the `get_machine_drivers` method.
|
||||
|
||||
Take a look at the most basic required code for a driver in this [example](./overview.md#example-driver). Next either implement the [`print_label`](#machine.machine_types.LabelPrinterBaseDriver.print_label) or [`print_labels`](#machine.machine_types.LabelPrinterBaseDriver.print_labels) function.
|
||||
|
||||
### Label printer status
|
||||
### Label Printer Status
|
||||
|
||||
There are a couple of predefined status codes for label printers. By default the `UNKNOWN` status code is set for each machine, but they can be changed at any time by the driver. For more info about status code see [Machine status codes](./overview.md#machine-status).
|
||||
|
||||
|
@@ -13,7 +13,7 @@ InvenTree has a builtin machine registry. There are different machine types avai
|
||||
|
||||
The machine registry is the main component which gets initialized on server start and manages all configured machines.
|
||||
|
||||
#### Initialization process
|
||||
#### Initialization Process
|
||||
|
||||
The machine registry initialization process can be divided into three stages:
|
||||
|
||||
@@ -24,24 +24,27 @@ The machine registry initialization process can be divided into three stages:
|
||||
2. The driver.init_driver function is called for each used driver
|
||||
3. The machine.initialize function is called for each machine, which calls the driver.init_machine function for each machine, then the machine.initialized state is set to true
|
||||
|
||||
#### Production setup (with a worker)
|
||||
#### Production Setup
|
||||
|
||||
!!! warning "Cache Required"
|
||||
A [shared cache](../../start/processes.md#cache-server) is required to run the machine registry in production setup with workers.
|
||||
|
||||
If a worker is connected, there exist multiple instances of the machine registry (one in each worker thread and one in the main thread) due to the nature of how python handles state in different processes. Therefore the machine instances and drivers are instantiated multiple times (The `__init__` method is called multiple times). But the init functions and update hooks (e.g. `init_machine`) are only called once from the main process.
|
||||
|
||||
The registry, driver and machine state (e.g. machine status codes, errors, ...) is stored in the cache. Therefore a shared redis cache is needed. (The local in-memory cache which is used by default is not capable to cache across multiple processes)
|
||||
|
||||
|
||||
### Machine types
|
||||
### Machine Types
|
||||
|
||||
Each machine type can provide a different type of connection functionality between inventree and a physical machine. These machine types are already built into InvenTree.
|
||||
|
||||
#### Built-in types
|
||||
#### Builtin Types
|
||||
|
||||
| Name | Description |
|
||||
| --- | --- |
|
||||
| [Label printer](./label_printer.md) | Directly print labels for various items. |
|
||||
|
||||
#### Example machine type
|
||||
#### Example Machine Type
|
||||
|
||||
If you want to create your own machine type, please also take a look at the already existing machine types in `machines/machine_types/*.py`. The following example creates a machine type called `abc`.
|
||||
|
||||
@@ -109,24 +112,33 @@ The machine type class gets instantiated for each machine on server startup and
|
||||
|
||||
Drivers provide the connection layer between physical machines and inventree. There can be multiple drivers defined for the same machine type. Drivers are provided by plugins that are enabled and extend the corresponding base driver for the particular machine type. Each machine type already provides a base driver that needs to be inherited.
|
||||
|
||||
#### Example driver
|
||||
#### Example Driver
|
||||
|
||||
A basic driver only needs to specify the basic attributes like `SLUG`, `NAME`, `DESCRIPTION`. The others are given by the used base driver, so take a look at [Machine types](#machine-types). The following example will create an driver called `abc` for the `xyz` machine type. The class will be discovered if it is provided by an **installed & activated** plugin just like this:
|
||||
|
||||
```py
|
||||
```python
|
||||
from plugin.mixins import MachineDriverMixin
|
||||
from plugin import InvenTreePlugin
|
||||
from plugin.machine.machine_types import ABCBaseDriver
|
||||
|
||||
class MyXyzAbcDriverPlugin(InvenTreePlugin):
|
||||
NAME = "XyzAbcDriver"
|
||||
SLUG = "xyz-driver"
|
||||
TITLE = "Xyz Abc Driver"
|
||||
# ...
|
||||
|
||||
class XYZDriver(ABCBaseDriver):
|
||||
SLUG = 'my-xyz-driver'
|
||||
NAME = 'My XYZ driver'
|
||||
DESCRIPTION = 'This is an awesome XYZ driver for a ABC machine'
|
||||
|
||||
|
||||
class MyXyzAbcDriverPlugin(MachineDriverMixin, InvenTreePlugin):
|
||||
NAME = "XyzAbcDriver"
|
||||
SLUG = "xyz-driver"
|
||||
TITLE = "Xyz Abc Driver"
|
||||
# ...
|
||||
|
||||
def get_machine_drivers(self):
|
||||
"""Return a list of machine drivers for this plugin."""
|
||||
return [XYZDriver]
|
||||
|
||||
|
||||
```
|
||||
|
||||
#### Driver API
|
||||
@@ -161,7 +173,7 @@ class MyXYZDriver(ABCBaseDriver):
|
||||
|
||||
Settings can even marked as `'required': True` which prevents the machine from starting if the setting is not defined.
|
||||
|
||||
### Machine status
|
||||
### Machine Status
|
||||
|
||||
Machine status can be used to report the machine status to the users. They can be set by the driver for each machine, but get lost on a server restart.
|
||||
|
||||
@@ -186,7 +198,7 @@ class XYZMachineType(BaseMachineType):
|
||||
|
||||
And to set a status code for a machine by the driver.
|
||||
|
||||
```py
|
||||
```python
|
||||
class MyXYZDriver(ABCBaseDriver):
|
||||
# ...
|
||||
def init_machine(self, machine):
|
||||
|
@@ -2,7 +2,7 @@
|
||||
title: Barcode Mixin
|
||||
---
|
||||
|
||||
## Barcode Plugins
|
||||
## BarcodeMixin
|
||||
|
||||
InvenTree supports decoding of arbitrary barcode data and generation of internal barcode formats via a **Barcode Plugin** interface. Barcode data POSTed to the `/api/barcode/` endpoint will be supplied to all loaded barcode plugins, and the first plugin to successfully interpret the barcode data will return a response to the client.
|
||||
|
||||
|
53
docs/docs/plugins/mixins/machine.md
Normal file
53
docs/docs/plugins/mixins/machine.md
Normal file
@@ -0,0 +1,53 @@
|
||||
---
|
||||
title: Machine Mixin
|
||||
---
|
||||
|
||||
## MachineDriverMixin
|
||||
|
||||
The `MachineDriverMixin` class is used to implement custom machine drivers (or machine types) in InvenTree.
|
||||
|
||||
InvenTree supports integration with [external machines](../machines/overview.md), through the use of plugin-supplied device drivers.
|
||||
|
||||
### get_machine_drivers
|
||||
|
||||
To register custom machine driver(s), the `get_machine_drivers` method must be implemented. This method should return a list of machine driver classes that the plugin supports.
|
||||
|
||||
::: plugin.base.integration.MachineMixin.MachineDriverMixin.get_machine_drivers
|
||||
options:
|
||||
show_bases: False
|
||||
show_root_heading: False
|
||||
show_root_toc_entry: False
|
||||
summary: False
|
||||
members: []
|
||||
extra:
|
||||
show_source: True
|
||||
|
||||
The default implementation returns an empty list, meaning no custom machine drivers are registered.
|
||||
|
||||
### get_machine_types
|
||||
|
||||
To register custom machine type(s), the `get_machine_types` method must be implemented. This method should return a list of machine type classes that the plugin supports.
|
||||
|
||||
::: plugin.base.integration.MachineMixin.MachineDriverMixin.get_machine_types
|
||||
options:
|
||||
show_bases: False
|
||||
show_root_heading: False
|
||||
show_root_toc_entry: False
|
||||
summary: False
|
||||
members: []
|
||||
extra:
|
||||
show_source: True
|
||||
|
||||
The default implementation returns an empty list, meaning no custom machine types are registered.
|
||||
|
||||
## Sample Plugin
|
||||
|
||||
A sample plugin is provided which implements a simple [label printing](../machines/label_printer.md) machine driver:
|
||||
|
||||
::: plugin.samples.machines.sample_printer.SamplePrinterMachine
|
||||
options:
|
||||
show_bases: False
|
||||
show_root_heading: False
|
||||
show_root_toc_entry: False
|
||||
show_source: True
|
||||
members: []
|
@@ -4,7 +4,7 @@ title: Mail Mixin
|
||||
|
||||
## MailMixin
|
||||
|
||||
The MailMixin class provides basic functionality for processing in- and outgoing mails.
|
||||
The `MailMixin` class provides basic functionality for processing in- and outgoing mails.
|
||||
|
||||
### Sample Plugin
|
||||
|
||||
|
@@ -16,7 +16,7 @@ The ScheduleMixin class provides a plugin with the ability to call functions at
|
||||
|
||||
{{ image("plugin/enable_schedule.png", "Enable schedule integration") }}
|
||||
|
||||
### SamplePlugin
|
||||
### Sample Plugin
|
||||
|
||||
An example of a plugin which supports scheduled tasks:
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
title: User Interface Mixin
|
||||
---
|
||||
|
||||
## User Interface Mixin
|
||||
## UserInterfaceMixin
|
||||
|
||||
The `UserInterfaceMixin` class provides a set of methods to implement custom functionality for the InvenTree web interface.
|
||||
|
||||
|
@@ -75,6 +75,11 @@ A plugin is not allowed to override a *final method* from the `InvenTreePlugin`
|
||||
|
||||
This is a security measure to prevent plugins from changing the core functionality of InvenTree. The code of the plugin must be changed to not override functions that are marked as *final*.
|
||||
|
||||
#### INVE-E12
|
||||
**Plugin returned invalid machine type - Backend**
|
||||
|
||||
An error occurred when discovering or initializing a machine type from a plugin. This likely indicates a faulty or incompatible plugin.
|
||||
|
||||
### INVE-W (InvenTree Warning)
|
||||
Warnings - These are non-critical errors which should be addressed when possible.
|
||||
|
||||
|
@@ -228,6 +228,8 @@ nav:
|
||||
- Icon Pack Mixin: plugins/mixins/icon.md
|
||||
- Label Printing Mixin: plugins/mixins/label.md
|
||||
- Locate Mixin: plugins/mixins/locate.md
|
||||
- Machine Mixin: plugins/mixins/machine.md
|
||||
- Mail Mixin: plugins/mixins/mail.md
|
||||
- Navigation Mixin: plugins/mixins/navigation.md
|
||||
- Notification Mixin: plugins/mixins/notification.md
|
||||
- Report Mixin: plugins/mixins/report.md
|
||||
|
Reference in New Issue
Block a user